Paparazzi UAS  v5.14.0_stable-0-g3f680d1
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 #ifdef POWER_SWITCH_ENABLE
107  autopilot_set_power_switch(POWER_SWITCH_ENABLE); // set initial status
108 #else
109  gpio_clear(POWER_SWITCH_GPIO); // by default POWER OFF
110 #endif
111 #endif
112 
113  // call firmware specific init
115 
116  // static / generated AP init part is called later by main program
117  // and will set the correct initial mode
118 
119  // register messages
120  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
124 #ifdef ACTUATORS
126 #endif
127 #ifdef RADIO_CONTROL
129 #endif
130 }
131 
135 {
136 #if USE_GENERATED_AUTOPILOT
138 #else
140 #endif
141 }
142 
145 void WEAK autopilot_event(void) {}
146 
150 {
151 #if USE_GENERATED_AUTOPILOT
153 #else
155 #endif
156 }
157 
160 bool autopilot_set_mode(uint8_t new_autopilot_mode)
161 {
162 #if USE_GENERATED_AUTOPILOT
163  autopilot_generated_set_mode(new_autopilot_mode);
164 #else
165  autopilot_static_set_mode(new_autopilot_mode);
166 #endif
167  return (autopilot.mode != new_autopilot_mode);
168 }
169 
173 {
174 #if USE_GENERATED_AUTOPILOT
176 #else
178 #endif
179 }
180 
184 {
185  return autopilot.mode;
186 }
187 
191 {
193  autopilot.launch = false;
194 }
195 
199 void autopilot_set_motors_on(bool motors_on)
200 {
201 #if USE_GENERATED_AUTOPILOT
203 #else
205 #endif
207 }
208 
212 {
213  return autopilot.motors_on;
214 }
215 
219 {
220  if (kill) {
222  } else {
224  }
225 }
226 
230 {
231  return autopilot.kill_throttle;
232 }
233 
237 void WEAK autopilot_check_in_flight(bool motors_on __attribute__((unused))) {}
238 
243 
246 void autopilot_set_in_flight(bool in_flight)
247 {
248  autopilot.in_flight = in_flight;
249  if (!in_flight) {
251  }
252 }
253 
257 {
258  return autopilot.in_flight;
259 }
260 
263 void autopilot_set_power_switch(bool power_switch)
264 {
265 #ifdef POWER_SWITCH_GPIO
266  if (power_switch) {
268  } else {
270  }
271 #endif
272  autopilot.power_switch = power_switch;
273 }
274 
278 {
279  if (autopilot.kill_throttle) {
280  settings_store_flag = true;
281  settings_store();
282  }
283 }
284 
288 {
289  if (autopilot.kill_throttle) {
290  settings_clear_flag = true;
291  settings_clear();
292  }
293 }
294 
298 {
299  send_autopilot_version(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
300 }
301 
305 void WEAK autopilot_send_mode(void) {}
306 
bool launch
request launch
Definition: autopilot.h:66
void autopilot_reset_flight_time(void)
reset flight time and launch
Definition: autopilot.c:190
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:67
float phi
in radians
void WEAK autopilot_reset_in_flight_counter(void)
reset in_flight counter actual implementation is firmware dependent
Definition: autopilot.c:242
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:287
Periodic telemetry system header (includes downlink utility and generated code).
void autopilot_SetModeHandler(float mode)
AP mode setting handler.
Definition: autopilot.c:172
bool autopilot_throttle_killed(void)
get kill status
Definition: autopilot.c:229
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:211
bool in_flight
in flight status
Definition: autopilot.h:65
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:160
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:69
void WEAK autopilot_check_in_flight(bool motors_on)
in flight check utility function actual implementation is firmware dependent
Definition: autopilot.c:237
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
#define RADIO_CONTROL_NB_CHANNEL
Definition: intermcu_ap.h:48
void autopilot_store_settings(void)
store settings
Definition: autopilot.c:277
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:305
bool detect_ground_once
enable automatic detection of ground (one shot)
Definition: autopilot.h:70
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:68
Architecture independent timing functions.
bool autopilot_in_flight(void)
get in_flight flag
Definition: autopilot.c:256
void autopilot_set_kill_throttle(bool kill)
set kill throttle
Definition: autopilot.c:218
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:145
static void send_alive(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:60
PPRZ Autopilot structure definition.
Definition: autopilot.h:58
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
Core autopilot interface common to all firmwares.
bool motors_on
arming status
Definition: autopilot.h:63
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:69
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:199
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:134
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:149
void autopilot_set_in_flight(bool in_flight)
set in_flight flag
Definition: autopilot.c:246
bool kill_throttle
allow autopilot to use throttle
Definition: autopilot.h:64
bool ground_detected
automatic detection of landing
Definition: autopilot.h:69
void autopilot_set_power_switch(bool power_switch)
set power switch
Definition: autopilot.c:263
void autopilot_send_version(void)
send autopilot version
Definition: autopilot.c:297
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:183