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  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 pp(&robot, gIndex);
31  SonarProxy sp(&robot, gIndex);
32  Graphics2dProxy gp(&robot, gIndex);
33 
34  sp.RequestGeom(); // query the server for sonar positions
35 
36  while (1) {
37  // blocks until new data comes from Player
38  robot.Read();
39 
40  for (int i = 0; i < POP; i++) {
41  // compute the vector sum of the sonar ranges
42  double dx = 0, dy = 0;
43 
44  int num_ranges = sp.GetCount();
45  for (int s = 0; s < num_ranges; s++) {
46  player_pose3d_t spose = sp.GetPose(s);
47  double srange = sp.GetScan(s);
48 
49  dx += srange * cos(spose.pyaw);
50  dy += srange * sin(spose.pyaw);
51  }
52 
53  // compute the direction of the resultant vector
54  double resultant_angle = atan2(dy, dx);
55  // double resultant_magnitude = hypot( dy, dx );
56 
57  double forward_speed = 0.0;
58  double side_speed = 0.0;
59  double turn_speed = WGAIN * resultant_angle;
60 
61  // find the index of the forward-pointing sensor
62  int forward = num_ranges / 2 - 1;
63 
64  // if the front is clear, drive forwards
65  if ((sp.GetScan(forward - 1) > SAFE_DIST) && (sp.GetScan(forward) > SAFE_DIST)
66  && (sp.GetScan(forward + 1) > SAFE_DIST) && (fabs(resultant_angle) < SAFE_ANGLE)) {
67  forward_speed = VSPEED;
68  }
69 
70  // send a command to the robot's position device
71  pp.SetSpeed(forward_speed, side_speed, turn_speed);
72 
73  // draw the resultant vector on the robot to show what it
74  // is thinking
75  if (forward_speed > 0)
76  gp.Color(0, 255, 0, 0);
77  else
78  gp.Color(0, 255, 255, 0);
79 
80  player_point_2d_t pts[2];
81  pts[0].px = 0;
82  pts[0].py = 0;
83  pts[1].px = dx;
84  pts[1].py = dy;
85 
86  gp.Clear();
87  gp.DrawPolyline(pts, 2);
88  }
89  }
90  } catch (PlayerCc::PlayerError &e) {
91  std::cerr << e << std::endl;
92  return -1;
93  }
94 }
int parse_args(int argc, char **argv)
Definition: args.h:16
#define SAFE_DIST
Definition: single.cc:11
float s
Definition: glutgraphics.cc:51
#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 Feb 28 2022 23:48:56