Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
navigation.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #define NAV_C
29 
31 
32 #include "pprz_debug.h"
33 #include "modules/gps/gps.h" // needed by auto_nav from the flight plan
34 #include "modules/ins/ins.h"
35 #include "state.h"
36 
37 #include "autopilot.h"
38 #include "generated/modules.h"
39 #include "generated/flight_plan.h"
40 
41 #include "math/pprz_algebra_int.h"
42 
44 #include "pprzlink/messages.h"
45 #include "mcu_periph/uart.h"
46 
47 
48 PRINT_CONFIG_VAR(NAVIGATION_FREQUENCY)
49 
50 struct RoverNavigation nav;
51 
52 const float max_dist_from_home = MAX_DIST_FROM_HOME;
53 const float max_dist2_from_home = MAX_DIST_FROM_HOME * MAX_DIST_FROM_HOME;
55 
56 
58 {
59  nav.exception_flag[flag_num] = true;
60 }
61 
62 
63 #if PERIODIC_TELEMETRY
65 
66 static void send_wp_moved(struct transport_tx *trans, struct link_device *dev)
67 {
68  static uint8_t i;
69  i++;
70  if (i >= nb_waypoint) { i = 0; }
71  pprz_msg_send_WP_MOVED_ENU(trans, dev, AC_ID,
72  &i,
73  &(waypoints[i].enu_i.x),
74  &(waypoints[i].enu_i.y),
75  &(waypoints[i].enu_i.z));
76 }
77 #endif
78 
79 void nav_init(void)
80 {
82 
83  nav_block = 0;
84  nav_stage = 0;
85 
86  nav.mode = NAV_MODE_WAYPOINT;
87 
88  VECT3_COPY(nav.target, waypoints[WP_HOME].enu_f);
89  VECT3_COPY(nav.carrot, waypoints[WP_HOME].enu_f);
90  nav.heading = 0.f;
92  nav.speed = 0.f;
93  nav.turn = 0.f;
94  nav.shift = 0.f;
95 
96  nav.too_far_from_home = false;
97  nav.dist2_to_home = 0.f;
98 
99 #if PERIODIC_TELEMETRY
101 #endif
102 
103  // generated init function
104  auto_nav_init();
105 }
106 
107 
108 void nav_run(void)
109 {
111 }
112 
114 {
115  if (DL_BLOCK_ac_id(buf) != AC_ID) { return; }
116  nav_goto_block(DL_BLOCK_block_id(buf));
117 }
118 
120 {
121  uint8_t ac_id = DL_MOVE_WP_ac_id(buf);
122  if (ac_id != AC_ID) { return; }
124  uint8_t wp_id = DL_MOVE_WP_wp_id(buf);
125  struct LlaCoor_i lla;
126  lla.lat = DL_MOVE_WP_lat(buf);
127  lla.lon = DL_MOVE_WP_lon(buf);
128  /* WP_alt from message is alt above MSL in mm
129  * lla.alt is above ellipsoid in mm
130  */
131  lla.alt = DL_MOVE_WP_alt(buf) - state.ned_origin_i.hmsl +
133  waypoint_move_lla(wp_id, &lla);
134  }
135 }
136 
137 bool nav_check_wp_time(struct EnuCoor_f *wp, float stay_time)
138 {
139  (void) wp;
140  (void) stay_time;
141  return true;
142 // uint16_t time_at_wp;
143 // float dist_to_point;
144 // static uint16_t wp_entry_time = 0;
145 // static bool wp_reached = false;
146 // static struct EnuCoor_i wp_last = { 0, 0, 0 };
147 // struct Int32Vect2 diff;
148 //
149 // if ((wp_last.x != wp->x) || (wp_last.y != wp->y)) {
150 // wp_reached = false;
151 // wp_last = *wp;
152 // }
153 //
154 // VECT2_DIFF(diff, *wp, *stateGetPositionEnu_i());
155 // struct FloatVect2 diff_f = {POS_FLOAT_OF_BFP(diff.x), POS_FLOAT_OF_BFP(diff.y)};
156 // dist_to_point = float_vect2_norm(&diff_f);
157 // if (dist_to_point < ARRIVED_AT_WAYPOINT) {
158 // if (!wp_reached) {
159 // wp_reached = true;
160 // wp_entry_time = autopilot.flight_time;
161 // time_at_wp = 0;
162 // } else {
163 // time_at_wp = autopilot.flight_time - wp_entry_time;
164 // }
165 // } else {
166 // time_at_wp = 0;
167 // wp_reached = false;
168 // }
169 // if (time_at_wp > stay_time) {
170 // INT_VECT3_ZERO(wp_last);
171 // return true;
172 // }
173 // return false;
174 }
175 
176 
179 {
181  /* update local ENU coordinates of global waypoints */
183 }
184 
185 void nav_reset_alt(void)
186 {
189 }
190 
191 void nav_init_stage(void)
192 {
193  VECT3_COPY(nav.last_pos, *stateGetPositionEnu_f());
194  stage_time = 0;
195  //nav_circle_radians = 0; FIXME
196 }
197 
199 {
200  RunOnceEvery(NAVIGATION_FREQUENCY, { stage_time++; block_time++; });
201 
202  //nav.dist2_to_wp = 0; FIXME
203 
204  /* from flight_plan.h */
205  auto_nav();
206 
207  /* run carrot loop */
208  nav_run();
209 }
210 
212 {
213  return autopilot_in_flight();
214 }
215 
217 void nav_home(void)
218 {
219  nav.mode = NAV_MODE_WAYPOINT;
220  VECT3_COPY(nav.target, waypoints[WP_HOME].enu_f);
221 
222  //nav.dist2_to_wp = nav.dist2_to_home; FIXME
223 
224  /* run carrot loop */
225  nav_run();
226 }
227 
230 {
231  struct EnuCoor_f *pos = stateGetPositionEnu_f();
232  struct FloatVect2 pos_diff = {
233  .x = p->x - pos->x,
234  .y = p->y - pos->y
235  };
236  return pos_diff.x * pos_diff.x + pos_diff.y * pos_diff.y;
237 }
238 
241 {
242  return get_dist2_to_point(&waypoints[wp_id].enu_f);
243 }
244 
249 {
252 #ifdef InGeofenceSector
253  struct EnuCoor_f *pos = stateGetPositionEnu_f();
254  nav.too_far_from_home = nav.too_far_from_home || !(InGeofenceSector(pos->x, pos->y));
255 #endif
256 }
257 
259 void nav_set_heading_rad(float rad)
260 {
261  nav.heading = rad;
263 }
264 
266 void nav_set_heading_deg(float deg)
267 {
268  nav_set_heading_rad(RadOfDeg(deg));
269 }
270 
272 void nav_set_heading_towards(float x, float y)
273 {
274  struct FloatVect2 target = {x, y};
275  struct FloatVect2 pos_diff;
277  // don't change heading if closer than 0.5m to target
278  if (VECT2_NORM2(pos_diff) > 0.25f) {
279  nav.heading = atan2f(pos_diff.x, pos_diff.y);
280  }
281 }
282 
285 {
287 }
288 
291 {
293 }
294 
297 {
299 }
300 
302 {
303 #ifdef AP_MODE_FAILSAFE
305 #endif
306 }
307 
308 
313 {
317 }
318 
320 {
322 }
323 
325 {
328 }
329 
330 
bool autopilot_set_mode(uint8_t new_autopilot_mode)
set autopilot mode
Definition: autopilot.c:193
bool autopilot_in_flight(void)
get in_flight flag
Definition: autopilot.c:335
Core autopilot interface common to all firmwares.
uint8_t nav_stage
uint8_t nav_block
uint16_t stage_time
In s.
void nav_goto_block(uint8_t b)
uint16_t block_time
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:39
const uint8_t nb_waypoint
Definition: common_nav.c:38
#define WaypointX(_wp)
Definition: common_nav.h:45
float y
Definition: common_nav.h:41
float x
Definition: common_nav.h:40
#define WaypointY(_wp)
Definition: common_nav.h:46
Device independent GPS code (interface)
float psi
in radians
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:92
#define VECT2_COPY(_a, _b)
Definition: pprz_algebra.h:68
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
#define VECT2_NORM2(_v)
Definition: pprz_algebra.h:118
int32_t lat
in degrees*1e7
int32_t hmsl
Height above mean sea level in mm.
int32_t alt
in millimeters above WGS84 reference ellipsoid
struct LlaCoor_i lla
Reference point in lla.
int32_t lon
in degrees*1e7
vector in Latitude, Longitude and Altitude
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
struct State state
Definition: state.c:36
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
struct LtpDef_i ned_origin_i
Definition of the local (flat earth) coordinate system.
Definition: state.h:166
static bool stateIsLocalCoordinateValid(void)
Test if local coordinates are valid.
Definition: state.h:508
void WEAK ins_reset_local_origin(void)
INS local origin reset.
Definition: ins.c:55
void WEAK ins_reset_altitude_ref(void)
INS altitude reference reset.
Definition: ins.c:65
Integrated Navigation System interface.
static float p[2][2]
void waypoints_localize_all(void)
update local ENU coordinates of global waypoints
Definition: waypoints.c:357
void waypoints_init(void)
initialize global and local waypoints
Definition: waypoints.c:51
void waypoint_move_lla(uint8_t wp_id, struct LlaCoor_i *lla)
Definition: waypoints.c:253
void nav_oval_init(void)
Definition: nav.c:757
void nav_oval(uint8_t p1, uint8_t p2, float radius)
Definition: nav.c:763
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition: nav.h:49
static void nav_goto(struct EnuCoor_f *wp)
static void nav_route(struct EnuCoor_f *wp_start, struct EnuCoor_f *wp_end)
static void nav_circle(struct EnuCoor_f *wp_center, float radius)
static bool nav_approaching(struct EnuCoor_f *wp, struct EnuCoor_f *from, float approaching_time)
struct FloatVect2 pos_diff
Paparazzi fixed point algebra.
float y
in meters
float x
in meters
vector in East North Up coordinates Units: meters
#define AP_MODE_FAILSAFE
void nav_home(void)
Home mode navigation.
Definition: navigation.c:292
void nav_reset_reference(void)
Reset the geographic reference to the current GPS fix.
Definition: navigation.c:235
void nav_register_oval(navigation_oval_init _nav_oval_init, navigation_oval nav_oval)
Definition: navigation.c:402
void nav_set_heading_current(void)
Set heading to the current yaw angle.
Definition: navigation.c:373
void nav_init_stage(void)
needs to be implemented by fixedwing and rotorcraft seperately
Definition: navigation.c:248
void nav_reset_alt(void)
Definition: navigation.c:242
const float max_dist_from_home
Definition: navigation.c:53
void nav_set_heading_towards_waypoint(uint8_t wp)
Set heading in the direction of a waypoint.
Definition: navigation.c:361
void compute_dist2_to_home(void)
Computes squared distance to the HOME waypoint potentially sets too_far_from_home.
Definition: navigation.c:324
bool nav_is_in_flight(void)
Definition: navigation.c:275
void nav_run(void)
Definition: navigation.c:166
void nav_set_heading_towards(float x, float y)
Set heading to point towards x,y position in local coordinates.
Definition: navigation.c:348
struct RotorcraftNavigation nav
Definition: navigation.c:51
void nav_init(void)
Navigation Initialisation.
Definition: navigation.c:70
void nav_parse_MOVE_WP(uint8_t *buf)
Definition: navigation.c:122
void nav_register_circle(navigation_circle nav_circle)
Definition: navigation.c:397
void nav_register_goto_wp(navigation_goto nav_goto, navigation_route nav_route, navigation_approaching nav_approaching)
Registering functions.
Definition: navigation.c:390
void nav_parse_BLOCK(uint8_t *buf)
Definition: navigation.c:116
void nav_set_heading_towards_target(void)
Set heading in the direction of the target.
Definition: navigation.c:367
float get_dist2_to_point(struct EnuCoor_f *p)
Returns squared horizontal distance to given point.
Definition: navigation.c:306
const float max_dist2_from_home
Definition: navigation.c:54
void nav_set_heading_rad(float rad)
Set nav_heading in radians.
Definition: navigation.c:335
void nav_set_heading_deg(float deg)
Set nav_heading in degrees.
Definition: navigation.c:342
bool nav_check_wp_time(struct EnuCoor_f *wp, uint16_t stay_time)
Check the time spent in a radius of 'ARRIVED_AT_WAYPOINT' around a wp
Definition: navigation.c:181
void nav_periodic_task(void)
Navigation main: call to the code generated from the XML flight plan.
Definition: navigation.c:257
float get_dist2_to_waypoint(uint8_t wp_id)
Returns squared horizontal distance to given waypoint.
Definition: navigation.c:316
void nav_set_failsafe(void)
Definition: navigation.c:378
struct EnuCoor_f speed
speed setpoint (in m/s)
Definition: navigation.h:128
float dist2_to_home
squared distance to home waypoint
Definition: navigation.h:142
navigation_oval nav_oval
Definition: navigation.h:155
navigation_approaching nav_approaching
Definition: navigation.h:152
#define DEFAULT_CIRCLE_RADIUS
default nav_circle_radius in meters
Definition: navigation.h:42
navigation_circle nav_circle
Definition: navigation.h:153
float radius
radius setpoint (in meters)
Definition: navigation.h:136
navigation_oval_init nav_oval_init
Definition: navigation.h:154
float heading
heading setpoint (in radians)
Definition: navigation.h:133
struct EnuCoor_f carrot
carrot position (also used for GCS display)
Definition: navigation.h:127
navigation_goto nav_goto
Definition: navigation.h:150
navigation_route nav_route
Definition: navigation.h:151
#define FAILSAFE_MODE_DISTANCE
Maximum distance from HOME waypoint before going into failsafe mode.
Definition: navigation.h:60
struct EnuCoor_f target
final target position (in meters)
Definition: navigation.h:126
bool too_far_from_home
too_far flag
Definition: navigation.h:143
float failsafe_mode_dist2
Definition: navigation.c:54
static void send_wp_moved(struct transport_tx *trans, struct link_device *dev)
Definition: navigation.c:66
void set_exception_flag(uint8_t flag_num)
Definition: navigation.c:57
Rover navigation functions.
void(* nav_rover_oval_init)(void)
Definition: navigation.h:82
void(* nav_rover_goto)(struct EnuCoor_f *wp)
Definition: navigation.h:78
void(* nav_rover_circle)(struct EnuCoor_f *wp_center, float radius)
Definition: navigation.h:81
#define NormCourseRad(x)
Normalize a rad angle between 0 and 2*PI.
Definition: navigation.h:156
#define NAV_MODE_WAYPOINT
Nav modes.
Definition: navigation.h:72
void(* nav_rover_route)(struct EnuCoor_f *wp_start, struct EnuCoor_f *wp_end)
Definition: navigation.h:79
void(* nav_rover_oval)(struct EnuCoor_f *wp1, struct EnuCoor_f *wp2, float radius)
Definition: navigation.h:83
bool(* nav_rover_approaching)(struct EnuCoor_f *wp_to, struct EnuCoor_f *wp_from, float approaching_time)
Definition: navigation.h:80
General Navigation structure.
Definition: navigation.h:88
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
struct target_t target
Definition: target_pos.c:64
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98