Paparazzi UAS  v5.12_stable-4-g9b43e9b
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) 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 "subsystems/commands.h"
41 #include "subsystems/actuators.h"
42 //#include "subsystems/electrical.h"
44 
45 #include "subsystems/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 
91 void autopilot_init(void)
92 {
93 #ifdef MODE_AUTO2
94  autopilot.mode_auto2 = MODE_AUTO2; // FIXME
95 #endif
97  autopilot.motors_on = false;
98  autopilot.kill_throttle = true;
99  autopilot.in_flight = false;
100  autopilot.ground_detected = false;
102  autopilot.use_rc = true;
103  autopilot.power_switch = false;
104 #ifdef POWER_SWITCH_GPIO
106  gpio_clear(POWER_SWITCH_GPIO); // POWER OFF
107 #endif
108 
109  // call firmware specific init
111 
112  // static / generated AP init part is called later by main program
113  // and will set the correct initial mode
114 
115  // register messages
116  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
120 #ifdef ACTUATORS
122 #endif
123 #ifdef RADIO_CONTROL
125 #endif
126 }
127 
131 {
132 #if USE_GENERATED_AUTOPILOT
134 #else
136 #endif
137 }
138 
141 void WEAK autopilot_event(void) {}
142 
146 {
147 #if USE_GENERATED_AUTOPILOT
149 #else
151 #endif
152 }
153 
156 bool autopilot_set_mode(uint8_t new_autopilot_mode)
157 {
158 #if USE_GENERATED_AUTOPILOT
159  autopilot_generated_set_mode(new_autopilot_mode);
160 #else
161  autopilot_static_set_mode(new_autopilot_mode);
162 #endif
163  return (autopilot.mode != new_autopilot_mode);
164 }
165 
169 {
170 #if USE_GENERATED_AUTOPILOT
172 #else
174 #endif
175 }
176 
180 {
181  return autopilot.mode;
182 }
183 
187 {
189  autopilot.launch = false;
190 }
191 
195 void autopilot_set_motors_on(bool motors_on)
196 {
197 #if USE_GENERATED_AUTOPILOT
199 #else
201 #endif
203 }
204 
208 {
209  return autopilot.motors_on;
210 }
211 
215 {
216  if (kill) {
218  } else {
220  }
221 }
222 
226 {
227  return autopilot.kill_throttle;
228 }
229 
233 void WEAK autopilot_check_in_flight(bool motors_on __attribute__((unused))) {}
234 
239 
242 void autopilot_set_in_flight(bool in_flight)
243 {
244  autopilot.in_flight = in_flight;
245  if (!in_flight) {
247  }
248 }
249 
253 {
254  return autopilot.in_flight;
255 }
256 
259 void autopilot_set_power_switch(bool power_switch)
260 {
261 #ifdef POWER_SWITCH_GPIO
262  if (power_switch) {
264  } else {
266  }
267 #endif
268  autopilot.power_switch = power_switch;
269 }
270 
274 {
275  if (autopilot.kill_throttle) {
276  settings_store_flag = true;
277  settings_store();
278  }
279 }
280 
284 {
285  if (autopilot.kill_throttle) {
286  settings_clear_flag = true;
287  settings_clear();
288  }
289 }
290 
294 {
295  send_autopilot_version(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
296 }
297 
301 void WEAK autopilot_send_mode(void) {}
302 
bool launch
request launch
Definition: autopilot.h:65
void autopilot_reset_flight_time(void)
reset flight time and launch
Definition: autopilot.c:186
static void send_dl_value(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:71
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void autopilot_firmware_init(void)
Init function.
bool use_rc
enable/disable RC input
Definition: autopilot.h:66
float phi
in radians
void WEAK autopilot_reset_in_flight_counter(void)
reset in_flight counter actual implementation is firmware dependent
Definition: autopilot.c:238
void autopilot_generated_SetModeHandler(float mode)
AP mode setting handler.
#define POWER_SWITCH_GPIO
Definition: apogee_1.0.h:95
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_arch.h:108
void autopilot_clear_settings(void)
clear settings
Definition: autopilot.c:283
Periodic telemetry system header (includes downlink utility and generated code).
void autopilot_SetModeHandler(float mode)
AP mode setting handler.
Definition: autopilot.c:168
bool autopilot_throttle_killed(void)
get kill status
Definition: autopilot.c:225
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:207
bool in_flight
in flight status
Definition: autopilot.h:64
Some architecture independent helper functions for GPIOs.
uint16_t flight_time
flight time in seconds
Definition: autopilot.h:61
float psi
in radians
void autopilot_static_SetModeHandler(float new_autopilot_mode)
bool autopilot_set_mode(uint8_t new_autopilot_mode)
set autopilot mode
Definition: autopilot.c:156
void autopilot_init(void)
Autopilot initialization function.
Definition: autopilot.c:91
void autopilot_generated_on_rc_frame(void)
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:58
void WEAK autopilot_check_in_flight(bool motors_on)
in flight check utility function actual implementation is firmware dependent
Definition: autopilot.c:233
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
int32_t settings_store(void)
store settings marked as persistent to flash
Definition: settings.c:57
static void send_actuators(struct transport_tx *trans, struct link_device *dev)
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:50
void autopilot_static_set_mode(uint8_t new_autopilot_mode)
euler angles
void autopilot_store_settings(void)
store settings
Definition: autopilot.c:273
float theta
in radians
Hardware independent API for actuators (servos, motor controllers).
void WEAK autopilot_send_mode(void)
send autopilot mode actual implementation is firmware dependent
Definition: autopilot.c:301
bool detect_ground_once
enable automatic detection of ground (one shot)
Definition: autopilot.h:69
void autopilot_generated_set_mode(uint8_t new_autopilot_mode)
int32_t settings_clear(void)
clear all persistent settings from flash
Definition: settings.c:77
void autopilot_static_on_rc_frame(void)
Function to be called when a message from FBW is available.
void autopilot_static_periodic(void)
bool power_switch
enable/disable power from power switch (if any)
Definition: autopilot.h:67
Architecture independent timing functions.
bool autopilot_in_flight(void)
get in_flight flag
Definition: autopilot.c:252
void autopilot_set_kill_throttle(bool kill)
set kill throttle
Definition: autopilot.c:214
uint8_t mode_auto2
FIXME hide this in a private part ?
Definition: autopilot.h:60
static void send_attitude(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:65
unsigned long uint32_t
Definition: types.h:18
#define MODE_AUTO2
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void autopilot_generated_set_motors_on(bool motors_on)
Hardware independent code for commands handling.
struct RadioControl radio_control
Definition: radio_control.c:30
bool settings_store_flag
flag for setting feedback.
Definition: settings.c:38
void WEAK autopilot_event(void)
AP event call.
Definition: autopilot.c:141
static void send_alive(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:60
PPRZ Autopilot structure definition.
Definition: autopilot.h:58
#define RADIO_CONTROL_NB_CHANNEL
Definition: spektrum_arch.h:34
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
Core autopilot interface common to all firmwares.
bool motors_on
motor status
Definition: autopilot.h:62
unsigned char uint8_t
Definition: types.h:14
void autopilot_static_set_motors_on(bool motors_on)
Persistent settings interface.
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:66
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:195
static void send_rc(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:77
void autopilot_periodic(void)
AP periodic call.
Definition: autopilot.c:130
static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:53
void autopilot_on_rc_frame(void)
RC frame handler.
Definition: autopilot.c:145
void autopilot_set_in_flight(bool in_flight)
set in_flight flag
Definition: autopilot.c:242
bool kill_throttle
allow autopilot to use throttle
Definition: autopilot.h:63
bool ground_detected
automatic detection of landing
Definition: autopilot.h:68
void autopilot_set_power_switch(bool power_switch)
set power switch
Definition: autopilot.c:259
void autopilot_send_version(void)
send autopilot version
Definition: autopilot.c:293
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:98
uint8_t mode
current autopilot mode
Definition: autopilot.h:59
void autopilot_generated_periodic(void)
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
bool settings_clear_flag
Definition: settings.c:40
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition: autopilot.c:179