br3ttb / Arduino-PID-Library

1.91k stars 1.1k forks source link

Trying to incorporate the PID library into a boat autopilot project #100

Open trotamares opened 4 years ago

trotamares commented 4 years ago

Hi, I am trying to incorporate the PID library (Thanks Brett) into a boat autopilot project with the following parameters:

Input: absolute value of the angle of deviation from the desired heading (heading error) in degrees. Integer values ​​between 1 and 180 (normally between 0 and 15).

Output: Angle that must turn the helm of the ship to correct the deviation in degrees. Between 0 and 30 (OutputLimits).

Setpoint: 0

The heading data (and therefore AnguloDesvio) is updated every 1000 ms

In the sketch:

PID PID_Rumbo (& AnguloDesvio, & AnguloTimonPID, 0, Kp, Ki, Kd, ​​DIRECT);

void loop () { ...

PID_Rumbo.Compute ();

... }

void ConfigPID () // It is called at the start and every time there is a change in the parameters { PID_Rumbo.SetOutputLimits (0, MaxTimon); // limits for the PID output value (0-30) PID_Rumbo.SetTunings (Kp Ki, Kd);//tested with values ​​between 0 and 10 PID_Rumbo.SetSampleTime (500); // interval between PID calculations PID_Rumbo.SetMode (AUTOMATIC); // PID on }

The output is almost always 0.00 (exceptionally it gives a different value).

Can anybody help me? Regards

hildred commented 4 years ago

OK first things first, input:

heading error is a fine choice but the range must include negative numbers so that you can correct to either port or starboard. a second option for input is heading error + 180 which removes negative numbers if that is a problem. A third option is ticks heading error instead of degrees (a click in this context is the smallest possibly valid change of your measurement of heading).

next output:

heading adjust is not a good choice for output, but if used it must include negative numbers so that again you can steer both ways. a better choice is rudder angle or better yet rudder position (measured in whatever units you set your rudder i.e. servo values.) again if negative numbers are a problem an offset may be used so that a strait rudder is in the middle of your range.

timing:

boats are slow waves are fast (relative) and introduce sensor jitter. you probably want to slow your pid loop and average readings or something. also, how fast is your rudder?

setpoint:

zero is fine if you allow negative numbers. if you use an offset to avoid negative numbers this will of course change.

tuning:

start with ki = kd=0 and play with kp. when the boat starts steering in a reasonable time, divide it by two and then start playing with kd and ki. I think you will probably want them about the same. don't be afraid of large values or small values.