Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
main.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2010 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 #define MODULES_C
30 
31 #define ABI_C
32 
33 #include <inttypes.h>
34 #include "mcu.h"
35 #include "mcu_periph/sys_time.h"
36 #include "led.h"
37 
41 #include "subsystems/settings.h"
42 
43 #include "subsystems/commands.h"
44 #include "subsystems/actuators.h"
45 #if USE_MOTOR_MIXING
47 #endif
48 
49 #if USE_IMU
50 #include "subsystems/imu.h"
51 #endif
52 #if USE_GPS
53 #include "subsystems/gps.h"
54 #endif
55 
56 #if USE_BARO_BOARD
58 PRINT_CONFIG_MSG_VALUE("USE_BARO_BOARD is TRUE, reading onboard baro: ", BARO_BOARD)
59 #endif
60 
61 #include "subsystems/electrical.h"
62 
64 
66 
69 
70 #include "subsystems/ahrs.h"
71 #if USE_AHRS_ALIGNER
73 #endif
74 #include "subsystems/ins.h"
75 
76 #include "state.h"
77 
79 
80 #ifdef SITL
81 #include "nps_autopilot.h"
82 #endif
83 
84 #include "generated/modules.h"
85 #include "subsystems/abi.h"
86 
87 /* if PRINT_CONFIG is defined, print some config options */
88 PRINT_CONFIG_VAR(PERIODIC_FREQUENCY)
89 
90 /* TELEMETRY_FREQUENCY is defined in generated/periodic_telemetry.h
91  * defaults to 60Hz or set by TELEMETRY_FREQUENCY configure option in airframe file
92  */
93 PRINT_CONFIG_VAR(TELEMETRY_FREQUENCY)
94 
95 /* MODULES_FREQUENCY is defined in generated/modules.h
96  * according to main_freq parameter set for modules in airframe file
97  */
98 PRINT_CONFIG_VAR(MODULES_FREQUENCY)
99 
100 #ifndef BARO_PERIODIC_FREQUENCY
101 #define BARO_PERIODIC_FREQUENCY 50
102 #endif
103 PRINT_CONFIG_VAR(BARO_PERIODIC_FREQUENCY)
104 
105 #if USE_AHRS && USE_IMU && (defined AHRS_PROPAGATE_FREQUENCY)
106 #if (AHRS_PROPAGATE_FREQUENCY > PERIODIC_FREQUENCY)
107 #warning "PERIODIC_FREQUENCY should be least equal or greater than AHRS_PROPAGATE_FREQUENCY"
108 INFO_VALUE("it is recommended to configure in your airframe PERIODIC_FREQUENCY to at least ", AHRS_PROPAGATE_FREQUENCY)
109 #endif
110 #endif
111 
118 #if USE_BARO_BOARD
119 tid_t baro_tid;
120 #endif
121 
122 #ifndef SITL
123 int main(void)
124 {
125  main_init();
126 
127 #if LIMIT_EVENT_POLLING
128  /* Limit main loop frequency to 1kHz.
129  * This is a kludge until we can better leverage threads and have real events.
130  * Without this limit the event flags will constantly polled as fast as possible,
131  * resulting on 100% cpu load on boards with an (RT)OS.
132  * On bare metal boards this is not an issue, as you have nothing else running anyway.
133  */
134  uint32_t t_begin = 0;
135  uint32_t t_diff = 0;
136  while (1) {
137  t_begin = get_sys_time_usec();
138 
140  main_event();
141 
142  /* sleep remaining time to limit to 1kHz */
143  t_diff = get_sys_time_usec() - t_begin;
144  if (t_diff < 1000) {
145  sys_time_usleep(1000 - t_diff);
146  }
147  }
148 #else
149  while (1) {
151  main_event();
152  }
153 #endif
154 
155  return 0;
156 }
157 #endif /* SITL */
158 
160 {
161  mcu_init();
162 
163 #if defined(PPRZ_TRIG_INT_COMPR_FLASH)
164  pprz_trig_int_init();
165 #endif
166 
167  electrical_init();
168 
169  stateInit();
170 
171 #ifndef INTER_MCU_AP
172  actuators_init();
173 #else
174  intermcu_init();
175 #endif
176 
177 #if USE_MOTOR_MIXING
179 #endif
180 
181 #ifndef INTER_MCU_AP
183 #endif
184 
185 #if USE_BARO_BOARD
186  baro_init();
187 #endif
188 #if USE_IMU
189  imu_init();
190 #endif
191 #if USE_AHRS_ALIGNER
193 #endif
194 
195 #if USE_AHRS
196  ahrs_init();
197 #endif
198 
199  ins_init();
200 
201 #if USE_GPS
202  gps_init();
203 #endif
204 
205  autopilot_init();
206 
207  modules_init();
208 
209  settings_init();
210 
211  mcu_int_enable();
212 
213 #if DOWNLINK
214  downlink_init();
215 #endif
216 
217 #ifdef INTER_MCU_AP
218  intermcu_init();
219 #endif
220 
221  // register the timers for the periodic functions
222  main_periodic_tid = sys_time_register_timer((1. / PERIODIC_FREQUENCY), NULL);
223  modules_tid = sys_time_register_timer(1. / MODULES_FREQUENCY, NULL);
224  radio_control_tid = sys_time_register_timer((1. / 60.), NULL);
225  failsafe_tid = sys_time_register_timer(0.05, NULL);
226  electrical_tid = sys_time_register_timer(0.1, NULL);
227  telemetry_tid = sys_time_register_timer((1. / TELEMETRY_FREQUENCY), NULL);
228 #if USE_BARO_BOARD
229  baro_tid = sys_time_register_timer(1. / BARO_PERIODIC_FREQUENCY, NULL);
230 #endif
231 
232 #if USE_IMU
233  // send body_to_imu from here for now
234  AbiSendMsgBODY_TO_IMU_QUAT(1, orientationGetQuat_f(&imu.body_to_imu));
235 #endif
236 
237  // Do a failsafe check first
238  failsafe_check();
239 }
240 
242 {
243  if (sys_time_check_and_ack_timer(main_periodic_tid)) {
244  main_periodic();
245  }
246  if (sys_time_check_and_ack_timer(modules_tid)) {
247  modules_periodic_task();
248  }
249  if (sys_time_check_and_ack_timer(radio_control_tid)) {
251  }
252  if (sys_time_check_and_ack_timer(failsafe_tid)) {
253  failsafe_check();
254  }
255  if (sys_time_check_and_ack_timer(electrical_tid)) {
257  }
258  if (sys_time_check_and_ack_timer(telemetry_tid)) {
260  }
261 #if USE_BARO_BOARD
262  if (sys_time_check_and_ack_timer(baro_tid)) {
263  baro_periodic();
264  }
265 #endif
266 }
267 
269 {
270 
271 #if USE_IMU
272  imu_periodic();
273 #endif
274 
275  //FIXME: temporary hack, remove me
276 #ifdef InsPeriodic
277  InsPeriodic();
278 #endif
279 
280  /* run control loops */
282  /* set actuators */
283  //actuators_set(autopilot_motors_on);
284 #ifndef INTER_MCU_AP
285  SetActuatorsFromCommands(commands, autopilot_mode);
286 #else
288 #endif
289 
290  if (autopilot_in_flight) {
291  RunOnceEvery(PERIODIC_FREQUENCY, autopilot_flight_time++);
292  }
293 
294 #if defined DATALINK || defined SITL
295  RunOnceEvery(PERIODIC_FREQUENCY, datalink_time++);
296 #endif
297 
298  RunOnceEvery(10, LED_PERIODIC());
299 }
300 
302 {
303  static uint8_t boot = TRUE;
304 
305  /* initialisation phase during boot */
306  if (boot) {
307 #if DOWNLINK
308  send_autopilot_version(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
309 #endif
310  boot = FALSE;
311  }
312  /* then report periodicly */
313  else {
314 #if PERIODIC_TELEMETRY
315  periodic_telemetry_send_Main(DefaultPeriodic, &(DefaultChannel).trans_tx, &(DefaultDevice).device);
316 #endif
317  }
318 }
319 
321 #ifndef RC_LOST_MODE
322 #define RC_LOST_MODE AP_MODE_FAILSAFE
323 #endif
324 
326 {
333  }
334 
335 #if FAILSAFE_ON_BAT_CRITICAL
336  if (autopilot_mode != AP_MODE_KILL &&
339  }
340 #endif
341 
342 #if USE_GPS
344  if (autopilot_mode == AP_MODE_NAV &&
346 #if NO_GPS_LOST_WITH_RC_VALID
348 #endif
349  GpsIsLost()) {
351  }
352 
353  if (autopilot_mode == AP_MODE_HOME &&
356  }
357 #endif
358 
360 }
361 
363 {
364  /* event functions for mcu peripherals: i2c, usb_serial.. */
365  mcu_event();
366 
367  DatalinkEvent();
368 
369  if (autopilot_rc) {
371  }
372 
373 #if USE_IMU
374  ImuEvent();
375 #endif
376 
377 #ifdef InsEvent
378  TODO("calling InsEvent, remove me..")
379  InsEvent();
380 #endif
381 
382 #if USE_BARO_BOARD
383  BaroEvent();
384 #endif
385 
386 #if USE_GPS
387  GpsEvent();
388 #endif
389 
390 #if FAILSAFE_GROUND_DETECT || KILL_ON_GROUND_DETECT
392 #endif
393 
394  modules_event_task();
395 }
Interface to align the AHRS via low-passed measurements at startup.
#define AP_MODE_KILL
Definition: autopilot.h:36
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:71
Dispatcher to register actual AHRS implementations.
Common barometric sensor implementation.
STATIC_INLINE void telemetry_periodic(void)
Definition: main.c:301
void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:158
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:51
void autopilot_init(void)
Autopilot inititalization.
Definition: autopilot.c:175
uint8_t autopilot_mode
Definition: autopilot.c:65
Periodic telemetry system header (includes downlink utility and generated code).
bool_t autopilot_motors_on
Definition: autopilot.c:72
uint16_t autopilot_flight_time
flight time in seconds.
Definition: autopilot.c:48
uint8_t status
Definition: radio_control.h:53
#define InsEvent
Main include for ABI (AirBorneInterface).
uint8_t tid_t
sys_time timer id type
Definition: sys_time.h:57
STATIC_INLINE void main_init(void)
Definition: main.c:159
void imu_periodic(void)
optional.
Definition: imu_apogee.c:95
Integrated Navigation System interface.
void baro_init(void)
Definition: baro_board.c:68
Autopilot modes.
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main.c:241
tid_t modules_tid
id for modules_periodic_task() timer
Definition: main.c:113
bool_t bat_critical
battery critical status
Definition: electrical.h:53
void ahrs_init(void)
AHRS initialization.
Definition: ahrs.c:68
#define BARO_BOARD
Definition: baro_board.h:33
void radio_control_init(void)
Definition: radio_control.c:32
#define FALSE
Definition: std.h:5
void baro_periodic(void)
Definition: baro_board.c:77
Rotorcraft main loop.
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
void settings_init(void)
Definition: settings.c:43
void autopilot_check_in_flight(bool_t motors_on)
Definition: autopilot.c:552
void stateInit(void)
Definition: state.c:43
#define RadioControlEvent(_received_frame_handler)
Definition: dummy.h:28
tid_t radio_control_tid
id for radio_control_periodic_task() timer
Definition: main.c:115
Hardware independent API for actuators (servos, motor controllers).
#define TRUE
Definition: std.h:4
void electrical_init(void)
Definition: electrical.c:99
tid_t telemetry_tid
id for telemetry_periodic() timer
Definition: main.c:117
Interface for electrical status: supply voltage, current, battery status, etc.
Architecture independent timing functions.
void motor_mixing_init(void)
Definition: motor_mixing.c:96
void radio_control_periodic_task(void)
Definition: radio_control.c:46
Device independent GPS code (interface)
bool_t autopilot_in_flight
Definition: autopilot.c:68
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
void ins_init(void)
INS initialization.
Definition: ins.c:60
STATIC_INLINE void main_event(void)
Definition: main.c:362
unsigned long uint32_t
Definition: types.h:18
#define BaroEvent
Definition: baro_board.h:36
int main(void)
Definition: main.c:42
bool_t autopilot_rc
Definition: autopilot.c:75
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define ImuEvent
Definition: imu_apogee.h:113
void autopilot_on_rc_frame(void)
Get autopilot mode from two 2way switches.
Definition: autopilot.c:644
Hardware independent code for commands handling.
struct RadioControl radio_control
Definition: radio_control.c:30
tid_t failsafe_tid
id for failsafe_check() timer
Definition: main.c:114
#define AP_MODE_NAV
Definition: autopilot.h:49
Inertial Measurement Unit interface.
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:37
static bool_t sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:111
struct OrientationReps body_to_imu
rotation from body to imu frame
Definition: imu.h:53
void autopilot_periodic(void)
Definition: autopilot.c:334
#define RC_REALLY_LOST
Definition: radio_control.h:50
#define RC_OK
Definition: radio_control.h:48
void electrical_periodic(void)
Definition: electrical.c:121
uint16_t datalink_time
Definition: sim_ap.c:43
tid_t main_periodic_tid
id for main_periodic() timer
Definition: main.c:112
void intermcu_init(void)
Definition: intermcu_ap.c:44
static void sys_time_usleep(uint32_t us)
Definition: sys_time_arch.h:55
Arch independent mcu ( Micro Controller Unit ) utilities.
#define mcu_int_enable()
Definition: mcu_arch.h:35
unsigned char uint8_t
Definition: types.h:14
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
API to get/set the generic vehicle states.
Persistent settings interface.
void gps_periodic_check(void)
Periodic GPS check.
Definition: gps.c:166
#define LED_PERIODIC()
Definition: led_hw.h:47
General stabilization interface for rotorcrafts.
#define TELEMETRY_FREQUENCY
Definition: main_ap.c:120
#define STATIC_INLINE
Definition: main.h:34
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:47
arch independent LED (Light Emitting Diodes) API
#define AP_MODE_FAILSAFE
Definition: autopilot.h:37
#define AP_MODE_HOME
Definition: autopilot.h:38
STATIC_INLINE void main_periodic(void)
Definition: main.c:268
void mcu_event(void)
MCU event functions.
Definition: mcu.c:192
struct Electrical electrical
Definition: electrical.c:65
#define GpsIsLost()
Definition: gps.h:49
void intermcu_set_actuators(pprz_t *command_values, uint8_t ap_mode)
Definition: intermcu_ap.c:59
STATIC_INLINE void failsafe_check(void)
Definition: main.c:325
Motor Mixing.
void imu_init(void)
Definition: imu.c:110
#define BARO_PERIODIC_FREQUENCY
Definition: main.c:101
void autopilot_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot.c:395
void ahrs_aligner_init(void)
Definition: ahrs_aligner.c:79
static void DetectGroundEvent(void)
Ground detection based on vertical acceleration.
Definition: autopilot.h:150
void gps_init(void)
initialize the global GPS state
Definition: gps.c:135
static struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).
tid_t electrical_tid
id for electrical_periodic() timer
Definition: main.c:116
#define RC_LOST_MODE
mode to enter when RC is lost while using a mode with RC input (not AP_MODE_NAV)
Definition: main.c:322