Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
main_ap.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
28 #define MODULES_C
29 
30 #define ABI_C
31 
32 #include <inttypes.h>
33 #include "mcu.h"
34 #include "mcu_periph/sys_time.h"
35 #include "led.h"
36 
40 #include "subsystems/settings.h"
41 
42 #include "subsystems/commands.h"
43 #include "subsystems/actuators.h"
44 
45 #if USE_IMU
46 #include "subsystems/imu.h"
47 #endif
48 #if USE_GPS
49 #include "subsystems/gps.h"
50 #endif
51 
52 #if USE_BARO_BOARD
54 PRINT_CONFIG_MSG_VALUE("USE_BARO_BOARD is TRUE, reading onboard baro: ", BARO_BOARD)
55 #endif
56 
57 #include "subsystems/electrical.h"
58 
59 #include "autopilot.h"
60 
62 
63 #include "subsystems/ahrs.h"
64 
65 #include "state.h"
66 
68 
69 #include "generated/modules.h"
70 #include "subsystems/abi.h"
71 
72 // needed for stop-gap measure waypoints_localize_all()
74 
75 
76 /* if PRINT_CONFIG is defined, print some config options */
77 PRINT_CONFIG_VAR(PERIODIC_FREQUENCY)
78 /* SYS_TIME_FREQUENCY/PERIODIC_FREQUENCY should be an integer, otherwise the timer will not be correct */
79 #if !(SYS_TIME_FREQUENCY/PERIODIC_FREQUENCY*PERIODIC_FREQUENCY == SYS_TIME_FREQUENCY)
80 #warning "The SYS_TIME_FREQUENCY can not be divided by PERIODIC_FREQUENCY. Make sure this is the case for correct timing."
81 #endif
82 
83 /* TELEMETRY_FREQUENCY is defined in generated/periodic_telemetry.h
84  * defaults to 60Hz or set by TELEMETRY_FREQUENCY configure option in airframe file
85  */
86 PRINT_CONFIG_VAR(TELEMETRY_FREQUENCY)
87 
88 /* MODULES_FREQUENCY is defined in generated/modules.h
89  * according to main_freq parameter set for modules in airframe file
90  */
91 PRINT_CONFIG_VAR(MODULES_FREQUENCY)
92 
93 #ifndef BARO_PERIODIC_FREQUENCY
94 #define BARO_PERIODIC_FREQUENCY 50
95 #endif
96 PRINT_CONFIG_VAR(BARO_PERIODIC_FREQUENCY)
97 
98 #if USE_AHRS && USE_IMU && (defined AHRS_PROPAGATE_FREQUENCY)
99 #if (AHRS_PROPAGATE_FREQUENCY > PERIODIC_FREQUENCY)
100 #warning "PERIODIC_FREQUENCY should be least equal or greater than AHRS_PROPAGATE_FREQUENCY"
101 INFO_VALUE("it is recommended to configure in your airframe PERIODIC_FREQUENCY to at least ", AHRS_PROPAGATE_FREQUENCY)
102 #endif
103 #endif
104 
111 #if USE_BARO_BOARD
112 tid_t baro_tid;
113 #endif
114 
115 void main_init(void)
116 {
117  mcu_init();
118 
119 #if defined(PPRZ_TRIG_INT_COMPR_FLASH)
120  pprz_trig_int_init();
121 #endif
122 
123  electrical_init();
124 
125  stateInit();
126 
127  actuators_init();
128 
129 #if USE_MOTOR_MIXING
131 #endif
132 
134 
135 #if USE_BARO_BOARD
136  baro_init();
137 #endif
138 
139 #if USE_AHRS
140  ahrs_init();
141 #endif
142 
143  autopilot_init();
144 
145  modules_init();
146 
147  // call autopilot implementation init after guidance modules init
148  // it will set startup mode
150 
151  /* temporary hack:
152  * Since INS is now a module, LTP_DEF is not yet initialized when autopilot_init is called
153  * This led to the problem that global waypoints were not "localized",
154  * so as a stop-gap measure we localize them all (again) here..
155  */
156  //waypoints_localize_all();
157 
158  settings_init();
159 
160  mcu_int_enable();
161 
162 #if DOWNLINK
163  downlink_init();
164 #endif
165 
166  // register the timers for the periodic functions
167  main_periodic_tid = sys_time_register_timer((1. / PERIODIC_FREQUENCY), NULL);
168 #if PERIODIC_FREQUENCY != MODULES_FREQUENCY
169  modules_tid = sys_time_register_timer(1. / MODULES_FREQUENCY, NULL);
170 #endif
171  radio_control_tid = sys_time_register_timer((1. / 60.), NULL);
175 #if USE_BARO_BOARD
176  baro_tid = sys_time_register_timer(1. / BARO_PERIODIC_FREQUENCY, NULL);
177 #endif
178 
179 #if USE_IMU
180  // send body_to_imu from here for now
181  AbiSendMsgBODY_TO_IMU_QUAT(1, orientationGetQuat_f(&imu.body_to_imu));
182 #endif
183 
184  // Do a failsafe check first
185  failsafe_check();
186 
187 }
188 
190 {
192  main_periodic();
193 #if PERIODIC_FREQUENCY == MODULES_FREQUENCY
194  /* Use the main periodc freq timer for modules if the freqs are the same
195  * This is mainly useful for logging each step.
196  */
197  modules_periodic_task();
198 #else
199  }
200  /* separate timer for modules, since it has a different freq than main */
202  modules_periodic_task();
203 #endif
204  }
207  }
209  failsafe_check();
210  }
213  }
216  }
217 #if USE_BARO_BOARD
218  if (sys_time_check_and_ack_timer(baro_tid)) {
219  baro_periodic();
220  }
221 #endif
222 }
223 
224 void main_periodic(void)
225 {
226  /* run control loops */
228  /* set actuators */
229  //actuators_set(autopilot_get_motors_on());
230  SetActuatorsFromCommands(commands, autopilot_get_mode());
231 
232  if (autopilot_in_flight()) {
233  RunOnceEvery(PERIODIC_FREQUENCY, autopilot.flight_time++);
234  }
235 
236 #if defined DATALINK || defined SITL
237  RunOnceEvery(PERIODIC_FREQUENCY, datalink_time++);
238 #endif
239 
240  RunOnceEvery(10, LED_PERIODIC());
241 }
242 
244 {
245  static uint8_t boot = true;
246 
247  /* initialisation phase during boot */
248  if (boot) {
249 #if DOWNLINK
251 #endif
252  boot = false;
253  }
254  /* then report periodicly */
255  else {
256 #if PERIODIC_TELEMETRY
257  periodic_telemetry_send_Main(DefaultPeriodic, &(DefaultChannel).trans_tx, &(DefaultDevice).device);
258 #endif
259  }
260 }
261 
263 #ifndef RC_LOST_MODE
264 #define RC_LOST_MODE AP_MODE_FAILSAFE
265 #endif
266 
267 void failsafe_check(void)
268 {
270 }
271 
272 void main_event(void)
273 {
274  /* event functions for mcu peripherals: i2c, usb_serial.. */
275  mcu_event();
276 
277  if (autopilot.use_rc) {
279  }
280 
281 #if USE_BARO_BOARD
282  BaroEvent();
283 #endif
284 
285  autopilot_event();
286 
287  modules_event_task();
288 }
289 
main_init
void main_init(void)
Definition: main_ap.c:118
main_ap.h
autopilot_get_motors_on
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:212
radio_control.h
electrical.h
stateInit
void stateInit(void)
Definition: state.c:43
radio_control_periodic_task
void radio_control_periodic_task(void)
Definition: radio_control.c:46
electrical_tid
tid_t electrical_tid
id for electrical_periodic() timer
Definition: main_ap.c:112
autopilot_generated_init
void autopilot_generated_init(void)
Definition: autopilot_generated.c:41
settings.h
mcu_int_enable
#define mcu_int_enable()
Definition: mcu_arch.h:36
abi.h
main_event
void main_event(void)
Definition: main_ap.c:336
radio_control_tid
tid_t radio_control_tid
id for radio_control_periodic_task() timer
Definition: main_ap.c:111
pprz_autopilot::flight_time
uint16_t flight_time
flight time in seconds
Definition: autopilot.h:65
mcu_event
void mcu_event(void)
MCU event functions.
Definition: mcu.c:258
autopilot_init
void autopilot_init(void)
Autopilot initialization function.
Definition: autopilot.c:91
Imu::body_to_imu
struct OrientationReps body_to_imu
rotation from body to imu frame
Definition: imu.h:49
pprz_autopilot::use_rc
bool use_rc
enable/disable RC input
Definition: autopilot.h:72
autopilot_event
void WEAK autopilot_event(void)
AP event call.
Definition: autopilot.c:146
BARO_BOARD
#define BARO_BOARD
Definition: baro_board.h:33
baro_init
void baro_init(void)
Definition: baro_board.c:76
baro_periodic
void baro_periodic(void)
Definition: baro_board.c:90
motor_mixing_init
void motor_mixing_init(void)
Definition: motor_mixing.c:109
main_periodic
void main_periodic(void)
Definition: main_ap.c:237
ahrs_init
void ahrs_init(void)
AHRS initialization.
Definition: ahrs.c:73
autopilot_in_flight
bool autopilot_in_flight(void)
get in_flight flag
Definition: autopilot.c:257
telemetry.h
imu.h
waypoints.h
autopilot_send_version
void autopilot_send_version(void)
send autopilot version
Definition: autopilot.c:298
gps.h
Device independent GPS code (interface)
BARO_PERIODIC_FREQUENCY
#define BARO_PERIODIC_FREQUENCY
Definition: main_ap.c:94
autopilot
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:50
settings_init
void settings_init(void)
Definition: settings.c:43
orientationGetQuat_f
static struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).
Definition: pprz_orientation_conversion.h:225
BaroEvent
#define BaroEvent
Definition: baro_board.h:36
mcu.h
Arch independent mcu ( Micro Controller Unit ) utilities.
electrical_periodic
void electrical_periodic(void)
Definition: electrical.c:123
main_periodic_tid
tid_t main_periodic_tid
id for main_periodic() timer
Definition: main_ap.c:108
sys_time.h
Architecture independent timing functions.
LED_PERIODIC
#define LED_PERIODIC()
Definition: led_hw.h:55
uint8_t
unsigned char uint8_t
Definition: types.h:14
telemetry_periodic
void telemetry_periodic(void)
Definition: main_ap.c:270
electrical_init
void electrical_init(void)
Definition: electrical.c:100
tid_t
int8_t tid_t
sys_time timer id type
Definition: sys_time.h:60
handle_periodic_tasks
void handle_periodic_tasks(void)
Definition: main_ap.c:202
autopilot.h
led.h
arch independent LED (Light Emitting Diodes) API
telemetry_tid
tid_t telemetry_tid
id for telemetry_periodic() timer
Definition: main_ap.c:137
ahrs.h
imu
struct Imu imu
global IMU state
Definition: imu.c:108
radio_control_init
void radio_control_init(void)
Definition: radio_control.c:32
TELEMETRY_FREQUENCY
#define TELEMETRY_FREQUENCY
Definition: main_ap.c:109
commands
static const ShellCommand commands[]
Definition: shell_arch.c:71
baro.h
RadioControlEvent
#define RadioControlEvent(_received_frame_handler)
Definition: cc2500_paparazzi.h:37
autopilot_periodic
void autopilot_periodic(void)
AP periodic call.
Definition: autopilot.c:135
datalink_time
uint16_t datalink_time
Definition: sim_ap.c:41
sys_time_register_timer
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:43
autopilot_check_in_flight
void WEAK autopilot_check_in_flight(bool motors_on)
in flight check utility function actual implementation is firmware dependent
Definition: autopilot.c:238
AHRS_PROPAGATE_FREQUENCY
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:55
commands.h
Hardware independent code for commands handling.
autopilot_get_mode
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition: autopilot.c:184
state.h
autopilot_on_rc_frame
void autopilot_on_rc_frame(void)
RC frame handler.
Definition: autopilot.c:150
failsafe_check
void failsafe_check(void)
Definition: main_ap.c:294
inttypes.h
failsafe_tid
tid_t failsafe_tid
id for failsafe_check() timer
Definition: main_ap.c:110
mcu_init
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:87
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
sys_time_check_and_ack_timer
static bool sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:114
modules_tid
tid_t modules_tid
id for modules_periodic_task() timer
Definition: main_ap.c:136
actuators.h