colored_blob_omnidirectional_camera_rotzonly_sensor.cpp
Go to the documentation of this file.
2 #include <argos3/core/simulator/simulator.h>
3 #include <argos3/core/simulator/space/positional_indices/positional_index.h>
4 #include <argos3/core/simulator/entity/composable_entity.h>
5 #include <argos3/core/simulator/entity/embodied_entity.h>
6 #include <argos3/plugins/simulator/entities/led_entity.h>
7 #include <argos3/plugins/simulator/entities/omnidirectional_camera_equipped_entity.h>
8 #include <argos3/plugins/simulator/media/led_medium.h>
9 
10 namespace argos {
11 
12  /****************************************/
13  /****************************************/
14 
15  class COmnidirectionalCameraLEDCheckOperation : public CPositionalIndex<CLEDEntity>::COperation {
16 
17  public:
18 
21  COmnidirectionalCameraEquippedEntity& c_omnicam_entity,
22  CEmbodiedEntity& c_embodied_entity,
23  CControllableEntity& c_controllable_entity,
24  bool b_show_rays,
25  Real f_noise_std_dev) :
26  m_tBlobs(t_blobs),
27  m_cOmnicamEntity(c_omnicam_entity),
28  m_cEmbodiedEntity(c_embodied_entity),
29  m_cControllableEntity(c_controllable_entity),
30  m_bShowRays(b_show_rays),
31  m_fDistanceNoiseStdDev(f_noise_std_dev),
32  m_pcRNG(NULL) {
33  m_pcRootSensingEntity = &m_cEmbodiedEntity.GetParent();
34  if(m_fDistanceNoiseStdDev > 0.0f) {
35  m_pcRNG = CRandom::CreateRNG("argos");
36  }
37  }
39  while(! m_tBlobs.empty()) {
40  delete m_tBlobs.back();
41  m_tBlobs.pop_back();
42  }
43  }
44 
45  virtual bool operator()(CLEDEntity& c_led) {
46  /* Process this LED only if it's lit */
47  if(c_led.GetColor() != CColor::BLACK) {
48  if(c_led.HasParent()) {
49  /* Filter out the LEDs belonging to the sensing entity by checking if they share the same parent entity */
50  m_pcRootOfLEDEntity = &c_led.GetParent();
51  while(m_pcRootOfLEDEntity->HasParent()) m_pcRootOfLEDEntity = &m_pcRootOfLEDEntity->GetParent();
52  if(m_pcRootSensingEntity == m_pcRootOfLEDEntity) {
53  return true;
54  }
55  }
56  /* If we are here, it's because the LED must be processed */
57  m_cOcclusionCheckRay.SetEnd(c_led.GetPosition());
58  m_cLEDRelativePos = c_led.GetPosition();
59  m_cLEDRelativePos -= m_cCameraPos;
60  m_cLEDRelativePosXY.Set(m_cLEDRelativePos.GetX(),
61  m_cLEDRelativePos.GetY());
62  if(Abs(m_cLEDRelativePos.GetX()) < m_fGroundHalfRange &&
63  Abs(m_cLEDRelativePos.GetY()) < m_fGroundHalfRange &&
64  m_cLEDRelativePos.GetZ() < m_cCameraPos.GetZ() &&
65  !GetClosestEmbodiedEntityIntersectedByRay(m_sIntersectionItem,
66  m_cOcclusionCheckRay,
67  m_cEmbodiedEntity)) {
68  /* If noise was setup, add it */
69  if(m_fDistanceNoiseStdDev > 0.0f) {
70  m_cLEDRelativePosXY += CVector2(
71  m_cLEDRelativePosXY.Length() * m_pcRNG->Gaussian(m_fDistanceNoiseStdDev),
73  }
75  c_led.GetColor(),
76  NormalizedDifference(m_cLEDRelativePosXY.Angle(), m_cCameraOrient),
77  m_cLEDRelativePosXY.Length() * 100.0f));
78  if(m_bShowRays) {
79  m_cControllableEntity.AddCheckedRay(false, CRay3(m_cCameraPos, c_led.GetPosition()));
80  }
81  }
82  }
83  return true;
84  }
85 
86  void Setup(Real f_ground_half_range) {
87  while(! m_tBlobs.empty()) {
88  delete m_tBlobs.back();
89  m_tBlobs.pop_back();
90  }
91  m_fGroundHalfRange = f_ground_half_range;
92  m_cEmbodiedEntity.GetOriginAnchor().Orientation.ToEulerAngles(m_cCameraOrient, m_cTmp1, m_cTmp2);
93  m_cCameraPos = m_cEmbodiedEntity.GetOriginAnchor().Position;
94  m_cCameraPos += m_cOmnicamEntity.GetOffset();
95  m_cOcclusionCheckRay.SetStart(m_cCameraPos);
96  }
97 
98  private:
99 
101  COmnidirectionalCameraEquippedEntity& m_cOmnicamEntity;
102  CEmbodiedEntity& m_cEmbodiedEntity;
103  CControllableEntity& m_cControllableEntity;
104  Real m_fGroundHalfRange;
105  bool m_bShowRays;
106  CEntity* m_pcRootSensingEntity;
107  CEntity* m_pcRootOfLEDEntity;
108  CVector3 m_cCameraPos;
109  CRadians m_cCameraOrient;
110  CRadians m_cTmp1, m_cTmp2;
111  CVector3 m_cLEDRelativePos;
112  CVector2 m_cLEDRelativePosXY;
113  SEmbodiedEntityIntersectionItem m_sIntersectionItem;
114  CRay3 m_cOcclusionCheckRay;
115  Real m_fDistanceNoiseStdDev;
116  CRandom::CRNG* m_pcRNG;
117  };
118 
119  /****************************************/
120  /****************************************/
121 
123  m_bEnabled(false),
124  m_pcOmnicamEntity(NULL),
125  m_pcControllableEntity(NULL),
126  m_pcEmbodiedEntity(NULL),
127  m_pcLEDIndex(NULL),
128  m_pcEmbodiedIndex(NULL),
129  m_bShowRays(false) {
130  }
131 
132  /****************************************/
133  /****************************************/
134 
136  }
137 
138  /****************************************/
139  /****************************************/
140 
142  /* Get omndirectional camera equipped entity */
143  m_pcOmnicamEntity = &(c_entity.GetComponent<COmnidirectionalCameraEquippedEntity>("omnidirectional_camera"));
144  /* Get controllable entity */
145  m_pcControllableEntity = &(c_entity.GetComponent<CControllableEntity>("controller"));
146  /* Get embodied entity */
147  m_pcEmbodiedEntity = &(c_entity.GetComponent<CEmbodiedEntity>("body"));
148  }
149 
150  /****************************************/
151  /****************************************/
152 
154  try {
155  /* Parent class init */
157  /* Show rays? */
158  GetNodeAttributeOrDefault(t_tree, "show_rays", m_bShowRays, m_bShowRays);
159  /* Parse noise */
160  Real fDistanceNoiseStdDev = 0;
161  GetNodeAttributeOrDefault(t_tree, "noise_std_dev", fDistanceNoiseStdDev, fDistanceNoiseStdDev);
162  /* Get LED medium from id specified in the XML */
163  std::string strMedium;
164  GetNodeAttribute(t_tree, "medium", strMedium);
165  m_pcLEDIndex = &(CSimulator::GetInstance().GetMedium<CLEDMedium>(strMedium).GetIndex());
166  /* Create check operation */
172  m_bShowRays,
173  fDistanceNoiseStdDev);
174  }
175  catch(CARGoSException& ex) {
176  THROW_ARGOSEXCEPTION_NESTED("Error initializing the colored blob omnidirectional camera rotzonly sensor", ex);
177  }
178  }
179 
180  /****************************************/
181  /****************************************/
182 
184  if(m_bEnabled) {
185  /* Increase data counter */
187  /* Calculate range on the ground */
188  CVector3 cCameraPos = m_pcOmnicamEntity->GetOffset();
190  Real fGroundHalfRange = cCameraPos.GetZ() * Tan(m_pcOmnicamEntity->GetAperture());
191  /* Prepare the operation */
192  m_pcOperation->Setup(fGroundHalfRange);
193  /* Go through LED entities in box range */
194  m_pcLEDIndex->ForEntitiesInBoxRange(
195  CVector3(cCameraPos.GetX(),
196  cCameraPos.GetY(),
197  cCameraPos.GetZ() * 0.5f),
198  CVector3(fGroundHalfRange, fGroundHalfRange, cCameraPos.GetZ() * 0.5f),
199  *m_pcOperation);
200  }
201  }
202 
203  /****************************************/
204  /****************************************/
205 
207  m_sReadings.Counter = 0;
208  m_sReadings.BlobList.clear();
209  }
210 
211  /****************************************/
212  /****************************************/
213 
215  delete m_pcOperation;
216  }
217 
218  /****************************************/
219  /****************************************/
220 
223  m_bEnabled = true;
224  }
225 
226  /****************************************/
227  /****************************************/
228 
231  m_bEnabled = false;
232  }
233 
234  /****************************************/
235  /****************************************/
236 
238  "colored_blob_omnidirectional_camera", "rot_z_only",
239  "Carlo Pinciroli [ilpincy@gmail.com]",
240  "1.0",
241  "A generic omnidirectional camera sensor to detect colored blobs.",
242  "This sensor accesses an omnidirectional camera that detects colored blobs. The\n"
243  "sensor returns a list of blobs, each defined by a color and a position with\n"
244  "respect to the robot reference point on the ground. In controllers, you must\n"
245  "include the ci_colored_blob_omnidirectional_camera_sensor.h header.\n\n"
246  "REQUIRED XML CONFIGURATION\n\n"
247  " <controllers>\n"
248  " ...\n"
249  " <my_controller ...>\n"
250  " ...\n"
251  " <sensors>\n"
252  " ...\n"
253  " <colored_blob_omnidirectional_camera implementation=\"rot_z_only\"\n"
254  " medium=\"leds\" />\n"
255  " ...\n"
256  " </sensors>\n"
257  " ...\n"
258  " </my_controller>\n"
259  " ...\n"
260  " </controllers>\n\n"
261  "The 'medium' attribute must be set to the id of the leds medium declared in the\n"
262  "<media> section.\n\n"
263  "OPTIONAL XML CONFIGURATION\n\n"
264  "It is possible to draw the rays shot by the camera sensor in the OpenGL\n"
265  "visualization. This can be useful for sensor debugging but also to understand\n"
266  "what's wrong in your controller. In OpenGL, the rays are drawn in cyan when\n"
267  "they are not obstructed and in purple when they are. In case a ray is\n"
268  "obstructed, a black dot is drawn where the intersection occurred.\n"
269  "To turn this functionality on, add the attribute \"show_rays\" as in this\n"
270  "example:\n\n"
271  " <controllers>\n"
272  " ...\n"
273  " <my_controller ...>\n"
274  " ...\n"
275  " <sensors>\n"
276  " ...\n"
277  " <colored_blob_omnidirectional_camera implementation=\"rot_z_only\"\n"
278  " medium=\"leds\" />\n"
279  " show_rays=\"true\" />\n"
280  " ...\n"
281  " </sensors>\n"
282  " ...\n"
283  " </my_controller>\n"
284  " ...\n"
285  " </controllers>\n\n"
286  "It is possible to add uniform noise to the blobs, thus matching the\n"
287  "characteristics of a real robot better. This can be done with the attribute\n"
288  "\"noise_std_dev\".\n\n"
289  " <controllers>\n"
290  " ...\n"
291  " <my_controller ...>\n"
292  " ...\n"
293  " <sensors>\n"
294  " ...\n"
295  " <colored_blob_omnidirectional_camera implementation=\"rot_z_only\"\n"
296  " medium=\"leds\" />\n"
297  " noise_std_dev=\"0.1\" />\n"
298  " ...\n"
299  " </sensors>\n"
300  " ...\n"
301  " </my_controller>\n"
302  " ...\n"
303  " </controllers>\n",
304  "Usable"
305  );
306 
307 }
The RNG.
Definition: rng.h:90
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
An entity that contains a pointer to the user-defined controller.
std::vector< SBlob * > TBlobList
Vector of pointers to colored blobs.
A 3D vector class.
Definition: vector3.h:29
static const CRange< CRadians > UNSIGNED_RANGE
The unsigned normalization range [0:TWO_PI].
Definition: angles.h:274
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.
virtual void Update()
Updates the state of the entity associated to this sensor.
static CColor BLACK
Definition: color.h:29
void Enable()
Enables the entity.
Definition: entity.h:265
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:93
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition: general.h:25
The basic entity type.
Definition: entity.h:89
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:109
virtual void Reset()
Resets the sensor to the state it had just after Init().
bool HasParent() const
Returns true if this entity has a parent.
Definition: entity.h:171
A data structure that contains positional entities.
const CColor & GetColor() const
Returns the current color of the LED.
Definition: led_entity.h:58
CQuaternion Orientation
The orientation of the anchor wrt the global coordinate system.
Definition: physics_model.h:53
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
const CRadians & GetAperture() const
Returns the aperture of the visibility cone of the omnidirectional camera.
This entity is a link to a body in the physics engine.
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception...
const CVector3 & GetPosition() const
const CVector3 & GetOffset() const
Returns the offset of the omnidirectional camera with respect to the reference point.
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
void SetEnd(const CVector3 &c_end)
Definition: ray3.h:57
CRadians Angle() const
Returns the angle of this vector.
Definition: vector2.h:160
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
Definition: rng.cpp:87
Basic class for an entity that contains other entities.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
A 2D vector class.
Definition: vector2.h:25
void ToEulerAngles(CRadians &c_z_angle, CRadians &c_y_angle, CRadians &c_x_angle) const
Definition: quaternion.h:172
virtual void Init(TConfigurationNode &t_node)
Initializes the sensor from the XML configuration tree.
Definition: ci_sensor.h:54
Real Tan(const CRadians &c_radians)
Computes the tangent of the passed value in radians.
Definition: angles.h:604
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
void SetStart(const CVector3 &c_start)
Definition: ray3.h:53
The exception that wraps all errors in ARGoS.
bool GetClosestEmbodiedEntityIntersectedByRay(SEmbodiedEntityIntersectionItem &s_item, const CRay3 &c_ray)
Returns the closest intersection with an embodied entity to the ray start.
CRadians NormalizedDifference(const CRadians &c_angle1, const CRadians &c_angle2)
Calculates the normalized difference between the given angles.
Definition: angles.h:510
Real Length() const
Returns the length of this vector.
Definition: vector2.h:141
void Disable()
Disables the entity.
Definition: entity.h:275
T & GetMedium(const std::string &str_id)
Returns a reference to a medium.
Definition: simulator.h:129
Real Gaussian(Real f_std_dev, Real f_mean=0.0f)
Returns a random value from a Gaussian distribution.
Definition: rng.cpp:131
CVector3 Position
The position of the anchor wrt the global coordinate system.
Definition: physics_model.h:51
COmnidirectionalCameraLEDCheckOperation(CCI_ColoredBlobOmnidirectionalCameraSensor::TBlobList &t_blobs, COmnidirectionalCameraEquippedEntity &c_omnicam_entity, CEmbodiedEntity &c_embodied_entity, CControllableEntity &c_controllable_entity, bool b_show_rays, Real f_noise_std_dev)
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
Definition: rng.cpp:326
void Set(Real f_x, Real f_y)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:111
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:91
void AddCheckedRay(bool b_obstructed, const CRay3 &c_ray)
Adds a ray to the list of checked rays.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
Real GetZ() const
Returns the z coordinate of this vector.
Definition: vector3.h:125
An SBlob represents a generic colored 2D segment in the image.
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition: simulator.cpp:78
const SAnchor & GetOriginAnchor() const
Returns a const reference to the origin anchor associated to this entity.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
REGISTER_SENSOR(CEyeBotLightRotZOnlySensor,"eyebot_light","rot_z_only","Carlo Pinciroli [ilpincy@gmail.com]","1.0","The eye-bot light sensor (optimized for 2D).","This sensor accesses a set of light sensors. The sensors all return a value\n""between 0 and 1, where 0 means nothing within range and 1 means the perceived\n""light saturates the sensor. Values between 0 and 1 depend on the distance of\n""the perceived light. Each reading R is calculated with R=(I/x)^2, where x is the\n""distance between a sensor and the light, and I is the reference intensity of the\n""perceived light. The reference intensity corresponds to the minimum distance at\n""which the light saturates a sensor. The reference intensity depends on the\n""individual light, and it is set with the \"intensity\" attribute of the light\n""entity. In case multiple lights are present in the environment, each sensor\n""reading is calculated as the sum of the individual readings due to each light.\n""In other words, light wave interference is not taken into account. In\n""controllers, you must include the ci_light_sensor.h header.\n\n""REQUIRED XML CONFIGURATION\n\n"" <controllers>\n"" ...\n"" <my_controller ...>\n"" ...\n"" <sensors>\n"" ...\n"" <eyebot_light implementation=\"rot_z_only\" />\n"" ...\n"" </sensors>\n"" ...\n"" </my_controller>\n"" ...\n"" </controllers>\n\n""OPTIONAL XML CONFIGURATION\n\n""It is possible to draw the rays shot by the light sensor in the OpenGL\n""visualization. This can be useful for sensor debugging but also to understand\n""what's wrong in your controller. In OpenGL, the rays are drawn in cyan when\n""they are not obstructed and in purple when they are. In case a ray is\n""obstructed, a black dot is drawn where the intersection occurred.\n""To turn this functionality on, add the attribute \"show_rays\" as in this\n""example:\n\n"" <controllers>\n"" ...\n"" <my_controller ...>\n"" ...\n"" <sensors>\n"" ...\n"" <eyebot_light implementation=\"rot_z_only\"\n"" show_rays=\"true\" />\n"" ...\n"" </sensors>\n"" ...\n"" </my_controller>\n"" ...\n"" </controllers>\n\n""It is possible to add uniform noise to the sensors, thus matching the\n""characteristics of a real robot better. This can be done with the attribute\n""\"noise_level\", whose allowed range is in [-1,1] and is added to the calculated\n""reading. The final sensor reading is always normalized in the [0-1] range.\n\n"" <controllers>\n"" ...\n"" <my_controller ...>\n"" ...\n"" <sensors>\n"" ...\n"" <eyebot_light implementation=\"rot_z_only\"\n"" noise_level=\"0.1\" />\n"" ...\n"" </sensors>\n"" ...\n"" </my_controller>\n"" ...\n"" </controllers>\n\n""OPTIONAL XML CONFIGURATION\n\n""None.\n","Usable")