expand_swarm.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.3 2008-02-01 03:11:02 rtv Exp $
7 // License: GPL
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "stage.hh"
15 using namespace Stg;
16 
17 typedef struct
18 {
19  ModelRanger* laser;
20  ModelPosition* position;
21  ModelRanger* ranger;
22 } robot_t;
23 
24 // swarmbot
25 const double VSPEED = 0.3; // meters per second
26 const double WGAIN = 1.0; // turn speed gain
27 const double SAFE_DIST = 0.5; // meters
28 const double SAFE_ANGLE = 0.5; // radians
29 
30 // forward declare
31 int RangerUpdate( ModelRanger* mod, robot_t* robot );
32 
33 // Stage calls this when the model starts up
34 extern "C" int Init( Model* mod )
35 {
36  robot_t* robot = new robot_t;
37  robot->position = (ModelPosition*)mod;
38 
39  // subscribe to the ranger, which we use for navigating
40  robot->ranger = (ModelRanger*)mod->GetUnusedModelOfType( "ranger" );
41  assert( robot->ranger );
42 
43 
44  // ask Stage to call into our ranger update function
46 
47  // subscribe to the laser, though we don't use it for navigating
48  //robot->laser = (ModelLaser*)mod->GetUnusedModelofType( "laser" );
49  //assert( robot->laser );
50 
51  // start the models updating
52  robot->position->Subscribe();
53  robot->ranger->Subscribe();
54  //robot->laser->Subscribe();
55 
56  return 0; //ok
57 }
58 
59 int RangerUpdate( ModelRanger* rgr, robot_t* robot )
60 {
61  // compute the vector sum of the sonar ranges
62  double dx=0, dy=0;
63 
64  const std::vector<ModelRanger::Sensor>& sensors = rgr->GetSensors();
65 
66  FOR_EACH( it, sensors )
67  {
68  const ModelRanger::Sensor& s = *it;
69  dx += s.ranges[0] * cos( s.pose.a );
70  dy += s.ranges[0] * sin( s.pose.a );
71 
72  //printf( "sensor %d angle= %.2f\n", s, rgr->sensors[s].pose.a );
73  }
74 
75  if( (dx == 0) || (dy == 0) )
76  return 0;
77 
78  double resultant_angle = atan2( dy, dx );
79  double forward_speed = 0.0;
80  double side_speed = 0.0;
81  double turn_speed = WGAIN * resultant_angle;
82 
83  //printf( "resultant %.2f turn_speed %.2f\n", resultant_angle, turn_speed );
84 
85  // if the front is clear, drive forwards
86  if( (sensors[0].ranges[0] > SAFE_DIST) &&
87 
88  (sensors[1].ranges[0] > SAFE_DIST/1.5) &&
89  (sensors[2].ranges[0] > SAFE_DIST/3.0) &&
90  (sensors[3].ranges[0] > SAFE_DIST/5.0) &&
91 
92  (sensors[9].ranges[0] > SAFE_DIST/5.0) &&
93  (sensors[10].ranges[0] > SAFE_DIST/3.0) &&
94  (sensors[11].ranges[0] > SAFE_DIST/1.5) &&
95  (fabs( resultant_angle ) < SAFE_ANGLE) )
96  {
97  forward_speed = VSPEED;
98  }
99 
100  robot->position->SetSpeed( forward_speed, side_speed, turn_speed );
101 
102  return 0;
103 }
104 
Model class
Definition: stage.hh:1742
Model * GetUnusedModelOfType(const std::string &type)
Definition: model.cc:914
The Stage library uses its own namespace.
Definition: canvas.hh:8
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
float s
Definition: glutgraphics.cc:58
ModelPosition * position
ModelPosition class
Definition: stage.hh:2927
const double VSPEED
Definition: expand_swarm.cc:25
void AddCallback(callback_type_t type, model_callback_t cb, void *user)
const double SAFE_DIST
Definition: expand_swarm.cc:27
const std::vector< Sensor > & GetSensors() const
Definition: stage.hh:2804
int RangerUpdate(ModelRanger *mod, robot_t *robot)
Definition: expand_swarm.cc:59
void Subscribe()
Definition: model.cc:646
ModelRanger class
Definition: stage.hh:2747
const double WGAIN
Definition: expand_swarm.cc:26
const double SAFE_ANGLE
Definition: expand_swarm.cc:28
#define FOR_EACH(I, C)
Definition: stage.hh:616
radians_t a
rotation about the z axis.
Definition: stage.hh:252
std::vector< meters_t > ranges
Definition: stage.hh:2782
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