Skip to content

Commit cb60b3d

Browse files
authored
LinearBatteryPlugin stability improvement (#2725)
Signed-off-by: Maksim Derbasov <[email protected]>
1 parent c72be04 commit cb60b3d

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

src/systems/battery_plugin/LinearBatteryPlugin.cc

+25-25
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,6 @@ class gz::sim::systems::LinearBatteryPluginPrivate
9797
/// \brief Pointer to battery contained in link.
9898
public: common::BatteryPtr battery;
9999

100-
/// \brief Whether warning that battery has drained has been printed once.
101-
public: bool drainPrinted{false};
102-
103-
/// \brief Battery consumer identifier.
104-
/// Current implementation limits one consumer (Model) per battery.
105-
public: int32_t consumerId;
106-
107100
/// \brief Battery entity
108101
public: Entity batteryEntity{kNullEntity};
109102

@@ -136,15 +129,11 @@ class gz::sim::systems::LinearBatteryPluginPrivate
136129
/// \brief State of charge [0, 1].
137130
public: double soc{1.0};
138131

139-
/// \brief Recharge status
140-
public: std::atomic_bool startCharging{false};
141-
142132
/// \brief Hours taken to fully charge battery
143133
public: double tCharge{0.0};
144134

145-
/// \TODO(caguero) Remove this flag in Gazebo Dome.
146-
/// \brief Flag to enable some battery fixes.
147-
public: bool fixIssue225{false};
135+
/// \brief Initial power load set trough config
136+
public: double initialPowerLoad{0.0};
148137

149138
/// \TODO(caguero) Remove in Gazebo Dome.
150139
/// \brief Battery current for a historic time window
@@ -157,16 +146,6 @@ class gz::sim::systems::LinearBatteryPluginPrivate
157146
/// \brief Simulation time handled during a single update.
158147
public: std::chrono::steady_clock::duration stepSize;
159148

160-
/// \brief Flag on whether the battery should start draining
161-
public: bool startDraining = false;
162-
163-
/// \brief The start time when battery starts draining in seconds
164-
public: int drainStartTime = -1;
165-
166-
/// \brief Book keep the last time printed, so as to not pollute dbg messages
167-
/// in minutes
168-
public: int lastPrintTime = -1;
169-
170149
/// \brief Model interface
171150
public: Model model{kNullEntity};
172151

@@ -176,11 +155,32 @@ class gz::sim::systems::LinearBatteryPluginPrivate
176155
/// \brief Battery state of charge message publisher
177156
public: transport::Node::Publisher statePub;
178157

179-
/// \brief Initial power load set trough config
180-
public: double initialPowerLoad = 0.0;
158+
/// \brief Battery consumer identifier.
159+
/// Current implementation limits one consumer (Model) per battery.
160+
public: int32_t consumerId;
161+
162+
/// \brief The start time when battery starts draining in seconds
163+
public: int drainStartTime{-1};
164+
165+
/// \brief Book keep the last time printed, so as to not pollute dbg messages
166+
/// in minutes
167+
public: int lastPrintTime{-1};
168+
169+
/// \brief Recharge status
170+
public: std::atomic_bool startCharging{false};
171+
172+
/// \brief Flag on whether the battery should start draining
173+
public: std::atomic_bool startDraining{false};
174+
175+
/// \brief Whether warning that battery has drained has been printed once.
176+
public: bool drainPrinted{false};
181177

182178
/// \brief Flag to invert the current sign
183179
public: bool invertCurrentSign{false};
180+
181+
/// \TODO(caguero) Remove this flag in Gazebo Dome.
182+
/// \brief Flag to enable some battery fixes.
183+
public: bool fixIssue225{false};
184184
};
185185

186186
/////////////////////////////////////////////////

test/integration/battery_plugin.cc

+11-8
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,26 @@ TEST_F(BatteryPluginTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(PowerDrainTopic))
400400
// Send a message on one of the <power_draining_topic> topics, which will
401401
// start the battery draining when the server starts again.
402402
gz::transport::Node node;
403-
auto pub = node.Advertise<msgs::StringMsg>("/battery/discharge");
403+
auto dischargePub = node.Advertise<msgs::StringMsg>("/battery/discharge");
404404
msgs::StringMsg msg;
405-
pub.Publish(msg);
405+
dischargePub.Publish(msg);
406406

407407
// Run the server again.
408408
server.Run(true, 100, false);
409409

410+
// Send a message on one of the <stop_power_draining_topic> topics, which
411+
// will stop the battery draining when the server starts again.
412+
auto stopPub = node.Advertise<msgs::StringMsg>("/battery/stop_discharge");
413+
stopPub.Publish(msg);
414+
415+
// Run the server a little bit to allow msg be propagated inside plugin
416+
server.Run(true, 50, false);
417+
410418
// The state of charge should be <1, since the battery has started
411419
// draining.
412-
double stateOfCharge = batComp->Data();
420+
const double stateOfCharge = batComp->Data();
413421
EXPECT_LT(batComp->Data(), 1.0);
414422

415-
// Send a message on one of the <stop_power_draining_topic> topics, which
416-
// will stop the battery draining when the server starts again.
417-
auto pub2 = node.Advertise<msgs::StringMsg>("/battery/stop_discharge");
418-
pub2.Publish(msg);
419-
420423
// Run the server again.
421424
server.Run(true, 100, false);
422425

0 commit comments

Comments
 (0)