-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionExploreVelocity.cpp
43 lines (39 loc) · 1.43 KB
/
ActionExploreVelocity.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
#include "ActionExploreVelocity.h"
ActionExploreVelocity::ActionExploreVelocity(double maxSpeed, double stopDistance):
ArAction ("exploreVelocity"),
mySonar(NULL),
myMaxSpeed(maxSpeed),
myStopDistance(stopDistance)
{
setNextArgument(ArArg("maximumSpeedExploreVelocity", &myMaxSpeed, "maximum speed while exploring"));
setNextArgument(ArArg("stopDistanceExploreVelocity", &myStopDistance, "minimum distance to stop"));
}
void ActionExploreVelocity::setRobot(ArRobot *robot) {
robot->lock();
ArAction::setRobot(robot);
robot->unlock();
mySonar = robot->findRangeDevice("sonar");
if (mySonar == NULL) {
ArLog::log(ArLog::Terse, "ActionExploreVelocity: setRobot: Warning: found no sonar, deactivating.");
deactivate();
}
}
ArActionDesired *ActionExploreVelocity::fire(ArActionDesired currentDesired) {
myDesired.reset();
if (mySonar == NULL) {
deactivate();
return NULL;
}
// get the range of the sonar
mySonar->lockDevice();
range = mySonar->currentReadingPolar(-21, 21) - myRobot->getRobotRadius();
mySonar->unlockDevice();
// if the range is greater than the stop distance, find some speed to go
if (range > myStopDistance) {
myDesired.setVel(myMaxSpeed);
} else {
myDesired.setVel(myMaxSpeed*(range/myStopDistance));
}
// return a pointer to the actionDesired to the resolver to make our request
return &myDesired;
}