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(nullptr),
26  m_pcBatteryEntity(nullptr),
27  m_pcRNG(nullptr),
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  /* sensor is enabled by default */
62  Enable();
63  }
64 
65  /****************************************/
66  /****************************************/
67 
69  /* sensor is disabled--nothing to do */
70  if (IsDisabled()) {
71  return;
72  }
73  /* Save old charge value (used later for time left estimation) */
74  Real fOldCharge = m_sReading.AvailableCharge;
75  /* Update available charge as seen by the robot */
79  /* Add noise */
80  if(m_bAddNoise) {
82  /* To trunc battery level between 0 and 1 */
84  }
85  /* Update time left */
86  if(m_sReading.AvailableCharge > 0.0) {
87  Real fDiff = fOldCharge - m_sReading.AvailableCharge;
88  if(Abs(fDiff) > 1e-6) {
90  fOldCharge *
92  fDiff;
93  }
94  else {
95  m_sReading.TimeLeft = std::numeric_limits<Real>::infinity();
96  }
97  }
98  else {
99  m_sReading.TimeLeft = 0.0;
100  }
101  }
102 
103  /****************************************/
104  /****************************************/
105 
107  /* TODO */
108  }
109 
110  /****************************************/
111  /****************************************/
112 
114  "battery", "default",
115  "Adhavan Jayabalan [jadhu94@gmail.com]",
116  "1.0",
117  "A generic battery level sensor.",
118 
119  "This sensor returns the current battery level of a robot. This sensor\n"
120  "can be used with any robot, since it accesses only the body component. In\n"
121  "controllers, you must include the ci_battery_sensor.h header.\n\n"
122 
123  "This sensor is enabled by default.\n\n"
124 
125  "REQUIRED XML CONFIGURATION\n\n"
126  " <controllers>\n"
127  " ...\n"
128  " <my_controller ...>\n"
129  " ...\n"
130  " <sensors>\n"
131  " ...\n"
132  " <battery implementation=\"default\" />\n"
133  " ...\n"
134  " </sensors>\n"
135  " ...\n"
136  " </my_controller>\n"
137  " ...\n"
138  " </controllers>\n\n"
139 
140  "OPTIONAL XML CONFIGURATION\n\n"
141 
142  "It is possible to add uniform noise to the sensor, thus matching the\n"
143  "characteristics of a real robot better. You can add noise through the\n"
144  "attribute 'noise_range' as follows:\n\n"
145  " <controllers>\n"
146  " ...\n"
147  " <my_controller ...>\n"
148  " ...\n"
149  " <sensors>\n"
150  " ...\n"
151  " <battery implementation=\"default\"\n"
152  " noise_range=\"-0.3:0.4\" />\n"
153  " ...\n"
154  " </sensors>\n"
155  " ...\n"
156  " </my_controller>\n"
157  " ...\n"
158  " </controllers>\n\n",
159 
160  "Usable"
161  );
162 
163 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
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.
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition: general.h:25
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.
void Enable()
Enables the entity.
Definition: entity.h:265
static Real GetSimulationClockTick()
Returns the simulation clock tick.
The exception that wraps all errors in ARGoS.
void TruncValue(T &t_value) const
Definition: range.h:97
T GetSpan() const
Definition: range.h:64
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
Real AvailableCharge
Available charge, between 0 and 1.
Real TimeLeft
Time left in seconds.
CBatteryEquippedEntity * m_pcBatteryEntity
Reference to battery sensor equipped entity associated to this sensor.
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.
CRange< Real > m_cNoiseRange
Noise range on battery level.
CEmbodiedEntity * m_pcEmbodiedEntity
Reference to embodied entity associated to this sensor.
bool m_bAddNoise
Whether to add noise or not.
CRandom::CRNG * m_pcRNG
Random number generator.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
virtual void Update()
Updates the state of the entity associated to this sensor, if the sensor is currently enabled.