Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
autopilot.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2012 The Paparazzi Team
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, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #include <stdint.h>
31 
32 #include "mcu_periph/uart.h"
33 #include "mcu_periph/sys_time.h"
35 #include "subsystems/commands.h"
36 #include "subsystems/actuators.h"
37 #include "subsystems/electrical.h"
38 #include "subsystems/settings.h"
42 
46 
47 #if USE_STABILIZATION_RATE
49 #endif
50 
51 #include "generated/settings.h"
52 
53 #if USE_GPS
54 #include "subsystems/gps.h"
55 #else
56 #if NO_GPS_NEEDED_FOR_NAV
57 #define GpsIsLost() FALSE
58 #else
59 #define GpsIsLost() TRUE
60 #endif
61 #endif
62 
63 #ifdef POWER_SWITCH_GPIO
64 #include "mcu_periph/gpio.h"
65 #endif
66 
67 #include "pprz_version.h"
68 
71 
75 
78 
81 
84 
85 /* Geofence exceptions */
87 
89 #ifndef AUTOPILOT_IN_FLIGHT_TIME
90 #define AUTOPILOT_IN_FLIGHT_TIME 20
91 #endif
92 
94 #ifndef AUTOPILOT_IN_FLIGHT_MIN_SPEED
95 #define AUTOPILOT_IN_FLIGHT_MIN_SPEED 0.2
96 #endif
97 
99 #ifndef AUTOPILOT_IN_FLIGHT_MIN_ACCEL
100 #define AUTOPILOT_IN_FLIGHT_MIN_ACCEL 2.0
101 #endif
102 
104 #ifndef AUTOPILOT_IN_FLIGHT_MIN_THRUST
105 #define AUTOPILOT_IN_FLIGHT_MIN_THRUST 500
106 #endif
107 
108 #ifndef AUTOPILOT_DISABLE_AHRS_KILL
109 static inline int ahrs_is_aligned(void)
110 {
111  return stateIsAttitudeValid();
112 }
113 #else
114 PRINT_CONFIG_MSG("Using AUTOPILOT_DISABLE_AHRS_KILL")
115 static inline int ahrs_is_aligned(void)
116 {
117  return true;
118 }
119 #endif
120 
122 #ifndef FAILSAFE_DESCENT_SPEED
123 #define FAILSAFE_DESCENT_SPEED 1.5
124 PRINT_CONFIG_VAR(FAILSAFE_DESCENT_SPEED)
125 #endif
126 
128 #ifndef FAILSAFE_MODE_TOO_FAR_FROM_HOME
129 #define FAILSAFE_MODE_TOO_FAR_FROM_HOME AP_MODE_FAILSAFE
130 #endif
131 
132 
133 #if USE_KILL_SWITCH_FOR_MOTOR_ARMING
134 #include "autopilot_arming_switch.h"
135 PRINT_CONFIG_MSG("Using kill switch for motor arming")
136 #elif USE_THROTTLE_FOR_MOTOR_ARMING
138 PRINT_CONFIG_MSG("Using throttle for motor arming")
139 #else
140 #include "autopilot_arming_yaw.h"
141 PRINT_CONFIG_MSG("Using 2 sec yaw for motor arming")
142 #endif
143 
144 #ifndef MODE_STARTUP
145 #define MODE_STARTUP AP_MODE_KILL
146 PRINT_CONFIG_MSG("Using default AP_MODE_KILL as MODE_STARTUP")
147 #endif
148 
149 #ifndef UNLOCKED_HOME_MODE
150 #if MODE_AUTO1 == AP_MODE_HOME
151 #define UNLOCKED_HOME_MODE TRUE
152 PRINT_CONFIG_MSG("Enabled UNLOCKED_HOME_MODE since MODE_AUTO1 is AP_MODE_HOME")
153 #elif MODE_AUTO2 == AP_MODE_HOME
154 #define UNLOCKED_HOME_MODE TRUE
155 PRINT_CONFIG_MSG("Enabled UNLOCKED_HOME_MODE since MODE_AUTO2 is AP_MODE_HOME")
156 #else
157 #define UNLOCKED_HOME_MODE FALSE
158 #endif
159 #endif
160 
161 #if MODE_MANUAL == AP_MODE_NAV
162 #error "MODE_MANUAL mustn't be AP_MODE_NAV"
163 #endif
164 
165 void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
166 {
167  static uint32_t ap_version = PPRZ_VERSION_INT;
168  static char *ver_desc = PPRZ_VERSION_DESC;
169  pprz_msg_send_AUTOPILOT_VERSION(trans, dev, AC_ID, &ap_version, strlen(ver_desc), ver_desc);
170 }
171 
172 static void send_alive(struct transport_tx *trans, struct link_device *dev)
173 {
174  pprz_msg_send_ALIVE(trans, dev, AC_ID, 16, MD5SUM);
175 }
176 
177 static void send_attitude(struct transport_tx *trans, struct link_device *dev)
178 {
179  struct FloatEulers *att = stateGetNedToBodyEulers_f();
180  pprz_msg_send_ATTITUDE(trans, dev, AC_ID, &(att->phi), &(att->psi), &(att->theta));
181 };
182 
183 #if USE_MOTOR_MIXING
185 #endif
186 
187 static void send_status(struct transport_tx *trans, struct link_device *dev)
188 {
189  uint32_t imu_nb_err = 0;
190 #if USE_MOTOR_MIXING
192 #else
193  uint8_t _motor_nb_err = 0;
194 #endif
195 #if USE_GPS
196  uint8_t fix = gps.fix;
197 #else
198  uint8_t fix = 0;
199 #endif
200  uint8_t in_flight = autopilot_in_flight;
201  uint8_t motors_on = autopilot_motors_on;
202  uint16_t time_sec = sys_time.nb_sec;
203  pprz_msg_send_ROTORCRAFT_STATUS(trans, dev, AC_ID,
204  &imu_nb_err, &_motor_nb_err,
206  &fix, &autopilot_mode, &in_flight, &motors_on,
208  &electrical.vsupply, &time_sec);
209 }
210 
211 static void send_energy(struct transport_tx *trans, struct link_device *dev)
212 {
214  if (fabs(electrical.energy) >= INT16_MAX) {
215  e = INT16_MAX;
216  }
217  float vsup = ((float)electrical.vsupply) / 10.0f;
218  float curs = ((float)electrical.current) / 1000.0f;
219  float power = vsup * curs;
220  pprz_msg_send_ENERGY(trans, dev, AC_ID, &vsup, &curs, &e, &power);
221 }
222 
223 static void send_fp(struct transport_tx *trans, struct link_device *dev)
224 {
225  int32_t carrot_up = -guidance_v_z_sp;
226  pprz_msg_send_ROTORCRAFT_FP(trans, dev, AC_ID,
227  &(stateGetPositionEnu_i()->x),
228  &(stateGetPositionEnu_i()->y),
229  &(stateGetPositionEnu_i()->z),
230  &(stateGetSpeedEnu_i()->x),
231  &(stateGetSpeedEnu_i()->y),
232  &(stateGetSpeedEnu_i()->z),
236  &guidance_h.sp.pos.y,
237  &guidance_h.sp.pos.x,
238  &carrot_up,
240  &stabilization_cmd[COMMAND_THRUST],
242 }
243 
244 #ifdef RADIO_CONTROL
245 static void send_rc(struct transport_tx *trans, struct link_device *dev)
246 {
247  pprz_msg_send_RC(trans, dev, AC_ID, RADIO_CONTROL_NB_CHANNEL, radio_control.values);
248 }
249 
250 static void send_rotorcraft_rc(struct transport_tx *trans, struct link_device *dev)
251 {
252 #ifdef RADIO_KILL_SWITCH
254 #else
255  int16_t _kill_switch = 42;
256 #endif
257  pprz_msg_send_ROTORCRAFT_RADIO_CONTROL(trans, dev, AC_ID,
263  &_kill_switch,
265 }
266 #endif
267 
268 #ifdef ACTUATORS
269 static void send_actuators(struct transport_tx *trans, struct link_device *dev)
270 {
271  pprz_msg_send_ACTUATORS(trans, dev, AC_ID , ACTUATORS_NB, actuators);
272 }
273 #endif
274 
275 static void send_dl_value(struct transport_tx *trans, struct link_device *dev)
276 {
277  PeriodicSendDlValue(trans, dev);
278 }
279 
280 static void send_rotorcraft_cmd(struct transport_tx *trans, struct link_device *dev)
281 {
282  pprz_msg_send_ROTORCRAFT_CMD(trans, dev, AC_ID,
283  &stabilization_cmd[COMMAND_ROLL],
284  &stabilization_cmd[COMMAND_PITCH],
285  &stabilization_cmd[COMMAND_YAW],
286  &stabilization_cmd[COMMAND_THRUST]);
287 }
288 
289 
290 void autopilot_init(void)
291 {
292  /* mode is finally set at end of init if MODE_STARTUP is not KILL */
294  autopilot_motors_on = false;
296  autopilot_in_flight = false;
302  autopilot_rc = true;
303  autopilot_power_switch = false;
304 #ifdef POWER_SWITCH_GPIO
306  gpio_clear(POWER_SWITCH_GPIO); // POWER OFF
307 #endif
308 
310 
311  nav_init();
312  guidance_h_init();
313  guidance_v_init();
314 
317 #if USE_STABILIZATION_RATE
319 #endif
321 
322  /* set startup mode, propagates through to guidance h/v */
324 
325  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
327  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_ROTORCRAFT_STATUS, send_status);
330  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_ROTORCRAFT_FP, send_fp);
333 #ifdef ACTUATORS
335 #endif
336 #ifdef RADIO_CONTROL
338  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_ROTORCRAFT_RADIO_CONTROL, send_rotorcraft_rc);
339 #endif
340 }
341 
342 
343 #define NAV_PRESCALER (PERIODIC_FREQUENCY / NAV_FREQ)
345 {
346 
347  RunOnceEvery(NAV_PRESCALER, compute_dist2_to_home());
348 
353  } else {
355  }
356  }
357  }
358 
359  if (autopilot_mode == AP_MODE_HOME) {
360  RunOnceEvery(NAV_PRESCALER, nav_home());
361  } else {
362  // otherwise always call nav_periodic_task so that carrot is always updated in GCS for other modes
363  RunOnceEvery(NAV_PRESCALER, nav_periodic_task());
364  }
365 
366 
367  /* If in FAILSAFE mode and either already not in_flight anymore
368  * or just "detected" ground, go to KILL mode.
369  */
371  if (!autopilot_in_flight) {
373  }
374 
375 #if FAILSAFE_GROUND_DETECT
376  INFO("Using FAILSAFE_GROUND_DETECT: KILL")
379  }
380 #endif
381  }
382 
383  /* Reset ground detection _after_ running flight plan
384  */
385  if (!autopilot_in_flight) {
388  }
389 
390  /* Set fixed "failsafe" commands from airframe file if in KILL mode.
391  * If in FAILSAFE mode, run normal loops with failsafe attitude and
392  * downwards velocity setpoints.
393  */
394  if (autopilot_mode == AP_MODE_KILL) {
396  } else {
400  }
401 
402 }
403 
408 void autopilot_SetModeHandler(float mode)
409 {
410  if (mode == AP_MODE_KILL || mode == AP_MODE_FAILSAFE || mode == AP_MODE_HOME) {
411  // safety modes are always accessible via settings
412  autopilot_set_mode(mode);
413  } else {
414  if (radio_control.status != RC_OK &&
415  (mode == AP_MODE_NAV || mode == AP_MODE_GUIDED || mode == AP_MODE_FLIP || mode == AP_MODE_MODULE)) {
416  // without RC, only nav-like modes are accessible
417  autopilot_set_mode(mode);
418  }
419  }
420  // with RC, other modes can only be changed from the RC
421 }
422 
423 
424 void autopilot_set_mode(uint8_t new_autopilot_mode)
425 {
426 
427  /* force startup mode (default is kill) as long as AHRS is not aligned */
428  if (!ahrs_is_aligned()) {
429  new_autopilot_mode = MODE_STARTUP;
430  }
431 
432  if (new_autopilot_mode != autopilot_mode) {
433  /* horizontal mode */
434  switch (new_autopilot_mode) {
435  case AP_MODE_FAILSAFE:
436 #ifndef KILL_AS_FAILSAFE
439  break;
440 #endif
441  case AP_MODE_KILL:
442  autopilot_in_flight = false;
445  break;
446  case AP_MODE_RC_DIRECT:
448  break;
450  case AP_MODE_RATE_DIRECT:
451  case AP_MODE_RATE_Z_HOLD:
452 #if USE_STABILIZATION_RATE
454 #else
455  return;
456 #endif
457  break;
463  break;
464  case AP_MODE_FORWARD:
466  break;
469  break;
471  case AP_MODE_HOVER_CLIMB:
474  break;
475  case AP_MODE_HOME:
476  case AP_MODE_NAV:
478  break;
479  case AP_MODE_MODULE:
480 #ifdef GUIDANCE_H_MODE_MODULE_SETTING
482 #endif
483  break;
484  case AP_MODE_FLIP:
486  break;
487  case AP_MODE_GUIDED:
489  break;
490  default:
491  break;
492  }
493  /* vertical mode */
494  switch (new_autopilot_mode) {
495  case AP_MODE_FAILSAFE:
496 #ifndef KILL_AS_FAILSAFE
499  break;
500 #endif
501  case AP_MODE_KILL:
503  stabilization_cmd[COMMAND_THRUST] = 0;
505  break;
506  case AP_MODE_RC_DIRECT:
507  case AP_MODE_RATE_DIRECT:
511  case AP_MODE_FORWARD:
513  break;
517  break;
519  case AP_MODE_HOVER_CLIMB:
521  break;
522  case AP_MODE_RATE_Z_HOLD:
526  break;
527  case AP_MODE_HOME:
528  case AP_MODE_NAV:
530  break;
531  case AP_MODE_MODULE:
532 #ifdef GUIDANCE_V_MODE_MODULE_SETTING
534 #endif
535  break;
536  case AP_MODE_FLIP:
538  break;
539  case AP_MODE_GUIDED:
541  break;
542  default:
543  break;
544  }
545  //if switching to rate mode but rate mode is not defined, the function returned
546  autopilot_mode = new_autopilot_mode;
547  }
548 }
549 
550 bool autopilot_guided_goto_ned(float x, float y, float z, float heading)
551 {
556  return true;
557  }
558  return false;
559 }
560 
561 bool autopilot_guided_goto_ned_relative(float dx, float dy, float dz, float dyaw)
562 {
564  float x = stateGetPositionNed_f()->x + dx;
565  float y = stateGetPositionNed_f()->y + dy;
566  float z = stateGetPositionNed_f()->z + dz;
567  float heading = stateGetNedToBodyEulers_f()->psi + dyaw;
568  return autopilot_guided_goto_ned(x, y, z, heading);
569  }
570  return false;
571 }
572 
573 bool autopilot_guided_goto_body_relative(float dx, float dy, float dz, float dyaw)
574 {
576  float psi = stateGetNedToBodyEulers_f()->psi;
577  float x = stateGetPositionNed_f()->x + cosf(-psi) * dx + sinf(-psi) * dy;
578  float y = stateGetPositionNed_f()->y - sinf(-psi) * dx + cosf(-psi) * dy;
579  float z = stateGetPositionNed_f()->z + dz;
580  float heading = psi + dyaw;
581  return autopilot_guided_goto_ned(x, y, z, heading);
582  }
583  return false;
584 }
585 
586 bool autopilot_guided_move_ned(float vx, float vy, float vz, float heading)
587 {
592  return true;
593  }
594  return false;
595 }
596 
597 /* Set guided mode setpoint
598  * Note: Offset position command in NED frame or body frame will only be implemented if
599  * local reference frame has been initialised.
600  * Flag definition:
601  bit 0: x,y as offset coordinates
602  bit 1: x,y in body coordinates
603  bit 2: z as offset coordinates
604  bit 3: yaw as offset coordinates
605  bit 4: free
606  bit 5: x,y as vel
607  bit 6: z as vel
608  bit 7: yaw as rate
609  */
610 void autopilot_guided_update(uint8_t flags, float x, float y, float z, float yaw)
611 {
612  /* only update setpoints when in guided mode */
614  return;
615  }
616 
617  // handle x,y
618  struct FloatVect2 setpoint = {.x = x, .y = y};
619  if (bit_is_set(flags, 5)) { // velocity setpoint
620  if (bit_is_set(flags, 1)) { // set velocity in body frame
621  guidance_h_set_guided_body_vel(setpoint.x, setpoint.y);
622  } else {
623  guidance_h_set_guided_vel(setpoint.x, setpoint.y);
624  }
625  } else { // position setpoint
626  if (!bit_is_set(flags, 0) && !bit_is_set(flags, 1)) { // set absolute position setpoint
627  guidance_h_set_guided_pos(setpoint.x, setpoint.y);
628  } else {
630  if (bit_is_set(flags, 1)) { // set position as offset in body frame
631  float psi = stateGetNedToBodyEulers_f()->psi;
632 
633  setpoint.x = stateGetPositionNed_f()->x + cosf(-psi) * x + sinf(-psi) * y;
634  setpoint.y = stateGetPositionNed_f()->y - sinf(-psi) * x + cosf(-psi) * y;
635  } else { // set position as offset in NED frame
636  setpoint.x += stateGetPositionNed_f()->x;
637  setpoint.y += stateGetPositionNed_f()->y;
638  }
639  guidance_h_set_guided_pos(setpoint.x, setpoint.y);
640  }
641  }
642  }
643 
644  //handle z
645  if (bit_is_set(flags, 6)) { // speed set-point
647  } else { // position set-point
648  if (bit_is_set(flags, 2)) { // set position as offset in NED frame
650  z += stateGetPositionNed_f()->z;
652  }
653  } else {
655  }
656  }
657 
658  //handle yaw
659  if (bit_is_set(flags, 7)) { // speed set-point
661  } else { // position set-point
662  if (bit_is_set(flags, 3)) { // set yaw as offset
663  yaw += stateGetNedToBodyEulers_f()->psi; // will be wrapped to [-pi,pi] later
664  }
666  }
667 }
668 
669 void autopilot_check_in_flight(bool motors_on)
670 {
671  if (autopilot_in_flight) {
672  if (autopilot_in_flight_counter > 0) {
673  /* probably in_flight if thrust, speed and accel above IN_FLIGHT_MIN thresholds */
674  if ((stabilization_cmd[COMMAND_THRUST] <= AUTOPILOT_IN_FLIGHT_MIN_THRUST) &&
678  if (autopilot_in_flight_counter == 0) {
679  autopilot_in_flight = false;
680  }
681  } else { /* thrust, speed or accel not above min threshold, reset counter */
683  }
684  }
685  } else { /* currently not in flight */
687  motors_on) {
688  /* if thrust above min threshold, assume in_flight.
689  * Don't check for velocity and acceleration above threshold here...
690  */
691  if (stabilization_cmd[COMMAND_THRUST] > AUTOPILOT_IN_FLIGHT_MIN_THRUST) {
694  autopilot_in_flight = true;
695  }
696  } else { /* currently not in_flight and thrust below threshold, reset counter */
698  }
699  }
700  }
701 }
702 
703 
704 void autopilot_set_motors_on(bool motors_on)
705 {
706  if (autopilot_mode != AP_MODE_KILL && ahrs_is_aligned() && motors_on) {
707  autopilot_motors_on = true;
708  } else {
709  autopilot_motors_on = false;
710  }
713 }
714 
715 
716 #define THRESHOLD_1_PPRZ (MIN_PPRZ / 2)
717 #define THRESHOLD_2_PPRZ (MAX_PPRZ / 2)
718 
721 {
723  return autopilot_mode_auto2;
725  return MODE_AUTO1;
726  } else {
727  return MODE_MANUAL;
728  }
729 }
730 
739 #if defined RADIO_AUTO_MODE || defined(__DOXYGEN__)
740 static uint8_t ap_mode_of_two_switches(void)
741 {
743  /* RADIO_MODE in MANUAL position */
744  return MODE_MANUAL;
745  } else {
746  /* RADIO_MODE not in MANUAL position.
747  * Select AUTO mode bassed on RADIO_AUTO_MODE channel
748  */
749  if (radio_control.values[RADIO_AUTO_MODE] > THRESHOLD_2_PPRZ) {
750  return autopilot_mode_auto2;
751  } else {
752  return MODE_AUTO1;
753  }
754  }
755 }
756 #endif
757 
759 {
760 
761  if (kill_switch_is_on()) {
763  } else {
764 #ifdef RADIO_AUTO_MODE
765  INFO("Using RADIO_AUTO_MODE to switch between AUTO1 and AUTO2.")
766  uint8_t new_autopilot_mode = ap_mode_of_two_switches();
767 #else
768  uint8_t new_autopilot_mode = ap_mode_of_3way_switch();
769 #endif
770 
771  /* don't enter NAV mode if GPS is lost (this also prevents mode oscillations) */
772  if (!(new_autopilot_mode == AP_MODE_NAV && GpsIsLost())) {
773  /* always allow to switch to manual */
774  if (new_autopilot_mode == MODE_MANUAL) {
775  autopilot_set_mode(new_autopilot_mode);
776  }
777  /* if in HOME mode, don't allow switching to non-manual modes */
778  else if ((autopilot_mode != AP_MODE_HOME)
780  /* Allowed to leave home mode when UNLOCKED_HOME_MODE */
782 #endif
783  ) {
784  autopilot_set_mode(new_autopilot_mode);
785  }
786  }
787  }
788 
789  /* an arming sequence is used to start/stop motors.
790  * only allow arming if ahrs is aligned
791  */
792  if (ahrs_is_aligned()) {
795  }
796 
797  /* if not in FAILSAFE or HOME mode, read RC and set commands accordingly */
799 
800  /* if there are some commands that should always be set from RC, do it */
801 #ifdef SetAutoCommandsFromRC
802  SetAutoCommandsFromRC(commands, radio_control.values);
803 #endif
804 
805  /* if not in NAV_MODE set commands from the rc */
806 #ifdef SetCommandsFromRC
807  if (autopilot_mode != AP_MODE_NAV) {
808  SetCommandsFromRC(commands, radio_control.values);
809  }
810 #endif
811 
814  }
815 
816 }
bool guidance_v_set_guided_z(float z)
Set z setpoint in GUIDED mode.
Definition: guidance_v.c:481
int32_t current
current in milliamps
Definition: electrical.h:49
int32_t guidance_v_z_sp
altitude setpoint in meters (input).
Definition: guidance_v.c:119
unsigned short uint16_t
Definition: types.h:16
#define AP_MODE_KILL
Definition: autopilot.h:36
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
static void autopilot_arming_set(bool motors_on)
uint32_t nb_failure
Definition: motor_mixing.h:42
#define AP_MODE_ATTITUDE_DIRECT
Definition: autopilot.h:40
static void autopilot_arming_check_motors_on(void)
State machine to check if motors should be turned ON or OFF using the kill switch.
#define GUIDANCE_V_MODE_NAV
Definition: guidance_v.h:40
void stabilization_none_init(void)
#define GUIDANCE_H_MODE_RC_DIRECT
Definition: guidance_h.h:57
static void send_alive(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:172
float y
in meters
float phi
in radians
bool guidance_v_set_guided_vz(float vz)
Set z velocity setpoint in GUIDED mode.
Definition: guidance_v.c:491
#define GUIDANCE_H_MODE_CARE_FREE
Definition: guidance_h.h:58
#define POWER_SWITCH_GPIO
Definition: apogee_1.0.h:95
#define RADIO_ROLL
Definition: spektrum_arch.h:43
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1125
void autopilot_init(void)
Autopilot inititalization.
Definition: autopilot.c:175
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_arch.h:103
#define THRESHOLD_2_PPRZ
Definition: autopilot.c:717
uint8_t autopilot_mode
Definition: autopilot.c:69
#define RADIO_YAW
Definition: spektrum_arch.h:45
void guidance_h_read_rc(bool in_flight)
Definition: guidance_h.c:306
static bool stateIsLocalCoordinateValid(void)
Test if local coordinates are valid.
Definition: state.h:502
Dummy stabilization for rotorcrafts.
Periodic telemetry system header (includes downlink utility and generated code).
General attitude stabilization interface for rotorcrafts.
static bool kill_switch_is_on(void)
void guidance_h_run(bool in_flight)
Definition: guidance_h.c:365
#define AP_MODE_HOVER_DIRECT
Definition: autopilot.h:46
uint16_t autopilot_flight_time
flight time in seconds.
Definition: autopilot.c:48
uint8_t status
Definition: radio_control.h:53
void autopilot_set_motors_on(bool motors_on)
Definition: autopilot.c:704
#define FAILSAFE_MODE_TOO_FAR_FROM_HOME
Mode that is set when the plane is really too far from home.
Definition: autopilot.c:129
void guidance_v_init(void)
Definition: guidance_v.c:175
Some architecture independent helper functions for GPIOs.
#define AP_MODE_RATE_RC_CLIMB
Definition: autopilot.h:41
#define AUTOPILOT_IN_FLIGHT_MIN_ACCEL
minimum vertical acceleration for in_flight condition in m/s^2
Definition: autopilot.c:100
#define AP_MODE_FORWARD
Definition: autopilot.h:52
#define AP_MODE_RATE_Z_HOLD
Definition: autopilot.h:44
void guidance_v_mode_changed(uint8_t new_mode)
Definition: guidance_v.c:225
#define GUIDANCE_H_MODE_ATTITUDE
Definition: guidance_h.h:54
void stabilization_attitude_init(void)
stabilization_attitude_init
#define AP_MODE_ATTITUDE_Z_HOLD
Definition: autopilot.h:45
float psi
in radians
bool guidance_h_set_guided_body_vel(float vx, float vy)
Set body relative horizontal velocity setpoint in GUIDED mode.
Definition: guidance_h.c:703
Autopilot modes.
static void send_rotorcraft_rc(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:250
static void send_energy(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:211
void stabilization_init(void)
Definition: stabilization.c:30
void guidance_v_run(bool in_flight)
Definition: guidance_v.c:283
#define AP_MODE_HOVER_CLIMB
Definition: autopilot.h:47
uint8_t autopilot_mode_auto2
Definition: autopilot.c:70
#define AP_MODE_MODULE
Definition: autopilot.h:53
bool autopilot_motors_on
Definition: autopilot.c:76
uint32_t nb_saturation
Definition: motor_mixing.h:41
struct HorizontalGuidance guidance_h
Definition: guidance_h.c:80
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:58
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
void autopilot_check_in_flight(bool motors_on)
Definition: autopilot.c:669
static void send_actuators(struct transport_tx *trans, struct link_device *dev)
#define GUIDANCE_V_MODE_KILL
Definition: guidance_v.h:35
struct HorizontalGuidanceSetpoint sp
setpoints
Definition: guidance_h.h:99
#define FALSE
Definition: std.h:5
const pprz_t commands_failsafe[COMMANDS_NB]
Definition: commands.c:31
#define AP_MODE_RATE_DIRECT
Definition: autopilot.h:39
bool autopilot_guided_goto_body_relative(float dx, float dy, float dz, float dyaw)
Set position and heading setpoints wrt.
Definition: autopilot.c:573
Rate stabilization for rotorcrafts.
euler angles
float z
in meters
#define MODE_AUTO1
Definition: autopilot.h:85
Automatically arm the motors when applying throttle.
#define SetRotorcraftCommands(_cmd, _in_flight,_motor_on)
Set Rotorcraft commands.
Definition: autopilot.h:126
bool kill_throttle
Definition: autopilot.c:42
#define MODE_AUTO2
Definition: autopilot.h:88
static void send_dl_value(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:275
uint32_t autopilot_in_flight_counter
Definition: autopilot.c:73
float theta
in radians
void autopilot_SetModeHandler(float mode)
AP mode setting handler.
Definition: autopilot.c:408
bool guidance_h_set_guided_heading(float heading)
Set heading setpoint in GUIDED mode.
Definition: guidance_h.c:692
bool autopilot_power_switch
Definition: autopilot.c:80
#define AP_MODE_RC_DIRECT
Definition: autopilot.h:50
#define GUIDANCE_H_MODE_HOVER
Definition: guidance_h.h:55
Hardware independent API for actuators (servos, motor controllers).
static float heading
Definition: ahrs_infrared.c:45
#define AP_MODE_CARE_FREE_DIRECT
Definition: autopilot.h:51
Arm the motors using a switch.
static bool stateIsAttitudeValid(void)
Test if attitudes are valid.
Definition: state.h:1049
bool autopilot_ground_detected
Definition: autopilot.c:82
#define AUTOPILOT_IN_FLIGHT_MIN_THRUST
minimum thrust for in_flight condition in pprz_t units (max = 9600)
Definition: autopilot.c:105
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:896
Interface for electrical status: supply voltage, current, battery status, etc.
Architecture independent timing functions.
#define RADIO_PITCH
Definition: spektrum_arch.h:44
bool guidance_h_set_guided_pos(float x, float y)
Set horizontal position setpoint in GUIDED mode.
Definition: guidance_h.c:681
bool autopilot_guided_goto_ned_relative(float dx, float dy, float dz, float dyaw)
Set position and heading setpoints wrt.
Definition: autopilot.c:561
static void send_rotorcraft_cmd(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:280
Device independent GPS code (interface)
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1020
static void send_fp(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:223
#define GUIDANCE_V_MODE_CLIMB
Definition: guidance_v.h:38
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
signed short int16_t
Definition: types.h:17
void stabilization_attitude_set_failsafe_setpoint(void)
static void autopilot_arming_init(void)
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define FAILSAFE_DESCENT_SPEED
Set descent speed in failsafe mode.
Definition: autopilot.c:123
void autopilot_on_rc_frame(void)
Get autopilot mode from two 2way switches.
Definition: autopilot.c:758
#define GUIDANCE_V_MODE_HOVER
Definition: guidance_v.h:39
Hardware independent code for commands handling.
struct RadioControl radio_control
Definition: radio_control.c:30
#define GUIDANCE_H_MODE_NAV
Definition: guidance_h.h:56
static void send_status(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:187
#define RADIO_THROTTLE
Definition: spektrum_arch.h:42
#define AP_MODE_NAV
Definition: autopilot.h:49
#define GUIDANCE_H_MODE_MODULE_SETTING
#define GUIDANCE_H_MODE_KILL
Definition: guidance_h.h:52
bool guidance_h_set_guided_heading_rate(float rate)
Set heading rate setpoint in GUIDED mode.
Definition: guidance_h.c:722
void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:64
Arm the motors by with max yaw stick.
void autopilot_periodic(void)
Definition: autopilot.c:344
signed long int32_t
Definition: types.h:19
#define RADIO_CONTROL_NB_CHANNEL
Definition: spektrum_arch.h:34
#define RC_OK
Definition: radio_control.h:48
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
#define RADIO_MODE
Definition: spektrum_arch.h:59
uint8_t guidance_v_mode
Definition: guidance_v.c:99
static uint8_t ap_mode_of_3way_switch(void)
get autopilot mode as set by RADIO_MODE 3-way switch
Definition: autopilot.c:720
bool autopilot_guided_goto_ned(float x, float y, float z, float heading)
Set position and heading setpoints in GUIDED mode.
Definition: autopilot.c:550
#define SetCommands(t)
Definition: commands.h:41
int32_t heading
with INT32_ANGLE_FRAC
Definition: guidance_h.h:72
unsigned char uint8_t
Definition: types.h:14
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
Persistent settings interface.
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
static void send_attitude(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:177
static struct EnuCoor_i * stateGetSpeedEnu_i(void)
Get ground speed in local ENU coordinates (int).
Definition: state.h:860
uint16_t vsupply
supply voltage in decivolts
Definition: electrical.h:48
void guidance_h_mode_changed(uint8_t new_mode)
Definition: guidance_h.c:222
General stabilization interface for rotorcrafts.
#define AUTOPILOT_IN_FLIGHT_MIN_SPEED
minimum vertical speed for in_flight condition in m/s
Definition: autopilot.c:95
void guidance_v_read_rc(void)
Definition: guidance_v.c:203
bool autopilot_guided_move_ned(float vx, float vy, float vz, float heading)
Set velocity and heading setpoints in GUIDED mode.
Definition: autopilot.c:586
void autopilot_guided_update(uint8_t flags, float x, float y, float z, float yaw)
Set guided setpoints using flag mask in GUIDED mode.
Definition: autopilot.c:610
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:28
#define AP_MODE_HOVER_Z_HOLD
Definition: autopilot.h:48
#define GUIDANCE_V_MODE_FLIP
Definition: guidance_v.h:42
static int ahrs_is_aligned(void)
Definition: autopilot.c:109
#define AP_MODE_ATTITUDE_RC_CLIMB
Definition: autopilot.h:42
int32_t guidance_v_zd_sp
vertical speed setpoint in meter/s (input).
Definition: guidance_v.c:120
struct MotorMixing motor_mixing
Definition: motor_mixing.c:94
#define AP_MODE_FAILSAFE
Definition: autopilot.h:37
float energy
consumed energy in mAh
Definition: electrical.h:51
#define AP_MODE_HOME
Definition: autopilot.h:38
#define MODE_STARTUP
Definition: autopilot.c:145
#define SPEED_BFP_OF_REAL(_af)
struct Electrical electrical
Definition: electrical.c:65
bool autopilot_in_flight
Definition: autopilot.c:72
#define MODE_MANUAL
Default RC mode.
Definition: autopilot.h:82
#define GpsIsLost()
Definition: gps.h:45
#define AUTOPILOT_IN_FLIGHT_TIME
time steps for in_flight detection (at 20Hz, so 20=1second)
Definition: autopilot.c:90
#define GUIDANCE_V_MODE_RC_CLIMB
Definition: guidance_v.h:37
void stabilization_rate_init(void)
#define UNLOCKED_HOME_MODE
Definition: autopilot.c:151
static struct EnuCoor_i * stateGetPositionEnu_i(void)
Get position in local ENU coordinates (int).
Definition: state.h:668
#define NAV_PRESCALER
Definition: autopilot.c:343
#define RADIO_KILL_SWITCH
Definition: intermcu_ap.h:44
#define GUIDANCE_H_MODE_FORWARD
Definition: guidance_h.h:59
Motor Mixing.
#define GUIDANCE_V_MODE_RC_DIRECT
Definition: guidance_v.h:36
static void send_rc(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:245
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition: state.h:1107
#define GUIDANCE_H_MODE_RATE
Definition: guidance_h.h:53
uint8_t fix
status of fix
Definition: gps.h:99
void autopilot_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot.c:424
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition: state.h:704
#define AP_MODE_ATTITUDE_CLIMB
Definition: autopilot.h:43
#define THRESHOLD_1_PPRZ
Definition: autopilot.c:716
#define AP_MODE_GUIDED
Definition: autopilot.h:55
#define GUIDANCE_H_MODE_FLIP
Definition: guidance_h.h:61
#define GUIDANCE_V_MODE_MODULE_SETTING
#define GUIDANCE_V_MODE_GUIDED
Definition: guidance_v.h:43
bool guidance_h_set_guided_vel(float vx, float vy)
Set horizontal velocity setpoint in GUIDED mode.
Definition: guidance_h.c:711
struct GpsState gps
global GPS state
Definition: gps.c:75
void guidance_h_init(void)
Definition: guidance_h.c:169
bool autopilot_detect_ground_once
Definition: autopilot.c:83
bool autopilot_rc
Definition: autopilot.c:79
#define AP_MODE_FLIP
Definition: autopilot.h:54
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
struct Int32Vect2 pos
horizontal position setpoint in NED.
Definition: guidance_h.h:70
#define GUIDANCE_H_MODE_GUIDED
Definition: guidance_h.h:62
float x
in meters
uint8_t frame_rate
Definition: radio_control.h:56