Hey,

does anyone know of a PID algorithm that can use the inertial heading value to turn left. Since the Inertial heading ranges from 0 - 360, I can’t think of an algorithm with PID that can make the inertial heading reach a negative value so that: -90 ( heading) = -90 (desired)

You can do something like the following:

```
error = target - imu.heading();
if(fabs(error) >= 180){
error = -fabs(error)/error * 360 - fabs(error);
}
```

Basically if the error you find is greater than 180 you will always turn the shorter way. With this method your target should still be 1-360 but you can pick 270 and it will turn left.

If you want your parameter to allow a negative turn you can simply convert that at the beginning of your function.

` if(target < 0){target += 360;}`

1 Like