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


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