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.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 
75 #include "state.h"
76 
78 
79 #ifdef SITL
80 #include "nps_autopilot.h"
81 #endif
82 
83 #include "generated/modules.h"
84 #include "subsystems/abi.h"
85 
86 // needed for stop-gap measure waypoints_localize_all()
88 
89 
90 /* if PRINT_CONFIG is defined, print some config options */
91 PRINT_CONFIG_VAR(PERIODIC_FREQUENCY)
92 
93 /* TELEMETRY_FREQUENCY is defined in generated/periodic_telemetry.h
94  * defaults to 60Hz or set by TELEMETRY_FREQUENCY configure option in airframe file
95  */
96 PRINT_CONFIG_VAR(TELEMETRY_FREQUENCY)
97 
98 /* MODULES_FREQUENCY is defined in generated/modules.h
99  * according to main_freq parameter set for modules in airframe file
100  */
101 PRINT_CONFIG_VAR(MODULES_FREQUENCY)
102 
103 #ifndef BARO_PERIODIC_FREQUENCY
104 #define BARO_PERIODIC_FREQUENCY 50
105 #endif
106 PRINT_CONFIG_VAR(BARO_PERIODIC_FREQUENCY)
107 
108 #if USE_AHRS && USE_IMU && (defined AHRS_PROPAGATE_FREQUENCY)
109 #if (AHRS_PROPAGATE_FREQUENCY > PERIODIC_FREQUENCY)
110 #warning "PERIODIC_FREQUENCY should be least equal or greater than AHRS_PROPAGATE_FREQUENCY"
111 INFO_VALUE("it is recommended to configure in your airframe PERIODIC_FREQUENCY to at least ", AHRS_PROPAGATE_FREQUENCY)
112 #endif
113 #endif
114 
121 #if USE_BARO_BOARD
122 tid_t baro_tid;
123 #endif
124 
125 #ifndef SITL
126 int main(void)
127 {
128  main_init();
129 
130 #if LIMIT_EVENT_POLLING
131  /* Limit main loop frequency to 1kHz.
132  * This is a kludge until we can better leverage threads and have real events.
133  * Without this limit the event flags will constantly polled as fast as possible,
134  * resulting on 100% cpu load on boards with an (RT)OS.
135  * On bare metal boards this is not an issue, as you have nothing else running anyway.
136  */
137  uint32_t t_begin = 0;
138  uint32_t t_diff = 0;
139  while (1) {
140  t_begin = get_sys_time_usec();
141 
143  main_event();
144 
145  /* sleep remaining time to limit to 1kHz */
146  t_diff = get_sys_time_usec() - t_begin;
147  if (t_diff < 1000) {
148  sys_time_usleep(1000 - t_diff);
149  }
150  }
151 #else
152  while (1) {
154  main_event();
155  }
156 #endif
157 
158  return 0;
159 }
160 #endif /* SITL */
161 
163 {
164  mcu_init();
165 
166 #if defined(PPRZ_TRIG_INT_COMPR_FLASH)
167  pprz_trig_int_init();
168 #endif
169 
170  electrical_init();
171 
172  stateInit();
173 
174 #ifndef INTER_MCU_AP
175  actuators_init();
176 #else
177  intermcu_init();
178 #endif
179 
180 #if USE_MOTOR_MIXING
182 #endif
183 
184 #ifndef INTER_MCU_AP
186 #endif
187 
188 #if USE_BARO_BOARD
189  baro_init();
190 #endif
191 
192 #if USE_AHRS_ALIGNER
194 #endif
195 
196 #if USE_AHRS
197  ahrs_init();
198 #endif
199 
200  autopilot_init();
201 
202  modules_init();
203 
204  /* temporary hack:
205  * Since INS is now a module, LTP_DEF is not yet initialized when autopilot_init is called
206  * This led to the problem that global waypoints were not "localized",
207  * so as a stop-gap measure we localize them all (again) here..
208  */
210 
211  settings_init();
212 
213  mcu_int_enable();
214 
215 #if DOWNLINK
216  downlink_init();
217 #endif
218 
219 #ifdef INTER_MCU_AP
220  intermcu_init();
221 #endif
222 
223  // register the timers for the periodic functions
224  main_periodic_tid = sys_time_register_timer((1. / PERIODIC_FREQUENCY), NULL);
225  modules_tid = sys_time_register_timer(1. / MODULES_FREQUENCY, NULL);
226  radio_control_tid = sys_time_register_timer((1. / 60.), NULL);
227  failsafe_tid = sys_time_register_timer(0.05, NULL);
228  electrical_tid = sys_time_register_timer(0.1, NULL);
229  telemetry_tid = sys_time_register_timer((1. / TELEMETRY_FREQUENCY), NULL);
230 #if USE_BARO_BOARD
231  baro_tid = sys_time_register_timer(1. / BARO_PERIODIC_FREQUENCY, NULL);
232 #endif
233 
234 #if USE_IMU
235  // send body_to_imu from here for now
236  AbiSendMsgBODY_TO_IMU_QUAT(1, orientationGetQuat_f(&imu.body_to_imu));
237 #endif
238 
239  // Do a failsafe check first
240  failsafe_check();
241 }
242 
244 {
245  if (sys_time_check_and_ack_timer(main_periodic_tid)) {
246  main_periodic();
247  }
248  if (sys_time_check_and_ack_timer(modules_tid)) {
249  modules_periodic_task();
250  }
251  if (sys_time_check_and_ack_timer(radio_control_tid)) {
253  }
254  if (sys_time_check_and_ack_timer(failsafe_tid)) {
255  failsafe_check();
256  }
257  if (sys_time_check_and_ack_timer(electrical_tid)) {
259  }
260  if (sys_time_check_and_ack_timer(telemetry_tid)) {
262  }
263 #if USE_BARO_BOARD
264  if (sys_time_check_and_ack_timer(baro_tid)) {
265  baro_periodic();
266  }
267 #endif
268 }
269 
271 {
272 
273  /* run control loops */
275  /* set actuators */
276  //actuators_set(autopilot_motors_on);
277 #ifndef INTER_MCU_AP
278  SetActuatorsFromCommands(commands, autopilot_mode);
279 #else
281 #endif
282 
283  if (autopilot_in_flight) {
284  RunOnceEvery(PERIODIC_FREQUENCY, autopilot_flight_time++);
285  }
286 
287 #if defined DATALINK || defined SITL
288  RunOnceEvery(PERIODIC_FREQUENCY, datalink_time++);
289 #endif
290 
291  RunOnceEvery(10, LED_PERIODIC());
292 }
293 
295 {
296  static uint8_t boot = true;
297 
298  /* initialisation phase during boot */
299  if (boot) {
300 #if DOWNLINK
301  send_autopilot_version(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
302 #endif
303  boot = false;
304  }
305  /* then report periodicly */
306  else {
307 #if PERIODIC_TELEMETRY
308  periodic_telemetry_send_Main(DefaultPeriodic, &(DefaultChannel).trans_tx, &(DefaultDevice).device);
309 #endif
310  }
311 }
312 
314 #ifndef RC_LOST_MODE
315 #define RC_LOST_MODE AP_MODE_FAILSAFE
316 #endif
317 
319 {
329  }
330 
331 #if FAILSAFE_ON_BAT_CRITICAL
332  if (autopilot_mode != AP_MODE_KILL &&
335  }
336 #endif
337 
338 #if USE_GPS
339  if (autopilot_mode == AP_MODE_NAV &&
341 #if NO_GPS_LOST_WITH_RC_VALID
343 #endif
344  GpsIsLost()) {
346  }
347 
348  if (autopilot_mode == AP_MODE_HOME &&
351  }
352 #endif
353 
355 }
356 
358 {
359  /* event functions for mcu peripherals: i2c, usb_serial.. */
360  mcu_event();
361 
362  DatalinkEvent();
363 
364  if (autopilot_rc) {
366  }
367 
368 #if USE_BARO_BOARD
369  BaroEvent();
370 #endif
371 
372 #if FAILSAFE_GROUND_DETECT || KILL_ON_GROUND_DETECT
374 #endif
375 
376  modules_event_task();
377 }
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:76
Dispatcher to register actual AHRS implementations.
Common barometric sensor implementation.
STATIC_INLINE void telemetry_periodic(void)
Definition: main.c:294
#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:69
Periodic telemetry system header (includes downlink utility and generated code).
uint16_t autopilot_flight_time
flight time in seconds.
Definition: autopilot.c:48
uint8_t status
Definition: radio_control.h:53
Main include for ABI (AirBorneInterface).
STATIC_INLINE void main_init(void)
Definition: main.c:162
void baro_init(void)
Definition: baro_board.c:76
Autopilot modes.
struct Imu imu
global IMU state
Definition: imu.c:108
#define AP_MODE_MODULE
Definition: autopilot.h:53
bool autopilot_motors_on
Definition: autopilot.c:76
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main.c:243
tid_t modules_tid
id for modules_periodic_task() timer
Definition: main.c:116
#define mcu_int_enable()
Definition: mcu_arch.h:38
void autopilot_check_in_flight(bool motors_on)
Definition: autopilot.c:669
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
void baro_periodic(void)
Definition: baro_board.c:90
static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Rotorcraft main loop.
void settings_init(void)
Definition: settings.c:43
void stateInit(void)
Definition: state.c:43
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
Definition: sys_time_arch.c:95
#define RadioControlEvent(_received_frame_handler)
Definition: dummy.h:28
tid_t radio_control_tid
id for radio_control_periodic_task() timer
Definition: main.c:118
Hardware independent API for actuators (servos, motor controllers).
void electrical_init(void)
Definition: electrical.c:99
bool bat_critical
battery critical status
Definition: electrical.h:53
tid_t telemetry_tid
id for telemetry_periodic() timer
Definition: main.c:120
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
Device independent GPS code (interface)
STATIC_INLINE void main_event(void)
Definition: main.c:357
unsigned long uint32_t
Definition: types.h:18
#define BaroEvent
Definition: baro_board.h:36
int main(void)
Definition: main.c:42
int8_t tid_t
sys_time timer id type
Definition: sys_time.h:60
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void autopilot_on_rc_frame(void)
Get autopilot mode from two 2way switches.
Definition: autopilot.c:758
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:117
#define AP_MODE_NAV
Definition: autopilot.h:49
Inertial Measurement Unit interface.
struct OrientationReps body_to_imu
rotation from body to imu frame
Definition: imu.h:49
void autopilot_periodic(void)
Definition: autopilot.c:344
#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:42
tid_t main_periodic_tid
id for main_periodic() timer
Definition: main.c:115
void intermcu_init(void)
Definition: intermcu_ap.c:64
Arch independent mcu ( Micro Controller Unit ) utilities.
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.
static bool sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:114
General stabilization interface for rotorcrafts.
#define TELEMETRY_FREQUENCY
Definition: main_ap.c:119
#define LED_PERIODIC()
Definition: led_hw.h:52
#define STATIC_INLINE
Definition: main.h:34
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:270
void mcu_event(void)
MCU event functions.
Definition: mcu.c:217
struct Electrical electrical
Definition: electrical.c:65
bool autopilot_in_flight
Definition: autopilot.c:72
#define GpsIsLost()
Definition: gps.h:45
void intermcu_set_actuators(pprz_t *command_values, uint8_t ap_mode)
Definition: intermcu_ap.c:91
STATIC_INLINE void failsafe_check(void)
Definition: main.c:318
Motor Mixing.
void waypoints_localize_all(void)
update local ENU coordinates of global waypoints
Definition: waypoints.c:265
#define BARO_PERIODIC_FREQUENCY
Definition: main.c:104
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
void autopilot_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot.c:424
#define AP_MODE_GUIDED
Definition: autopilot.h:55
void ahrs_aligner_init(void)
Definition: ahrs_aligner.c:79
static void DetectGroundEvent(void)
Ground detection based on vertical acceleration.
Definition: autopilot.h:151
bool autopilot_rc
Definition: autopilot.c:79
#define AP_MODE_FLIP
Definition: autopilot.h:54
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:43
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:119
#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:315