ARGoS 3
A parallel, multi-engine simulator for swarm robotics
epuck_proximity_default_sensor.cpp
Go to the documentation of this file.
1
6
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
14namespace 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"));
39 m_pcProximityEntity->Enable();
40
41 /* sensor is enabled by default */
42 Enable();
43 }
44 catch(CARGoSException& ex) {
45 THROW_ARGOSEXCEPTION_NESTED("Can't set robot for the proximity default sensor", ex);
46 }
47 }
48
49 /****************************************/
50 /****************************************/
51
53 try {
55 /* Show rays? */
57 /* Parse noise level */
58 Real fNoiseLevel = 0.0f;
59 GetNodeAttributeOrDefault(t_tree, "noise_level", fNoiseLevel, fNoiseLevel);
60 if(fNoiseLevel < 0.0f) {
61 THROW_ARGOSEXCEPTION("Can't specify a negative value for the noise level of the proximity sensor");
62 }
63 else if(fNoiseLevel > 0.0f) {
64 m_bAddNoise = true;
65 m_cNoiseRange.Set(-fNoiseLevel, fNoiseLevel);
66 m_pcRNG = CRandom::CreateRNG("argos");
67 }
68 }
69 catch(CARGoSException& ex) {
70 THROW_ARGOSEXCEPTION_NESTED("Initialization error in default proximity sensor", ex);
71 }
72 }
73
74 /****************************************/
75 /****************************************/
76
78 {
79 /* sensor is disabled--nothing to do */
80 if (IsDisabled()) {
81 return;
82 }
83 /* Ray used for scanning the environment for obstacles */
84 CRay3 cScanningRay;
85 CVector3 cRayStart, cRayEnd;
86 /* Buffers to contain data about the intersection */
88 /* Go through the sensors */
89 for(UInt32 i = 0; i < m_tReadings.size(); ++i) {
90 /* Compute ray for sensor i */
91 cRayStart = m_pcProximityEntity->GetSensor(i).Offset;
92 cRayStart.Rotate(m_pcProximityEntity->GetSensor(i).Anchor.Orientation);
93 cRayStart += m_pcProximityEntity->GetSensor(i).Anchor.Position;
94 cRayEnd = m_pcProximityEntity->GetSensor(i).Offset;
95 cRayEnd += m_pcProximityEntity->GetSensor(i).Direction;
96 cRayEnd.Rotate(m_pcProximityEntity->GetSensor(i).Anchor.Orientation);
97 cRayEnd += m_pcProximityEntity->GetSensor(i).Anchor.Position;
98 cScanningRay.Set(cRayStart,cRayEnd);
99 /* Compute reading */
100 /* Get the closest intersection */
102 cScanningRay,
104 /* There is an intersection */
105 if(m_bShowRays) {
106 m_pcControllableEntity->AddIntersectionPoint(cScanningRay,
107 sIntersection.TOnRay);
108 m_pcControllableEntity->AddCheckedRay(true, cScanningRay);
109 }
110 m_tReadings[i].Value = CalculateReading(cScanningRay.GetDistance(sIntersection.TOnRay));
111 }
112 else {
113 /* No intersection */
114 m_tReadings[i].Value = 0.0f;
115
116 if(m_bShowRays) {
117 m_pcControllableEntity->AddCheckedRay(false, cScanningRay);
118 }
119 }
120 /* Apply noise to the sensor */
121 if(m_bAddNoise)
122 {
123 m_tReadings[i].Value += m_pcRNG->Uniform(m_cNoiseRange);
124 }
125 /* Trunc the reading between 0 and 1 */
126 UNIT.TruncValue(m_tReadings[i].Value);
127 }
128 }
129
130 /****************************************/
131 /****************************************/
132
134 {
135 for(UInt32 i = 0; i < GetReadings().size(); ++i)
136 m_tReadings[i].Value = 0.0f;
137 }
138
139 /****************************************/
140 /****************************************/
141
143 {
144 //return Exp(-f_distance);
145
146 // from the e-puck model by Lorenzo Garattoni and Gianpiero Francesca
147 Real value = 0.0f;
148 if(f_distance <= 0.05) // less than 5cm - the IR readings drop pretty fast
149 {
150 value = 298.701f * pow(f_distance,2) - 36.8961f * f_distance + 1.08212f;
151 }
152 CRange<Real>(0.0f,1.0f).TruncValue(value);
153 return value;
154 }
155
156 /****************************************/
157 /****************************************/
158
160 "epuck_proximity", "default",
161 "Danesh Tarapore [daneshtarapore@gmail.com]",
162 "1.0",
163 "The E-Puck proximity sensor.",
164
165 "This sensor accesses the epuck proximity sensor. For a complete description\n"
166 "of its usage, refer to the ci_epuck_proximity_sensor.h interface. For the XML\n"
167 "configuration, refer to the default proximity sensor.\n",
168
169 "Usable"
170 );
171
172}
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.
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.
This entity is a link to a body in the physics engine.
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
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
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
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
CControllableEntity * m_pcControllableEntity
Reference to controllable entity associated to this sensor.
CRandom::CRNG * m_pcRNG
Random number generator.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
CEmbodiedEntity * m_pcEmbodiedEntity
Reference to embodied entity associated to this sensor.
virtual void Reset()
Resets the sensor to the state it had just after Init().
bool m_bShowRays
Flag to show rays in the simulator.
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 Real CalculateReading(Real f_distance)
Calculates the proximity reading when the closest occluding object is located as the given distance.