Go Down

### Topic: loop input to a PID controller (Read 2834 times)previous topic - next topic

#### matze88

##### Oct 13, 2014, 04:08 pm
hi there,

I built my own quadrocopter based on an arduino mega. Everything works fine so far.
Now I added a compass module (cmps10) to get and set the current bearing of the quadrocopter.
The bearing gets controlled by a PID controller (http://playground.arduino.cc/Code/PIDLibrary).
The PID input is the bearing from the compass and the PID setpoint is a value between 0 and 359. With these two values, the PID controller controls the rotors to turn the quadrocopter to the desired bearing (setpoint).
This algorithm works but it has a problem:
Let's say that the bearing setpoint is 350 deg and the quadrocopter's current bearing is 10 deg. The quadrocopter now turns right till its bearing is about 350 deg. That's not really wrong, but for this example it would have been easier to just turn left 20 degs.
The problem is that the PID controller doesn't know that the bearing values build a loop, so that after 359 degs comes 0 degs.

My question is, is it somehow possible to tell the PID controller that the input values form a loop??

#### johnwasser

#1
##### Oct 14, 2014, 12:42 am
You could fudge the "current heading" (Input to the PID) to be a value within 180 degrees of the Setpoint.  Just tell the PID that the current heading is 370° and it will give you an Output to turn left.  Likewise if the Setpoint is 10° and the heading is 350° just tell the PID that the current heading is -10° and it will give an Output to turn right.

Code: [Select]
`   // Normalize the heading to be within 180° of Setpoint (desired heading: 0-359°)    if (heading - Setpoint < -180)  // Setpoint = 350, heading = 10...         heading += 360;                    // change heading to 370   if (heading - Setpoint > 180)   // Setpoint = 10, heading = 350...         heading -= 360;                  // change heading to -10`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats

#### MarkT

#2
##### Oct 17, 2014, 10:55 pmLast Edit: Oct 17, 2014, 10:56 pm by MarkT Reason: 1
Yes, make the set point zero, pass the angle error as the control input.  You can ensure the
angle error is in the range -180 to +180 before its passed to the PID and allow it to turn the
most direct way.

In other words do this sort of thing before the PID
Code: [Select]
`  angle_error = actual_angle - setpoint_angle ;  if (angle_error < -180) angle_error += 360 ;  if (angle_error > 180) angle_error -= 360 ;`
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

Go Up