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


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