/
main.c
326 lines (252 loc) · 7.09 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (C) 2008-2010 The Paparazzi Team
*
* This file is part of Paparazzi.
*
* Paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/**
* @file firmwares/rotorcraft/main.c
*
* Rotorcraft main loop.
*/
#define MODULES_C
#include <inttypes.h>
#include "mcu.h"
#include "mcu_periph/sys_time.h"
#include "led.h"
#include "subsystems/datalink/telemetry.h"
#include "subsystems/datalink/datalink.h"
#include "subsystems/settings.h"
#include "subsystems/datalink/xbee.h"
#if DATALINK == UDP
#include "subsystems/datalink/udp.h"
#endif
#include "subsystems/commands.h"
#include "subsystems/actuators.h"
#if USE_MOTOR_MIXING
#include "subsystems/actuators/motor_mixing.h"
#endif
#include "subsystems/imu.h"
#include "subsystems/gps.h"
#include "subsystems/sensors/baro.h"
#include "subsystems/electrical.h"
#include "firmwares/rotorcraft/autopilot.h"
#include "subsystems/radio_control.h"
#include "firmwares/rotorcraft/stabilization.h"
#include "firmwares/rotorcraft/guidance.h"
#include "subsystems/ahrs.h"
#include "subsystems/ahrs/ahrs_aligner.h"
#include "subsystems/ins.h"
#include "state.h"
#include "firmwares/rotorcraft/main.h"
#ifdef SITL
#include "nps_autopilot_rotorcraft.h"
#endif
#include "generated/modules.h"
/* if PRINT_CONFIG is defined, print some config options */
PRINT_CONFIG_VAR(PERIODIC_FREQUENCY)
#ifndef TELEMETRY_FREQUENCY
#define TELEMETRY_FREQUENCY 60
#endif
PRINT_CONFIG_VAR(TELEMETRY_FREQUENCY)
#ifndef MODULES_FREQUENCY
#define MODULES_FREQUENCY 512
#endif
PRINT_CONFIG_VAR(MODULES_FREQUENCY)
#ifndef BARO_PERIODIC_FREQUENCY
#define BARO_PERIODIC_FREQUENCY 50
#endif
PRINT_CONFIG_VAR(BARO_PERIODIC_FREQUENCY)
static inline void on_gyro_event( void );
static inline void on_accel_event( void );
static inline void on_baro_abs_event( void );
static inline void on_baro_dif_event( void );
static inline void on_gps_event( void );
static inline void on_mag_event( void );
tid_t main_periodic_tid; ///< id for main_periodic() timer
tid_t modules_tid; ///< id for modules_periodic_task() timer
tid_t failsafe_tid; ///< id for failsafe_check() timer
tid_t radio_control_tid; ///< id for radio_control_periodic_task() timer
tid_t electrical_tid; ///< id for electrical_periodic() timer
tid_t baro_tid; ///< id for baro_periodic() timer
tid_t telemetry_tid; ///< id for telemetry_periodic() timer
#ifndef SITL
int main( void ) {
main_init();
while(1) {
handle_periodic_tasks();
main_event();
}
return 0;
}
#endif /* SITL */
STATIC_INLINE void main_init( void ) {
mcu_init();
electrical_init();
stateInit();
actuators_init();
#if USE_MOTOR_MIXING
motor_mixing_init();
#endif
radio_control_init();
baro_init();
imu_init();
autopilot_init();
nav_init();
guidance_h_init();
guidance_v_init();
stabilization_init();
ahrs_aligner_init();
ahrs_init();
ins_init();
#if USE_GPS
gps_init();
#endif
modules_init();
settings_init();
mcu_int_enable();
#if DATALINK == XBEE
xbee_init();
#endif
#if DATALINK == UDP
udp_init();
#endif
// register the timers for the periodic functions
main_periodic_tid = sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
modules_tid = sys_time_register_timer(1./MODULES_FREQUENCY, NULL);
radio_control_tid = sys_time_register_timer((1./60.), NULL);
failsafe_tid = sys_time_register_timer(0.05, NULL);
electrical_tid = sys_time_register_timer(0.1, NULL);
baro_tid = sys_time_register_timer(1./BARO_PERIODIC_FREQUENCY, NULL);
telemetry_tid = sys_time_register_timer((1./TELEMETRY_FREQUENCY), NULL);
}
STATIC_INLINE void handle_periodic_tasks( void ) {
if (sys_time_check_and_ack_timer(main_periodic_tid))
main_periodic();
if (sys_time_check_and_ack_timer(modules_tid))
modules_periodic_task();
if (sys_time_check_and_ack_timer(radio_control_tid))
radio_control_periodic_task();
if (sys_time_check_and_ack_timer(failsafe_tid))
failsafe_check();
if (sys_time_check_and_ack_timer(electrical_tid))
electrical_periodic();
if (sys_time_check_and_ack_timer(baro_tid))
baro_periodic();
if (sys_time_check_and_ack_timer(telemetry_tid))
telemetry_periodic();
}
STATIC_INLINE void main_periodic( void ) {
imu_periodic();
/* run control loops */
autopilot_periodic();
/* set actuators */
//actuators_set(autopilot_motors_on);
SetActuatorsFromCommands(commands, autopilot_mode);
if (autopilot_in_flight) {
RunOnceEvery(PERIODIC_FREQUENCY, { autopilot_flight_time++; datalink_time++; });
}
RunOnceEvery(10, LED_PERIODIC());
}
STATIC_INLINE void telemetry_periodic(void) {
periodic_telemetry_send_Main();
}
STATIC_INLINE void failsafe_check( void ) {
if (radio_control.status != RC_OK &&
autopilot_mode != AP_MODE_KILL &&
autopilot_mode != AP_MODE_NAV)
{
autopilot_set_mode(AP_MODE_FAILSAFE);
}
#if USE_GPS
if (autopilot_mode == AP_MODE_NAV &&
#if NO_GPS_LOST_WITH_RC_VALID
radio_control.status != RC_OK &&
#endif
GpsIsLost())
{
autopilot_set_mode(AP_MODE_FAILSAFE);
}
#endif
}
STATIC_INLINE void main_event( void ) {
i2c_event();
DatalinkEvent();
if (autopilot_rc) {
RadioControlEvent(autopilot_on_rc_frame);
}
ImuEvent(on_gyro_event, on_accel_event, on_mag_event);
BaroEvent(on_baro_abs_event, on_baro_dif_event);
#if USE_GPS
GpsEvent(on_gps_event);
#endif
#if FAILSAFE_GROUND_DETECT || KILL_ON_GROUND_DETECT
DetectGroundEvent();
#endif
modules_event_task();
}
static inline void on_accel_event( void ) {
ImuScaleAccel(imu);
if (ahrs.status != AHRS_UNINIT) {
ahrs_update_accel();
}
}
static inline void on_gyro_event( void ) {
ImuScaleGyro(imu);
if (ahrs.status == AHRS_UNINIT) {
ahrs_aligner_run();
if (ahrs_aligner.status == AHRS_ALIGNER_LOCKED)
ahrs_align();
}
else {
ahrs_propagate();
#ifdef SITL
if (nps_bypass_ahrs) sim_overwrite_ahrs();
#endif
ins_propagate();
}
#ifdef USE_VEHICLE_INTERFACE
vi_notify_imu_available();
#endif
}
static inline void on_baro_abs_event( void ) {
ins_update_baro();
#ifdef USE_VEHICLE_INTERFACE
vi_notify_baro_abs_available();
#endif
}
static inline void on_baro_dif_event( void ) {
}
static inline void on_gps_event(void) {
ins_update_gps();
#ifdef USE_VEHICLE_INTERFACE
if (gps.fix == GPS_FIX_3D)
vi_notify_gps_available();
#endif
}
static inline void on_mag_event(void) {
ImuScaleMag(imu);
#if USE_MAGNETOMETER
if (ahrs.status == AHRS_RUNNING) {
ahrs_update_mag();
}
#endif
#ifdef USE_VEHICLE_INTERFACE
vi_notify_mag_available();
#endif
}