epuck_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  /* 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? */
56  GetNodeAttributeOrDefault(t_tree, "show_rays", m_bShowRays, m_bShowRays);
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 */
87  SEmbodiedEntityIntersectionItem sIntersection;
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;
94  cRayEnd = m_pcProximityEntity->GetSensor(i).Offset;
98  cScanningRay.Set(cRayStart,cRayEnd);
99  /* Compute reading */
100  /* Get the closest intersection */
102  cScanningRay,
103  *m_pcEmbodiedEntity)) {
104  /* There is an intersection */
105  if(m_bShowRays) {
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  {
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 }
#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
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
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
bool m_bAddNoise
Whether to add noise or not.
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.