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
main_fbw.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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  */
22 
29 #include <inttypes.h>
30 #include "mcu.h"
31 #include "led.h"
32 #include "mcu_periph/sys_time.h"
33 
34 #include "subsystems/commands.h"
35 #include "subsystems/actuators.h"
36 #if USE_MOTOR_MIXING
38 #endif
39 
40 #include "subsystems/electrical.h"
45 
46 #define MODULES_C
47 #include "generated/modules.h"
48 
49 /* So one can use these in command_laws section */
50 #define And(x, y) ((x) && (y))
51 #define Or(x, y) ((x) || (y))
52 #define Min(x,y) (x < y ? x : y)
53 #define Max(x,y) (x > y ? x : y)
54 #define LessThan(_x, _y) ((_x) < (_y))
55 #define MoreThan(_x, _y) ((_x) > (_y))
56 
57 
61 bool fbw_motors_on = false;
62 
63 /* MODULES_FREQUENCY is defined in generated/modules.h
64  * according to main_freq parameter set for modules in airframe file
65  */
66 PRINT_CONFIG_VAR(MODULES_FREQUENCY)
67 
73 
75 int main(void)
76 {
77  main_init();
78 
79  while (1) {
81  main_event();
82  }
83 
84  return 0;
85 }
86 
89 {
90  // Set startup mode to Failsafe
92 
93  mcu_init();
94 
95  actuators_init();
96 
98 
99 #if USE_MOTOR_MIXING
101 #endif
102 
104 
105  modules_init();
106 
107  mcu_int_enable();
108 
109  intermcu_init();
110 
111  // Register the timers for the periodic functions
112  main_periodic_tid = sys_time_register_timer((1. / PERIODIC_FREQUENCY), NULL);
113  modules_tid = sys_time_register_timer(1. / MODULES_FREQUENCY, NULL);
114  radio_control_tid = sys_time_register_timer((1. / 60.), NULL);
116  telemetry_tid = sys_time_register_timer((1. / 10.), NULL);
117 }
118 
119 
121 // PERIODIC
122 
124 {
126  main_periodic();
127  }
129  modules_periodic_task();
130  }
133  }
136  }
139  }
140 }
141 
143 {
144  /* Send status to AP */
146 
147  /* Handle Modems */
148  // TODO
149 }
150 
151 /* Checks the different safety cases and sets the correct FBW mode */
153 {
154  /* Safety logic */
155  bool ap_lost = (intermcu.status == INTERMCU_LOST);
156  bool rc_lost = (radio_control.status == RC_REALLY_LOST);
157 
158  // Both the RC and AP are lost
159  if (rc_lost && ap_lost) {
161  }
162  // RC is valid but lost AP
163  else if (!rc_lost && ap_lost) {
164  // Only crucial when AP was in control
165  if (fbw_mode == FBW_MODE_AUTO) {
167  }
168  }
169  // RC is lost but AP is valid
170  else if (rc_lost && !ap_lost) {
171 
172  // Lost RC while flying in manual trough FBW
173  if (fbw_mode == FBW_MODE_MANUAL) {
175  }
176  // Allways keep failsafe when RC is lost
177  else if (fbw_mode == FBW_MODE_FAILSAFE) {
178  // No change: failsafe stays failsafe
179  }
180  // Lost RC while in working Auto mode
181  else {
183  }
184  }
185 }
186 
187 /* Sets the actual actuator commands */
189 {
190  /* Inter-MCU watchdog */
192 
193  /* Safety check and set FBW mode */
195 
196 #ifdef BOARD_PX4IO
197  //due to a baud rate issue on PX4, for a few seconds the baud is 1500000 however this may result in package loss, causing the motors to spin at random
198  //to prevent this situation:
199  if (intermcu.stable_px4_baud != PPRZ_BAUD) {
201  fbw_motors_on = false;
202  //signal to user whether fbw can be flashed:
203 #ifdef FBW_MODE_LED
204  LED_OFF(FBW_MODE_LED); // causes really fast blinking
205 #endif
206  }
207 #endif
208 
209  // TODO make module out of led blink?
210 #ifdef FBW_MODE_LED
211  static uint16_t dv = 0;
212  if (fbw_mode == FBW_MODE_FAILSAFE) {
213  if (!(dv++ % (PERIODIC_FREQUENCY / 20))) { LED_TOGGLE(FBW_MODE_LED);}
214  } else if (fbw_mode == FBW_MODE_MANUAL) {
215  if (!(dv++ % (PERIODIC_FREQUENCY))) { LED_TOGGLE(FBW_MODE_LED);}
216  } else if (fbw_mode == FBW_MODE_AUTO) {
217  LED_ON(FBW_MODE_LED);
218  }
219 #endif // FWB_MODE_LED
220 
221  /* Set failsafe commands */
222  if (fbw_mode == FBW_MODE_FAILSAFE) {
223  fbw_motors_on = false;
225  }
226 
227  /* If in auto copy autopilot motors on */
228  if (fbw_mode == FBW_MODE_AUTO) {
230  }
231 
232  /* Set actuators */
233  SetActuatorsFromCommands(commands, autopilot_mode);
234 
235  /* Periodic blinking */
236  RunOnceEvery(10, LED_PERIODIC());
237 }
238 
239 
241 // Event
242 
244 static void fbw_on_rc_frame(void)
245 {
246  /* get autopilot fbw mode as set by RADIO_MODE 3-way switch */
249  } else {
251  }
252 
253  /* Failsafe check if intermcu is lost while AP was in control */
254  if ((intermcu.status == INTERMCU_LOST) &&
255  (fbw_mode == FBW_MODE_AUTO)) {
257  }
258 
259  /* If the FBW is in control */
260  if (fbw_mode == FBW_MODE_MANUAL) {
261  fbw_motors_on = true;
263 #ifdef SetCommandsFromRC
264  SetCommandsFromRC(commands, radio_control.values);
265 #else
266 #warning "FBW: needs commands from RC in order to be useful."
267 #endif
268  }
269 
270  /* Forward radiocontrol to AP */
272 }
273 
275 static void fbw_on_ap_command(void)
276 {
277  // Only set the command from AP when we are in AUTO mode
278  if (fbw_mode == FBW_MODE_AUTO) {
280  }
281 }
282 
284 {
285  /* Event functions for mcu peripherals: i2c, usb_serial.. */
286  mcu_event();
287 
288  /* Handle RC */
290 
291  /* InterMCU (gives autopilot commands as output) */
293 
294  /* FBW modules */
295  modules_event_task();
296 }
unsigned short uint16_t
Definition: types.h:16
void intermcu_send_status(uint8_t mode)
Definition: intermcu_fbw.c:121
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:76
void InterMcuEvent(void(*frame_handler)(void))
Definition: intermcu_fbw.c:175
tid_t radio_control_tid
id for radio_control_periodic_task() timer
Definition: main_fbw.c:70
bool fbw_motors_on
Definition: main_fbw.c:61
tid_t modules_tid
id for modules_periodic_task() timer
Definition: main_fbw.c:69
#define MIN_PPRZ
Definition: paparazzi.h:9
uint8_t autopilot_mode
Definition: autopilot.c:69
pprz_t intermcu_commands[COMMANDS_NB]
Definition: intermcu_fbw.c:61
tid_t electrical_tid
id for electrical_periodic() timer
Definition: main_fbw.c:77
uint8_t status
Definition: radio_control.h:53
#define LED_OFF(i)
Definition: led_hw.h:50
int main(void)
Real main function handling initialization, periodic- and event functions.
Definition: main_fbw.c:75
struct intermcu_t intermcu
Definition: intermcu_ap.c:41
STATIC_INLINE void telemetry_periodic(void)
Definition: main_fbw.c:142
static void fbw_on_ap_command(void)
Callback when receive commands from the AP.
Definition: main_fbw.c:275
void intermcu_periodic(void)
Definition: intermcu_ap.c:74
enum intermcu_status status
Status of the INTERMCU.
Definition: intermcu.h:71
bool autopilot_motors_on
Definition: autopilot.c:76
tid_t telemetry_tid
id for telemetry_periodic() timer
Definition: main_fbw.c:72
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:58
#define mcu_int_enable()
Definition: mcu_arch.h:38
void radio_control_init(void)
Definition: radio_control.c:32
const pprz_t commands_failsafe[COMMANDS_NB]
Definition: commands.c:31
Rotorcraft Inter-MCU on FlyByWire.
STATIC_INLINE void fbw_safety_check(void)
Definition: main_fbw.c:152
#define RadioControlEvent(_received_frame_handler)
Definition: dummy.h:28
No interMCU communication anymore.
Definition: intermcu.h:46
Hardware independent API for actuators (servos, motor controllers).
void electrical_init(void)
Definition: electrical.c:99
Interface for electrical status: supply voltage, current, battery status, etc.
Architecture independent timing functions.
void motor_mixing_init(void)
Definition: motor_mixing.c:109
void radio_control_periodic_task(void)
Definition: radio_control.c:46
int8_t tid_t
sys_time timer id type
Definition: sys_time.h:60
tid_t main_periodic_tid
id for main_periodic() timer
Definition: main_fbw.c:68
Hardware independent code for commands handling.
struct RadioControl radio_control
Definition: radio_control.c:30
void intermcu_on_rc_frame(uint8_t fbw_mode)
Definition: intermcu_fbw.c:88
#define LED_TOGGLE(i)
Definition: led_hw.h:51
#define RC_REALLY_LOST
Definition: radio_control.h:50
Some helper functions to check RC sticks.
void electrical_periodic(void)
Definition: electrical.c:121
void intermcu_init(void)
Definition: intermcu_ap.c:64
STATIC_INLINE void main_periodic(void)
Definition: main_fbw.c:188
uint8_t fbw_mode
Definition: main_fbw.c:63
Fly By Wire:
Arch independent mcu ( Micro Controller Unit ) utilities.
#define SetCommands(t)
Definition: commands.h:41
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
#define RC_LOST_FBW_MODE
mode to enter when RC is lost while using a mode with RC input
Definition: main_fbw.h:47
static bool sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:114
#define RADIO_FBW_MODE
Switching between FBW and autopilot is done with RADIO_FBW_MODE: default is to re-use RADIO_MODE...
Definition: main_fbw.h:64
#define LED_PERIODIC()
Definition: led_hw.h:52
STATIC_INLINE void main_init(void)
Main initialization.
Definition: main_fbw.c:88
#define STATIC_INLINE
Definition: main.h:34
arch independent LED (Light Emitting Diodes) API
#define LED_ON(i)
Definition: led_hw.h:49
void mcu_event(void)
MCU event functions.
Definition: mcu.c:217
STATIC_INLINE void main_event(void)
Definition: main_fbw.c:283
Motor Mixing.
#define AP_LOST_FBW_MODE
mode to enter when AP is lost while using autopilot
Definition: main_fbw.h:57
#define RC_LOST_IN_AUTO_FBW_MODE
mode to enter when AP is lost while using autopilot
Definition: main_fbw.h:52
fbw_mode_enum
Fly by wire modes.
Definition: main_fbw.c:59
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main_fbw.c:123
static void fbw_on_rc_frame(void)
Callback when we received an RC frame.
Definition: main_fbw.c:244
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:43