Paparazzi UAS  v5.15_devel-230-gc96ce27
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.throttle = 0;
98  autopilot.motors_on = false;
99  autopilot.kill_throttle = true;
100  autopilot.in_flight = false;
101  autopilot.ground_detected = false;
103  autopilot.use_rc = true;
104  autopilot.power_switch = false;
105 #ifdef POWER_SWITCH_GPIO
107 #ifdef POWER_SWITCH_ENABLE
108  autopilot_set_power_switch(POWER_SWITCH_ENABLE); // set initial status
109 #else
110  gpio_clear(POWER_SWITCH_GPIO); // by default POWER OFF
111 #endif
112 #endif
113 
114  // call firmware specific init
116 
117  // static / generated AP init part is called later by main program
118  // and will set the correct initial mode
119 
120  // register messages
121  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
125 #ifdef ACTUATORS
127 #endif
128 #ifdef RADIO_CONTROL
130 #endif
131 }
132 
136 {
137 #if USE_GENERATED_AUTOPILOT
139 #else
141 #endif
142 }
143 
146 void WEAK autopilot_event(void) {}
147 
151 {
152 #if USE_GENERATED_AUTOPILOT
154 #else
156 #endif
157 }
158 
161 bool autopilot_set_mode(uint8_t new_autopilot_mode)
162 {
163 #if USE_GENERATED_AUTOPILOT
164  autopilot_generated_set_mode(new_autopilot_mode);
165 #else
166  autopilot_static_set_mode(new_autopilot_mode);
167 #endif
168  return (autopilot.mode != new_autopilot_mode);
169 }
170 
174 {
175 #if USE_GENERATED_AUTOPILOT
177 #else
179 #endif
180 }
181 
185 {
186  return autopilot.mode;
187 }
188 
192 {
194  autopilot.launch = false;
195 }
196 
200 void autopilot_set_motors_on(bool motors_on)
201 {
202 #if USE_GENERATED_AUTOPILOT
204 #else
206 #endif
208 }
209 
213 {
214  return autopilot.motors_on;
215 }
216 
220 {
221  if (kill) {
223  } else {
225  }
226 }
227 
231 {
232  return autopilot.kill_throttle;
233 }
234 
238 void WEAK autopilot_check_in_flight(bool motors_on __attribute__((unused))) {}
239 
244 
247 void autopilot_set_in_flight(bool in_flight)
248 {
249  autopilot.in_flight = in_flight;
250  if (!in_flight) {
252  }
253 }
254 
258 {
259  return autopilot.in_flight;
260 }
261 
264 void autopilot_set_power_switch(bool power_switch)
265 {
266 #ifdef POWER_SWITCH_GPIO
267  if (power_switch) {
269  } else {
271  }
272 #endif
273  autopilot.power_switch = power_switch;
274 }
275 
279 {
280  if (autopilot.kill_throttle) {
281  settings_store_flag = true;
282  settings_store();
283  }
284 }
285 
289 {
290  if (autopilot.kill_throttle) {
291  settings_clear_flag = true;
292  settings_clear();
293  }
294 }
295 
299 {
300  send_autopilot_version(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
301 }
302 
306 void WEAK autopilot_send_mode(void) {}
307 
bool launch
request launch
Definition: autopilot.h:71
void autopilot_reset_flight_time(void)
reset flight time and launch
Definition: autopilot.c:191
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:72
float phi
in radians
void WEAK autopilot_reset_in_flight_counter(void)
reset in_flight counter actual implementation is firmware dependent
Definition: autopilot.c:243
void autopilot_generated_SetModeHandler(float mode)
AP mode setting handler.
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:288
Periodic telemetry system header (includes downlink utility and generated code).
void autopilot_SetModeHandler(float mode)
AP mode setting handler.
Definition: autopilot.c:173
bool autopilot_throttle_killed(void)
get kill status
Definition: autopilot.c:230
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:212
bool in_flight
in flight status
Definition: autopilot.h:70
Some architecture independent helper functions for GPIOs.
uint16_t flight_time
flight time in seconds
Definition: autopilot.h:65
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:161
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:238
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:49
void autopilot_store_settings(void)
store settings
Definition: autopilot.c:278
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:306
bool detect_ground_once
enable automatic detection of ground (one shot)
Definition: autopilot.h:75
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:73
Architecture independent timing functions.
bool autopilot_in_flight(void)
get in_flight flag
Definition: autopilot.c:257
void autopilot_set_kill_throttle(bool kill)
set kill throttle
Definition: autopilot.c:219
uint8_t mode_auto2
FIXME hide this in a private part ?
Definition: autopilot.h:64
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 POWER_SWITCH_GPIO
Definition: board.h:153
#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:146
static void send_alive(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:60
PPRZ Autopilot structure definition.
Definition: autopilot.h:62
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:68
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:200
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:135
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:150
void autopilot_set_in_flight(bool in_flight)
set in_flight flag
Definition: autopilot.c:247
bool kill_throttle
allow autopilot to use throttle
Definition: autopilot.h:69
bool ground_detected
automatic detection of landing
Definition: autopilot.h:74
void autopilot_set_power_switch(bool power_switch)
set power switch
Definition: autopilot.c:264
void autopilot_send_version(void)
send autopilot version
Definition: autopilot.c:298
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:63
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:184
pprz_t throttle
throttle level as will be displayed in GCS
Definition: autopilot.h:66