Skip to content

Commit 211f229

Browse files
committed
Add parameter for adjust current sign for ROS
Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp>
1 parent 6f7d21f commit 211f229

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/systems/battery_plugin/LinearBatteryPlugin.cc

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class gz::sim::systems::LinearBatteryPluginPrivate
178178

179179
/// \brief Initial power load set trough config
180180
public: double initialPowerLoad = 0.0;
181+
182+
/// \brief Adjusts the sign of the current to align with ROS conventions.
183+
public: bool adjustCurrentSignForROS{false};
181184
};
182185

183186
/////////////////////////////////////////////////
@@ -273,6 +276,10 @@ void LinearBatteryPlugin::Configure(const Entity &_entity,
273276
if (_sdf->HasElement("fix_issue_225"))
274277
this->dataPtr->fixIssue225 = _sdf->Get<bool>("fix_issue_225");
275278

279+
if (_sdf->HasElement("adjust_current_sign_for_ros"))
280+
this->dataPtr->adjustCurrentSignForROS =
281+
_sdf->Get<bool>("adjust_current_sign_for_ros");
282+
276283
if (_sdf->HasElement("battery_name") && _sdf->HasElement("voltage"))
277284
{
278285
this->dataPtr->batteryName = _sdf->Get<std::string>("battery_name");
@@ -669,14 +676,22 @@ double LinearBatteryPlugin::OnUpdateVoltage(
669676
totalpower += powerLoad.second;
670677
}
671678

672-
this->dataPtr->iraw = totalpower / _battery->Voltage();
679+
if (this->dataPtr->adjustCurrentSignForROS)
680+
this->dataPtr->iraw = -totalpower / _battery->Voltage();
681+
else
682+
this->dataPtr->iraw = totalpower / _battery->Voltage();
673683

674684
// compute charging current
675685
auto iCharge = this->dataPtr->c / this->dataPtr->tCharge;
676686

677687
// add charging current to battery
678688
if (this->dataPtr->startCharging && this->dataPtr->StateOfCharge() < 0.9)
679-
this->dataPtr->iraw -= iCharge;
689+
{
690+
if (this->dataPtr->adjustCurrentSignForROS)
691+
this->dataPtr->iraw += iCharge;
692+
else
693+
this->dataPtr->iraw -= iCharge;
694+
}
680695

681696
this->dataPtr->ismooth = this->dataPtr->ismooth + k *
682697
(this->dataPtr->iraw - this->dataPtr->ismooth);
@@ -693,13 +708,23 @@ double LinearBatteryPlugin::OnUpdateVoltage(
693708
}
694709

695710
// Convert dt to hours
696-
this->dataPtr->q = this->dataPtr->q - ((dt * this->dataPtr->ismooth) /
697-
3600.0);
711+
if (this->dataPtr->adjustCurrentSignForROS)
712+
this->dataPtr->q = this->dataPtr->q + ((dt * this->dataPtr->ismooth) /
713+
3600.0);
714+
else
715+
this->dataPtr->q = this->dataPtr->q - ((dt * this->dataPtr->ismooth) /
716+
3600.0);
698717

699718
// open circuit voltage
700-
double voltage = this->dataPtr->e0 + this->dataPtr->e1 * (
701-
1 - this->dataPtr->q / this->dataPtr->c)
702-
- this->dataPtr->r * this->dataPtr->ismooth;
719+
double voltage;
720+
if (this->dataPtr->adjustCurrentSignForROS)
721+
voltage = this->dataPtr->e0 + this->dataPtr->e1 * (
722+
1 - this->dataPtr->q / this->dataPtr->c)
723+
+ this->dataPtr->r * this->dataPtr->ismooth;
724+
else
725+
voltage = this->dataPtr->e0 + this->dataPtr->e1 * (
726+
1 - this->dataPtr->q / this->c)
727+
- this->dataPtr->r * this->dataPtr->ismooth;
703728

704729
// Estimate state of charge
705730
if (this->dataPtr->fixIssue225)
@@ -709,7 +734,10 @@ double LinearBatteryPlugin::OnUpdateVoltage(
709734
double isum = 0.0;
710735
for (size_t i = 0; i < this->dataPtr->iList.size(); ++i)
711736
isum += (this->dataPtr->iList[i] * this->dataPtr->dtList[i] / 3600.0);
712-
this->dataPtr->soc = this->dataPtr->soc - isum / this->dataPtr->c;
737+
if (this->dataPtr->adjustCurrentSignForROS)
738+
this->dataPtr->soc = this->dataPtr->soc + isum / this->dataPtr->c;
739+
else
740+
this->dataPtr->soc = this->dataPtr->soc - isum / this->dataPtr->c;
713741
}
714742

715743
// Throttle debug messages

0 commit comments

Comments
 (0)