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
13 {
16  int avoidcount, randcount;
17 } robot_t;
18 
19 int LaserUpdate( Model* mod, robot_t* robot );
20 int PositionUpdate( Model* mod, robot_t* robot );
21 
22 // Stage calls this when the model starts up
23 extern "C" int Init( Model* mod, CtrlArgs* args )
24 {
25  // local arguments
26  /* printf( "\nWander controller initialised with:\n"
27  "\tworldfile string \"%s\"\n"
28  "\tcmdline string \"%s\"",
29  args->worldfile.c_str(),
30  args->cmdline.c_str() );
31  */
32 
33  robot_t* robot = new robot_t;
34 
35  robot->avoidcount = 0;
36  robot->randcount = 0;
37 
38  robot->pos = (ModelPosition*)mod;
39 
40  if( verbose )
42 
43  robot->pos->Subscribe(); // starts the position updates
44 
45  robot->laser = (ModelRanger*)mod->GetChild( "ranger:1" );
47  robot->laser->Subscribe(); // starts the ranger updates
48 
49  return 0; //ok
50 }
51 
52 
53 // inspect the ranger data and decide what to do
54 int LaserUpdate( Model* mod, robot_t* robot )
55 {
56  // get the data
57  const std::vector<meters_t>& scan = robot->laser->GetSensors()[0].ranges;
58  uint32_t sample_count = scan.size();
59  if( sample_count < 1 )
60  return 0;
61 
62  bool obstruction = false;
63  bool stop = false;
64 
65  // find the closest distance to the left and right and check if
66  // there's anything in front
67  double minleft = 1e6;
68  double minright = 1e6;
69 
70  for (uint32_t i = 0; i < sample_count; i++)
71  {
72 
73  if( verbose ) printf( "%.3f ", scan[i] );
74 
75  if( (i > (sample_count/3))
76  && (i < (sample_count - (sample_count/3)))
77  && scan[i] < minfrontdistance)
78  {
79  if( verbose ) puts( " obstruction!" );
80  obstruction = true;
81  }
82 
83  if( scan[i] < stopdist )
84  {
85  if( verbose ) puts( " stopping!" );
86  stop = true;
87  }
88 
89  if( i > sample_count/2 )
90  minleft = std::min( minleft, scan[i] );
91  else
92  minright = std::min( minright, scan[i] );
93  }
94 
95  if( verbose )
96  {
97  puts( "" );
98  printf( "minleft %.3f \n", minleft );
99  printf( "minright %.3f\n ", minright );
100  }
101 
102  if( obstruction || stop || (robot->avoidcount>0) )
103  {
104  if( verbose ) printf( "Avoid %d\n", robot->avoidcount );
105 
106  robot->pos->SetXSpeed( stop ? 0.0 : avoidspeed );
107 
108  /* once we start avoiding, select a turn direction and stick
109  with it for a few iterations */
110  if( robot->avoidcount < 1 )
111  {
112  if( verbose ) puts( "Avoid START" );
113  robot->avoidcount = random() % avoidduration + avoidduration;
114 
115  if( minleft < minright )
116  {
117  robot->pos->SetTurnSpeed( -avoidturn );
118  if( verbose ) printf( "turning right %.2f\n", -avoidturn );
119  }
120  else
121  {
122  robot->pos->SetTurnSpeed( +avoidturn );
123  if( verbose ) printf( "turning left %2f\n", +avoidturn );
124  }
125  }
126 
127  robot->avoidcount--;
128  }
129  else
130  {
131  if( verbose ) puts( "Cruise" );
132 
133  robot->avoidcount = 0;
134  robot->pos->SetXSpeed( cruisespeed );
135  robot->pos->SetTurnSpeed( 0 );
136  }
137 
138  // if( robot->pos->Stalled() )
139  // {
140  // robot->pos->SetSpeed( 0,0,0 );
141  // robot->pos->SetTurnSpeed( 0 );
142  // }
143 
144  return 0; // run again
145 }
146 
147 int PositionUpdate( Model* mod, robot_t* robot )
148 {
149  Pose pose = robot->pos->GetPose();
150 
151  printf( "Pose: [%.2f %.2f %.2f %.2f]\n",
152  pose.x, pose.y, pose.z, pose.a );
153 
154  return 0; // run again
155 }
156 
void SetTurnSpeed(double a)
Model class
Definition: stage.hh:1742
int randcount
Definition: wander.cc:16
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:18
int(* model_callback_t)(Model *mod, void *user)
Definition: stage.hh:568
int avoidcount
Definition: wander.cc:16
static const bool verbose
Definition: wander.cc:8
meters_t z
location in 3 axes
Definition: stage.hh:251
meters_t y
Definition: stage.hh:251
ModelPosition class
Definition: stage.hh:2927
static const double minfrontdistance
Definition: wander.cc:7
int LaserUpdate(Model *mod, robot_t *robot)
Definition: wander.cc:54
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:147
static const double avoidspeed
Definition: wander.cc:5
const std::vector< Sensor > & GetSensors() const
Definition: stage.hh:2804
void Subscribe()
Definition: model.cc:646
static const double cruisespeed
Definition: wander.cc:4
ModelRanger class
Definition: stage.hh:2747
static const double stopdist
Definition: wander.cc:9
Pose GetPose() const
Definition: stage.hh:2382
radians_t a
rotation about the z axis.
Definition: stage.hh:252
Model * GetChild(const std::string &name) const
Definition: model.cc:1018
ModelPosition * pos
Definition: wander.cc:14
static const double avoidturn
Definition: wander.cc:6
meters_t x
Definition: stage.hh:251
ModelRanger * laser
Definition: wander.cc:15


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