ground_rotzonly_sensor.cpp
Go to the documentation of this file.
1 
7 #include <argos3/core/simulator/simulator.h>
8 #include <argos3/core/simulator/entity/composable_entity.h>
9 #include <argos3/core/simulator/entity/embodied_entity.h>
10 #include <argos3/core/simulator/entity/floor_entity.h>
11 #include <argos3/plugins/simulator/entities/ground_sensor_equipped_entity.h>
12 
13 #include "ground_rotzonly_sensor.h"
14 
15 namespace argos {
16 
17  /****************************************/
18  /****************************************/
19 
20  static CRange<Real> UNIT(0.0f, 1.0f);
21 
22  /****************************************/
23  /****************************************/
24 
26  m_pcEmbodiedEntity(nullptr),
27  m_pcFloorEntity(nullptr),
28  m_pcGroundSensorEntity(nullptr),
29  m_pcRNG(nullptr),
30  m_bAddNoise(false),
31  m_cSpace(CSimulator::GetInstance().GetSpace()) {}
32 
33  /****************************************/
34  /****************************************/
35 
37  m_pcEmbodiedEntity = &(c_entity.GetComponent<CEmbodiedEntity>("body"));
38  m_pcGroundSensorEntity = &(c_entity.GetComponent<CGroundSensorEquippedEntity>("ground_sensors"));
41  }
42 
43  /****************************************/
44  /****************************************/
45 
47  try {
48  CCI_GroundSensor::Init(t_tree);
49  /* Parse noise level */
50  Real fNoiseLevel = 0.0f;
51  GetNodeAttributeOrDefault(t_tree, "noise_level", fNoiseLevel, fNoiseLevel);
52  if(fNoiseLevel < 0.0f) {
53  THROW_ARGOSEXCEPTION("Can't specify a negative value for the noise level of the ground sensor");
54  }
55  else if(fNoiseLevel > 0.0f) {
56  m_bAddNoise = true;
57  m_cNoiseRange.Set(-fNoiseLevel, fNoiseLevel);
58  m_pcRNG = CRandom::CreateRNG("argos");
59  }
61  }
62  catch(CARGoSException& ex) {
63  THROW_ARGOSEXCEPTION_NESTED("Initialization error in rotzonly ground sensor", ex);
64  }
65  /* sensor is enabled by default */
66  Enable();
67  }
68 
69  /****************************************/
70  /****************************************/
71 
73  /* sensor is disabled--nothing to do */
74  if (IsDisabled()) {
75  return;
76  }
77  /*
78  * We make the assumption that the robot is rotated only wrt to Z
79  */
80  CRadians cRotZ, cRotY, cRotX;
81  /* Set robot center */
82  CVector2 cCenterPos;
83  /* Position of sensor on the ground after rototranslation */
84  CVector2 cSensorPos;
85  /* Go through the sensors */
86  for(UInt32 i = 0; i < m_tReadings.size(); ++i) {
88  /* Get anchor position and orientation */
89  cCenterPos.Set(sSens.Anchor.Position.GetX(),
90  sSens.Anchor.Position.GetY());
91  sSens.Anchor.Orientation.ToEulerAngles(cRotZ, cRotY, cRotX);
92  /* Calculate sensor position on the ground */
93  cSensorPos = sSens.Offset;
94  cSensorPos.Rotate(cRotZ);
95  cSensorPos += cCenterPos;
96  /* Get the color */
97  const CColor& cColor = m_pcFloorEntity->GetColorAtPoint(cSensorPos.GetX(),
98  cSensorPos.GetY());
99  /* Set the reading */
100  m_tReadings[i] = cColor.ToGrayScale() / 255.0f;
101  /* Apply noise to the sensor */
102  if(m_bAddNoise) {
104  }
105  /* Is it a BW sensor? */
107  /* Yes, set 0 or 1 */
108  m_tReadings[i] = m_tReadings[i] < 0.5f ? 0.0f : 1.0f;
109  }
110  else {
111  /* No, clamp the reading between 0 and 1 */
112  UNIT.TruncValue(m_tReadings[i]);
113  }
114  }
115  }
116 
117  /****************************************/
118  /****************************************/
119 
121  for(UInt32 i = 0; i < GetReadings().size(); ++i) {
122  m_tReadings[i] = 0.0f;
123  }
124  }
125 
126  /****************************************/
127  /****************************************/
128 
130  "ground", "rot_z_only",
131  "Carlo Pinciroli [ilpincy@gmail.com]",
132  "1.0",
133  "A generic ground sensor (optimized for 2D).",
134 
135  "This sensor accesses a set of ground sensors. The sensors all return a value\n"
136  "between 0 and 1, where 0 means black and 1 means white. Depending on the type\n"
137  "of ground sensor, readings can either take 0 or 1 as value (bw sensors) or a\n"
138  "value in between (grayscale sensors). In controllers, you must include the\n"
139  "ci_ground_sensor.h header.\n\n"
140 
141  "This sensor is enabled by default.\n\n"
142 
143  "REQUIRED XML CONFIGURATION\n\n"
144  " <controllers>\n"
145  " ...\n"
146  " <my_controller ...>\n"
147  " ...\n"
148  " <sensors>\n"
149  " ...\n"
150  " <ground implementation=\"rot_z_only\" />\n"
151  " ...\n"
152  " </sensors>\n"
153  " ...\n"
154  " </my_controller>\n"
155  " ...\n"
156  " </controllers>\n\n"
157 
158  "OPTIONAL XML CONFIGURATION\n\n"
159 
160  "It is possible to add uniform noise to the sensors, thus matching the\n"
161  "characteristics of a real robot better. This can be done with the attribute\n"
162  "\"noise_level\", whose allowed range is in [-1,1] and is added to the calculated\n"
163  "reading. The final sensor reading is always normalized in the [0-1] range.\n\n"
164  " <controllers>\n"
165  " ...\n"
166  " <my_controller ...>\n"
167  " ...\n"
168  " <sensors>\n"
169  " ...\n"
170  " <ground implementation=\"rot_z_only\"\n"
171  " noise_level=\"0.1\" />\n"
172  " ...\n"
173  " </sensors>\n"
174  " ...\n"
175  " </my_controller>\n"
176  " ...\n"
177  " </controllers>\n\n",
178 
179  "Usable"
180  );
181 
182 }
#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
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.
This entity is a link to a body in the physics engine.
CColor GetColorAtPoint(Real f_x, Real f_y)
Returns the color at the given point.
Definition: floor_entity.h:99
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
CFloorEntity & GetFloorEntity()
Returns the floor entity.
Definition: space.h:239
The exception that wraps all errors in ARGoS.
The basic color type.
Definition: color.h:25
Real ToGrayScale() const
Returns the color in grayscale.
Definition: color.h:68
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
void ToEulerAngles(CRadians &c_z_angle, CRadians &c_y_angle, CRadians &c_x_angle) const
Definition: quaternion.h:172
void TruncValue(T &t_value) const
Definition: range.h:97
void Set(const T &t_min, const T &t_max)
Definition: range.h:68
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 2D vector class.
Definition: vector2.h:27
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:110
CVector2 & Rotate(const CRadians &c_angle)
Rotates this vector by the wanted angle.
Definition: vector2.h:194
void Set(Real f_x, Real f_y)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:127
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:94
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:105
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:121
std::vector< Real > m_tReadings
const std::vector< Real > & GetReadings() const
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().
CGroundSensorEquippedEntity * m_pcGroundSensorEntity
Reference to ground sensor equipped entity associated to this sensor.
virtual void Update()
Updates the state of the entity associated to this sensor, if the sensor is currently enabled.
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
CFloorEntity * m_pcFloorEntity
Reference to floor entity.
bool m_bAddNoise
Whether to add noise or not.
CSpace & m_cSpace
Reference to the space.
CRange< Real > m_cNoiseRange
Noise range.