/
sitaw.c
347 lines (316 loc) · 10.7 KB
/
sitaw.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2015 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/>.
*
* sitAw.h - Implementation of situation awareness.
*/
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include "log.h"
#include "param.h"
#include "trigger.h"
#include "sitaw.h"
#include "commander.h"
#include "stabilizer.h"
#include "motors.h"
/* Trigger object used to detect Free Fall situation. */
static trigger_t sitAwFFAccWZ;
/* Trigger object used to detect At Rest situation. */
static trigger_t sitAwARAccZ;
/* Trigger object used to detect Tumbled situation. */
static trigger_t sitAwTuAcc;
#if defined(SITAW_ENABLED)
#if defined(SITAW_LOG_ENABLED) /* Enable the log group. */
LOG_GROUP_START(sitAw)
#if defined(SITAW_FF_LOG_ENABLED) /* Log trigger variables for Free Fall detection. */
LOG_ADD(LOG_UINT32, FFAccWZTestCounter, &sitAwFFAccWZ.testCounter)
LOG_ADD(LOG_UINT8, FFAccWZDetected, &sitAwFFAccWZ.released)
#endif
#if defined(SITAW_AR_LOG_ENABLED) /* Log trigger variables for At Rest detection. */
LOG_ADD(LOG_UINT32, ARTestCounter, &sitAwARAccZ.testCounter)
LOG_ADD(LOG_UINT8, ARDetected, &sitAwARAccZ.released)
#endif
#if defined(SITAW_TU_LOG_ENABLED) /* Log trigger variables for Tumbled detection. */
LOG_ADD(LOG_UINT32, TuTestCounter, &sitAwTuAcc.testCounter)
LOG_ADD(LOG_UINT8, TuDetected, &sitAwTuAcc.released)
#endif
#if defined(SITAW_LOG_ALL_DETECT_ENABLED) /* Log all the 'Detected' flags. */
LOG_ADD(LOG_UINT8, FFAccWZDetected, &sitAwFFAccWZ.released)
LOG_ADD(LOG_UINT8, ARDetected, &sitAwARAccZ.released)
LOG_ADD(LOG_UINT8, TuDetected, &sitAwTuAcc.released)
#endif
LOG_GROUP_STOP(sitAw)
#endif /* SITAW_LOG_ENABLED */
#if defined(SITAW_PARAM_ENABLED) /* Enable the param group. */
PARAM_GROUP_START(sitAw)
#if defined(SITAW_FF_PARAM_ENABLED) /* Param variables for Free Fall detection. */
PARAM_ADD(PARAM_UINT8, FFActive, &sitAwFFAccWZ.active)
PARAM_ADD(PARAM_UINT32, FFTriggerCount, &sitAwFFAccWZ.triggerCount)
PARAM_ADD(PARAM_FLOAT, FFaccWZ, &sitAwFFAccWZ.threshold)
#endif
#if defined(SITAW_AR_PARAM_ENABLED) /* Param variables for At Rest detection. */
PARAM_ADD(PARAM_UINT8, ARActive, &sitAwARAccZ.active)
PARAM_ADD(PARAM_UINT32, ARTriggerCount, &sitAwARAccZ.triggerCount)
PARAM_ADD(PARAM_FLOAT, ARaccZ, &sitAwARAccZ.threshold)
#endif
#if defined(SITAW_TU_PARAM_ENABLED) /* Param variables for Tumbled detection. */
PARAM_ADD(PARAM_UINT8, TuActive, &sitAwTuAcc.active)
PARAM_ADD(PARAM_UINT32, TuTriggerCount, &sitAwTuAcc.triggerCount)
PARAM_ADD(PARAM_FLOAT, TuAcc, &sitAwTuAcc.threshold)
#endif
PARAM_GROUP_STOP(sitAw)
#endif /* SITAW_PARAM_ENABLED */
#endif /* SITAW_ENABLED */
// forward declaration of private functions
#ifdef SITAW_FF_ENABLED
static void sitAwFFTest(float accWZ, float accMag);
#endif
#ifdef SITAW_AR_ENABLED
static void sitAwARTest(float accX, float accY, float accZ);
#endif
#ifdef SITAW_TU_ENABLED
static void sitAwTuTest(float accz);
#endif
static void sitAwPostStateUpdateCallOut(const sensorData_t *sensorData,
const state_t *state)
{
/* Code that shall run AFTER each attitude update, should be placed here. */
#if defined(SITAW_ENABLED)
#ifdef SITAW_FF_ENABLED
float accMAG = (sensorData->acc.x*sensorData->acc.x) +
(sensorData->acc.y*sensorData->acc.y) +
(sensorData->acc.z*sensorData->acc.z);
/* Test values for Free Fall detection. */
sitAwFFTest(state->acc.z, accMAG);
#endif
#ifdef SITAW_TU_ENABLED
/* check if we actually fly */
int sumRatio = 0;
for (int i = 0; i < NBR_OF_MOTORS; ++i) {
sumRatio += motorsGetRatio(i);
}
bool isFlying = sumRatio > SITAW_TU_IN_FLIGHT_THRESHOLD;
if (isFlying) {
/* Test values for Tumbled detection. */
sitAwTuTest(sensorData->acc.z);
}
#endif
#ifdef SITAW_AR_ENABLED
/* Test values for At Rest detection. */
sitAwARTest(sensorData->acc.x, sensorData->acc.y, sensorData->acc.z);
#endif
#endif
}
static void sitAwPreThrustUpdateCallOut(setpoint_t *setpoint)
{
/* Code that shall run BEFORE each thrust distribution update, should be placed here. */
#if defined(SITAW_ENABLED)
#ifdef SITAW_TU_ENABLED
if(sitAwTuDetected()) {
/* Kill the thrust to the motors if a Tumbled situation is detected. */
stabilizerSetEmergencyStop();
}
#endif
#ifdef SITAW_FF_ENABLED
/* Force altHold mode if free fall is detected.
FIXME: Needs a flying/landing state (as soon as althold is enabled,
we are not freefalling anymore)
*/
if(sitAwFFDetected() && !sitAwTuDetected()) {
setpoint->mode.z = modeVelocity;
setpoint->velocity.z = 0;
}
#endif
#endif
}
/**
* Update setpoint according to current situation
*
* Called by the stabilizer after state and setpoint update. This function
* should update the setpoint accordig to the current state situation
*/
void sitAwUpdateSetpoint(setpoint_t *setpoint, const sensorData_t *sensorData,
const state_t *state)
{
sitAwPostStateUpdateCallOut(sensorData, state);
sitAwPreThrustUpdateCallOut(setpoint);
}
#ifdef SITAW_FF_ENABLED
/**
* Initialize the Free Fall detection.
*
* See the sitAwFFTest() function for details.
*/
void sitAwFFInit(void)
{
triggerInit(&sitAwFFAccWZ, triggerFuncIsLE, SITAW_FF_THRESHOLD, SITAW_FF_TRIGGER_COUNT);
triggerActivate(&sitAwFFAccWZ, true);
}
/**
* Test values for a Free Fall situation.
*
* A free fall situation is considered identified when the vertical
* acceleration of the crazyflie (regardless of orientation - given by
* AccWZ) is approaching -1 (AccWZ is 0 when crazyflie is at rest). We
* will look for when AccWZ is within SITAW_FF_THRESHOLD of -1.
*
* At the same time, there should be no other accelerations experienced
* by the crazyflie.
* This can be checked by looking at the accMAG (total acceleration). If
* the accMAG is approaching 0, there are no other accelerations than accWZ.
* This helps to distinguish free fall situations from other movements
* such as shaking.
*
* @param accWZ Vertical acceleration (regardless of orientation)
* @param accMAG All experienced accelerations.
*/
void sitAwFFTest(float accWZ, float accMAG)
{
/* Check that the total acceleration is close to zero. */
if(fabs(accMAG) > SITAW_FF_THRESHOLD) {
/* If the total acceleration deviates from 0, this is not a free fall situation. */
triggerReset(&sitAwFFAccWZ);
} else {
/**
* AccWZ approaches -1 in free fall. Check that the value stays within
* SITAW_FF_THRESHOLD of -1 for the triggerCount specified.
*/
triggerTestValue(&sitAwFFAccWZ, fabs(accWZ + 1));
}
}
/**
* Check if a Free Fall situation has been detected.
*
* @return True if the situation has been detected, otherwise false.
*/
bool sitAwFFDetected(void)
{
return sitAwFFAccWZ.released;
}
#endif
#ifdef SITAW_AR_ENABLED
/**
* Initialize the At Rest detection.
*
* See the sitAwARTest() function for details.
*/
void sitAwARInit(void)
{
triggerInit(&sitAwARAccZ, triggerFuncIsLE, SITAW_AR_THRESHOLD, SITAW_AR_TRIGGER_COUNT);
triggerActivate(&sitAwARAccZ, true);
}
/**
* Test values for an At Rest situation.
*
* An At Rest situation is considered identified when the crazyflie is
* placed on its feet (accZ = 1) and with no horizontal accelerations
* (accX = accY = 0).
*
* Since there is always some minor noise in the measurements, we use
* a margin of SITAW_AR_THRESHOLD from the ideal values. Since this function
* does not check for thrust, the SITAW_AR_THRESHOLD is assumed to be set
* sufficiently close to the absolute resting values so that these values cannot
* be achieved (over time) during hovering or flight.
*
* @param accX Horizontal X acceleration (when crazyflie is placed on its feet)
* @param accY Horizontal Y acceleration (when crazyflie is placed on its feet)
* @param accZ Vertical Z acceleration (when crazyflie is placed on its feet)
*/
void sitAwARTest(float accX, float accY, float accZ)
{
/* Check that there are no horizontal accelerations. At rest, these are 0. */
if((fabs(accX) > SITAW_AR_THRESHOLD) || (fabs(accY) > SITAW_AR_THRESHOLD)) {
/* If the X or Y accelerations are different than 0, the crazyflie is not at rest. */
triggerReset(&sitAwARAccZ);
}
/**
* If the test above indicates that there are no horizontal movements, test the
* vertical acceleration value against the trigger.
*
* The vertical acceleration must be close to 1, but is allowed to oscillate slightly
* around 1. Testing that the deviation from 1 stays within SITAW_AR_THRESHOLD.
*/
triggerTestValue(&sitAwARAccZ, fabs(accZ - 1));
}
/**
* Check if an At Rest situation has been detected.
*
* @return True if the situation has been detected, otherwise false.
*/
bool sitAwARDetected(void)
{
return sitAwARAccZ.released;
}
#endif
#ifdef SITAW_TU_ENABLED
/**
* Initialize the Tumbled detection.
*
* See the sitAwTuTest() function for details.
*/
void sitAwTuInit(void)
{
triggerInit(&sitAwTuAcc, triggerFuncIsLE, SITAW_TU_ACC_THRESHOLD, SITAW_TU_ACC_TRIGGER_COUNT);
triggerActivate(&sitAwTuAcc, true);
}
/**
* Test values for a Tumbled situation.
*
* A tumbled situation is considered identified when accelerometer reports
* a negative value.
*
* Once a tumbled situation is identified, this can be used for instance to
* cut the thrust to the motors, avoiding the crazyflie from running
* propellers at significant thrust when accidentally crashing into walls
* or the ground.
* @param The current accelerometer reading in z direction
*/
void sitAwTuTest(float accz)
{
triggerTestValue(&sitAwTuAcc, accz);
}
/**
* Check if a Tumbled situation has been detected.
*
* @return True if the situation has been detected, otherwise false.
*/
bool sitAwTuDetected(void)
{
return sitAwTuAcc.released;
}
#endif
/**
* Initialize the situation awareness subsystem.
*/
void sitAwInit(void)
{
#ifdef SITAW_FF_ENABLED
sitAwFFInit();
#endif
#ifdef SITAW_AR_ENABLED
sitAwARInit();
#endif
#ifdef SITAW_TU_ENABLED
sitAwTuInit();
#endif
}