battery_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/battery_equipped_entity.h>
11 
12 #include "battery_default_sensor.h"
13 
14 namespace argos {
15 
16  /****************************************/
17  /****************************************/
18 
19  static CRange<Real> UNIT(0.0f, 1.0f);
20 
21  /****************************************/
22  /****************************************/
23 
25  m_pcEmbodiedEntity(NULL),
26  m_pcBatteryEntity(NULL),
27  m_pcRNG(NULL),
28  m_bAddNoise(false) {}
29 
30  /****************************************/
31  /****************************************/
32 
34  try {
35  m_pcEmbodiedEntity = &(c_entity.GetComponent<CEmbodiedEntity>("body"));
36  m_pcBatteryEntity = &(c_entity.GetComponent<CBatteryEquippedEntity>("battery"));
38  }
39  catch(CARGoSException& ex) {
40  THROW_ARGOSEXCEPTION_NESTED("Can't set robot for the battery default sensor", ex);
41  }
42  }
43 
44  /****************************************/
45  /****************************************/
46 
48  try {
49  /* Execute standard logic */
51  /* Parse noise range */
52  GetNodeAttributeOrDefault(t_tree, "noise_range", m_cNoiseRange, m_cNoiseRange);
53  if(m_cNoiseRange.GetSpan() != 0) {
54  m_bAddNoise = true;
55  m_pcRNG = CRandom::CreateRNG("argos");
56  }
57  }
58  catch(CARGoSException& ex) {
59  THROW_ARGOSEXCEPTION_NESTED("Initialization error in default battery sensor", ex);
60  }
61  }
62 
63  /****************************************/
64  /****************************************/
65 
67  /* Save old charge value (used later for time left estimation) */
68  Real fOldCharge = m_sReading.AvailableCharge;
69  /* Update available charge as seen by the robot */
73  /* Add noise */
74  if(m_bAddNoise) {
76  /* To trunc battery level between 0 and 1 */
78  }
79  /* Update time left */
80  Real fDiff = fOldCharge - m_sReading.AvailableCharge;
81  if(Abs(fDiff) > 1e-6) {
83  fOldCharge *
85  fDiff;
86  }
87  else {
88  m_sReading.TimeLeft = std::numeric_limits<Real>::infinity();
89  }
90  }
91 
92  /****************************************/
93  /****************************************/
94 
96  /* TODO */
97  }
98 
99  /****************************************/
100  /****************************************/
101 
103  "battery", "default",
104  "Adhavan Jayabalan [jadhu94@gmail.com]",
105  "1.0",
106  "A generic battery level sensor.",
107  "This sensor returns the current battery level of a robot. This sensor\n"
108  "can be used with any robot, since it accesses only the body component. In\n"
109  "controllers, you must include the ci_battery_sensor.h header.\n\n"
110  "REQUIRED XML CONFIGURATION\n\n"
111  " <controllers>\n"
112  " ...\n"
113  " <my_controller ...>\n"
114  " ...\n"
115  " <sensors>\n"
116  " ...\n"
117  " <battery implementation=\"default\" />\n"
118  " ...\n"
119  " </sensors>\n"
120  " ...\n"
121  " </my_controller>\n"
122  " ...\n"
123  " </controllers>\n\n"
124  "OPTIONAL XML CONFIGURATION\n\n"
125  "It is possible to add uniform noise to the sensor, thus matching the\n"
126  "characteristics of a real robot better. You can add noise through the\n"
127  "attribute 'noise_range' as follows:\n\n"
128  " <controllers>\n"
129  " ...\n"
130  " <my_controller ...>\n"
131  " ...\n"
132  " <sensors>\n"
133  " ...\n"
134  " <battery implementation=\"default\"\n"
135  " noise_range=\"-0.3:0.4\" />\n"
136  " ...\n"
137  " </sensors>\n"
138  " ...\n"
139  " </my_controller>\n"
140  " ...\n"
141  " </controllers>\n\n"
142  "OPTIONAL XML CONFIGURATION\n\n"
143  "None.\n",
144  "Usable"
145  );
146 
147 }
CEmbodiedEntity * m_pcEmbodiedEntity
Reference to embodied entity associated to this sensor.
CRandom::CRNG * m_pcRNG
Random number generator.
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.
virtual void Update()
Updates the state of the entity associated to this sensor.
void Enable()
Enables the entity.
Definition: entity.h:265
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
CRange< Real > m_cNoiseRange
Noise range on battery level.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
void TruncValue(T &t_value) const
Definition: range.h:97
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition: general.h:25
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
This entity is a link to a body in the physics engine.
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception...
Real AvailableCharge
Available charge, between 0 and 1.
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
Definition: rng.cpp:87
Basic class for an entity that contains other entities.
CBatteryEquippedEntity * m_pcBatteryEntity
Reference to battery sensor equipped entity associated to this sensor.
virtual void Init(TConfigurationNode &t_node)
Initializes the sensor from the XML configuration tree.
Definition: ci_sensor.h:54
The exception that wraps all errors in ARGoS.
Real TimeLeft
Time left in seconds.
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
Definition: rng.cpp:326
bool m_bAddNoise
Whether to add noise or not.
static Real GetSimulationClockTick()
Returns the simulation clock tick.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
virtual void Reset()
Resets the sensor to the state it had just after Init().
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
T GetSpan() const
Definition: range.h:64
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
REGISTER_SENSOR(CEyeBotLightRotZOnlySensor,"eyebot_light","rot_z_only","Carlo Pinciroli [ilpincy@gmail.com]","1.0","The eye-bot light sensor (optimized for 2D).","This sensor accesses a set of light sensors. The sensors all return a value\n""between 0 and 1, where 0 means nothing within range and 1 means the perceived\n""light saturates the sensor. Values between 0 and 1 depend on the distance of\n""the perceived light. Each reading R is calculated with R=(I/x)^2, where x is the\n""distance between a sensor and the light, and I is the reference intensity of the\n""perceived light. The reference intensity corresponds to the minimum distance at\n""which the light saturates a sensor. The reference intensity depends on the\n""individual light, and it is set with the \"intensity\" attribute of the light\n""entity. In case multiple lights are present in the environment, each sensor\n""reading is calculated as the sum of the individual readings due to each light.\n""In other words, light wave interference is not taken into account. In\n""controllers, you must include the ci_light_sensor.h header.\n\n""REQUIRED XML CONFIGURATION\n\n"" <controllers>\n"" ...\n"" <my_controller ...>\n"" ...\n"" <sensors>\n"" ...\n"" <eyebot_light implementation=\"rot_z_only\" />\n"" ...\n"" </sensors>\n"" ...\n"" </my_controller>\n"" ...\n"" </controllers>\n\n""OPTIONAL XML CONFIGURATION\n\n""It is possible to draw the rays shot by the light sensor in the OpenGL\n""visualization. This can be useful for sensor debugging but also to understand\n""what's wrong in your controller. In OpenGL, the rays are drawn in cyan when\n""they are not obstructed and in purple when they are. In case a ray is\n""obstructed, a black dot is drawn where the intersection occurred.\n""To turn this functionality on, add the attribute \"show_rays\" as in this\n""example:\n\n"" <controllers>\n"" ...\n"" <my_controller ...>\n"" ...\n"" <sensors>\n"" ...\n"" <eyebot_light implementation=\"rot_z_only\"\n"" show_rays=\"true\" />\n"" ...\n"" </sensors>\n"" ...\n"" </my_controller>\n"" ...\n"" </controllers>\n\n""It is possible to add uniform noise to the sensors, thus matching the\n""characteristics of a real robot better. This can be done with the attribute\n""\"noise_level\", whose allowed range is in [-1,1] and is added to the calculated\n""reading. The final sensor reading is always normalized in the [0-1] range.\n\n"" <controllers>\n"" ...\n"" <my_controller ...>\n"" ...\n"" <sensors>\n"" ...\n"" <eyebot_light implementation=\"rot_z_only\"\n"" noise_level=\"0.1\" />\n"" ...\n"" </sensors>\n"" ...\n"" </my_controller>\n"" ...\n"" </controllers>\n\n""OPTIONAL XML CONFIGURATION\n\n""None.\n","Usable")