-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontroller.cc
71 lines (57 loc) · 1.59 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
/*****
controller.cc
version 3
Copyright Richard Vaughan, 2013.1.10
****/
#include "universe.h"
static bool invert = false;
// Examine the robot's pixels vector and set the speed sensibly.
void Controller( Uni::Robot& r, void* dummy_data )
{
r.speed[0] = 0.005; // constant forward speed
r.speed[1] = 0.0; // no turning. we may change this below
// steer away from the closest roboot
int closest = -1;
double dist = r.range; // max sensor range
const size_t pixel_count = r.pixels.size();
for( unsigned int p=0; p<pixel_count; p++ )
if( r.pixels[p].range < dist )
{
closest = (int)p;
dist = r.pixels[p].range;
}
if( closest < 0 ) // nothing nearby: cruise
return;
if( closest < (int)pixel_count / 2 )
r.speed[1] = 0.04; // rotate right
else
r.speed[1] = -0.04; // rotate left
if( invert )
r.speed[1] *= -1.0; // invert turn direction
}
int main( int argc, char* argv[] )
{
// configure global robot settings
Uni::Init( argc, argv );
// parse remaining cmdline arguments to configure swarmer
int c=0;
while( ( c = getopt( argc, argv, "i")) != -1 )
switch( c )
{
case 'i': invert = true;
puts( "[Ctrl] invert" );
break;
}
// configure the robots the way I want 'em
FOR_EACH( r, Uni::population )
{
Uni::RandomPose( r->pose );
// install our callback function
r->callback = Controller;
r->callback_data = NULL;
}
// and start the simulation running
Uni::Run();
// we'll probably never get here, but this keeps the compiler happy.
return 0;
}