Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
autopilot_firmware.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008-2012 The Paparazzi Team
3 * Copyright (C) 2016-2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
30
31#include "generated/modules.h"
32
33#include <stdint.h>
34//#include "mcu_periph/sys_time.h"
38
39#if USE_GPS
40#include "modules/gps/gps.h"
41#else
42#if NO_GPS_NEEDED_FOR_NAV
43#define GpsIsLost() FALSE
44#else
45#define GpsIsLost() TRUE
46#endif
47#endif
48
51
52/* Geofence exceptions */
54
56#ifndef AUTOPILOT_IN_FLIGHT_TIME
57#define AUTOPILOT_IN_FLIGHT_TIME 20
58#endif
59
61#ifndef AUTOPILOT_IN_FLIGHT_MIN_SPEED
62#define AUTOPILOT_IN_FLIGHT_MIN_SPEED 0.2
63#endif
64
66#ifndef AUTOPILOT_IN_FLIGHT_MIN_ACCEL
67#define AUTOPILOT_IN_FLIGHT_MIN_ACCEL 2.0
68#endif
69
71#ifndef AUTOPILOT_IN_FLIGHT_MIN_THRUST
72#define AUTOPILOT_IN_FLIGHT_MIN_THRUST 500
73#endif
74
76#ifndef THRESHOLD_GROUND_DETECT
77#define THRESHOLD_GROUND_DETECT 25.0
78#endif
79
82 struct NedCoor_f *accel = stateGetAccelNed_f();
83 if (accel->z < -THRESHOLD_GROUND_DETECT ||
84 accel->z > THRESHOLD_GROUND_DETECT) {
85 return true;
86 }
87 return false;
88}
89
90
94 /* probably in_flight if thrust, speed and accel above IN_FLIGHT_MIN thresholds */
100 return true;
101 }
102 } else { /* thrust, speed or accel not above min threshold, reset counter */
104 }
105 }
106 return false;
107}
108
109
110#if USE_MOTOR_MIXING
112#endif
113
114static void send_status(struct transport_tx *trans, struct link_device *dev)
115{
117#if USE_MOTOR_MIXING
119#else
121#endif
122#if USE_GPS
123 uint8_t fix = gps.fix;
124#else
125 uint8_t fix = 0;
126#endif
127 uint8_t in_flight = autopilot.in_flight;
128 uint8_t motors_on = autopilot.motors_on;
133 &fix, &autopilot.mode, &in_flight, &motors_on,
136}
137
138static void send_energy(struct transport_tx *trans, struct link_device *dev)
139{
140 uint8_t throttle = 100 * autopilot.throttle / MAX_PPRZ;
141 float power = electrical.vsupply * electrical.current;
142 float avg_power = 0;
143 if(electrical.avg_cnt != 0) {
145 }
146
148 &throttle, &electrical.vsupply, &electrical.current, &power, &avg_power, &electrical.charge, &electrical.energy);
149}
150
151static void send_fp(struct transport_tx *trans, struct link_device *dev)
152{
156 struct EnuCoor_i *pos = stateGetPositionEnu_i();
157#if GUIDANCE_INDI_HYBRID
158 struct FloatEulers eulers_zxy;
160 struct Int32Eulers att;
162#else
164#endif
166 &pos->x,
167 &pos->y,
168 &pos->z,
169 &(stateGetSpeedEnu_i()->x),
170 &(stateGetSpeedEnu_i()->y),
171 &(stateGetSpeedEnu_i()->z),
172 &att.phi,
173 &att.theta,
174 &att.psi,
177 &carrot_up,
179 &thrust,
181}
182
183static void send_body_rates_accel(struct transport_tx *trans, struct link_device *dev)
184{
187 &(stateGetBodyRates_f()->q),
188 &(stateGetBodyRates_f()->r),
189 &(stateGetAccelBody_i()->x),
190 &(stateGetAccelBody_i()->y),
191 &(stateGetAccelBody_i()->z));
192}
193
194static void send_fp_min(struct transport_tx *trans, struct link_device *dev)
195{
196#if USE_GPS
197 uint16_t gspeed = gps.gspeed;
198#else
199 // ground speed in cm/s
201#endif
203 &(stateGetPositionEnu_i()->x),
204 &(stateGetPositionEnu_i()->y),
205 &(stateGetPositionEnu_i()->z),
206 &gspeed);
207}
208
209#ifdef RADIO_CONTROL
226#endif
227
228#if defined(COMMAND_ROLL) && defined(COMMAND_PITCH) && defined(COMMAND_YAW)
229static void send_rotorcraft_cmd(struct transport_tx *trans, struct link_device *dev)
230{
236}
237#else
238static void send_rotorcraft_cmd(struct transport_tx *trans UNUSED, struct link_device *dev UNUSED) {}
239#endif
240
241
260
262{
263#if DOWNLINK
264 send_status(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
265#endif
266}
267
273{
277#endif
278 ) {
282 }
283 }
284}
285
292
295void autopilot_check_in_flight(bool motors_on)
296{
297 if (autopilot.in_flight) {
298 if (autopilot_in_flight_end_detection(motors_on)) {
299 autopilot.in_flight = false;
301 }
302 } else { /* currently not in flight */
304 motors_on) {
305 /* if thrust above min threshold, assume in_flight.
306 * Don't check for velocity and acceleration above threshold here...
307 */
311 autopilot.in_flight = true;
312 }
313 } else { /* currently not in_flight and thrust below threshold, reset counter */
315 }
316 }
317 }
318}
319
struct pprz_autopilot autopilot
Global autopilot structure.
Definition autopilot.c:49
bool motors_on
motor status
Definition autopilot.h:68
bool ground_detected
automatic detection of landing
Definition autopilot.h:73
uint8_t arming_status
arming status
Definition autopilot.h:67
pprz_t throttle
throttle level as will be displayed in GCS
Definition autopilot.h:66
bool detect_ground_once
enable automatic detection of ground (one shot)
Definition autopilot.h:74
uint8_t mode
current autopilot mode
Definition autopilot.h:63
bool in_flight
in flight status
Definition autopilot.h:70
uint16_t flight_time
flight time in seconds
Definition autopilot.h:65
#define UNUSED(x)
struct Electrical electrical
Definition electrical.c:92
Interface for electrical status: supply voltage, current, battery status, etc.
uint32_t avg_power
average power sum
Definition electrical.h:53
float energy
consumed energy in Wh
Definition electrical.h:49
float current
current in A
Definition electrical.h:47
uint32_t avg_cnt
average power counter
Definition electrical.h:54
float charge
consumed electric charge in Ah
Definition electrical.h:48
float vboard
board voltage in V
Definition electrical.h:46
float vsupply
supply voltage in V
Definition electrical.h:45
void autopilot_firmware_init(void)
Init function.
void autopilot_send_mode(void)
Report autopilot mode on default downlink channel.
static void send_energy(struct transport_tx *trans, struct link_device *dev)
struct GpsState gps
global GPS state
Definition gps.c:74
Device independent GPS code (interface)
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition gps.h:97
uint8_t fix
status of fix
Definition gps.h:107
void float_eulers_of_quat_zxy(struct FloatEulers *e, struct FloatQuat *q)
euler rotation 'ZXY' This rotation order is useful if you need 90 deg pitch
euler angles
#define EULERS_BFP_OF_REAL(_ei, _ef)
int32_t phi
in rad with INT32_ANGLE_FRAC
int32_t psi
in rad with INT32_ANGLE_FRAC
int32_t theta
in rad with INT32_ANGLE_FRAC
#define ANGLE_BFP_OF_REAL(_af)
euler angles
int32_t y
North.
int32_t z
Up.
int32_t x
East.
vector in East North Up coordinates
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition state.h:1195
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition state.h:1288
static struct FloatQuat * stateGetNedToBodyQuat_f(void)
Get vehicle body attitude quaternion (float).
Definition state.h:1294
static struct EnuCoor_i * stateGetPositionEnu_i(void)
Get position in local ENU coordinates (int).
Definition state.h:803
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition state.h:1367
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition state.h:1076
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition state.h:1049
static struct Int32Vect3 * stateGetAccelBody_i(void)
Get acceleration in Body coordinates (int).
Definition state.h:1094
static struct EnuCoor_i * stateGetSpeedEnu_i(void)
Get ground speed in local ENU coordinates (int).
Definition state.h:1013
struct FloatEulers eulers_zxy
state eulers in zxy order
static float p[2][2]
uint16_t foo
Definition main_demo5.c:58
struct MotorMixing motor_mixing
Motor Mixing.
uint32_t nb_failure
uint32_t nb_saturation
Optional exceptions triggeringg HOME_MODE 1) GEOFENCE_DATALINK_LOST_TIME: go to HOME mode if datalink...
#define MAX_PPRZ
Definition paparazzi.h:8
float z
in meters
vector in North East Down coordinates Units: meters
struct RadioControl radio_control
Generic interface for radio control modules.
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
uint8_t frame_rate
void autopilot_event(void)
autopilot event function
#define AUTOPILOT_IN_FLIGHT_MIN_SPEED
minimum vertical speed for in_flight condition in m/s
static void send_status(struct transport_tx *trans, struct link_device *dev)
uint8_t autopilot_mode_auto2
#define AUTOPILOT_IN_FLIGHT_TIME
time steps for in_flight detection (at 20Hz, so 20=1second)
static void send_body_rates_accel(struct transport_tx *trans, struct link_device *dev)
#define THRESHOLD_GROUND_DETECT
Z-acceleration threshold to detect ground in m/s^2.
bool WEAK autopilot_in_flight_end_detection(bool motors_on UNUSED)
Default end-of-in-flight detection estimation based on thrust and speed.
static void send_rotorcraft_rc(struct transport_tx *trans, struct link_device *dev)
void autopilot_check_in_flight(bool motors_on)
in flight check utility function
static void send_fp_min(struct transport_tx *trans, struct link_device *dev)
#define AUTOPILOT_IN_FLIGHT_MIN_ACCEL
minimum vertical acceleration for in_flight condition in m/s^2
static void send_rotorcraft_cmd(struct transport_tx *trans UNUSED, struct link_device *dev UNUSED)
static void send_fp(struct transport_tx *trans, struct link_device *dev)
bool WEAK autopilot_ground_detection(void)
Default ground-detection estimation based on accelerometer shock.
static uint32_t autopilot_in_flight_counter
void autopilot_reset_in_flight_counter(void)
reset in_flight counter
#define AUTOPILOT_IN_FLIGHT_MIN_THRUST
minimum thrust for in_flight condition in pprz_t units (max = 9600)
Rotorcraft specific autopilot interface and initialization.
#define MODE_AUTO2
#define AP_MODE_FAILSAFE
struct HorizontalGuidance guidance_h
Definition guidance_h.c:45
struct HorizontalGuidanceSetpoint sp
setpoints
Definition guidance_h.h:108
struct VerticalGuidance guidance_v
Definition guidance_v.c:60
int32_t z_sp
altitude setpoint in meters (input).
Definition guidance_v.h:50
struct Stabilization stabilization
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
struct Int32Vect2 pos
horizontal position setpoint in NED.
Definition guidance_h.h:73
volatile uint32_t nb_sec
full seconds since startup
Definition sys_time.h:72
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition telemetry.h:66
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.