single.cc
Go to the documentation of this file.
1 #include <libplayerc++/playerc++.h>
2 #include <iostream>
3 #include <assert.h>
4 
5 // For argument parsing
6 #include "args.h"
7 
8 #define POP 100 // population size
9 #define VSPEED 0.4 // meters per second
10 #define WGAIN 1.0 // turn speed gain
11 #define SAFE_DIST 0.3 // meters
12 #define SAFE_ANGLE 0.4 // radians
13 
14 int main(int argc, char **argv)
15 {
16  // Parse command line options
17  parse_args(argc,argv);
18 
19  // We throw exceptions on creation if we fail
20  try
21  {
22  using namespace PlayerCc;
23  using namespace std;
24 
25  // Create a player client object, using the variables assigned by the
26  // call to parse_args()
27  PlayerClient robot (gHostname, gPort);
28  robot.SetDataMode( PLAYER_DATAMODE_PULL );
29  robot.SetReplaceRule( true );
30 
31  Position2dProxy pp( &robot, gIndex );
32  SonarProxy sp( &robot, gIndex );
33  Graphics2dProxy gp( &robot, gIndex );
34 
35  sp.RequestGeom(); // query the server for sonar positions
36 
37  while(1)
38  {
39  // blocks until new data comes from Player
40  robot.Read();
41 
42  for( int i=0; i<POP; i++ )
43  {
44  // compute the vector sum of the sonar ranges
45  double dx=0, dy=0;
46 
47  int num_ranges = sp.GetCount();
48  for( int s=0; s<num_ranges; s++ )
49  {
50  player_pose3d_t spose = sp.GetPose(s);
51  double srange = sp.GetScan(s);
52 
53  dx += srange * cos( spose.pyaw );
54  dy += srange * sin( spose.pyaw );
55  }
56 
57  // compute the direction of the resultant vector
58  double resultant_angle = atan2( dy, dx );
59  //double resultant_magnitude = hypot( dy, dx );
60 
61  double forward_speed = 0.0;
62  double side_speed = 0.0;
63  double turn_speed = WGAIN * resultant_angle;
64 
65  // find the index of the forward-pointing sensor
66  int forward = num_ranges/2 -1 ;
67 
68  // if the front is clear, drive forwards
69  if( (sp.GetScan(forward-1) > SAFE_DIST) &&
70  (sp.GetScan(forward) > SAFE_DIST) &&
71  (sp.GetScan(forward+1) > SAFE_DIST) &&
72  (fabs( resultant_angle ) < SAFE_ANGLE) )
73  {
74  forward_speed = VSPEED;
75  }
76 
77  // send a command to the robot's position device
78  pp.SetSpeed( forward_speed, side_speed, turn_speed );
79 
80  // draw the resultant vector on the robot to show what it
81  // is thinking
82  if( forward_speed > 0 )
83  gp.Color( 0,255,0,0 );
84  else
85  gp.Color( 0,255,255,0 );
86 
87  player_point_2d_t pts[2];
88  pts[0].px = 0;
89  pts[0].py = 0;
90  pts[1].px = dx;
91  pts[1].py = dy;
92 
93  gp.Clear();
94  gp.DrawPolyline( pts, 2 );
95 
96  }
97  }
98  }
99  catch (PlayerCc::PlayerError e)
100  {
101  std::cerr << e << std::endl;
102  return -1;
103  }
104 }
int parse_args(int argc, char **argv)
Definition: args.h:16
#define SAFE_DIST
Definition: single.cc:11
float s
Definition: glutgraphics.cc:58
#define POP
Definition: single.cc:8
#define SAFE_ANGLE
Definition: single.cc:12
uint gPort(PlayerCc::PLAYER_PORTNUM)
static int argc
static char * argv
std::string gHostname(PlayerCc::PLAYER_HOSTNAME)
#define VSPEED
Definition: single.cc:9
uint gIndex(0)
#define WGAIN
Definition: single.cc:10
int main(int argc, char **argv)
Definition: single.cc:14


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09