-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBehavior.cpp
421 lines (351 loc) · 9.47 KB
/
Behavior.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// Filename: "Behavior.cpp"
//
// Programmer: Ross Mead
// Last modified: 30Nov2009
//
// Description: This class implements a robot behavior.
//
// preprocessor directives
#include "Behavior.h"
// <constructors>
//
// Behavior(s, t, r, speed)
// Last modified: 04Sep2006
//
// Default constructor that initializes
// this behavior to the parameterized values.
//
// Returns: <none>
// Parameters:
// s in the status of the behavior (default INACTIVE)
// t in the translational velocity of the behavior (default 0)
// r in the rotational velocity of the behavior (default 0)
// speed in the maximum speed of the behavior (default 1)
//
Behavior::Behavior(const Status s,
const GLfloat t,
const GLfloat r,
const GLfloat speed)
{
setMaxSpeed(speed);
setVelocity(t, r);
setStatus(s);
} // Behavior(const Status, const GLfloat, const GLfloat, const GLfloat)
//
// Behavior(beh)
// Last modified: 27Aug2006
//
// Copy constructor that copies the contents of
// the parameterized behavior into this behavior.
//
// Returns: <none>
// Parameters:
// beh in/out the behavior being copied
//
Behavior::Behavior(const Behavior &beh)
{
*this = beh;
} // Behavior(const Behavior &)
// <public mutator functions>
//
// bool setStatus(s)
// Last modified: 26Aug2006
//
// Attempts to set the status to the parameterized status,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// s in the status to be set to
//
bool Behavior::setStatus(const Status s)
{
switch (s)
{
case INACTIVE: case DONE: case ACTIVE: status = s; break;
default: return false;
}
return true;
} // setStatus(const Status)
//
// bool setTransVel(t)
// Last modified: 26Aug2006
//
// Attempts to set the translational velocity
// to the parameterized translational velocity,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// t in the translational velocity to be set to
//
bool Behavior::setTransVel(const GLfloat t)
{
transVel = t;
scaleVel();
return true;
} // setTransVel(const GLfloat)
//
// bool setRotVel(r)
// Last modified: 26Aug2006
//
// Attempts to set the rotational velocity
// to the parameterized rotational velocity,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// r in the rotational velocity to be set to
//
bool Behavior::setRotVel(const GLfloat r)
{
rotVel = r;
scaleVel();
return true;
} // setRotVel(const GLfloat)
//
// bool setDiffVel(right, left)
// Last modified: 07Nov2009
//
// Attempts to set the translational and rotational velocities
// based on left and right (differential) velocities,
// returning true if successful, false otherwise
//
// Returns: true if successful, false otherwise
// Parameters:
// right in the right velocity to be set to
// left in the left velocity to be set to
//
bool Behavior::setDiffVel(GLfloat right, GLfloat left)
{
return setVelocity(0.5f * (right + left), 1.0f * (right - left));
} // setDiffVel(GLfloat, GLfloat)
//
// bool setVel(t, r)
// Last modified: 26Aug2006
//
// Attempts to set the translational and rotational velocities
// to the parameterized translational and rotational velocities,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// t in the translational velocity to be set to
// r in the rotational velocity to be set to
//
bool Behavior::setVelocity(const GLfloat t, const GLfloat r)
{
transVel = t;
rotVel = r;
scaleVel();
return true;
} // setVelocity(const GLfloat, const GLfloat)
//
// bool setMaxSpeed(speed)
// Last modified: 26Aug2006
//
// Attempts to set the max speed to the parameterized max speed,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// speed in the max speed to be set to
//
bool Behavior::setMaxSpeed(const GLfloat speed)
{
maxSpeed = speed;
scaleVel();
return true;
} // setMaxSpeed(const GLfloat)
// <public accessor functions>
//
// Status getStatus() const
// Last modified: 26Aug2006
//
// Returns the status of this behavior.
//
// Returns: the status of this behavior
// Parameters: <none>
//
Status Behavior::getStatus() const
{
return status;
} // getStatus() const
//
// bool isActive() const
// Last modified: 26Aug2006
//
// Returns true if the status of this behavior is active, false otherwise.
//
// Returns: true if the status is active, false otherwise
// Parameters: <none>
//
bool Behavior::isActive() const
{
return status == ACTIVE;
} // isActive() const
//
// bool isDone() const
// Last modified: 26Aug2006
//
// Returns true if the status of this behavior is done, false otherwise.
//
// Returns: true if the status is done, false otherwise
// Parameters: <none>
//
bool Behavior::isDone() const
{
return status == DONE;
} // isDone() const
//
// bool isInactive() const
// Last modified: 26Aug2006
//
// Returns true if the status of this behavior is inactive, false otherwise.
//
// Returns: true if the status is inactive, false otherwise
// Parameters: <none>
//
bool Behavior::isInactive() const
{
return status == INACTIVE;
} // isActive() const
//
// GLfloat getTransVel() const
// Last modified: 26Aug2006
//
// Returns the translational velocity of this behavior.
//
// Returns: the translational velocity of this behavior
// Parameters: <none>
//
GLfloat Behavior::getTransVel() const
{
return transVel;
} // getTransVel() const
//
// GLfloat getRotVel() const
// Last modified: 26Aug2006
//
// Returns the rotational velocity of this behavior.
//
// Returns: the rotational velocity of this behavior
// Parameters: <none>
//
GLfloat Behavior::getRotVel() const
{
return rotVel;
} // getRotVel() const
//
// GLfloat getVelocity() const
// Last modified: 26Aug2006
//
// Returns the velocity of this behavior.
//
// Returns: the velocity of this behavior
// Parameters: <none>
//
GLfloat Behavior::getVelocity() const
{
return transVel + rotVel;
} // getVelocity() const
//
// GLfloat getSpeed() const
// Last modified: 26Aug2006
//
// Returns the speed of this behavior.
//
// Returns: the speed of this behavior
// Parameters: <none>
//
GLfloat Behavior::getSpeed() const
{
return abs(getVelocity());
} // getSpeed() const
//
// GLfloat getMaxSpeed() const
// Last modified: 26Aug2006
//
// Returns the max speed of this behavior.
//
// Returns: the max speed of this behavior
// Parameters: <none>
//
GLfloat Behavior::getMaxSpeed() const
{
return maxSpeed;
} // getMaxSpeed() const
// <overloaded operators>
//
// Behavior& =(beh)
// Last modified: 26Aug2006
//
// Copies the contents of the parameterized behavior into this behavior.
//
// Returns: this behavior
// Parameters:
// beh in/out the behavior being copied
//
Behavior& Behavior::operator =(const Behavior &beh)
{
setMaxSpeed(beh.maxSpeed);
setVelocity(beh.transVel, beh.rotVel);
setStatus(beh.status);
return *this;
} // =(const Behavior &)
// <friend functions>
//
// Behavior subsumeBehaviors(behLow, behHigh)
// Last modified: 14May2006
//
// Returns the parameterized behavior with the highest activation level.
//
// Returns: the parameterized behavior with the highest activation level
// Parameters:
// low in the lower priority behavior to compare to
// high in the higher priority behavior to compare to
//
Behavior subsumeBehaviors(const Behavior behLow, const Behavior behHigh)
{
return (behHigh.status > behLow.status) ? behHigh : behLow;
} // subsumeBehaviors(const Behavior, const Behavior)
//
// Behavior sumBehaviors(beh1, beh2)
// Last modified: 16May2006
//
// Returns the sum of the parameterized behaviors.
//
// Returns: the sum of the parameterized behaviors
// Parameters:
// beh1 in the first behavior to sum
// beh2 in the second behavior to sum
//
Behavior sumBehaviors(const Behavior beh1, const Behavior beh2)
{
return Behavior (max(beh1.status, beh2.status),
beh1.transVel + beh2.transVel,
beh1.rotVel + beh2.rotVel,
min(beh1.maxSpeed, beh2.maxSpeed));
} // subsumeBehaviors(const Behavior, const Behavior)
// <protected utility functions>
//
// GLfloat scaleVel()
// Last modified: 26Aug2006
//
// Scales the velocity of this behavior based on the max speed.
//
// Returns: the scaled angle (in degrees)
// Parameters: <none>
//
GLfloat Behavior::scaleVel()
{
if (getSpeed() == 0.0f) return 0.0f;
GLfloat scale = maxSpeed / getSpeed();
if (scale < 1.0f)
{
transVel *= scale;
rotVel *= scale;
}
return scale;
} // scaleVel()