pioneer_flocking.cc
Go to the documentation of this file.
1 // File: pioneer_flocking.cc
3 // Desc: Flocking behaviour, Stage controller demo
4 // Created: 2009.7.8
5 // Author: Richard Vaughan <vaughan@sfu.ca>
6 // License: GPL
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "stage.hh"
14 using namespace Stg;
15 
16 typedef struct
17 {
21 
26 
27 } robot_t;
28 
29 
30 const double VSPEED = 0.4; // meters per second
31 const double EXPAND_WGAIN = 0.3; // turn speed gain
32 const double FLOCK_WGAIN = 0.3; // turn speed gain
33 const double SAFE_DIST = 1.0; // meters
34 const double SAFE_ANGLE = 0.5; // radians
35 
36 
37 // forward declare
38 int RangerUpdate( ModelRanger* mod, robot_t* robot );
39 int FiducialUpdate( ModelFiducial* fid, robot_t* robot );
40 
41 // Stage calls this when the model starts up
42 extern "C" int Init( Model* mod )
43 {
44  robot_t* robot = new robot_t;
45  robot->position = (ModelPosition*)mod;
46 
47  // subscribe to the ranger, which we use for navigating
48  robot->ranger = (ModelRanger*)mod->GetUnusedModelOfType( "ranger" );
49  assert( robot->ranger );
50 
51  // ask Stage to call into our ranger update function
53 
54  robot->fiducial = (ModelFiducial*)mod->GetUnusedModelOfType( "fiducial" ) ;
55  assert( robot->fiducial );
57 
58  robot->fiducial->Subscribe();
59  robot->ranger->Subscribe();
60  robot->position->Subscribe();
61 
62  return 0; //ok
63 }
64 
65 int RangerUpdate( ModelRanger* rgr, robot_t* robot )
66 {
67  // compute the vector sum of the sonar ranges
68  double dx=0, dy=0;
69 
70  const std::vector<ModelRanger::Sensor>& sensors = rgr->GetSensors();
71 
72  // use the front-facing sensors only
73  for( unsigned int i=0; i < 8; i++ )
74  {
75  dx += sensors[i].ranges[0] * cos( sensors[i].pose.a );
76  dy += sensors[i].ranges[0] * sin( sensors[i].pose.a );
77  }
78 
79  if( (dx == 0) || (dy == 0) )
80  return 0;
81 
82  double resultant_angle = atan2( dy, dx );
83  double forward_speed = 0.0;
84  double side_speed = 0.0;
85  double turn_speed = EXPAND_WGAIN * resultant_angle;
86 
87  // if the front is clear, drive forwards
88  if( (sensors[3].ranges[0] > SAFE_DIST) && // forwards
89  (sensors[4].ranges[0] > SAFE_DIST) &&
90  (sensors[5].ranges[0] > SAFE_DIST ) && //
91  (sensors[6].ranges[0] > SAFE_DIST/2.0) &&
92  (sensors[2].ranges[0] > SAFE_DIST ) &&
93  (sensors[1].ranges[0] > SAFE_DIST/2.0) &&
94  (fabs( resultant_angle ) < SAFE_ANGLE) )
95  {
96  forward_speed = VSPEED;
97 
98  // and steer to match the heading of the nearest robot
99  if( robot->closest )
100  turn_speed += FLOCK_WGAIN * robot->closest_heading_error;
101  }
102  else
103  {
104  // front not clear. we might be stuck, so wiggle a bit
105  if( fabs(turn_speed) < 0.1 )
106  turn_speed = drand48();
107  }
108 
109  robot->position->SetSpeed( forward_speed, side_speed, turn_speed );
110 
111  return 0;
112 }
113 
114 
116 {
117  // find the closest teammate
118 
119  double dist = 1e6; // big
120 
121  robot->closest = NULL;
122 
123  FOR_EACH( it, fid->GetFiducials() )
124  {
125  ModelFiducial::Fiducial* other = &(*it);
126 
127  if( other->range < dist )
128  {
129  dist = other->range;
130  robot->closest = other;
131  }
132  }
133 
134  if( robot->closest ) // if we saw someone
135  {
136  robot->closest_bearing = robot->closest->bearing;
137  robot->closest_range = robot->closest->range;
138  robot->closest_heading_error = robot->closest->geom.a;
139  }
140 
141  return 0;
142 }
meters_t range
range to the target
Definition: stage.hh:2694
Model class
Definition: stage.hh:1742
Model * GetUnusedModelOfType(const std::string &type)
Definition: model.cc:914
const double FLOCK_WGAIN
The Stage library uses its own namespace.
Definition: canvas.hh:8
std::vector< Fiducial > & GetFiducials()
Definition: stage.hh:2733
const double SAFE_ANGLE
ModelFiducial::Fiducial * closest
meters_t closest_range
void SetSpeed(double x, double y, double a)
void Init(int *argc, char **argv[])
Definition: stage.cc:18
int(* model_callback_t)(Model *mod, void *user)
Definition: stage.hh:568
int FiducialUpdate(ModelFiducial *fid, robot_t *robot)
ModelPosition * position
ModelPosition class
Definition: stage.hh:2927
const double SAFE_DIST
int RangerUpdate(ModelRanger *mod, robot_t *robot)
void AddCallback(callback_type_t type, model_callback_t cb, void *user)
radians_t bearing
bearing to the target
Definition: stage.hh:2695
double meters_t
Definition: stage.hh:174
const std::vector< Sensor > & GetSensors() const
Definition: stage.hh:2804
radians_t closest_bearing
void Subscribe()
Definition: model.cc:646
ModelFiducial * fiducial
const double VSPEED
ModelFiducial class
Definition: stage.hh:2687
ModelRanger class
Definition: stage.hh:2747
Pose geom
size and relative angle of the target
Definition: stage.hh:2696
const double EXPAND_WGAIN
#define FOR_EACH(I, C)
Definition: stage.hh:616
radians_t closest_heading_error
radians_t a
rotation about the z axis.
Definition: stage.hh:252
double radians_t
Definition: stage.hh:177
ModelRanger * ranger


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