Paparazzi UAS  v6.1.0_stable
Paparazzi is a free software Unmanned Aircraft System.
autopilot.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Pascal Brisset, Antoine Drouin
3  * Copyright (C) 2008-2012 The Paparazzi Team
4  * Copyright (C) 2016-2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, see
20  * <http://www.gnu.org/licenses/>.
21  */
22 
32 #include "autopilot.h"
33 
34 #include "generated/modules.h"
35 
36 #include "mcu_periph/uart.h"
37 #include "mcu_periph/sys_time.h"
38 #include "mcu_periph/gpio.h"
40 #include "modules/core/commands.h"
42 //#include "modules/energy/electrical.h"
44 
45 #include "modules/core/settings.h"
46 #include "generated/settings.h"
47 
48 #include "pprz_version.h"
49 
51 
52 
53 static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
54 {
55  static uint32_t ap_version = PPRZ_VERSION_INT;
56  static char *ver_desc = PPRZ_VERSION_DESC;
57  pprz_msg_send_AUTOPILOT_VERSION(trans, dev, AC_ID, &ap_version, strlen(ver_desc), ver_desc);
58 }
59 
60 static void send_alive(struct transport_tx *trans, struct link_device *dev)
61 {
62  pprz_msg_send_ALIVE(trans, dev, AC_ID, 16, MD5SUM);
63 }
64 
65 static void send_attitude(struct transport_tx *trans, struct link_device *dev)
66 {
67  struct FloatEulers *att = stateGetNedToBodyEulers_f();
68  pprz_msg_send_ATTITUDE(trans, dev, AC_ID, &(att->phi), &(att->psi), &(att->theta));
69 };
70 
71 static void send_dl_value(struct transport_tx *trans, struct link_device *dev)
72 {
73  PeriodicSendDlValue(trans, dev);
74 }
75 
76 #ifdef RADIO_CONTROL
77 static void send_rc(struct transport_tx *trans, struct link_device *dev)
78 {
79  pprz_msg_send_RC(trans, dev, AC_ID, RADIO_CONTROL_NB_CHANNEL, radio_control.values);
80 }
81 #endif
82 
83 #ifdef ACTUATORS
84 static void send_actuators(struct transport_tx *trans, struct link_device *dev)
85 {
86  pprz_msg_send_ACTUATORS(trans, dev, AC_ID , ACTUATORS_NB, actuators);
87 }
88 #endif
89 
90 static void send_minimal_com(struct transport_tx *trans, struct link_device *dev)
91 {
92  float lat = DegOfRad(stateGetPositionLla_f()->lat);
93  float lon = DegOfRad(stateGetPositionLla_f()->lon);
94  float hmsl = stateGetPositionUtm_f()->alt;
95  float gspeed = stateGetHorizontalSpeedNorm_f();
97  float climb = stateGetSpeedEnu_f()->z;
98  uint8_t throttle = (uint8_t)(100 * autopilot.throttle / MAX_PPRZ);
99 #if USE_GPS
100  uint8_t gps_fix = gps.fix;
101 #else
102  uint8_t gps_fix = 0;
103 #endif
104  pprz_msg_send_MINIMAL_COM(trans, dev, AC_ID,
105  &lat, &lon, &hmsl, &gspeed, &course, &climb,
106  &electrical.vsupply, &throttle, &autopilot.mode,
107  &nav_block, &gps_fix, &autopilot.flight_time);
108 }
109 
110 void autopilot_init(void)
111 {
112 #ifdef MODE_AUTO2
113  autopilot.mode_auto2 = MODE_AUTO2; // FIXME
114 #endif
116  autopilot.throttle = 0;
117  autopilot.motors_on = false;
118  autopilot.kill_throttle = true;
119  autopilot.in_flight = false;
120  autopilot.ground_detected = false;
122  autopilot.use_rc = true;
123  autopilot.power_switch = false;
124 #ifdef POWER_SWITCH_GPIO
126 #ifdef POWER_SWITCH_ENABLE
127  autopilot_set_power_switch(POWER_SWITCH_ENABLE); // set initial status
128 #else
129  gpio_clear(POWER_SWITCH_GPIO); // by default POWER OFF
130 #endif
131 #endif
132 
133  // call firmware specific init
135 
136  // call autopilot implementation init after guidance modules init
137  // (should be guaranteed by modules dependencies)
138  // it will set startup mode
139 #if USE_GENERATED_AUTOPILOT
141 #else
143 #endif
144 
145  // register messages
146  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
150 #ifdef ACTUATORS
152 #endif
153 #ifdef RADIO_CONTROL
155 #endif
157 }
158 
162 {
163 #if USE_GENERATED_AUTOPILOT
165 #else
167 #endif
168 }
169 
172 void WEAK autopilot_event(void) {}
173 
177 {
178 #if USE_GENERATED_AUTOPILOT
180 #else
182 #endif
183 }
184 
187 bool autopilot_set_mode(uint8_t new_autopilot_mode)
188 {
190 #if USE_GENERATED_AUTOPILOT
191  autopilot_generated_set_mode(new_autopilot_mode);
192 #else
193  autopilot_static_set_mode(new_autopilot_mode);
194 #endif
195  return (autopilot.mode != mode);
196 }
197 
201 {
202 #if USE_GENERATED_AUTOPILOT
204 #else
206 #endif
207 }
208 
212 {
213  return autopilot.mode;
214 }
215 
219 {
221  autopilot.launch = false;
222 }
223 
227 void autopilot_set_motors_on(bool motors_on)
228 {
229 #if USE_GENERATED_AUTOPILOT
231 #else
233 #endif
235 }
236 
240 {
241  return autopilot.motors_on;
242 }
243 
247 {
248  if (kill) {
250  } else {
252  }
253 }
254 
258 {
259  return autopilot.kill_throttle;
260 }
261 
265 void WEAK autopilot_check_in_flight(bool motors_on __attribute__((unused))) {}
266 
271 
274 void autopilot_set_in_flight(bool in_flight)
275 {
276  autopilot.in_flight = in_flight;
277  if (!in_flight) {
279  }
280 }
281 
285 {
286  return autopilot.in_flight;
287 }
288 
291 void autopilot_set_power_switch(bool power_switch)
292 {
293 #ifdef POWER_SWITCH_GPIO
294  if (power_switch) {
296  } else {
298  }
299 #endif
300  autopilot.power_switch = power_switch;
301 }
302 
306 {
307  if (autopilot.kill_throttle) {
308  settings_store_flag = true;
309  settings_store();
310  }
311 }
312 
316 {
317  if (autopilot.kill_throttle) {
318  settings_clear_flag = true;
319  settings_clear();
320  }
321 }
322 
326 {
327  send_autopilot_version(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
328 }
329 
333 void WEAK autopilot_send_mode(void) {}
334 
autopilot_get_motors_on
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:239
radio_control.h
autopilot_clear_settings
void autopilot_clear_settings(void)
clear settings
Definition: autopilot.c:315
autopilot_set_in_flight
void autopilot_set_in_flight(bool in_flight)
set in_flight flag
Definition: autopilot.c:274
autopilot_reset_in_flight_counter
void WEAK autopilot_reset_in_flight_counter(void)
reset in_flight counter actual implementation is firmware dependent
Definition: autopilot.c:270
MAX_PPRZ
#define MAX_PPRZ
Definition: paparazzi.h:8
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
stateGetHorizontalSpeedDir_f
static float stateGetHorizontalSpeedDir_f(void)
Get dir of horizontal ground speed (float).
Definition: state.h:944
autopilot_generated_periodic
void autopilot_generated_periodic(void)
Definition: autopilot_generated.c:50
settings_clear_flag
bool settings_clear_flag
Definition: settings.c:40
autopilot_generated_init
void autopilot_generated_init(void)
Definition: autopilot_generated.c:41
MODE_AUTO2
#define MODE_AUTO2
Definition: autopilot_static.h:66
settings.h
autopilot_reset_flight_time
void autopilot_reset_flight_time(void)
reset flight time and launch
Definition: autopilot.c:218
autopilot_static_set_motors_on
void autopilot_static_set_motors_on(bool motors_on)
Definition: autopilot_static.c:205
autopilot_generated_on_rc_frame
void autopilot_generated_on_rc_frame(void)
Definition: autopilot_generated.c:104
pprz_autopilot::flight_time
uint16_t flight_time
flight time in seconds
Definition: autopilot.h:65
pprz_autopilot::mode
uint8_t mode
current autopilot mode
Definition: autopilot.h:63
autopilot_generated_SetModeHandler
void autopilot_generated_SetModeHandler(float mode)
AP mode setting handler.
Definition: autopilot_generated.c:71
autopilot_init
void autopilot_init(void)
Autopilot initialization function.
Definition: autopilot.c:110
stateGetNedToBodyEulers_f
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
autopilot_static_init
void autopilot_static_init(void)
Static autopilot API.
Definition: autopilot_static.c:90
POWER_SWITCH_GPIO
#define POWER_SWITCH_GPIO
Definition: board.h:153
gpio_setup_output
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
pprz_autopilot::use_rc
bool use_rc
enable/disable RC input
Definition: autopilot.h:72
autopilot_SetModeHandler
void autopilot_SetModeHandler(float mode)
AP mode setting handler.
Definition: autopilot.c:200
pprz_autopilot
PPRZ Autopilot structure definition.
Definition: autopilot.h:62
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
autopilot_event
void WEAK autopilot_event(void)
AP event call.
Definition: autopilot.c:172
stateGetPositionUtm_f
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:692
send_autopilot_version
static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:53
Electrical::vsupply
float vsupply
supply voltage in V
Definition: electrical.h:45
EnuCoor_f::z
float z
in meters
Definition: pprz_geodetic_float.h:75
FloatEulers::theta
float theta
in radians
Definition: pprz_algebra_float.h:86
autopilot_in_flight
bool autopilot_in_flight(void)
get in_flight flag
Definition: autopilot.c:284
gpio_clear
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_arch.h:108
telemetry.h
autopilot_set_power_switch
void autopilot_set_power_switch(bool power_switch)
set power switch
Definition: autopilot.c:291
autopilot_send_version
void autopilot_send_version(void)
send autopilot version
Definition: autopilot.c:325
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
pprz_autopilot::motors_on
bool motors_on
motor status
Definition: autopilot.h:68
GpsState::fix
uint8_t fix
status of fix
Definition: gps.h:107
FloatEulers::phi
float phi
in radians
Definition: pprz_algebra_float.h:85
autopilot
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:50
autopilot_throttle_killed
bool autopilot_throttle_killed(void)
get kill status
Definition: autopilot.c:257
pprz_autopilot::in_flight
bool in_flight
in flight status
Definition: autopilot.h:70
UtmCoor_f::alt
float alt
in meters (above WGS84 reference ellipsoid or above MSL)
Definition: pprz_geodetic_float.h:84
pprz_autopilot::throttle
pprz_t throttle
throttle level as will be displayed in GCS
Definition: autopilot.h:66
nav_block
uint8_t nav_block
Definition: common_flight_plan.c:35
settings_store_flag
bool settings_store_flag
flag for setting feedback.
Definition: settings.c:38
sys_time.h
Architecture independent timing functions.
RADIO_CONTROL_NB_CHANNEL
#define RADIO_CONTROL_NB_CHANNEL
Definition: intermcu_ap.h:49
register_periodic_telemetry
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
autopilot_generated_set_mode
void autopilot_generated_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot_generated.c:77
pprz_autopilot::ground_detected
bool ground_detected
automatic detection of landing
Definition: autopilot.h:74
autopilot_send_mode
void WEAK autopilot_send_mode(void)
send autopilot mode actual implementation is firmware dependent
Definition: autopilot.c:333
autopilot.h
settings_clear
int32_t settings_clear(void)
clear all persistent settings from flash
Definition: settings.c:77
autopilot_firmware_init
void autopilot_firmware_init(void)
Init function.
Definition: autopilot_firmware.c:114
autopilot_set_motors_on
void autopilot_set_motors_on(bool motors_on)
turn motors on/off, eventually depending of the current mode set kill_throttle accordingly FIXME is i...
Definition: autopilot.c:227
autopilot_set_kill_throttle
void autopilot_set_kill_throttle(bool kill)
set kill throttle
Definition: autopilot.c:246
autopilot_generated_set_motors_on
void autopilot_generated_set_motors_on(bool motors_on)
Definition: autopilot_generated.c:85
course
static int16_t course[3]
Definition: airspeed_uADC.c:58
autopilot_periodic
void autopilot_periodic(void)
AP periodic call.
Definition: autopilot.c:161
autopilot_static_set_mode
void autopilot_static_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot_static.c:193
stateGetHorizontalSpeedNorm_f
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:935
autopilot_store_settings
void autopilot_store_settings(void)
store settings
Definition: autopilot.c:305
send_alive
static void send_alive(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:60
autopilot_static_periodic
void autopilot_static_periodic(void)
Definition: autopilot_static.c:104
gpio.h
stateGetSpeedEnu_f
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition: state.h:917
actuators.h
autopilot_set_mode
bool autopilot_set_mode(uint8_t new_autopilot_mode)
set autopilot mode
Definition: autopilot.c:187
send_actuators
static void send_actuators(struct transport_tx *trans, struct link_device *dev)
Definition: demo_ahrs_actuators.c:154
autopilot_check_in_flight
void WEAK autopilot_check_in_flight(bool motors_on)
in flight check utility function actual implementation is firmware dependent
Definition: autopilot.c:265
pprz_autopilot::mode_auto2
uint8_t mode_auto2
FIXME hide this in a private part ?
Definition: autopilot.h:64
autopilot_static_SetModeHandler
void autopilot_static_SetModeHandler(float new_autopilot_mode)
Definition: autopilot_static.c:200
send_attitude
static void send_attitude(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:65
send_minimal_com
static void send_minimal_com(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:90
FloatEulers
euler angles
Definition: pprz_algebra_float.h:84
mode
static uint8_t mode
mode holds the current sonar mode mode = 0 used at high altitude, uses 16 wave patterns mode = 1 used...
Definition: sonar_bebop.c:69
commands.h
Hardware independent code for commands handling.
electrical
struct Electrical electrical
Definition: electrical.c:66
autopilot_get_mode
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition: autopilot.c:211
FloatEulers::psi
float psi
in radians
Definition: pprz_algebra_float.h:87
stateGetPositionLla_f
static struct LlaCoor_f * stateGetPositionLla_f(void)
Get position in LLA coordinates (float).
Definition: state.h:728
autopilot_on_rc_frame
void autopilot_on_rc_frame(void)
RC frame handler.
Definition: autopilot.c:176
send_rc
static void send_rc(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:77
pprz_autopilot::power_switch
bool power_switch
enable/disable power from power switch (if any)
Definition: autopilot.h:73
settings_store
int32_t settings_store(void)
store settings marked as persistent to flash
Definition: settings.c:57
send_dl_value
static void send_dl_value(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:71
gpio_set
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:98
autopilot_static_on_rc_frame
void autopilot_static_on_rc_frame(void)
Function to be called when a message from FBW is available.
Definition: autopilot_static.c:111
gps
struct GpsState gps
global GPS state
Definition: gps.c:69
pprz_autopilot::kill_throttle
bool kill_throttle
allow autopilot to use throttle
Definition: autopilot.h:69
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
pprz_autopilot::launch
bool launch
request launch
Definition: autopilot.h:71
pprz_autopilot::detect_ground_once
bool detect_ground_once
enable automatic detection of ground (one shot)
Definition: autopilot.h:75
radio_control
struct RadioControl radio_control
Definition: radio_control.c:30
RadioControl::values
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:69