I'm building a robot that needs control better than I've ever needed to think about before. The details don't matter, suffice it to say I'm using the opportunity to learn about PID.
I think I understand the idea, but I'm having trouble getting a sample working. I coded a simple simulation of a CarBot in Python and am trying to use PID to keep it going in a straight line, but I'm not totally succeeding.
In fact, I'm not even succeeding with just a PI algorithm. Graphing the output (of sideways velocity, which is what I've chosen my variable to be) I see that it oscillates and then settles down, but not at the setpoint.
I've done a lot of manual tuning (maybe "messing around with the knobs" would be a better way to put it) and this is about the best I can get. Shouldn't I be able to hit the setpoint on the nose with PI?
(I realize this situation has no deadtime and thus no need of the D in PID--I'll add that once I get PI working. Also, there's some useless math in there--division by 1, subtraction of 0, etc--that's because I want to make sure I know where to put the real numbers later.)
#!/usr/bin/python
class Car: x_pos = 0 y_pos = 0 xv = 1 yv = 0
def Step(self): self.x_pos += self.xv self.y_pos += self.yv
self.yv += .001
def PrintPos(self): print self.x_pos, self.y_pos
def PrintYV(self): print self.yv
bot = Car()
last_y = 0 yi = 0 last_yv = 0
KP = .7 KI = 0.2 KD = 0.0
for t in range(100): bot.Step(); bot.PrintYV();
yv = (bot.y_pos - last_y)/1 yi = yi + (yv - 0)*1 yd = (yv - last_yv)/1
last_y = bot.y_pos last_yv = yv
# print yv, yi
bot.yv += (-(KP * yv + KI * yi - KD * yd))