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