stest.cc
Go to the documentation of this file.
1 // File: stest.c
3 // Desc: Stage library test program
4 // Created: 2004.9.15
5 // Author: Richard Vaughan <vaughan@sfu.ca>
6 // CVS: $Id: stest.cc,v 1.1 2008-01-15 01:29:10 rtv Exp $
7 // License: GPL
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include <sstream>
15 #include <iostream>
16 #include <cmath>
17 
18 #include "stage.hh"
19 
20 class Robot
21 {
22 public:
25 };
26 
27 class Logic
28 {
29 public:
30  static int Callback(Stg::World* world, void* userarg)
31  {
32  Logic* lg = reinterpret_cast<Logic*>(userarg);
33 
34  lg->Tick(world);
35 
36  // never remove this call-back
37  return 0;
38  }
39 
40  Logic(unsigned int popsize)
41  :
42  population_size(popsize),
43  robots(new Robot[population_size])
44  {
45 
46  }
47 
48  void connect(Stg::World* world)
49  {
50  // connect the first population_size robots to this controller
51  for(unsigned int idx = 0; idx < population_size; idx++)
52  {
53  // the robots' models are named r0 .. r1999
54  std::stringstream name;
55  name << "r" << idx;
56 
57  // get the robot's model and subscribe to it
58  Stg::ModelPosition* posmod = reinterpret_cast<Stg::ModelPosition*>(
59  world->GetModel(name.str())
60  );
61  assert(posmod != 0);
62 
63  robots[idx].position = posmod;
64  robots[idx].position->Subscribe();
65 
66  // get the robot's ranger model and subscribe to it
67  Stg::ModelRanger* rngmod = reinterpret_cast<Stg::ModelRanger*>(
68  robots[idx].position->GetChild( "ranger:0" )
69  );
70  assert(rngmod != 0);
71 
72  robots[idx].ranger = rngmod;
73  robots[idx].ranger->Subscribe();
74  }
75 
76  // register with the world
77  world->AddUpdateCallback(Logic::Callback, reinterpret_cast<void*>(this));
78  }
79 
81  {
82  delete[] robots;
83  }
84 
85 
87  {
88  // the controllers parameters
89  const double vspeed = 0.4; // meters per second
90  const double wgain = 1.0; // turn speed gain
91  const double safe_dist = 0.1; // meters
92  const double safe_angle = 0.3; // radians
93 
94  // each robot has a group of ir sensors
95  // each sensor takes one sample
96  // the forward sensor is the middle sensor
97  for(unsigned int idx = 0; idx < population_size; idx++)
98  {
99  Stg::ModelRanger* rgr = robots[idx].ranger;
100 
101  // compute the vector sum of the sonar ranges
102  double dx=0, dy=0;
103 
104  // the range model has multiple sensors
105  typedef std::vector<Stg::ModelRanger::Sensor>::const_iterator sensor_iterator;
106  const std::vector<Stg::ModelRanger::Sensor> sensors = rgr->GetSensors();
107 
108  for(sensor_iterator sensor = sensors.begin(); sensor != sensors.end(); sensor++)
109  {
110  // each sensor takes a single sample (as specified in the .world)
111  const double srange = (*sensor).ranges[0];
112  const double angle = (*sensor).pose.a;
113 
114  dx += srange * std::cos(angle);
115  dy += srange * std::sin(angle);
116  }
117 
118  if(dx == 0)
119  continue;
120 
121  if(dy == 0)
122  continue;
123 
124  // calculate the angle towards the farthest obstacle
125  const double resultant_angle = std::atan2( dy, dx );
126 
127  // check whether the front is clear
128  const unsigned int forward_idx = sensors.size() / 2u - 1u;
129 
130  const double forwardm_range = sensors[forward_idx - 1].ranges[0];
131  const double forward_range = sensors[forward_idx + 0].ranges[0];
132  const double forwardp_range = sensors[forward_idx + 1].ranges[0];
133 
134  bool front_clear = (
135  (forwardm_range > safe_dist / 5.0) &&
136  (forward_range > safe_dist) &&
137  (forwardp_range > safe_dist / 5.0) &&
138  (std::abs(resultant_angle) < safe_angle)
139  );
140 
141  // turn the sensor input into movement commands
142 
143  // move forwards if the front is clear
144  const double forward_speed = front_clear ? vspeed : 0.0;
145  // do not strafe
146  const double side_speed = 0.0;
147 
148  // turn towards the farthest obstacle
149  const double turn_speed = wgain * resultant_angle;
150 
151  // finally, relay the commands to the robot
152  robots[idx].position->SetSpeed( forward_speed, side_speed, turn_speed );
153  }
154  }
155 
156 protected:
157  unsigned int population_size;
159 };
160 
161 int main( int argc, char* argv[] )
162 {
163  // check and handle the argumets
164  if( argc < 3 )
165  {
166  puts( "Usage: stest <worldfile> <number of robots>" );
167  exit(0);
168  }
169 
170  const int popsize = atoi(argv[2] );
171 
172  // initialize libstage
173  Stg::Init( &argc, &argv );
174 
175  // create the world
176  //Stg::World world;
177  Stg::WorldGui world(800, 700, "Stage Benchmark Program");
178  world.Load( argv[1] );
179 
180  // create the logic and connect it to the world
181  Logic logic(popsize);
182  logic.connect(&world);
183 
184  // and then run the simulation
185  world.Run();
186 
187  return 0;
188 }
Stg::ModelPosition * position
Definition: stest.cc:23
unsigned int population_size
Definition: stest.cc:157
World class
Definition: stage.hh:814
Definition: fasr.cc:43
Logic(unsigned int popsize)
Definition: stest.cc:40
void Init(int *argc, char **argv[])
Definition: stage.cc:18
void AddUpdateCallback(world_callback_t cb, void *user)
Definition: world.cc:538
virtual void Load(const std::string &filename)
Definition: worldgui.cc:256
ModelPosition class
Definition: stage.hh:2927
static int argc
const std::vector< Sensor > & GetSensors() const
Definition: stage.hh:2804
Definition: stest.cc:27
void Subscribe()
Definition: model.cc:646
static char * argv
~Logic()
Definition: stest.cc:80
Stg::ModelRanger * ranger
Definition: stest.cc:24
ModelRanger class
Definition: stage.hh:2747
int main(int argc, char *argv[])
Definition: stest.cc:161
static void Run()
Definition: world.cc:207
void Tick(Stg::World *)
Definition: stest.cc:86
void connect(Stg::World *world)
Definition: stest.cc:48
Model * GetModel(const std::string &name) const
Definition: world.cc:707
Robot * robots
Definition: stest.cc:158
static int Callback(Stg::World *world, void *userarg)
Definition: stest.cc:30


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:10