Skip to content

Commit

Permalink
Fix a bug with PID actuators, when plugin definition order differs fr…
Browse files Browse the repository at this point in the history
…om actuator order.

PiperOrigin-RevId: 587729879
Change-Id: If71c7e374f36ef08c5147e7316066d6f5365925f
  • Loading branch information
nimrod-gileadi authored and copybara-github committed Dec 4, 2023
1 parent c0b7b98 commit 236dcb4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions plugin/actuator/pid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mjtNum Pid::GetCtrl(const mjModel* m, const mjData* d, const State& state,
}

void Pid::ActDot(const mjModel* m, mjData* d, int instance) const {
State state = GetState(m, d, instance);
State state = GetState(m, d, actuator_idx_);
mjtNum ctrl = GetCtrl(m, d, state, /*actearly=*/false);
mjtNum error = ctrl - d->actuator_length[actuator_idx_];

Expand All @@ -180,7 +180,7 @@ void Pid::ActDot(const mjModel* m, mjData* d, int instance) const {
}

void Pid::Compute(const mjModel* m, mjData* d, int instance) {
State state = GetState(m, d, instance);
State state = GetState(m, d, actuator_idx_);
mjtNum ctrl = GetCtrl(m, d, state, m->actuator_actearly[actuator_idx_]);

mjtNum error = ctrl - d->actuator_length[actuator_idx_];
Expand Down Expand Up @@ -217,9 +217,9 @@ int Pid::ActDim(const mjModel* m, int instance, int actuator_id) {
return (i_gain ? 1 : 0) + (HasSlew(m, instance) ? 1 : 0);
}

Pid::State Pid::GetState(const mjModel* m, mjData* d, int instance) const {
Pid::State Pid::GetState(const mjModel* m, mjData* d, int actuator_idx) const {
State state;
int state_idx = m->actuator_actadr[instance];
int state_idx = m->actuator_actadr[actuator_idx];
if (config_.i_gain) {
state.integral = d->act[state_idx++];
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/actuator/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Pid {
mjtNum integral = 0;
};
// Reads data from d->act and returns it as a State struct.
State GetState(const mjModel* m, mjData* d, int instance) const;
State GetState(const mjModel* m, mjData* d, int actuator_idx) const;

// Returns the PID setpoint, which is normally d->ctrl, but can be d->act for
// actuators with dyntype != none.
Expand Down
12 changes: 8 additions & 4 deletions test/plugin/actuator/pid_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,6 @@ TEST_F(PidTest, ITerm) {
<mujoco>
<extension>
<plugin plugin="mujoco.pid">
<instance name="pid1">
<config key="kp" value="40.0"/>
<config key="kd" value="4"/>
</instance>
<instance name="pid2">
<config key="kp" value="40.0"/>
<config key="ki" value="40"/>
Expand All @@ -337,6 +333,14 @@ TEST_F(PidTest, ITerm) {
<config key="kd" value="4"/>
<config key="imax" value="5"/>
</instance>
<!-- intentionally put the plugin instances out of order, to test
any accidentaly dependency on the plugin definition order matching
the actuator order.
-->
<instance name="pid1">
<config key="kp" value="40.0"/>
<config key="kd" value="4"/>
</instance>
</plugin>
</extension>
Expand Down

0 comments on commit 236dcb4

Please sign in to comment.