wander.cc
Go to the documentation of this file.
1 #include "stage.hh"
2 using namespace Stg;
3 
4 static const double cruisespeed = 0.4;
5 static const double avoidspeed = 0.05;
6 static const double avoidturn = 0.5;
7 static const double minfrontdistance = 1.0; // 0.6
8 static const bool verbose = false;
9 static const double stopdist = 0.3;
10 static const int avoidduration = 10;
11 
12 typedef struct {
15  int avoidcount, randcount;
16 } robot_t;
17 
18 int LaserUpdate(Model *mod, robot_t *robot);
19 int PositionUpdate(Model *mod, robot_t *robot);
20 
21 // Stage calls this when the model starts up
22 extern "C" int Init(Model *mod, CtrlArgs *)
23 {
24  // local arguments
25  /* printf( "\nWander controller initialised with:\n"
26  "\tworldfile string \"%s\"\n"
27  "\tcmdline string \"%s\"",
28  args->worldfile.c_str(),
29  args->cmdline.c_str() );
30  */
31 
32  robot_t *robot = new robot_t();
33 
34  robot->avoidcount = 0;
35  robot->randcount = 0;
36 
37  robot->pos = dynamic_cast<ModelPosition *>(mod);
38  if (!robot->pos) {
39  PRINT_ERR("No position model given in wander controller.");
40  exit(1);
41  }
42 
43  if (verbose)
45 
46  robot->pos->Subscribe(); // starts the position updates
47 
48  // find a range finder
49 
50  ModelRanger *laser = NULL;
51 
52  printf( "\nWander ctrl for robot %s:\n", robot->pos->Token() );
53  for( int i=0; i<16; i++ )
54  {
55  char name[32];
56  snprintf( name, 32, "ranger:%d", i ); // generate sequence of model names
57 
58  printf( " looking for a suitable ranger at \"%s:%s\" ... ", robot->pos->Token(), name );
59  laser = dynamic_cast<ModelRanger *>(robot->pos->GetChild( name ));
60 
61  if( laser && laser->GetSensors()[0].sample_count > 8 )
62  {
63  puts( "yes." );
64  break;
65  }
66 
67  puts( "no." );
68  }
69 
70  if( !laser ) {
71  PRINT_ERR(" Failed to find a ranger with more than 8 samples. Exit.");
72  exit(2);
73  }
74 
75  robot->laser = laser;
77  robot->laser->Subscribe(); // starts the ranger updates
78 
79  return 0; // ok
80 }
81 
82 // inspect the ranger data and decide what to do
83 int LaserUpdate(Model *, robot_t *robot)
84 {
85  // get the data
86  const std::vector<meters_t> &scan = robot->laser->GetSensors()[0].ranges;
87  uint32_t sample_count = scan.size();
88  if (sample_count < 1)
89  return 0;
90 
91  bool obstruction = false;
92  bool stop = false;
93 
94  // find the closest distance to the left and right and check if
95  // there's anything in front
96  double minleft = 1e6;
97  double minright = 1e6;
98 
99  for (uint32_t i = 0; i < sample_count; i++) {
100  if (verbose)
101  printf("%.3f ", scan[i]);
102 
103  if ((i > (sample_count / 3)) && (i < (sample_count - (sample_count / 3)))
104  && scan[i] < minfrontdistance) {
105  if (verbose)
106  puts(" obstruction!");
107  obstruction = true;
108  }
109 
110  if (scan[i] < stopdist) {
111  if (verbose)
112  puts(" stopping!");
113  stop = true;
114  }
115 
116  if (i > sample_count / 2)
117  minleft = std::min(minleft, scan[i]);
118  else
119  minright = std::min(minright, scan[i]);
120  }
121 
122  if (verbose) {
123  puts("");
124  printf("minleft %.3f \n", minleft);
125  printf("minright %.3f\n ", minright);
126  }
127 
128  if (obstruction || stop || (robot->avoidcount > 0)) {
129  if (verbose)
130  printf("Avoid %d\n", robot->avoidcount);
131 
132  robot->pos->SetXSpeed(stop ? 0.0 : avoidspeed);
133 
134  /* once we start avoiding, select a turn direction and stick
135  with it for a few iterations */
136  if (robot->avoidcount < 1) {
137  if (verbose)
138  puts("Avoid START");
139  robot->avoidcount = random() % avoidduration + avoidduration;
140 
141  if (minleft < minright) {
142  robot->pos->SetTurnSpeed(-avoidturn);
143  if (verbose)
144  printf("turning right %.2f\n", -avoidturn);
145  } else {
146  robot->pos->SetTurnSpeed(+avoidturn);
147  if (verbose)
148  printf("turning left %2f\n", +avoidturn);
149  }
150  }
151 
152  robot->avoidcount--;
153  } else {
154  if (verbose)
155  puts("Cruise");
156 
157  robot->avoidcount = 0;
158  robot->pos->SetXSpeed(cruisespeed);
159  robot->pos->SetTurnSpeed(0);
160  }
161 
162  // if( robot->pos->Stalled() )
163  // {
164  // robot->pos->SetSpeed( 0,0,0 );
165  // robot->pos->SetTurnSpeed( 0 );
166  // }
167 
168  return 0; // run again
169 }
170 
172 {
173  Pose pose = robot->pos->GetPose();
174 
175  printf("Pose: [%.2f %.2f %.2f %.2f]\n", pose.x, pose.y, pose.z, pose.a);
176 
177  return 0; // run again
178 }
void SetTurnSpeed(double a)
Model class
Definition: stage.hh:1651
int randcount
Definition: wander.cc:15
The Stage library uses its own namespace.
Definition: canvas.hh:8
static const int avoidduration
Definition: wander.cc:10
void Init(int *argc, char **argv[])
Definition: stage.cc:17
Pose GetPose() const
Definition: stage.hh:2250
int(* model_callback_t)(Model *mod, void *user)
Definition: stage.hh:540
int avoidcount
Definition: wander.cc:15
static const bool verbose
Definition: wander.cc:8
const std::vector< Sensor > & GetSensors() const
Definition: stage.hh:2661
meters_t z
location in 3 axes
Definition: stage.hh:259
meters_t y
Definition: stage.hh:259
ModelPosition class
Definition: stage.hh:2777
static const double minfrontdistance
Definition: wander.cc:7
int LaserUpdate(Model *mod, robot_t *robot)
Definition: wander.cc:83
void SetXSpeed(double x)
void AddCallback(callback_type_t type, model_callback_t cb, void *user)
int PositionUpdate(Model *mod, robot_t *robot)
Definition: wander.cc:171
#define PRINT_ERR(m)
Definition: stage.hh:590
static const double avoidspeed
Definition: wander.cc:5
void Subscribe()
Definition: model.cc:516
static const double cruisespeed
Definition: wander.cc:4
ModelRanger class
Definition: stage.hh:2610
static const double stopdist
Definition: wander.cc:9
Model * GetChild(const std::string &name) const
Definition: model.cc:862
radians_t a
rotation about the z axis.
Definition: stage.hh:260
ModelPosition * pos
Definition: wander.cc:13
static const double avoidturn
Definition: wander.cc:6
meters_t x
Definition: stage.hh:259
ModelRanger * laser
Definition: wander.cc:14
const char * Token() const
Definition: stage.hh:689


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