ARGoS 3
A parallel, multi-engine simulator for swarm robotics
light_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/light_entity.h>
11#include <argos3/plugins/simulator/entities/light_sensor_equipped_entity.h>
12
14
15namespace argos {
16
17 /****************************************/
18 /****************************************/
19
20 static CRange<Real> UNIT(0.0f, 1.0f);
21
22 /****************************************/
23 /****************************************/
24
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_pcControllableEntity = &(c_entity.GetComponent<CControllableEntity>("controller"));
37 m_pcLightEntity = &(c_entity.GetComponent<CLightSensorEquippedEntity>("light_sensors"));
39 }
40 catch(CARGoSException& ex) {
41 THROW_ARGOSEXCEPTION_NESTED("Can't set robot for the light default sensor", ex);
42 }
43 }
44
45 /****************************************/
46 /****************************************/
47
49 try {
51 /* Show rays? */
53 /* Parse noise level */
54 Real fNoiseLevel = 0.0f;
55 GetNodeAttributeOrDefault(t_tree, "noise_level", fNoiseLevel, fNoiseLevel);
56 if(fNoiseLevel < 0.0f) {
57 THROW_ARGOSEXCEPTION("Can't specify a negative value for the noise level of the light sensor");
58 }
59 else if(fNoiseLevel > 0.0f) {
60 m_bAddNoise = true;
61 m_cNoiseRange.Set(-fNoiseLevel, fNoiseLevel);
62 m_pcRNG = CRandom::CreateRNG("argos");
63 }
65 }
66 catch(CARGoSException& ex) {
67 THROW_ARGOSEXCEPTION_NESTED("Initialization error in default light sensor", ex);
68 }
69 /* sensor is enabled by default */
70 Enable();
71 }
72
73 /****************************************/
74 /****************************************/
75
77 /* sensor is disabled--nothing to do */
78 if (IsDisabled()) {
79 return;
80 }
81 /* Erase readings */
82 for(size_t i = 0; i < m_tReadings.size(); ++i) m_tReadings[i] = 0.0f;
83 /* Ray used for scanning the environment for obstacles */
84 CRay3 cScanningRay;
85 CVector3 cRayStart;
86 CVector3 cSensorToLight;
87 /* Buffers to contain data about the intersection */
89 /* Get the map of light entities */
90 auto itLights = m_cSpace.GetEntityMapPerTypePerId().find("light");
91 if (itLights != m_cSpace.GetEntityMapPerTypePerId().end()) {
92 CSpace::TMapPerType& mapLights = itLights->second;
93 /* Go through the sensors */
94 for(UInt32 i = 0; i < m_tReadings.size(); ++i) {
95 /* Set ray start */
96 cRayStart = m_pcLightEntity->GetSensor(i).Position;
99 /* Go through all the light entities */
100 for(auto it = mapLights.begin();
101 it != mapLights.end();
102 ++it) {
103 /* Get a reference to the light */
104 CLightEntity& cLight = *any_cast<CLightEntity*>(it->second);
105 /* Consider the light only if it has non zero intensity */
106 if(cLight.GetIntensity() > 0.0f) {
107 /* Set ray end to light position */
108 cScanningRay.Set(cRayStart, cLight.GetPosition());
109 /* Check occlusions */
111 cScanningRay)) {
112 /* No occlusion, the light is visibile */
113 if(m_bShowRays) {
114 m_pcControllableEntity->AddCheckedRay(false, cScanningRay);
115 }
116 /* Calculate reading */
117 cScanningRay.ToVector(cSensorToLight);
118 m_tReadings[i] += CalculateReading(cSensorToLight.Length(),
119 cLight.GetIntensity());
120 }
121 else {
122 /* There is an occlusion, the light is not visible */
123 if(m_bShowRays) {
125 sIntersection.TOnRay);
126 m_pcControllableEntity->AddCheckedRay(true, cScanningRay);
127 }
128 }
129 }
130 }
131 /* Apply noise to the sensor */
132 if(m_bAddNoise) {
134 }
135 /* Trunc the reading between 0 and 1 */
136 UNIT.TruncValue(m_tReadings[i]);
137 }
138 }
139 else {
140 /* There are no lights in the environment */
141 if(m_bAddNoise) {
142 /* Go through the sensors */
143 for(UInt32 i = 0; i < m_tReadings.size(); ++i) {
144 /* Apply noise to the sensor */
146 /* Trunc the reading between 0 and 1 */
147 UNIT.TruncValue(m_tReadings[i]);
148 }
149 }
150 }
151 }
152
153 /****************************************/
154 /****************************************/
155
157 for(UInt32 i = 0; i < GetReadings().size(); ++i) {
158 m_tReadings[i] = 0.0f;
159 }
160 }
161
162 /****************************************/
163 /****************************************/
164
166 return (f_intensity * f_intensity) / (f_distance * f_distance);
167 }
168
169 /****************************************/
170 /****************************************/
171
173 "light", "default",
174 "Carlo Pinciroli [ilpincy@gmail.com]",
175 "1.0",
176 "A generic light sensor.",
177
178 "This sensor accesses a set of light sensors. The sensors all return a value\n"
179 "between 0 and 1, where 0 means nothing within range and 1 means the perceived\n"
180 "light saturates the sensor. Values between 0 and 1 depend on the distance of\n"
181 "the perceived light. Each reading R is calculated with R=(I/x)^2, where x is the\n"
182 "distance between a sensor and the light, and I is the reference intensity of the\n"
183 "perceived light. The reference intensity corresponds to the minimum distance at\n"
184 "which the light saturates a sensor. The reference intensity depends on the\n"
185 "individual light, and it is set with the \"intensity\" attribute of the light\n"
186 "entity. In case multiple lights are present in the environment, each sensor\n"
187 "reading is calculated as the sum of the individual readings due to each light.\n"
188 "In other words, light wave interference is not taken into account. In\n"
189 "controllers, you must include the ci_light_sensor.h header.\n\n"
190
191 "This sensor is enabled by default.\n\n"
192
193 "REQUIRED XML CONFIGURATION\n\n"
194 " <controllers>\n"
195 " ...\n"
196 " <my_controller ...>\n"
197 " ...\n"
198 " <sensors>\n"
199 " ...\n"
200 " <light implementation=\"default\" />\n"
201 " ...\n"
202 " </sensors>\n"
203 " ...\n"
204 " </my_controller>\n"
205 " ...\n"
206 " </controllers>\n\n"
207
208 "OPTIONAL XML CONFIGURATION\n\n"
209
210 "It is possible to draw the rays shot by the light sensor in the OpenGL\n"
211 "visualization. This can be useful for sensor debugging but also to understand\n"
212 "what's wrong in your controller. In OpenGL, the rays are drawn in cyan when\n"
213 "they are not obstructed and in purple when they are. In case a ray is\n"
214 "obstructed, a black dot is drawn where the intersection occurred.\n"
215 "To turn this functionality on, add the attribute \"show_rays\" as in this\n"
216 "example:\n\n"
217 " <controllers>\n"
218 " ...\n"
219 " <my_controller ...>\n"
220 " ...\n"
221 " <sensors>\n"
222 " ...\n"
223 " <light implementation=\"default\"\n"
224 " show_rays=\"true\" />\n"
225 " ...\n"
226 " </sensors>\n"
227 " ...\n"
228 " </my_controller>\n"
229 " ...\n"
230 " </controllers>\n\n"
231
232 "It is possible to add uniform noise to the sensors, thus matching the\n"
233 "characteristics of a real robot better. This can be done with the attribute\n"
234 "\"noise_level\", whose allowed range is in [-1,1] and is added to the calculated\n"
235 "reading. The final sensor reading is always normalized in the [0-1] range.\n\n"
236
237 " <controllers>\n"
238 " ...\n"
239 " <my_controller ...>\n"
240 " ...\n"
241 " <sensors>\n"
242 " ...\n"
243 " <light implementation=\"default\"\n"
244 " noise_level=\"0.1\" />\n"
245 " ...\n"
246 " </sensors>\n"
247 " ...\n"
248 " </my_controller>\n"
249 " ...\n"
250 " </controllers>\n\n"
251
252 "OPTIMIZATION HINTS\n\n"
253
254 "1. For small swarms, enabling the light sensor (and therefore causing ARGoS to\n"
255 " update its readings each timestep) unconditionally does not impact performance too\n"
256 " much. For large swarms, it can impact performance, and selectively\n"
257 " enabling/disabling the light sensor according to when each individual robot needs it\n"
258 " (e.g., only when it is returning to the nest from foraging) can increase performance\n"
259 " by only requiring ARGoS to update the readings for a robot on the timesteps will be\n"
260 " used.\n",
261
262 "Usable"
263 );
264
265}
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
float Real
Collects all ARGoS code.
Definition datatypes.h:39
#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.
#define REGISTER_SENSOR(CLASSNAME, LABEL, IMPLEMENTATION, AUTHOR, VERSION, BRIEF_DESCRIPTION, LONG_DESCRIPTION, STATUS)
Registers a new sensor model inside ARGoS.
Definition sensor.h:63
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.
T * any_cast(CAny *pc_any)
Performs a cast on the any type to the desired type, when the any type is passed by non-const pointer...
Definition any.h:148
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.
const CVector3 & GetPosition() const
CQuaternion Orientation
The orientation of the anchor wrt the global coordinate system.
CVector3 Position
The position of the anchor wrt the global coordinate system.
The core class of ARGOS.
Definition simulator.h:62
std::map< std::string, CAny, std::less< std::string > > TMapPerType
A map of entities indexed by type description.
Definition space.h:56
TMapPerTypePerId & GetEntityMapPerTypePerId()
Returns a nested map of entities, ordered by type and by id.
Definition space.h:205
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
void Set(const CVector3 &c_start, const CVector3 &c_end)
Definition ray3.h:67
CVector3 & ToVector(CVector3 &c_buffer) const
Definition ray3.h:100
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
Real Length() const
Returns the length of this vector.
Definition vector3.h:227
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
CSpace & m_cSpace
Reference to the space.
virtual void Reset()
Resets the sensor to the state it had just after Init().
CRange< Real > m_cNoiseRange
Noise range.
virtual void Update()
Updates the state of the entity associated to this sensor, if the sensor is currently enabled.
CRandom::CRNG * m_pcRNG
Random number generator.
CLightSensorEquippedEntity * m_pcLightEntity
Reference to light sensor equipped entity associated to this sensor.
bool m_bAddNoise
Whether to add noise or not.
CControllableEntity * m_pcControllableEntity
Reference to controllable entity associated to this sensor.
bool m_bShowRays
Flag to show rays in the simulator.
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
virtual Real CalculateReading(Real f_distance, Real f_intensity)
Calculates the light reading resulting from a light source at the given distance.
Real GetIntensity() const