battery_equipped_entity.cpp
Go to the documentation of this file.
1 
7 #include <argos3/core/simulator/simulator.h>
8 #include <argos3/core/simulator/space/space.h>
9 #include <argos3/core/simulator/entity/composable_entity.h>
10 
11 namespace argos {
12 
13  /****************************************/
14  /****************************************/
15 
17  CEntity(pc_parent),
18  m_fFullCharge(1.0),
19  m_fAvailableCharge(m_fFullCharge),
20  m_pcDischargeModel(nullptr) {
22  Disable();
23  }
24 
25  /****************************************/
26  /****************************************/
27 
29  const std::string &str_id,
30  CBatteryDischargeModel* pc_discharge_model,
31  Real f_start_charge,
32  Real f_full_charge) :
33  CEntity(pc_parent, str_id),
34  m_fFullCharge(f_full_charge),
35  m_fAvailableCharge(f_start_charge),
36  m_pcDischargeModel(nullptr) {
37  SetDischargeModel(pc_discharge_model);
38  Disable();
39  }
40 
41  /****************************************/
42  /****************************************/
43 
45  const std::string& str_id,
46  const std::string& str_discharge_model,
47  Real f_start_charge,
48  Real f_full_charge) :
49  CEntity(pc_parent, str_id),
50  m_fFullCharge(f_full_charge),
51  m_fAvailableCharge(f_start_charge),
52  m_pcDischargeModel(nullptr) {
53  SetDischargeModel(str_discharge_model);
54  Disable();
55  }
56 
57  /****************************************/
58  /****************************************/
59 
61  /* Get rid of battery discharge model */
62  delete m_pcDischargeModel;
63  }
64 
65  /****************************************/
66  /****************************************/
67 
69  try {
70  CEntity::Init(t_tree);
71  /* Get initial battery level */
72  std::string strDischargeModel = "time";
73  GetNodeAttributeOrDefault(t_tree, "discharge_model", strDischargeModel, strDischargeModel);
74  SetDischargeModel(strDischargeModel);
75  m_pcDischargeModel->Init(t_tree);
76  /* Get initial battery charge */
78  /* Get full battery charge */
79  GetNodeAttributeOrDefault(t_tree, "full_charge", m_fFullCharge, m_fFullCharge);
80  }
81  catch(CARGoSException& ex) {
82  THROW_ARGOSEXCEPTION_NESTED("Error initializing the battery sensor equipped entity \"" << GetId() << "\"", ex);
83  }
84  }
85 
86  /****************************************/
87  /****************************************/
88 
90  if(CSimulator::GetInstance().GetSpace().GetSimulationClock() > 0) {
92  /* Call the discharge model */
93  (*m_pcDischargeModel)();
94  }
95  }
96 
97  /****************************************/
98  /****************************************/
99 
102  m_pcDischargeModel = pc_model;
105  }
106 
107  /****************************************/
108  /****************************************/
109 
110  void CBatteryEquippedEntity::SetDischargeModel(const std::string& str_model) {
112  if(str_model != "") {
115  }
116  }
117 
118  /****************************************/
119  /****************************************/
120 
122  m_pcBattery(nullptr) {
123  }
124 
125  /****************************************/
126  /****************************************/
127 
129  }
130 
131  /****************************************/
132  /****************************************/
133 
135  m_pcBattery = pc_battery;
136  }
137 
138  /****************************************/
139  /****************************************/
140 
142  GetNodeAttributeOrDefault(t_tree, "delta", m_fDelta, m_fDelta);
143  }
144 
145  /****************************************/
146  /****************************************/
147 
149  if(m_pcBattery->GetAvailableCharge() > 0.0) {
151  Max<Real>(0.0,
153  }
154  }
155 
156  /****************************************/
157  /****************************************/
158 
160  GetNodeAttributeOrDefault(t_tree, "pos_delta", m_fPosFactor, m_fPosFactor);
162  }
163 
164  /****************************************/
165  /****************************************/
166 
168  try {
169  /* Execute default logic */
171  /* Get a hold of the body and anchor of the entity that contains the battery */
172  CEntity* pcRoot = &pc_battery->GetRootEntity();
173  auto* cComp = dynamic_cast<CComposableEntity*>(pcRoot);
174  if(cComp != nullptr) {
175  auto& cBody = cComp->GetComponent<CEmbodiedEntity>("body");
176  m_psAnchor = &cBody.GetOriginAnchor();
178  }
179  else {
180  THROW_ARGOSEXCEPTION("Root entity is not composable");
181  }
182  }
183  catch(CARGoSException& ex) {
184  THROW_ARGOSEXCEPTION_NESTED("While setting body for battery model \"motion\"", ex);
185  }
186  }
187 
188  /****************************************/
189  /****************************************/
190 
192  if(m_pcBattery->GetAvailableCharge() > 0.0) {
193  /* Calculate delta position */
194  Real fDeltaPos = Distance(m_psAnchor->Position,
196  /* Calculate delta orientation */
197  CQuaternion cDeltaOrient =
200  CRadians cDeltaAngle;
201  CVector3 cDeltaAxis;
202  cDeltaOrient.ToAngleAxis(cDeltaAngle, cDeltaAxis);
203  /* Calculate new level */
205  Max<Real>(
206  0.0,
208  m_fPosFactor * fDeltaPos -
209  m_fOrientFactor * cDeltaAngle.GetValue()
210  ));
211  /* Save position for next step */
214  }
215  }
216 
217  /****************************************/
218  /****************************************/
219 
221  GetNodeAttributeOrDefault(t_tree, "time_factor", m_fDelta, m_fDelta);
222  GetNodeAttributeOrDefault(t_tree, "pos_factor", m_fPosFactor, m_fPosFactor);
223  GetNodeAttributeOrDefault(t_tree, "orient_factor", m_fOrientFactor, m_fOrientFactor);
224  }
225 
226  /****************************************/
227  /****************************************/
228 
230  try {
231  /* Execute default logic */
233  /* Get a hold of the body and anchor of the entity that contains the battery */
234  CEntity* pcRoot = &pc_battery->GetRootEntity();
235  auto* cComp = dynamic_cast<CComposableEntity*>(pcRoot);
236  if(cComp != nullptr) {
237  auto& cBody = cComp->GetComponent<CEmbodiedEntity>("body");
238  m_psAnchor = &cBody.GetOriginAnchor();
240  }
241  else {
242  THROW_ARGOSEXCEPTION("Root entity is not composable");
243  }
244  }
245  catch(CARGoSException& ex) {
246  THROW_ARGOSEXCEPTION_NESTED("While setting body for battery model \"time_motion\"", ex);
247  }
248  }
249 
250  /****************************************/
251  /****************************************/
252 
254  if(m_pcBattery->GetAvailableCharge() > 0.0) {
255  /* Calculate delta position */
256  Real fDeltaPos = Distance(m_psAnchor->Position,
258  /* Calculate delta orientation */
259  CQuaternion cDeltaOrient =
262  CRadians cDeltaAngle;
263  CVector3 cDeltaAxis;
264  cDeltaOrient.ToAngleAxis(cDeltaAngle, cDeltaAxis);
265  /* Calculate new level */
267  Max<Real>(
268  0.0,
270  m_fDelta -
271  m_fPosFactor * fDeltaPos -
272  m_fOrientFactor * cDeltaAngle.GetValue()
273  ));
274  /* Save position for next step */
277  }
278  }
279 
280  /****************************************/
281  /****************************************/
282 
287 
288  /****************************************/
289  /****************************************/
290 
291 }
#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.
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
REGISTER_BATTERY_DISCHARGE_MODEL(CBatteryDischargeModelTime, "time")
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.
Real Distance(const CVector2 &c_v1, const CVector2 &c_v2)
Computes the distance between the passed vectors.
Definition: vector2.h:463
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CEntity)
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.
The basic entity type.
Definition: entity.h:90
void Disable()
Disables the entity.
Definition: entity.h:275
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
CEntity & GetRootEntity()
Returns the root entity containing this entity.
Definition: entity.cpp:115
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:40
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
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition: simulator.cpp:78
The exception that wraps all errors in ARGoS.
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
Real GetValue() const
Returns the value in radians.
Definition: angles.h:111
CQuaternion Inverse() const
Definition: quaternion.h:98
void ToAngleAxis(CRadians &c_angle, CVector3 &c_vector) const
Definition: quaternion.h:143
A 3D vector class.
Definition: vector3.h:31
static TYPE * New(const std::string &str_label)
Creates a new object of type TYPE
Definition: factory_impl.h:48
Real m_fAvailableCharge
Available charge.
void SetAvailableCharge(Real f_available_charge)
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
CBatteryEquippedEntity(CComposableEntity *pc_parent)
CBatteryDischargeModel * m_pcDischargeModel
Discharge model.
void SetDischargeModel(CBatteryDischargeModel *pc_model)
virtual void Update()
Updates the state of this entity.
The discharge model dictates how the battery discharges over time.
CBatteryEquippedEntity * m_pcBattery
virtual void SetBattery(CBatteryEquippedEntity *pc_battery)
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
A battery discharge model based only on time.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
A battery discharge model based only on motion.
virtual void SetBattery(CBatteryEquippedEntity *pc_battery)
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
A battery discharge model in which the charge decreases with both time and motion.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
virtual void SetBattery(CBatteryEquippedEntity *pc_battery)