Skip to content

Commit

Permalink
#533 Caping yaw in the PID controller for absolute setpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Jan 15, 2020
1 parent 2e5d259 commit f28ef7a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/modules/src/controller_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ bool controllerPidTest(void)
return pass;
}

static float capAngle(float angle) {
float result = angle;

while (result > 180.0f) {
result -= 360.0f;
}

while (result < -180.0f) {
result += 360.0f;
}

return result;
}

void controllerPid(control_t *control, setpoint_t *setpoint,
const sensorData_t *sensors,
const state_t *state,
Expand All @@ -52,13 +66,11 @@ void controllerPid(control_t *control, setpoint_t *setpoint,
// Rate-controled YAW is moving YAW angle setpoint
if (setpoint->mode.yaw == modeVelocity) {
attitudeDesired.yaw += setpoint->attitudeRate.yaw * ATTITUDE_UPDATE_DT;
while (attitudeDesired.yaw > 180.0f)
attitudeDesired.yaw -= 360.0f;
while (attitudeDesired.yaw < -180.0f)
attitudeDesired.yaw += 360.0f;
} else {
attitudeDesired.yaw = setpoint->attitude.yaw;
}

attitudeDesired.yaw = capAngle(attitudeDesired.yaw);
}

if (RATE_DO_EXECUTE(POSITION_RATE, tick)) {
Expand Down

0 comments on commit f28ef7a

Please sign in to comment.