16
16
*/
17
17
18
18
#include < ignition/msgs/battery_state.pb.h>
19
+ #include < ignition/msgs/boolean.pb.h>
19
20
20
- #include < string >
21
+ #include < atomic >
21
22
#include < functional>
23
+ #include < string>
22
24
23
- #include < ignition/plugin/Register.hh>
24
- #include < ignition/transport/Node.hh>
25
-
26
- #include < ignition/common/Util.hh>
27
25
#include < ignition/common/Battery.hh>
28
26
#include < ignition/common/Profiler.hh>
27
+ #include < ignition/common/Util.hh>
28
+
29
+ #include < ignition/plugin/Register.hh>
30
+ #include < ignition/transport/Node.hh>
29
31
30
32
#include < sdf/Element.hh>
31
33
#include < sdf/Physics.hh>
32
34
#include < sdf/Root.hh>
33
35
#include < sdf/World.hh>
34
36
35
- #include " ignition/gazebo/Model.hh"
36
37
#include " ignition/gazebo/components/BatterySoC.hh"
37
- #include " ignition/gazebo/components/Name.hh"
38
- #include " ignition/gazebo/components/World.hh"
38
+ #include " ignition/gazebo/components/Joint.hh"
39
39
#include " ignition/gazebo/components/JointForceCmd.hh"
40
40
#include " ignition/gazebo/components/JointVelocityCmd.hh"
41
+ #include " ignition/gazebo/components/Name.hh"
41
42
#include " ignition/gazebo/components/ParentEntity.hh"
42
- #include " ignition/gazebo/components/Joint.hh"
43
+ #include " ignition/gazebo/components/World.hh"
44
+ #include " ignition/gazebo/Model.hh"
43
45
44
46
#include " LinearBatteryPlugin.hh"
45
47
@@ -56,6 +58,14 @@ class ignition::gazebo::systems::LinearBatteryPluginPrivate
56
58
// / \return State of charge of the battery in range [0.0, 1.0].
57
59
public: double StateOfCharge () const ;
58
60
61
+ // / \brief Callback executed to start recharging.
62
+ // / \param[in] _req This value should be true.
63
+ public: void OnEnableRecharge (const ignition::msgs::Boolean &_req);
64
+
65
+ // / \brief Callback executed to stop recharging.
66
+ // / \param[in] _req This value should be true.
67
+ public: void OnDisableRecharge (const ignition::msgs::Boolean &_req);
68
+
59
69
// / \brief Name of model, only used for printing warning when battery drains.
60
70
public: std::string modelName;
61
71
@@ -102,14 +112,11 @@ class ignition::gazebo::systems::LinearBatteryPluginPrivate
102
112
public: double soc{1.0 };
103
113
104
114
// / \brief Recharge status
105
- public: bool startCharging{false };
115
+ public: std::atomic_bool startCharging{false };
106
116
107
117
// / \brief Hours taken to fully charge battery
108
118
public: double tCharge{0.0 };
109
119
110
- // / \brief SoC threshold for enabling recharge
111
- public: double socThreshold{0.0 };
112
-
113
120
// / \brief Battery current for a historic time window
114
121
public: std::deque<double > iList;
115
122
@@ -204,26 +211,6 @@ void LinearBatteryPlugin::Configure(const Entity &_entity,
204
211
if (_sdf->HasElement (" smooth_current_tau" ))
205
212
this ->dataPtr ->tau = _sdf->Get <double >(" smooth_current_tau" );
206
213
207
- if (_sdf->HasElement (" enable_recharge" ))
208
- {
209
- auto isCharging = _sdf->Get <bool >(" enable_recharge" );
210
- if (isCharging)
211
- {
212
- if (_sdf->HasElement (" charging_time" ) &&
213
- _sdf->HasElement (" soc_threshold" ))
214
- {
215
- this ->dataPtr ->tCharge = _sdf->Get <double >(" charging_time" );
216
- this ->dataPtr ->socThreshold = _sdf->Get <double >(" soc_threshold" );
217
- }
218
- else
219
- {
220
- ignerr << " No <charging_time> or <soc_threshold> specified."
221
- " Both are required to enable recharge.\n " ;
222
- return ;
223
- }
224
- }
225
- }
226
-
227
214
if (_sdf->HasElement (" battery_name" ) && _sdf->HasElement (" voltage" ))
228
215
{
229
216
auto batteryName = _sdf->Get <std::string>(" battery_name" );
@@ -233,7 +220,7 @@ void LinearBatteryPlugin::Configure(const Entity &_entity,
233
220
this ->dataPtr ->batteryEntity = _ecm.CreateEntity ();
234
221
// Initialize with initial voltage
235
222
_ecm.CreateComponent (this ->dataPtr ->batteryEntity ,
236
- components::BatterySoC (this ->dataPtr ->soc ));
223
+ components::BatterySoC (this ->dataPtr ->soc ));
237
224
_ecm.CreateComponent (this ->dataPtr ->batteryEntity , components::Name (
238
225
batteryName));
239
226
_ecm.SetParentEntity (this ->dataPtr ->batteryEntity , _entity);
@@ -252,6 +239,31 @@ void LinearBatteryPlugin::Configure(const Entity &_entity,
252
239
return ;
253
240
}
254
241
242
+ if (_sdf->HasElement (" enable_recharge" ))
243
+ {
244
+ auto isCharging = _sdf->Get <bool >(" enable_recharge" );
245
+ if (isCharging)
246
+ {
247
+ if (_sdf->HasElement (" charging_time" ))
248
+ this ->dataPtr ->tCharge = _sdf->Get <double >(" charging_time" );
249
+ else
250
+ {
251
+ ignerr << " No <charging_time> specified. "
252
+ " Parameter required to enable recharge.\n " ;
253
+ return ;
254
+ }
255
+ std::string enableRechargeService = this ->dataPtr ->modelName +
256
+ " /" + _sdf->Get <std::string>(" battery_name" ) + " /recharge/start" ;
257
+ std::string disableRechargeService = this ->dataPtr ->modelName +
258
+ " /" + _sdf->Get <std::string>(" battery_name" ) + " /recharge/stop" ;
259
+
260
+ this ->dataPtr ->node .Advertise (enableRechargeService,
261
+ &LinearBatteryPluginPrivate::OnEnableRecharge, this ->dataPtr .get ());
262
+ this ->dataPtr ->node .Advertise (disableRechargeService,
263
+ &LinearBatteryPluginPrivate::OnDisableRecharge, this ->dataPtr .get ());
264
+ }
265
+ }
266
+
255
267
// Consumer-specific
256
268
if (_sdf->HasElement (" power_load" ))
257
269
{
@@ -307,12 +319,29 @@ double LinearBatteryPluginPrivate::StateOfCharge() const
307
319
return this ->soc ;
308
320
}
309
321
322
+ // ////////////////////////////////////////////////
323
+ void LinearBatteryPluginPrivate::OnEnableRecharge (
324
+ const ignition::msgs::Boolean &/* _req*/ )
325
+ {
326
+ igndbg << " Request for start charging received" << std::endl;
327
+ this ->startCharging = true ;
328
+ }
329
+
330
+ // ////////////////////////////////////////////////
331
+ void LinearBatteryPluginPrivate::OnDisableRecharge (
332
+ const ignition::msgs::Boolean &/* _req*/ )
333
+ {
334
+ igndbg << " Request for stop charging received" << std::endl;
335
+ this ->startCharging = false ;
336
+ }
337
+
310
338
// ////////////////////////////////////////////////
311
339
void LinearBatteryPlugin::PreUpdate (
312
340
const ignition::gazebo::UpdateInfo &/* _info*/ ,
313
341
ignition::gazebo::EntityComponentManager &_ecm)
314
342
{
315
343
IGN_PROFILE (" LinearBatteryPlugin::PreUpdate" );
344
+ this ->dataPtr ->startDraining = false ;
316
345
// Start draining the battery if the robot has started moving
317
346
if (!this ->dataPtr ->startDraining )
318
347
{
@@ -370,7 +399,7 @@ void LinearBatteryPlugin::Update(const UpdateInfo &_info,
370
399
if (_info.paused )
371
400
return ;
372
401
373
- if (!this ->dataPtr ->startDraining )
402
+ if (!this ->dataPtr ->startDraining && ! this -> dataPtr -> startCharging )
374
403
return ;
375
404
376
405
// Find the time at which battery starts to drain
@@ -421,10 +450,14 @@ void LinearBatteryPlugin::PostUpdate(const UpdateInfo &_info,
421
450
msg.set_charge (this ->dataPtr ->q );
422
451
msg.set_capacity (this ->dataPtr ->c );
423
452
msg.set_percentage (this ->dataPtr ->soc );
424
- if (this ->dataPtr ->startDraining )
453
+ if (this ->dataPtr ->startCharging )
454
+ msg.set_power_supply_status (msgs::BatteryState::CHARGING);
455
+ else if (this ->dataPtr ->startDraining )
425
456
msg.set_power_supply_status (msgs::BatteryState::DISCHARGING);
426
- else
457
+ else if ( this -> dataPtr -> StateOfCharge () > 0.9 )
427
458
msg.set_power_supply_status (msgs::BatteryState::FULL);
459
+ else
460
+ msg.set_power_supply_status (msgs::BatteryState::NOT_CHARGING);
428
461
this ->dataPtr ->statePub .Publish (msg);
429
462
}
430
463
@@ -455,14 +488,8 @@ double LinearBatteryPlugin::OnUpdateVoltage(
455
488
// compute charging current
456
489
auto iCharge = this ->dataPtr ->c / this ->dataPtr ->tCharge ;
457
490
458
- // charging criteria
459
- if (this ->dataPtr ->StateOfCharge () < this ->dataPtr ->socThreshold )
460
- this ->dataPtr ->startCharging = true ;
461
- if (this ->dataPtr ->StateOfCharge () >= 0.9 )
462
- this ->dataPtr ->startCharging = false ;
463
-
464
491
// add charging current to battery
465
- if (this ->dataPtr ->startCharging )
492
+ if (this ->dataPtr ->startCharging && this -> dataPtr -> StateOfCharge () < 0.9 )
466
493
this ->dataPtr ->iraw -= iCharge;
467
494
468
495
this ->dataPtr ->ismooth = this ->dataPtr ->ismooth + k *
@@ -500,7 +527,7 @@ double LinearBatteryPlugin::OnUpdateVoltage(
500
527
igndbg << " PowerLoads().size(): " << _battery->PowerLoads ().size ()
501
528
<< std::endl;
502
529
igndbg << " charging status: " << std::boolalpha
503
- << this ->dataPtr ->startCharging << std::endl;
530
+ << this ->dataPtr ->startCharging << std::endl;
504
531
igndbg << " charging current: " << iCharge << std::endl;
505
532
igndbg << " voltage: " << voltage << std::endl;
506
533
igndbg << " state of charge: " << this ->dataPtr ->StateOfCharge ()
0 commit comments