Paparazzi UAS  v6.1.0_stable
Paparazzi is a free software Unmanned Aircraft System.
main_ap.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2021 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 
31 #define MODULES_C
32 
33 #define ABI_C
34 
35 #include <math.h>
36 
38 #include "mcu.h"
39 #include "mcu_periph/sys_time.h"
42 
43 #include "generated/airframe.h"
44 #include "generated/modules.h"
45 #include "modules/core/abi.h"
46 
47 #include "led.h"
48 
49 #ifdef USE_NPS
50 #include "nps_autopilot.h"
51 #endif
52 
53 /* Default trim commands for roll, pitch and yaw */
54 #ifndef COMMAND_ROLL_TRIM
55 #define COMMAND_ROLL_TRIM 0
56 #endif
57 
58 #ifndef COMMAND_PITCH_TRIM
59 #define COMMAND_PITCH_TRIM 0
60 #endif
61 
62 #ifndef COMMAND_YAW_TRIM
63 #define COMMAND_YAW_TRIM 0
64 #endif
65 
66 /* if PRINT_CONFIG is defined, print some config options */
67 PRINT_CONFIG_VAR(PERIODIC_FREQUENCY)
68 #if !USE_GENERATED_AUTOPILOT
69 PRINT_CONFIG_VAR(NAVIGATION_FREQUENCY)
70 #endif
71 PRINT_CONFIG_VAR(CONTROL_FREQUENCY)
72 
73 /* TELEMETRY_FREQUENCY is defined in generated/periodic_telemetry.h
74  * defaults to 60Hz or set by TELEMETRY_FREQUENCY configure option in airframe file
75  */
76 #ifndef TELEMETRY_FREQUENCY
77 #define TELEMETRY_FREQUENCY 60
78 #endif
79 PRINT_CONFIG_VAR(TELEMETRY_FREQUENCY)
80 
81 
82 #if USE_IMU
83 #ifdef AHRS_PROPAGATE_FREQUENCY
84 #if (AHRS_PROPAGATE_FREQUENCY > PERIODIC_FREQUENCY)
85 #warning "PERIODIC_FREQUENCY should be least equal or greater than AHRS_PROPAGATE_FREQUENCY"
86 INFO_VALUE("it is recommended to configure in your airframe PERIODIC_FREQUENCY to at least ", AHRS_PROPAGATE_FREQUENCY)
87 #endif
88 #endif
89 #endif // USE_IMU
90 
94 tid_t modules_mcu_core_tid; // single step
96 //tid_t modules_radio_control_tid; // done in FBW
97 tid_t modules_gnc_tid; // estimation, control, actuators, default in a single step
99 
100 #define SYS_PERIOD (1.f / PERIODIC_FREQUENCY)
101 #define SENSORS_PERIOD (1.f / PERIODIC_FREQUENCY)
102 #define DATALINK_PERIOD (1.f / TELEMETRY_FREQUENCY)
103 
104 void init_ap(void)
105 {
106 #ifndef SINGLE_MCU
107  modules_mcu_init();
108 #endif
109  modules_core_init();
110  modules_sensors_init();
111  modules_estimation_init();
112  //radio_control_init(); FIXME done in FBW
113  // modules_radio_control_init(); FIXME
114  modules_control_init();
115  modules_actuators_init();
116  modules_datalink_init();
117  modules_default_init();
118 
119  // register timers with temporal dependencies
121 
122  // common GNC group (estimation, control, actuators, default)
123  // is called with an offset of half the main period (1/PERIODIC_FREQUENCY)
124  // which is the default resolution of SYS_TIME_FREQUENCY,
125  // hence the resolution of the virtual timers.
126  // In practice, this is the best compromised between having enough time between
127  // the sensor readings (triggerd in sensors task group) and the lag between
128  // the state update and control/actuators update
129  //
130  // | PERIODIC_FREQ |
131  // | | |
132  // read gnc
133  //
134  modules_gnc_tid = sys_time_register_timer_offset(modules_sensors_tid, 1.f / (2.f * PERIODIC_FREQUENCY), NULL);
135 
136  // register the timers for the periodic functions
139 
140  /* set initial trim values.
141  * these are passed to fbw via inter_mcu.
142  */
143  PPRZ_MUTEX_LOCK(ap_state_mtx);
144  ap_state->command_roll_trim = COMMAND_ROLL_TRIM;
145  ap_state->command_pitch_trim = COMMAND_PITCH_TRIM;
146  ap_state->command_yaw_trim = COMMAND_YAW_TRIM;
147  PPRZ_MUTEX_UNLOCK(ap_state_mtx);
148 
149 #if USE_IMU
150  // send body_to_imu from here for now
151  AbiSendMsgBODY_TO_IMU_QUAT(1, orientationGetQuat_f(&imu.body_to_imu));
152 #endif
153 
154 }
155 
156 
158 {
160  modules_sensors_periodic_task();
161  }
162 
164  modules_estimation_periodic_task();
165  modules_control_periodic_task();
166  modules_default_periodic_task();
167  }
168 
170  modules_mcu_periodic_task();
171  modules_core_periodic_task();
172  LED_PERIODIC(); // FIXME periodic in led module
173  }
174 
176  reporting_task();
177  modules_datalink_periodic_task(); // FIXME integrate above
178 #if defined DATALINK || defined SITL
179  RunOnceEvery(TELEMETRY_FREQUENCY, datalink_time++);
180 #endif
181  }
182 
183 }
184 
185 
186 
187 /**************************** Periodic tasks ***********************************/
188 
192 void reporting_task(void)
193 {
194  static uint8_t boot = true;
195 
196  /* initialisation phase during boot */
197  if (boot) {
198 #if DOWNLINK
200 #endif
201  boot = false;
202  }
203  /* then report periodicly */
204  else {
205 #if PERIODIC_TELEMETRY
206  periodic_telemetry_send_Ap(DefaultPeriodic, &(DefaultChannel).trans_tx, &(DefaultDevice).device);
207 #endif
208  }
209 }
210 
211 
212 /*********** EVENT ***********************************************************/
213 void event_task_ap(void)
214 {
215 #ifndef SINGLE_MCU
216  /* for SINGLE_MCU done in main_fbw */
217  /* event functions for mcu peripherals: i2c, usb_serial.. */
218  modules_mcu_event_task();
219 #endif /* SINGLE_MCU */
220  modules_core_event_task();
221  modules_sensors_event_task();
222  modules_estimation_event_task();
223  modules_control_event_task();
224  modules_datalink_event_task();
225  modules_default_event_task();
226 
227 
228  // TODO integrate in modules
229 #if defined MCU_SPI_LINK || defined MCU_UART_LINK
231 #endif
233  /* receive radio control task from fbw */
234  inter_mcu_received_fbw = false;
236  }
237 
238 } /* event_task_ap() */
239 
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
init_ap
void init_ap(void)
Definition: main_ap.c:104
modules_gnc_tid
tid_t modules_gnc_tid
Definition: main_ap.c:97
sys_time_register_timer_offset
tid_t sys_time_register_timer_offset(tid_t timer, float offset, sys_time_cb cb)
Register a new system timer with an fixed offset from another one.
Definition: sys_time.c:59
COMMAND_YAW_TRIM
#define COMMAND_YAW_TRIM
Definition: main_ap.c:63
abi.h
inter_mcu.h
Imu::body_to_imu
struct OrientationReps body_to_imu
rotation from body to imu frame
Definition: imu.h:49
PPRZ_MUTEX_UNLOCK
#define PPRZ_MUTEX_UNLOCK(_mtx)
Definition: pprz_mutex.h:47
nps_autopilot.h
SYS_PERIOD
#define SYS_PERIOD
Definition: main_ap.c:100
autopilot_send_version
void autopilot_send_version(void)
send autopilot version
Definition: autopilot.c:325
orientationGetQuat_f
static struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).
Definition: pprz_orientation_conversion.h:225
inter_mcu_received_fbw
volatile bool inter_mcu_received_fbw
Definition: inter_mcu.c:40
mcu.h
Arch independent mcu ( Micro Controller Unit ) utilities.
sys_time.h
Architecture independent timing functions.
NAVIGATION_FREQUENCY
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition: nav.h:47
LED_PERIODIC
#define LED_PERIODIC()
Definition: led_hw.h:55
SENSORS_PERIOD
#define SENSORS_PERIOD
Definition: main_ap.c:101
COMMAND_ROLL_TRIM
#define COMMAND_ROLL_TRIM
Definition: main_ap.c:55
tid_t
int8_t tid_t
sys_time timer id type
Definition: sys_time.h:56
led.h
arch independent LED (Light Emitting Diodes) API
imu
struct Imu imu
global IMU state
Definition: imu.c:108
COMMAND_PITCH_TRIM
#define COMMAND_PITCH_TRIM
Definition: main_ap.c:59
f
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup)
Definition: wedgebug.c:204
TELEMETRY_FREQUENCY
#define TELEMETRY_FREQUENCY
Definition: main_ap.c:77
PPRZ_MUTEX_LOCK
#define PPRZ_MUTEX_LOCK(_mtx)
Definition: pprz_mutex.h:46
main_ap.h
datalink_time
uint16_t datalink_time
Definition: sim_ap.c:41
reporting_task
void reporting_task(void)
Send a series of initialisation messages followed by a stream of periodic ones.
Definition: main_ap.c:192
handle_periodic_tasks_ap
void handle_periodic_tasks_ap(void)
Definition: main_ap.c:157
modules_mcu_core_tid
tid_t modules_mcu_core_tid
IDs for timers.
Definition: main_ap.c:94
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
AHRS_PROPAGATE_FREQUENCY
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:55
ap_state
struct ap_state * ap_state
Definition: inter_mcu.c:37
autopilot_on_rc_frame
void autopilot_on_rc_frame(void)
RC frame handler.
Definition: autopilot.c:176
modules_sensors_tid
tid_t modules_sensors_tid
Definition: main_ap.c:95
modules_datalink_tid
tid_t modules_datalink_tid
Definition: main_ap.c:98
CONTROL_FREQUENCY
#define CONTROL_FREQUENCY
Definition: autopilot_static.h:60
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
event_task_ap
void event_task_ap(void)
Definition: main_ap.c:213
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:119
DATALINK_PERIOD
#define DATALINK_PERIOD
Definition: main_ap.c:102