@@ -178,6 +178,9 @@ class gz::sim::systems::LinearBatteryPluginPrivate
178
178
179
179
// / \brief Initial power load set trough config
180
180
public: double initialPowerLoad = 0.0 ;
181
+
182
+ // / \brief Adjusts the sign of the current to align with ROS conventions.
183
+ public: bool adjustCurrentSignForROS{false };
181
184
};
182
185
183
186
// ///////////////////////////////////////////////
@@ -273,6 +276,10 @@ void LinearBatteryPlugin::Configure(const Entity &_entity,
273
276
if (_sdf->HasElement (" fix_issue_225" ))
274
277
this ->dataPtr ->fixIssue225 = _sdf->Get <bool >(" fix_issue_225" );
275
278
279
+ if (_sdf->HasElement (" adjust_current_sign_for_ros" ))
280
+ this ->dataPtr ->adjustCurrentSignForROS =
281
+ _sdf->Get <bool >(" adjust_current_sign_for_ros" );
282
+
276
283
if (_sdf->HasElement (" battery_name" ) && _sdf->HasElement (" voltage" ))
277
284
{
278
285
this ->dataPtr ->batteryName = _sdf->Get <std::string>(" battery_name" );
@@ -669,14 +676,22 @@ double LinearBatteryPlugin::OnUpdateVoltage(
669
676
totalpower += powerLoad.second ;
670
677
}
671
678
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 ();
673
683
674
684
// compute charging current
675
685
auto iCharge = this ->dataPtr ->c / this ->dataPtr ->tCharge ;
676
686
677
687
// add charging current to battery
678
688
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
+ }
680
695
681
696
this ->dataPtr ->ismooth = this ->dataPtr ->ismooth + k *
682
697
(this ->dataPtr ->iraw - this ->dataPtr ->ismooth );
@@ -693,13 +708,23 @@ double LinearBatteryPlugin::OnUpdateVoltage(
693
708
}
694
709
695
710
// 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 );
698
717
699
718
// 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 ;
703
728
704
729
// Estimate state of charge
705
730
if (this ->dataPtr ->fixIssue225 )
@@ -709,7 +734,10 @@ double LinearBatteryPlugin::OnUpdateVoltage(
709
734
double isum = 0.0 ;
710
735
for (size_t i = 0 ; i < this ->dataPtr ->iList .size (); ++i)
711
736
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 ;
713
741
}
714
742
715
743
// Throttle debug messages
0 commit comments