/
crtp_commander_rpyt.c
275 lines (239 loc) · 7.37 KB
/
crtp_commander_rpyt.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
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie Firmware
*
* Copyright (C) 2011-2017 Bitcraze AB
*
* This program 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, in version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include <math.h>
#include <stdbool.h>
#include "crtp_commander.h"
#include "commander.h"
#include "crtp.h"
#include "param.h"
#include "FreeRTOS.h"
#include "num.h"
#include "position_controller.h"
#define MIN_THRUST 1000
#define MAX_THRUST 60000
/**
* CRTP commander rpyt packet format
*/
struct CommanderCrtpLegacyValues
{
float roll; // deg
float pitch; // deg
float yaw; // deg
uint16_t thrust;
} __attribute__((packed));
/**
* Stabilization modes for Roll, Pitch, Yaw.
*/
typedef enum
{
RATE = 0,
ANGLE = 1,
} RPYType;
/**
* Yaw flight Modes
*/
typedef enum
{
CAREFREE = 0, // Yaw is locked to world coordinates thus heading stays the same when yaw rotates
PLUSMODE = 1, // Plus-mode. Motor M1 is defined as front
XMODE = 2, // X-mode. M1 & M4 are defined as front
} YawModeType;
static RPYType stabilizationModeRoll = ANGLE; // Current stabilization type of roll (rate or angle)
static RPYType stabilizationModePitch = ANGLE; // Current stabilization type of pitch (rate or angle)
static RPYType stabilizationModeYaw = RATE; // Current stabilization type of yaw (rate or angle)
static YawModeType yawMode = DEFAULT_YAW_MODE; // Yaw mode configuration
static bool thrustLocked = true;
static bool altHoldMode = false;
static bool posHoldMode = false;
static bool posSetMode = false;
static bool modeSet = false;
/**
* Rotate Yaw so that the Crazyflie will change what is considered front.
*
* @param yawRad Amount of radians to rotate yaw.
*/
static void rotateYaw(setpoint_t *setpoint, float yawRad)
{
float cosy = cosf(yawRad);
float siny = sinf(yawRad);
float originalRoll = setpoint->attitude.roll;
float originalPitch = setpoint->attitude.pitch;
setpoint->attitude.roll = originalRoll * cosy - originalPitch * siny;
setpoint->attitude.pitch = originalPitch * cosy + originalRoll * siny;
}
/**
* Update Yaw according to current setting
*/
static void yawModeUpdate(setpoint_t *setpoint)
{
switch (yawMode)
{
case CAREFREE:
// TODO: Add frame of reference to setpoint
ASSERT(false);
break;
case PLUSMODE:
rotateYaw(setpoint, 45 * M_PI / 180);
break;
case XMODE: // Fall through
default:
// Default in x-mode. Do nothing
break;
}
}
void crtpCommanderRpytDecodeSetpoint(setpoint_t *setpoint, CRTPPacket *pk)
{
struct CommanderCrtpLegacyValues *values = (struct CommanderCrtpLegacyValues*)pk->data;
if (commanderGetActivePriority() == COMMANDER_PRIORITY_DISABLE) {
thrustLocked = true;
}
if (values->thrust == 0) {
thrustLocked = false;
}
// Thrust
uint16_t rawThrust = values->thrust;
if (thrustLocked || (rawThrust < MIN_THRUST)) {
setpoint->thrust = 0;
} else {
setpoint->thrust = fminf(rawThrust, MAX_THRUST);
}
if (altHoldMode) {
if (!modeSet) { //Reset filter and PID values on first initiation of assist mode to prevent sudden reactions.
modeSet = true;
positionControllerResetAllPID();
positionControllerResetAllfilters();
}
setpoint->thrust = 0;
setpoint->mode.z = modeVelocity;
setpoint->velocity.z = ((float) rawThrust - 32767.f) / 32767.f;
} else {
setpoint->mode.z = modeDisable;
modeSet = false;
}
// roll/pitch
if (posHoldMode) {
setpoint->mode.x = modeVelocity;
setpoint->mode.y = modeVelocity;
setpoint->mode.roll = modeDisable;
setpoint->mode.pitch = modeDisable;
setpoint->velocity.x = values->pitch/30.0f;
setpoint->velocity.y = values->roll/30.0f;
setpoint->attitude.roll = 0;
setpoint->attitude.pitch = 0;
} else if (posSetMode && values->thrust != 0) {
setpoint->mode.x = modeAbs;
setpoint->mode.y = modeAbs;
setpoint->mode.z = modeAbs;
setpoint->mode.roll = modeDisable;
setpoint->mode.pitch = modeDisable;
setpoint->mode.yaw = modeAbs;
setpoint->position.x = -values->pitch;
setpoint->position.y = values->roll;
setpoint->position.z = values->thrust/1000.0f;
setpoint->attitude.roll = 0;
setpoint->attitude.pitch = 0;
setpoint->attitude.yaw = values->yaw;
setpoint->thrust = 0;
} else {
setpoint->mode.x = modeDisable;
setpoint->mode.y = modeDisable;
if (stabilizationModeRoll == RATE) {
setpoint->mode.roll = modeVelocity;
setpoint->attitudeRate.roll = values->roll;
setpoint->attitude.roll = 0;
} else {
setpoint->mode.roll = modeAbs;
setpoint->attitudeRate.roll = 0;
setpoint->attitude.roll = values->roll;
}
if (stabilizationModePitch == RATE) {
setpoint->mode.pitch = modeVelocity;
setpoint->attitudeRate.pitch = values->pitch;
setpoint->attitude.pitch = 0;
} else {
setpoint->mode.pitch = modeAbs;
setpoint->attitudeRate.pitch = 0;
setpoint->attitude.pitch = values->pitch;
}
setpoint->velocity.x = 0;
setpoint->velocity.y = 0;
}
// Yaw
if (!posSetMode) {
if (stabilizationModeYaw == RATE) {
// legacy rate input is inverted
setpoint->attitudeRate.yaw = -values->yaw;
yawModeUpdate(setpoint);
setpoint->mode.yaw = modeVelocity;
} else {
setpoint->mode.yaw = modeAbs;
setpoint->attitudeRate.yaw = 0;
setpoint->attitude.yaw = values->yaw;
}
}
}
/**
* There are 2 levels to control: Position (X, Y, Z) and Attitude (pitch, roll, yaw or in quaternions)
*
* These can be controlled in different modes: Absolute mode, Velocity mode or Disabled
*
* These parameters have impact on which level and mode to use.
*/
PARAM_GROUP_START(flightmode)
/**
* @brief Keeps the quad at its current altitude automatically
*
* Thrust control becomes height velocity control.
*/
PARAM_ADD_CORE(PARAM_UINT8, althold, &altHoldMode)
/**
* @brief Keeps the quad at its current 3D position
*
* Pitch/Roll/Thrust control becomes X/Y/Z velocity control.
* X and U
*/
PARAM_ADD_CORE(PARAM_UINT8, poshold, &posHoldMode)
/**
* @brief Set to nonzero to select absolute mode for positioning
*/
PARAM_ADD_CORE(PARAM_UINT8, posSet, &posSetMode)
/**
* @brief Carefree(0), plusmode(1), xmode(2)
*/
PARAM_ADD(PARAM_UINT8, yawMode, &yawMode)
/**
* @brief Stabilization type for roll: rate(0) or angle(1)
*/
PARAM_ADD_CORE(PARAM_UINT8, stabModeRoll, &stabilizationModeRoll)
/**
* @brief Stabilization type for pitch: rate(0) or angle(1)
*/
PARAM_ADD_CORE(PARAM_UINT8, stabModePitch, &stabilizationModePitch)
/**
* @brief Stabilization type for yaw: rate(0) or angle(1)
*/
PARAM_ADD_CORE(PARAM_UINT8, stabModeYaw, &stabilizationModeYaw)
PARAM_GROUP_STOP(flightmode)