-
Notifications
You must be signed in to change notification settings - Fork 5
/
controller.cc
110 lines (94 loc) · 2.78 KB
/
controller.cc
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
/*****
controller.cc
version 1
Copyright Richard Vaughan, 2009.09.09
****/
#include <math.h>
#include "controller.h"
using namespace Antix;
Forager::Forager( Antix::Home* h )
: Robot( h, Pose() ),
lastx(home->x), // initial search location is close to my home
lasty(home->y)
{
double delta( 4.0 );
DistanceNormalize( pose.x = delta * drand48() -delta/2.0 + home->x );
DistanceNormalize( pose.y = delta * drand48() -delta/2.0 + home->y );
// static bool startup( true );
// if( startup )
// {
// RVOsim->setAgentDefaults(0.1,10,10,2,1.3*Robot::radius, 0.005);
// RVOsim->setTimeStep(1);
// startup = false;
// }
// this->RVOid = Robot::RVOsim->addAgent(RVO::Vector2(pose.x,pose.y));
}
void Forager::Controller()
{
double heading_error(0.0);
// distance and angle to home
double dx( WrapDistance( home->x - pose.x ));
double dy( WrapDistance( home->y - pose.y ));
double da( fast_atan2( dy, dx ));
double dist( hypot( dx, dy ));
if( Holding() )
{ // drive home
// turn towards home
heading_error = AngleNormalize( da - pose.a );// < 0 ? 0.05 : -0.05;
// if we're some random distance inside the home radius
if( dist < drand48() * home->r )
Drop(); // release the puck (implies we won't be holding
// next time round)
}
else // not holding
{
// if I see any pucks and I'm away from home
if( see_pucks.size() > 0 && dist > home->r )
{
// find the angle to the closest puck that is not being carried
double closest_range(1e9); //BIG
FOR_EACH( it, see_pucks )
{
if( it->range < closest_range && !it->held )
{
heading_error = it->bearing;
closest_range = it->range; // remember the closest range so far
}
}
// and attempt to pick something up
if( Pickup() )
{
// got one! remember where it was
lastx = pose.x;
lasty = pose.y;
}
}
else
{
double lx( WrapDistance( lastx - pose.x ));
double ly( WrapDistance( lasty - pose.y ));
// go towards the last place I picked up a puck
heading_error = AngleNormalize( fast_atan2(ly, lx) - pose.a );
// if I've arrived at the last place and not yet found a
// puck, choose another place
if( hypot( lx,ly ) < 0.05 )
{
lastx += drand48() * 1.0 - 0.5;
lasty += drand48() * 1.0 - 0.5;
DistanceNormalize( lastx );
DistanceNormalize( lasty );
}
}
}
// if I'm pointing in about the right direction
if( fabs( heading_error ) < 0.1 )
{
speed.v = 0.005; // drive fast
speed.w = 0.0; // don't turn
}
else
{
speed.v = 0.001; // drive slowly
speed.w = 0.2 * (heading_error); // turn to reduce the error
}
}