range_and_bearing_medium_sensor.cpp
Go to the documentation of this file.
1 
8 #include <argos3/core/simulator/simulator.h>
9 #include <argos3/core/simulator/entity/composable_entity.h>
10 #include <argos3/core/simulator/entity/controllable_entity.h>
11 #include <argos3/plugins/simulator/entities/rab_equipped_entity.h>
12 #include <argos3/plugins/simulator/media/rab_medium.h>
13 
14 namespace argos {
15 
16  /****************************************/
17  /****************************************/
18 
20 
21  /****************************************/
22  /****************************************/
23 
25  m_pcRangeAndBearingEquippedEntity(nullptr),
26  m_fDistanceNoiseStdDev(0.0f),
27  m_fPacketDropProb(0.0f),
28  m_pcRNG(nullptr),
29  m_cSpace(CSimulator::GetInstance().GetSpace()),
30  m_bShowRays(false) {}
31 
32  /****************************************/
33  /****************************************/
34 
36  /* Assign RAB equipped entity to this sensor */
37  m_pcRangeAndBearingEquippedEntity = &c_entity.GetComponent<CRABEquippedEntity>("rab");
38  /* Get reference to controllable entity */
39  m_pcControllableEntity = &c_entity.GetComponent<CControllableEntity>("controller");
40  }
41 
42  /****************************************/
43  /****************************************/
44 
46  try {
47  /* Parent class init */
49  /* Show rays? */
50  GetNodeAttributeOrDefault(t_tree, "show_rays", m_bShowRays, m_bShowRays);
51  /* Parse noise */
52  GetNodeAttributeOrDefault(t_tree, "noise_std_dev", m_fDistanceNoiseStdDev, m_fDistanceNoiseStdDev);
53  GetNodeAttributeOrDefault(t_tree, "packet_drop_prob", m_fPacketDropProb, m_fPacketDropProb);
54  if((m_fPacketDropProb > 0.0f) ||
55  (m_fDistanceNoiseStdDev > 0.0f)) {
56  m_pcRNG = CRandom::CreateRNG("argos");
57  }
58  /* Get RAB medium from id specified in the XML */
59  std::string strMedium;
60  GetNodeAttribute(t_tree, "medium", strMedium);
61  m_pcRangeAndBearingMedium = &(CSimulator::GetInstance().GetMedium<CRABMedium>(strMedium));
62  /* Assign RAB entity to the medium */
63  m_pcRangeAndBearingEquippedEntity->SetMedium(*m_pcRangeAndBearingMedium);
64  }
65  catch(CARGoSException& ex) {
66  THROW_ARGOSEXCEPTION_NESTED("Error initializing the range and bearing medium sensor", ex);
67  }
68  /* sensor is enabled by default */
69  Enable();
70  }
71 
72  /****************************************/
73  /****************************************/
74 
76  /* sensor is disabled--nothing to do */
77  if (IsDisabled()) {
78  return;
79  }
81  /* Delete old readings */
82  m_tReadings.clear();
83  /* Get list of communicating RABs */
84  const CSet<CRABEquippedEntity*,SEntityComparator>& setRABs = m_pcRangeAndBearingMedium->GetRABsCommunicatingWith(*m_pcRangeAndBearingEquippedEntity);
85  /* Buffer for calculating the message--robot distance */
86  CVector3 cVectorRobotToMessage;
87  /* Buffer for the received packet */
89  /* Go through communicating RABs and create packets */
90  for(CSet<CRABEquippedEntity*>::iterator it = setRABs.begin();
91  it != setRABs.end(); ++it) {
92  /* Should we drop this packet? */
93  if(m_pcRNG == nullptr || /* No noise to apply */
94  !(m_fPacketDropProb > 0.0f &&
95  m_pcRNG->Bernoulli(m_fPacketDropProb)) /* Packet is not dropped */
96  ) {
97  /* Create a reference to the RAB entity to process */
98  CRABEquippedEntity& cRABEntity = **it;
99  /* Add ray if requested */
100  if(m_bShowRays) {
101  m_pcControllableEntity->AddCheckedRay(false,
102  CRay3(cRABEntity.GetPosition(),
103  m_pcRangeAndBearingEquippedEntity->GetPosition()));
104  }
105  /* Calculate vector to entity */
106  cVectorRobotToMessage = cRABEntity.GetPosition();
107  cVectorRobotToMessage -= m_pcRangeAndBearingEquippedEntity->GetPosition();
108  /* If noise was setup, add it */
109  if(m_pcRNG && m_fDistanceNoiseStdDev > 0.0f) {
110  cVectorRobotToMessage += CVector3(
111  m_pcRNG->Gaussian(m_fDistanceNoiseStdDev),
112  m_pcRNG->Uniform(INCLINATION_RANGE),
114  }
115  /*
116  * Set range and bearing from cVectorRobotToMessage
117  * First, we must rotate the cVectorRobotToMessage so that
118  * it is local to the robot coordinate system. To do this,
119  * it enough to rotate cVectorRobotToMessage by the inverse
120  * of the robot orientation.
121  */
122  cVectorRobotToMessage.Rotate(m_pcRangeAndBearingEquippedEntity->GetOrientation().Inverse());
123  cVectorRobotToMessage.ToSphericalCoords(sPacket.Range,
124  sPacket.VerticalBearing,
125  sPacket.HorizontalBearing);
126  /* Convert range to cm */
127  sPacket.Range *= 100.0f;
128  /* Normalize horizontal bearing between [-pi,pi] */
130  /*
131  * The vertical bearing is defined as the angle between the local
132  * robot XY plane and the message source position, i.e., the elevation
133  * in math jargon. However, cVectorRobotToMessage.ToSphericalCoords()
134  * sets sPacket.VerticalBearing to the inclination, which is the angle
135  * between the azimuth vector (robot local Z axis) and
136  * cVectorRobotToMessage. Elevation = 90 degrees - Inclination.
137  */
138  sPacket.VerticalBearing.Negate();
141  /* Set message data */
142  sPacket.Data = cRABEntity.GetData();
143  /* Add message to the list */
144  m_tReadings.push_back(sPacket);
145  }
146  }
147  }
148 
149  /****************************************/
150  /****************************************/
151 
153  m_tReadings.clear();
154  }
155 
156 
157  /****************************************/
158  /****************************************/
160  m_pcRangeAndBearingEquippedEntity->Enable();
162  }
163 
164  /****************************************/
165  /****************************************/
166 
168  m_pcRangeAndBearingEquippedEntity->Disable();
170  }
171 
172  /****************************************/
173  /****************************************/
174 
176  m_pcRangeAndBearingMedium->RemoveEntity(*m_pcRangeAndBearingEquippedEntity);
177  }
178 
179  /****************************************/
180  /****************************************/
181 
183  "range_and_bearing", "medium",
184  "Carlo Pinciroli [ilpincy@gmail.com]",
185  "1.0",
186  "The range-and-bearing sensor.",
187 
188  "This sensor allows robots to perform situated communication, i.e., a form of\n"
189  "wireless communication whereby the receiver also knows the location of the\n"
190  "sender with respect to its own frame of reference.\n"
191  "This implementation of the range-and-bearing sensor is associated to the\n"
192  "range-and-bearing medium. To be able to use this sensor, you must add a\n"
193  "range-and-bearing medium to the <media> section.\n"
194  "This sensor allows a robot to receive messages. To send messages, you need the\n"
195  "range-and-bearing actuator.\n"
196  "To use this sensor, in controllers you must include the\n"
197  "ci_range_and_bearing_sensor.h header.\n\n"
198 
199  "This sensor is enabled by default.\n\n"
200 
201  "REQUIRED XML CONFIGURATION\n\n"
202  " <controllers>\n"
203  " ...\n"
204  " <my_controller ...>\n"
205  " ...\n"
206  " <sensors>\n"
207  " ...\n"
208  " <range_and_bearing implementation=\"medium\"\n"
209  " medium=\"rab\" />\n"
210  " ...\n"
211  " </sensors>\n"
212  " ...\n"
213  " </my_controller>\n"
214  " ...\n"
215  " </controllers>\n\n"
216 
217  "The 'medium' attribute must be set to the id of the range-and-bearing medium\n"
218  "declared in the <media> section.\n\n"
219 
220  "OPTIONAL XML CONFIGURATION\n\n"
221 
222  "It is possible to draw the rays shot by the range-and-bearing sensor in the\n"
223  "OpenGL visualization. This can be useful for sensor debugging but also to\n"
224  "understand what's wrong in your controller. In OpenGL, the rays are drawn in\n"
225  "cyan when two robots are communicating.\n"
226  "To turn this functionality on, add the attribute \"show_rays\" as in this\n"
227  "example:\n\n"
228 
229  " <controllers>\n"
230  " ...\n"
231  " <my_controller ...>\n"
232  " ...\n"
233  " <sensors>\n"
234  " ...\n"
235  " <range_and_bearing implementation=\"medium\"\n"
236  " medium=\"rab\"\n"
237  " show_rays=\"true\" />\n"
238  " ...\n"
239  " </sensors>\n"
240  " ...\n"
241  " </my_controller>\n"
242  " ...\n"
243  " </controllers>\n\n"
244 
245  "It is possible to add noise to the readings, thus matching the characteristics\n"
246  "of a real robot better. Noise is implemented as a random vector added to the\n"
247  "vector joining two communicating robots. For the random vector, the inclination\n"
248  "and azimuth are chosen uniformly in the range [0:PI] and [0:2PI], respectively,\n"
249  "and the length is drawn from a Gaussian distribution. The standard deviation of\n"
250  "the Gaussian distribution is expressed in meters and set by the user through\n"
251  "the attribute 'noise_std_dev' as shown in this example:\n\n"
252 
253  " <controllers>\n"
254  " ...\n"
255  " <my_controller ...>\n"
256  " ...\n"
257  " <sensors>\n"
258  " ...\n"
259  " <range_and_bearing implementation=\"medium\"\n"
260  " medium=\"rab\"\n"
261  " noise_std_dev=\"0.1\" />\n"
262  " ...\n"
263  " </sensors>\n"
264  " ...\n"
265  " </my_controller>\n"
266  " ...\n"
267  " </controllers>\n\n"
268 
269  "In addition, it is possible to specify the probability that a packet gets lost\n"
270  "even though the robot should have received it (i.e., packet dropping). To set\n"
271  "this probability, use the attribute 'packet_drop_prob' as shown in the example:\n"
272 
273  " <controllers>\n"
274  " ...\n"
275  " <my_controller ...>\n"
276  " ...\n"
277  " <sensors>\n"
278  " ...\n"
279  " <range_and_bearing implementation=\"medium\"\n"
280  " medium=\"rab\"\n"
281  " packet_drop_prob=\"0.1\" />\n"
282  " ...\n"
283  " </sensors>\n"
284  " ...\n"
285  " </my_controller>\n"
286  " ...\n"
287  " </controllers>\n" ,
288 
289  "Usable");
290 
291 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define ARGOS_PI
To be used when initializing static variables.
Definition: angles.h:32
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
CRange< CRadians > INCLINATION_RANGE(CRadians(0), CRadians(ARGOS_PI))
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
REGISTER_SENSOR(CEPuckProximityDefaultSensor, "epuck_proximity", "default", "Danesh Tarapore [daneshtarapore@gmail.com]", "1.0", "The E-Puck proximity sensor.", "This sensor accesses the epuck proximity sensor. For a complete description\n" "of its usage, refer to the ci_epuck_proximity_sensor.h interface. For the XML\n" "configuration, refer to the default proximity sensor.\n", "Usable")
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
virtual void Enable()
Enables updating of sensor information in the event loop.
Definition: ci_sensor.h:78
virtual void Init(TConfigurationNode &t_node)
Initializes the sensor from the XML configuration tree.
Definition: ci_sensor.h:54
bool IsDisabled() const
Definition: ci_sensor.h:86
virtual void Disable()
Disables updating of sensor information in the event loop.
Definition: ci_sensor.h:83
Basic class for an entity that contains other entities.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
An entity that contains a pointer to the user-defined controller.
void AddCheckedRay(bool b_obstructed, const CRay3 &c_ray)
Adds a ray to the list of checked rays.
void Disable()
Disables the entity.
Definition: entity.h:275
void Enable()
Enables the entity.
Definition: entity.h:265
const CQuaternion & GetOrientation() const
const CVector3 & GetPosition() const
The core class of ARGOS.
Definition: simulator.h:62
T & GetMedium(const std::string &str_id)
Returns a reference to a medium.
Definition: simulator.h:129
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition: simulator.cpp:78
The exception that wraps all errors in ARGoS.
The CSet iterator.
Definition: set.h:39
Defines a very simple double-linked list that stores unique elements.
Definition: set.h:101
iterator begin() const
Returns an iterator to the first element.
Definition: set.h:389
iterator end() const
Returns an invalid iterator.
Definition: set.h:397
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
CRadians & Negate()
Definition: angles.h:153
static const CRange< CRadians > UNSIGNED_RANGE
The unsigned normalization range [0:TWO_PI].
Definition: angles.h:274
static const CRadians PI_OVER_TWO
Set to PI / 2.
Definition: angles.h:59
CRadians & SignedNormalize()
Normalizes the value in the range [-PI:PI].
Definition: angles.h:137
CQuaternion Inverse() const
Definition: quaternion.h:98
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
Definition: rng.cpp:347
bool Bernoulli(Real f_true=0.5)
Returns a random value from a Bernoulli distribution.
Definition: rng.cpp:80
Real Gaussian(Real f_std_dev, Real f_mean=0.0f)
Returns a random value from a Gaussian distribution.
Definition: rng.cpp:152
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
Definition: rng.cpp:87
A 3D vector class.
Definition: vector3.h:31
CVector3 & Rotate(const CQuaternion &c_quaternion)
Rotates this vector by the given quaternion.
Definition: vector3.cpp:23
void ToSphericalCoords(Real &f_radius, CRadians &c_inclination, CRadians &c_azimuth) const
Returns the vector contents as spherical coordinates.
Definition: vector3.h:207
CRadians VerticalBearing
The vertical bearing is defined as the angle between the local robot XY plane and the message source ...
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
virtual void Update()
Updates the state of the entity associated to this sensor, if the sensor is currently enabled.
virtual void Enable()
Enables updating of sensor information in the event loop.
virtual void Reset()
Resets the sensor to the state it had just after Init().
virtual void Disable()
Disables updating of sensor information in the event loop.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
void SetMedium(CRABMedium &c_medium)
const CSet< CRABEquippedEntity *, SEntityComparator > & GetRABsCommunicatingWith(CRABEquippedEntity &c_entity) const
Returns an immutable vector of RAB entities that can communicated with the given entity.
Definition: rab_medium.cpp:224
void RemoveEntity(CRABEquippedEntity &c_entity)
Removes the specified entity from the list of managed entities.
Definition: rab_medium.cpp:213