p_ranger.cc
Go to the documentation of this file.
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2004, 2005 Richard Vaughan
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 /*
23  * Desc: Ranger interface for Stage's player driver
24  * Author: Richard Vaughan
25  * Date: 25 November 2010
26  * CVS: $Id$
27  */
28 
29 // DOCUMENTATION ------------------------------------------------------------
30 
39 // CODE ----------------------------------------------------------------------
40 
41 #include "p_driver.h"
42 using namespace Stg;
43 
44 InterfaceRanger::InterfaceRanger( player_devaddr_t addr,
45  StgDriver* driver,
46  ConfigFile* cf,
47  int section )
48  : InterfaceModel( addr, driver, cf, section, "ranger" )
49 {
50  this->scan_id = 0;
51 }
52 
54 {
55  ModelRanger* rgr = dynamic_cast<ModelRanger*>(this->mod);
56 
57  // the Player interface dictates that if multiple sensor poses are
58  // given, then we have exactly one range reading per sensor. To give
59  // multiple ranges from the same origin, only one sensor is allowed.
60 
61  // need to use the mutable version since we access the range data via a regular pointer
62  std::vector<ModelRanger::Sensor>& sensors = rgr->GetSensorsMutable();
63 
64  player_ranger_data_range_t prange;
65  memset( &prange, 0, sizeof(prange) );
66 
67  player_ranger_data_intns_t pintens;
68  memset( &pintens, 0, sizeof(pintens) );
69 
70  std::vector<double> rv, iv;
71 
72  if( sensors.size() == 1 ) // a laser scanner type, with one beam origin and many ranges
73  {
74  prange.ranges_count = sensors[0].ranges.size();
75  prange.ranges = prange.ranges_count ? &sensors[0].ranges[0] : NULL;
76 
77  pintens.intensities_count = sensors[0].intensities.size();
78  pintens.intensities = pintens.intensities_count ? &sensors[0].intensities[0] : NULL;
79  }
80  else
81  { // a sonar/IR type with one range per beam origin
82  FOR_EACH( it, sensors )
83  {
84  if( it->ranges.size() )
85  rv.push_back( it->ranges[0] );
86 
87  if( it->intensities.size() )
88  iv.push_back( it->intensities[0] );
89  }
90 
91  prange.ranges_count = rv.size();
92  prange.ranges = rv.size() ? &rv[0] : NULL;
93 
94  pintens.intensities_count = iv.size();
95  pintens.intensities = iv.size() ? &iv[0] : NULL;
96  }
97 
98  if( prange.ranges_count )
99  this->driver->Publish(this->addr,
100  PLAYER_MSGTYPE_DATA,
101  PLAYER_RANGER_DATA_RANGE,
102  (void*)&prange, sizeof(prange), NULL);
103 
104  if( pintens.intensities_count )
105  this->driver->Publish(this->addr,
106  PLAYER_MSGTYPE_DATA,
107  PLAYER_RANGER_DATA_INTNS,
108  (void*)&pintens, sizeof(pintens), NULL);
109 }
110 
111 
112 int InterfaceRanger::ProcessMessage(QueuePointer & resp_queue,
113  player_msghdr_t* hdr,
114  void* data)
115 {
116  ModelRanger* mod = (ModelRanger*)this->mod;
117 
118  // Is it a request to get the ranger's config?
119  if (Message::MatchMessage(hdr, PLAYER_MSGTYPE_REQ,
120  PLAYER_RANGER_REQ_GET_CONFIG,
121  this->addr))
122  {
123  if( hdr->size == 0 )
124  {
125  // the Player ranger config is a little weaker than Stage's
126  // natice device, so all we can do is warn about this.
127  PRINT_WARN( "stageplugin ranger config describes only the first sensor of the ranger." );
128 
129  player_ranger_config_t prc;
130  bzero(&prc,sizeof(prc));
131 
132  const ModelRanger::Sensor& s = mod->GetSensors()[0];
133 
134  prc.min_angle = -s.fov/2.0;
135  prc.max_angle = +s.fov/2.0;
136  prc.angular_res = s.fov / (double)s.sample_count;
137  prc.max_range = s.range.max;
138  prc.min_range = s.range.min;
139  prc.range_res = 1.0 / mod->GetWorld()->Resolution();
140  prc.frequency = 1.0E6 / mod->GetInterval();
141 
142  this->driver->Publish(this->addr, resp_queue,
143  PLAYER_MSGTYPE_RESP_ACK,
144  PLAYER_RANGER_REQ_GET_CONFIG,
145  (void*)&prc, sizeof(prc), NULL);
146  return(0);
147  }
148  else
149  {
150  PRINT_ERR2("config request len is invalid (%d != %d)", (int)hdr->size,0);
151  return(-1);
152  }
153  }
154  else // Is it a request to get the ranger's geom?
155  if (Message::MatchMessage(hdr, PLAYER_MSGTYPE_REQ,
156  PLAYER_RANGER_REQ_GET_GEOM,
157  this->addr))
158  {
159  if(hdr->size == 0)
160  {
161  Geom geom = mod->GetGeom();
162  Pose pose = mod->GetPose();
163 
164  const std::vector<ModelRanger::Sensor>& sensors = mod->GetSensors();
165 
166  // fill in the geometry data formatted player-like
167  player_ranger_geom_t pgeom;
168  bzero( &pgeom, sizeof(pgeom));
169  pgeom.pose.px = pose.x;
170  pgeom.pose.py = pose.y;
171  pgeom.pose.pyaw = pose.a;
172  pgeom.size.sl = geom.size.x;
173  pgeom.size.sw = geom.size.y;
174 
175  pgeom.element_poses_count = pgeom.element_sizes_count = sensors.size();
176 
177  player_pose3d_t poses[ sensors.size() ];
178  player_bbox3d_t sizes[ sensors.size() ];
179 
180  for( size_t s=0; s<pgeom.element_poses_count; s++ )
181  {
182  poses[s].px = sensors[s].pose.x;
183  poses[s].py = sensors[s].pose.y;
184  poses[s].pz = sensors[s].pose.z;
185  poses[s].proll = 0.0;
186  poses[s].ppitch = 0.0;
187  poses[s].pyaw = sensors[s].pose.a;
188 
189  sizes[s].sw = sensors[s].size.x;
190  sizes[s].sl = sensors[s].size.y;
191  sizes[s].sh = sensors[s].size.z;
192  }
193 
194  pgeom.element_poses = poses;
195  pgeom.element_sizes = sizes;
196 
197  this->driver->Publish(this->addr, resp_queue,
198  PLAYER_MSGTYPE_RESP_ACK,
199  PLAYER_RANGER_REQ_GET_GEOM,
200  (void*)&pgeom, sizeof(pgeom), NULL);
201  return(0);
202  }
203  else
204  {
205  PRINT_ERR2("config request len is invalid (%d != %d)", (int)hdr->size,0);
206  return(-1);
207  }
208  }
209 
210  // Don't know how to handle this message.
211  PRINT_WARN2( "stage ranger doesn't support message %d:%d.",
212  hdr->type, hdr->subtype);
213  return(-1);
214 }
215 
216 
double max
largest value in range, initially zero
Definition: stage.hh:435
The Stage library uses its own namespace.
Definition: canvas.hh:8
meters_t x
Definition: stage.hh:228
std::vector< Sensor > & GetSensorsMutable()
Definition: stage.hh:2808
#define PRINT_WARN2(m, a, b)
Definition: stage.hh:635
double min
smallest value in range, initially zero
Definition: stage.hh:433
Size size
extent
Definition: stage.hh:395
InterfaceRanger(player_devaddr_t addr, StgDriver *driver, ConfigFile *cf, int section)
Definition: p_ranger.cc:44
virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr_t *hdr, void *data)
Definition: p_ranger.cc:112
StgDriver * driver
Definition: p_driver.h:72
float s
Definition: glutgraphics.cc:58
World * GetWorld() const
Definition: stage.hh:2302
meters_t y
Definition: stage.hh:251
player_devaddr_t addr
Definition: p_driver.h:66
unsigned int sample_count
Definition: stage.hh:2779
meters_t y
Definition: stage.hh:228
const std::vector< Sensor > & GetSensors() const
Definition: stage.hh:2804
double Resolution() const
Definition: stage.hh:1171
#define PRINT_WARN(m)
Definition: stage.hh:633
usec_t GetInterval()
Definition: stage.hh:2044
Stg::Model * mod
Definition: p_driver.h:115
ModelRanger class
Definition: stage.hh:2747
#define PRINT_ERR2(m, a, b)
Definition: stage.hh:627
#define FOR_EACH(I, C)
Definition: stage.hh:616
Pose GetPose() const
Definition: stage.hh:2382
radians_t a
rotation about the z axis.
Definition: stage.hh:252
virtual void Publish(void)
Definition: p_ranger.cc:53
meters_t x
Definition: stage.hh:251
Geom GetGeom() const
Definition: stage.hh:2378


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