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
autopilot.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
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 
29 #ifndef AUTOPILOT_H
30 #define AUTOPILOT_H
31 
32 #include "std.h"
33 #include "generated/airframe.h"
34 #include "state.h"
35 
36 #define AP_MODE_KILL 0
37 #define AP_MODE_FAILSAFE 1
38 #define AP_MODE_HOME 2
39 #define AP_MODE_RATE_DIRECT 3
40 #define AP_MODE_ATTITUDE_DIRECT 4
41 #define AP_MODE_RATE_RC_CLIMB 5
42 #define AP_MODE_ATTITUDE_RC_CLIMB 6
43 #define AP_MODE_ATTITUDE_CLIMB 7
44 #define AP_MODE_RATE_Z_HOLD 8
45 #define AP_MODE_ATTITUDE_Z_HOLD 9
46 #define AP_MODE_HOVER_DIRECT 10
47 #define AP_MODE_HOVER_CLIMB 11
48 #define AP_MODE_HOVER_Z_HOLD 12
49 #define AP_MODE_NAV 13
50 #define AP_MODE_RC_DIRECT 14 // Safety Pilot Direct Commands for helicopter direct control
51 #define AP_MODE_CARE_FREE_DIRECT 15
52 #define AP_MODE_FORWARD 16
53 #define AP_MODE_MODULE 17
54 #define AP_MODE_FLIP 18
55 #define AP_MODE_GUIDED 19
56 
57 extern uint8_t autopilot_mode;
59 extern bool autopilot_motors_on;
60 extern bool autopilot_in_flight;
61 extern bool kill_throttle;
62 extern bool autopilot_rc;
63 
64 extern bool autopilot_power_switch;
65 
66 extern void autopilot_init(void);
67 extern void autopilot_periodic(void);
68 extern void autopilot_on_rc_frame(void);
69 extern void autopilot_set_mode(uint8_t new_autopilot_mode);
70 extern void autopilot_SetModeHandler(float new_autopilot_mode); // handler for dl_setting
71 extern void autopilot_set_motors_on(bool motors_on);
72 extern void autopilot_check_in_flight(bool motors_on);
73 
74 extern bool autopilot_ground_detected;
76 
78 
81 #ifndef MODE_MANUAL
82 #define MODE_MANUAL AP_MODE_ATTITUDE_DIRECT
83 #endif
84 #ifndef MODE_AUTO1
85 #define MODE_AUTO1 AP_MODE_HOVER_Z_HOLD
86 #endif
87 #ifndef MODE_AUTO2
88 #define MODE_AUTO2 AP_MODE_NAV
89 #endif
90 
91 
92 #define autopilot_KillThrottle(_kill) { \
93  if (_kill) \
94  autopilot_set_motors_on(FALSE); \
95  else \
96  autopilot_set_motors_on(TRUE); \
97  }
98 
99 #ifdef POWER_SWITCH_GPIO
100 #include "mcu_periph/gpio.h"
101 #define autopilot_SetPowerSwitch(_v) { \
102  autopilot_power_switch = _v; \
103  if (_v) { gpio_set(POWER_SWITCH_GPIO); } \
104  else { gpio_clear(POWER_SWITCH_GPIO); } \
105  }
106 #else
107 #define autopilot_SetPowerSwitch(_v) { \
108  autopilot_power_switch = _v; \
109  }
110 #endif
111 
116 #ifdef ROTORCRAFT_IS_HELI
117 #define SetRotorcraftCommands(_cmd, _in_flight, _motor_on) { \
118  commands[COMMAND_ROLL] = _cmd[COMMAND_ROLL]; \
119  commands[COMMAND_PITCH] = _cmd[COMMAND_PITCH]; \
120  commands[COMMAND_YAW] = _cmd[COMMAND_YAW]; \
121  commands[COMMAND_THRUST] = _cmd[COMMAND_THRUST]; \
122  }
123 #else
124 
125 #ifndef ROTORCRAFT_COMMANDS_YAW_ALWAYS_ENABLED
126 #define SetRotorcraftCommands(_cmd, _in_flight, _motor_on) { \
127  if (!(_in_flight)) { _cmd[COMMAND_YAW] = 0; } \
128  if (!(_motor_on)) { _cmd[COMMAND_THRUST] = 0; } \
129  commands[COMMAND_ROLL] = _cmd[COMMAND_ROLL]; \
130  commands[COMMAND_PITCH] = _cmd[COMMAND_PITCH]; \
131  commands[COMMAND_YAW] = _cmd[COMMAND_YAW]; \
132  commands[COMMAND_THRUST] = _cmd[COMMAND_THRUST]; \
133  }
134 #else
135 #define SetRotorcraftCommands(_cmd, _in_flight, _motor_on) { \
136  if (!(_motor_on)) { _cmd[COMMAND_THRUST] = 0; } \
137  commands[COMMAND_ROLL] = _cmd[COMMAND_ROLL]; \
138  commands[COMMAND_PITCH] = _cmd[COMMAND_PITCH]; \
139  commands[COMMAND_YAW] = _cmd[COMMAND_YAW]; \
140  commands[COMMAND_THRUST] = _cmd[COMMAND_THRUST]; \
141  }
142 #endif
143 #endif
144 
146 #ifndef THRESHOLD_GROUND_DETECT
147 #define THRESHOLD_GROUND_DETECT 25.0
148 #endif
149 
151 static inline void DetectGroundEvent(void)
152 {
154  struct NedCoor_f *accel = stateGetAccelNed_f();
155  if (accel->z < -THRESHOLD_GROUND_DETECT ||
156  accel->z > THRESHOLD_GROUND_DETECT) {
159  }
160  }
161 }
162 
163 #include "subsystems/settings.h"
164 
165 /* try to make sure that we don't write to flash while flying */
166 static inline void autopilot_StoreSettings(float store)
167 {
168  if (kill_throttle && store) {
169  settings_store_flag = store;
170  settings_store();
171  }
172 }
173 
174 static inline void autopilot_ClearSettings(float clear)
175 {
176  if (kill_throttle && clear) {
177  settings_clear_flag = clear;
178  settings_clear();
179  }
180 }
181 
182 #if DOWNLINK
183 #include "pprzlink/pprzlink_transport.h"
184 extern void send_autopilot_version(struct transport_tx *trans, struct link_device *dev);
185 #endif
186 
194 extern bool autopilot_guided_goto_ned(float x, float y, float z, float heading);
195 
203 extern bool autopilot_guided_goto_ned_relative(float dx, float dy, float dz, float dyaw);
204 
212 extern bool autopilot_guided_goto_body_relative(float dx, float dy, float dz, float dyaw);
213 
221 extern bool autopilot_guided_move_ned(float vx, float vy, float vz, float heading);
222 
242 extern void autopilot_guided_update(uint8_t flags, float x, float y, float z, float yaw);
243 
244 #endif /* AUTOPILOT_H */
static void autopilot_ClearSettings(float clear)
Definition: autopilot.h:174
unsigned short uint16_t
Definition: types.h:16
void autopilot_periodic(void)
Definition: autopilot.c:344
bool autopilot_ground_detected
Definition: autopilot.c:82
float y
in meters
bool autopilot_in_flight
Definition: autopilot.c:72
uint16_t autopilot_flight_time
flight time in seconds.
Definition: autopilot.c:48
void autopilot_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot.c:424
bool kill_throttle
Definition: autopilot.c:42
Some architecture independent helper functions for GPIOs.
bool autopilot_rc
Definition: autopilot.c:79
void autopilot_guided_update(uint8_t flags, float x, float y, float z, float yaw)
Set guided setpoints using flag mask in GUIDED mode.
Definition: autopilot.c:610
int32_t settings_store(void)
store settings marked as persistent to flash
Definition: settings.c:57
void autopilot_SetModeHandler(float new_autopilot_mode)
AP mode setting handler.
Definition: autopilot.c:408
bool autopilot_guided_move_ned(float vx, float vy, float vz, float heading)
Set velocity and heading setpoints in GUIDED mode.
Definition: autopilot.c:586
static void autopilot_StoreSettings(float store)
Definition: autopilot.h:166
float z
in meters
void autopilot_set_motors_on(bool motors_on)
Definition: autopilot.c:704
void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:64
static float heading
Definition: ahrs_infrared.c:45
int32_t settings_clear(void)
clear all persistent settings from flash
Definition: settings.c:77
vector in North East Down coordinates Units: meters
bool autopilot_guided_goto_ned(float x, float y, float z, float heading)
Set position and heading setpoints in GUIDED mode.
Definition: autopilot.c:550
#define THRESHOLD_GROUND_DETECT
Z-acceleration threshold to detect ground in m/s^2.
Definition: autopilot.h:147
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1020
void autopilot_init(void)
Autopilot inititalization.
Definition: autopilot.c:175
bool autopilot_detect_ground_once
Definition: autopilot.c:83
bool autopilot_guided_goto_body_relative(float dx, float dy, float dz, float dyaw)
Set position and heading setpoints wrt.
Definition: autopilot.c:573
bool settings_store_flag
flag for setting feedback.
Definition: settings.c:38
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
Persistent settings interface.
void autopilot_check_in_flight(bool motors_on)
Definition: autopilot.c:669
void autopilot_on_rc_frame(void)
Get autopilot mode from two 2way switches.
Definition: autopilot.c:758
#define AP_MODE_FAILSAFE
Definition: autopilot.h:37
uint8_t autopilot_mode_auto2
Definition: autopilot.c:70
bool autopilot_motors_on
Definition: autopilot.c:76
uint8_t autopilot_mode
Definition: autopilot.c:69
bool autopilot_guided_goto_ned_relative(float dx, float dy, float dz, float dyaw)
Set position and heading setpoints wrt.
Definition: autopilot.c:561
static void DetectGroundEvent(void)
Ground detection based on vertical acceleration.
Definition: autopilot.h:151
bool settings_clear_flag
Definition: settings.c:40
float x
in meters
bool autopilot_power_switch
Definition: autopilot.c:80