-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbehavior.c
More file actions
343 lines (244 loc) · 8.8 KB
/
behavior.c
File metadata and controls
343 lines (244 loc) · 8.8 KB
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
/**
* Copyright (c) 2011-2012, Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the University of California, Berkeley nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* I-Bird State Machine
*
* by Humphrey Hu
*
* v.beta
*
* Revisions:
* Humphrey Hu 2011-09-04 Initial implementation
* Humphrey Hu 2012-03-21 Update to new code base
*
* Notes:
*
* TODO:
*/
// ==== REFERENCES ==========================================
#include "camera.h"
#include "cv.h"
#include "attitude.h"
#include "regulator.h"
#include "behavior.h"
#include "sys_clock.h"
// ==== CONSTANTS ===========================================
#define TRACK_MIN_PIXELS (4) // Min number of pixels visible to consider a centroid valid
#define REACQUIRE_MOTION_PERIOD (50)
#define CIRCLE_MOTION_PERIOD (150)
#define SEARCH_MOTION_PERIOD (200)
#define TRACK_PITCH_OFFSET (80.0)
#define TRACK_YAW_OFFSET (0.0)
#define REACQUIRE_PITCH_OFFSET (80.0)
#define REACQUIRE_YAW_OFFSET (0.0)
#define REACQUIRE_YAW_OFFSET_F (0.524)
#define REACQUIRE_TURN_THRESHOLD (0.524) // 30 degrees in radians
#define REACQUIRE_MAX_TIME (20)
#define CIRCLE_PITCH_OFFSET (78.0)
#define CIRCLE_YAW_OFFSET (0.0)
#define SEARCH_PITCH_OFFSET (78.0)
#define SEARCH_YAW_OFFSET (45.0)
#define SEARCH_PITCH_INCREMENT (0.0)
#define SEARCH_YAW_INCREMENT (0.0)
#define SEARCH_YAW_MINIMUM (0.0)
#define SEARCH_YAW_MAXIMUM (0.0)
#define SEARCH_PERIOD (25)
#define DEFAULT_YAW_OFFSET (0.0)
#define DEFAULT_PITCH_OFFSET (80.0)
typedef enum {
BEHAV_CIRCLE,
BEHAV_TRACK,
BEHAV_REACQUIRE,
BEHAV_SEARCH,
} BehaviorState;
// ==== STATIC VARIABLES ====================================
static unsigned char is_running = 0;
static unsigned char is_ready = 0;
static BehaviorState behav_state = BEHAV_CIRCLE;
// Reacquiring behav_state variables
static unsigned int reacquiringStartTime;
// Searching behav_state variables
static unsigned int searchingStartTime;
// ==== FUNCTION STUBS ======================================
static void runTrack(void);
static void runReacquire(void);
static void runSearch(void);
static void runCircl(void);
// Valid transition handlers
static void transitionTrackReacquire(void);
static void transitionReacquireTrack(void);
static void transitionReacquireSearch(void);
static void transitionSearchTrack(void);
static void transitionCircle(void);
static void transitionSearch(void);
static void resetOffsets(void);
// ==== FUNCTION BODIES =====================================
void behavSetup(void) {
is_ready = 0;
is_running = 0;
behav_state = BEHAV_CIRCLE;
behavReset();
}
void behavReset(void) {
transitionCircle();
is_running = 0;
}
void behavSetState(unsigned char flag) {
if(flag == BEHAVIOR_SEARCH) {
transitionSearch();
} else if(flag == BEHAVIOR_CIRCL) {
transitionCircl();
}
}
void behavSetRunning(unsigned char flag) {
is_running = flag;
}
unsigned char behavIsRunning(void) {
return is_running;
}
// Note: We don't expect to see stuttering since the camera capture
// is synchronous
void behavRunBehavior(void) {
if(!is_ready) { return; }
if(!is_running) { return; }
switch(behav_state) {
case BEHAV_TRACK:
runTrack();
break;
case BEHAV_REACQUIRE:
runReacquire();
break;
case BEHAV_SEARCH:
runSearch();
break;
case BEHAV_CIRCLE:
runCircle();
break;
}
}
void runTrack(void) {
CamFrame frame;
FrameInfo info;
int cX, cY;
frame = camGetFrame();
if(frame == NULL) { return; }
info = cvProcessFrame(frame);
camReturnFrame(frame);
// If enough visible pixels
if(info->mass > TRACK_MIN_PIXELS) {
cX = info->centroid[0] - info->offset[0];
cY = info->centroid[1] - info->offset[1];
rgltrSetYawRef(cX + attGetYaw());
rgltrSetPitchRef(cY + attGetPitch());
return;
}
// Else transition to reacquiring behav_state
transitionTrackReacquire();
}
void runReacquire(void) {
FrameInfo frame;
unsigned long time;
frame = cvGetFrameInfo();
// If target found, transition to tracking behav_state
if(frame->mass > TRACK_MIN_PIXELS) {
transitionReacquireTrack();
runTrack();
return;
}
// Else attempt to reacquire
// Check time elapsed since reacquisition start
time = sclockTimeMillis();
// If max time exceeded, transition to search behav_state
if(time - reacquire_start_time > REACQUIRE_MAX_TIME) {
transitionReacquireSearch();
return;
}
}
void runSearch(void) {
FrameInfo frame;
unsigned int time;
frame = cvGetFrameInfo();
// If target found, transition to tracking behav_state
if(frame->mass > TRACK_MIN_PIXELS) {
transitionSearchTrack();
return;
}
// Check time passed
time = cntrRead(clock);
rgltrSetRemoteControlValues(SEARCH_PITCH_OFFSET, SEARCH_YAW_OFFSET);
}
void runCircle(void) {
rgltrSetRemoteControlValues(currentPitchOffset, currentYawOffset);
}
void transitionTrackReacquire(void) {
resetOffsets(); // Reset to defaults
//rgltrSetRemoteControl(0); // Disable RC
// Mark start time of reacquisition process
reacquiringStartTime = cntrRead(clock);
if(lastRelativeHeading > REACQUIRE_TURN_THRESHOLD) {
lastRelativeHeading += REACQUIRE_YAW_OFFSET_F;
} else if(lastRelativeHeading < -REACQUIRE_TURN_THRESHOLD) {
lastRelativeHeading += -REACQUIRE_YAW_OFFSET_F;
}
//rgltrSetYawRef(lastRelativeHeading + attGetYaw());
behav_state = BEHAV_REACQUIRE;
}
void transitionReacquireTrack(void) {
resetOffsets(); // Reset to defaults
//rgltrSetRemoteControl(0); // Disable RC
behav_state = BEHAV_TRACK;
}
void transitionReacquireSearch(void) {
resetOffsets(); // Reset to defaults
//rgltrSetRemoteControl(1); // Enable RC mode
rgltrSetRemoteControlValues(SEARCH_PITCH_OFFSET, SEARCH_YAW_OFFSET);
searchingStartTime = cntrRead(clock); // Read start time
behav_state = BEHAV_SEARCH;
}
void transitionSearchTrack(void) {
resetOffsets(); // Reset to defaults
//rgltrSetRemoteControl(0); // Disable RC
behav_state = BEHAV_TRACK;
}
// TODO: Merge w/ transitionReacquisitionSearching?
void transitionSearch(void) {
resetOffsets(); // Reset to defaults
//rgltrSetRemoteControl(1); // Enable RC mode
rgltrSetRemoteControlValues(SEARCHING_PITCH_OFFSET, SEARCHING_YAW_OFFSET);
behav_state = BEHAV_SEARCH;
}
void transitionCircle(void) {
resetOffsets(); // Reset to defaults
//rgltrSetRemoteControl(0); // Disable RC mode
//rgltrSetYawRef(attGetYaw()); // Set to fly straight
behav_state = BEHAV_CIRCLE;
}
void resetOffsets(void) {
rgltrSetRemoteControlValues(DEFAULT_PITCH_OFFSET, DEFAULT_YAW_OFFSET);
}