proximity_default_sensor.cpp
Go to the documentation of this file.
1 
7 #include <argos3/core/simulator/simulator.h>
8 #include <argos3/core/simulator/entity/embodied_entity.h>
9 #include <argos3/core/simulator/entity/composable_entity.h>
10 #include <argos3/plugins/simulator/entities/proximity_sensor_equipped_entity.h>
11 
13 
14 namespace argos {
15 
16  /****************************************/
17  /****************************************/
18 
19  static CRange<Real> UNIT(0.0f, 1.0f);
20 
21  /****************************************/
22  /****************************************/
23 
25  m_pcEmbodiedEntity(nullptr),
26  m_bShowRays(false),
27  m_pcRNG(nullptr),
28  m_bAddNoise(false),
29  m_cSpace(CSimulator::GetInstance().GetSpace()) {}
30 
31  /****************************************/
32  /****************************************/
33 
35  try {
36  m_pcEmbodiedEntity = &(c_entity.GetComponent<CEmbodiedEntity>("body"));
37  m_pcControllableEntity = &(c_entity.GetComponent<CControllableEntity>("controller"));
38  m_pcProximityEntity = &(c_entity.GetComponent<CProximitySensorEquippedEntity>("proximity_sensors"));
40  }
41  catch(CARGoSException& ex) {
42  THROW_ARGOSEXCEPTION_NESTED("Can't set robot for the proximity default sensor", ex);
43  }
44  }
45 
46  /****************************************/
47  /****************************************/
48 
50  try {
52  /* Show rays? */
53  GetNodeAttributeOrDefault(t_tree, "show_rays", m_bShowRays, m_bShowRays);
54  /* Parse noise level */
55  Real fNoiseLevel = 0.0f;
56  GetNodeAttributeOrDefault(t_tree, "noise_level", fNoiseLevel, fNoiseLevel);
57  if(fNoiseLevel < 0.0f) {
58  THROW_ARGOSEXCEPTION("Can't specify a negative value for the noise level of the proximity sensor");
59  }
60  else if(fNoiseLevel > 0.0f) {
61  m_bAddNoise = true;
62  m_cNoiseRange.Set(-fNoiseLevel, fNoiseLevel);
63  m_pcRNG = CRandom::CreateRNG("argos");
64  }
66  }
67  catch(CARGoSException& ex) {
68  THROW_ARGOSEXCEPTION_NESTED("Initialization error in default proximity sensor", ex);
69  }
70  /* This sensor is enabled by default */
71  Enable();
72  }
73 
74  /****************************************/
75  /****************************************/
76 
78  /* sensor is disabled--nothing to do */
79  if (IsDisabled()) {
80  return;
81  }
82  /* Ray used for scanning the environment for obstacles */
83  CRay3 cScanningRay;
84  CVector3 cRayStart, cRayEnd;
85  /* Buffers to contain data about the intersection */
86  SEmbodiedEntityIntersectionItem sIntersection;
87  /* Go through the sensors */
88  for(UInt32 i = 0; i < m_tReadings.size(); ++i) {
89  /* Compute ray for sensor i */
90  cRayStart = m_pcProximityEntity->GetSensor(i).Offset;
93  cRayEnd = m_pcProximityEntity->GetSensor(i).Offset;
97  cScanningRay.Set(cRayStart,cRayEnd);
98  /* Compute reading */
99  /* Get the closest intersection */
101  cScanningRay,
102  *m_pcEmbodiedEntity)) {
103  /* There is an intersection */
104  if(m_bShowRays) {
106  sIntersection.TOnRay);
107  m_pcControllableEntity->AddCheckedRay(true, cScanningRay);
108  }
109  m_tReadings[i] = CalculateReading(cScanningRay.GetDistance(sIntersection.TOnRay));
110  }
111  else {
112  /* No intersection */
113  m_tReadings[i] = 0.0f;
114  if(m_bShowRays) {
115  m_pcControllableEntity->AddCheckedRay(false, cScanningRay);
116  }
117  }
118  /* Apply noise to the sensor */
119  if(m_bAddNoise) {
121  }
122  /* Trunc the reading between 0 and 1 */
123  UNIT.TruncValue(m_tReadings[i]);
124  }
125  }
126 
127  /****************************************/
128  /****************************************/
129 
131  for(UInt32 i = 0; i < GetReadings().size(); ++i) {
132  m_tReadings[i] = 0.0f;
133  }
134  }
135 
136  /****************************************/
137  /****************************************/
138 
140  return Exp(-f_distance);
141  }
142 
143  /****************************************/
144  /****************************************/
145 
147  "proximity", "default",
148  "Carlo Pinciroli [ilpincy@gmail.com]",
149  "1.0",
150  "A generic proximity sensor.",
151 
152  "This sensor accesses a set of proximity sensors. The sensors all return a value\n"
153  "between 0 and 1, where 0 means nothing within range and 1 means an external\n"
154  "object is touching the sensor. Values between 0 and 1 depend on the distance of\n"
155  "the occluding object, and are calculated as value=exp(-distance). In\n"
156  "controllers, you must include the ci_proximity_sensor.h header.\n\n"
157 
158  "This sensor is enabled by default.\n\n"
159 
160  "REQUIRED XML CONFIGURATION\n\n"
161 
162  " <controllers>\n"
163  " ...\n"
164  " <my_controller ...>\n"
165  " ...\n"
166  " <sensors>\n"
167  " ...\n"
168  " <proximity implementation=\"default\" />\n"
169  " ...\n"
170  " </sensors>\n"
171  " ...\n"
172  " </my_controller>\n"
173  " ...\n"
174  " </controllers>\n\n"
175 
176  "OPTIONAL XML CONFIGURATION\n\n"
177 
178  "It is possible to draw the rays shot by the proximity sensor in the OpenGL\n"
179  "visualization. This can be useful for sensor debugging but also to understand\n"
180  "what's wrong in your controller. In OpenGL, the rays are drawn in cyan when\n"
181  "they are not obstructed and in purple when they are. In case a ray is\n"
182  "obstructed, a black dot is drawn where the intersection occurred.\n"
183  "To turn this functionality on, add the attribute \"show_rays\" as in this\n"
184  "example:\n\n"
185 
186  " <controllers>\n"
187  " ...\n"
188  " <my_controller ...>\n"
189  " ...\n"
190  " <sensors>\n"
191  " ...\n"
192  " <proximity implementation=\"default\"\n"
193  " show_rays=\"true\" />\n"
194  " ...\n"
195  " </sensors>\n"
196  " ...\n"
197  " </my_controller>\n"
198  " ...\n"
199  " </controllers>\n\n"
200 
201  "It is possible to add uniform noise to the sensors, thus matching the\n"
202  "characteristics of a real robot better. This can be done with the attribute\n"
203  "\"noise_level\", whose allowed range is in [-1,1] and is added to the calculated\n"
204  "reading. The final sensor reading is always normalized in the [0-1] range.\n\n"
205 
206  " <controllers>\n"
207  " ...\n"
208  " <my_controller ...>\n"
209  " ...\n"
210  " <sensors>\n"
211  " ...\n"
212  " <proximity implementation=\"default\"\n"
213  " noise_level=\"0.1\" />\n"
214  " ...\n"
215  " </sensors>\n"
216  " ...\n"
217  " </my_controller>\n"
218  " ...\n"
219  " </controllers>\n\n",
220 
221  "Usable"
222  );
223 
224 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
#define Exp
Definition: general.h:65
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
bool GetClosestEmbodiedEntityIntersectedByRay(SEmbodiedEntityIntersectionItem &s_item, const CRay3 &c_ray)
Returns the closest intersection with an embodied entity to the ray start.
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.
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
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 AddIntersectionPoint(const CRay3 &c_ray, Real f_t_on_ray)
Adds an intersection point to the list.
void AddCheckedRay(bool b_obstructed, const CRay3 &c_ray)
Adds a ray to the list of checked rays.
This entity is a link to a body in the physics engine.
CQuaternion Orientation
The orientation of the anchor wrt the global coordinate system.
Definition: physics_model.h:53
CVector3 Position
The position of the anchor wrt the global coordinate system.
Definition: physics_model.h:51
The core class of ARGOS.
Definition: simulator.h:62
The exception that wraps all errors in ARGoS.
void TruncValue(T &t_value) const
Definition: range.h:97
void Set(const T &t_min, const T &t_max)
Definition: range.h:68
Real GetDistance(Real f_t) const
Definition: ray3.h:117
void Set(const CVector3 &c_start, const CVector3 &c_end)
Definition: ray3.h:67
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
Definition: rng.cpp:347
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
std::vector< Real > m_tReadings
const std::vector< Real > & GetReadings() const
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
CRange< Real > m_cNoiseRange
Noise range.
CControllableEntity * m_pcControllableEntity
Reference to controllable entity associated to this sensor.
bool m_bShowRays
Flag to show rays in the simulator.
bool m_bAddNoise
Whether to add noise or not.
CRandom::CRNG * m_pcRNG
Random number generator.
virtual Real CalculateReading(Real f_distance)
Calculates the proximity reading when the closest occluding object is located as the given distance.
CEmbodiedEntity * m_pcEmbodiedEntity
Reference to embodied entity associated to this sensor.
virtual void Update()
Updates the state of the entity associated to this sensor, if the sensor is currently enabled.
CProximitySensorEquippedEntity * m_pcProximityEntity
Reference to proximity sensor equipped entity associated to this sensor.
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
virtual void Reset()
Resets the sensor to the state it had just after Init().