I think I’ve designed myself into a corner, and could use some help. I’m having an awful time tuning the PID loop on that line follower I posted about in the Robotics Project forum. I think I know why, but I’ll toss everything out here in case I’m missing something:
The robot is 178g and uses two GM18 motors to drive it. The motors have RW2 wheels on them. Right now they’re being driven in PWM mode with the duty cycle switching between powered and coast (which, I think, may be one of the problems, after re-reading some posts by Jim Remington and cruising his site again.)
Line sensing is being done by six sensors in front. The sensor algorithm spits back a single value from about -500 to +500 with zero being in the middle. So the PID loop has a zero set point it’s trying to drive to.
I should quit calling it a PID loop because right now I don’t have an I term, just P and D. Here’s the equation I’m using:
Given:
Kp = Proportional constant
Kd = Derivative constant
Err = Reading from line sensors
Last_Err = Line sensor reading from last cycle
Base_Speed = Base speed for the motors
Correction = Kp * (Err + Kd * (Last_Err - Err))
Speed_L = Base_Speed - Corretion
Speed_R = Base_Speed + Correction
I’ve seen the equation expressed a couple of ways. One is:
Correction = Kp * Err + Kd * (Last_Err - Err)
Another substitutes the error term from two cycles back for Last_Err rather than from one cycle back.
Regardless, the behavior is almost always the same: With Kp and Kd set to zero, it drives in a straight line and ignores the sensors (which is good!) With Kp set to some low number it under-corrects and tends to miss curves. With Kp set slightly higher it over-corrects. Trying to zero in on a Kp-only solution isn’t panning out.
So with Kp set a little high, I try bumping up Kd to try to damp things down. So far I haven’t been able to hit anything resembling a stable loop this way. It’s either vastly under-correcting or vastly over-correcting.
I’m down to one idea as to why: Several months ago Jim posted something to his web site on his PID velocity loop where he said that swapping between power and coast via PWM was not all that effective at lower speeds, and that he had better luck swapping between power and BRAKE. I haven’t tried this yet, but it makes good sense. Right now I think the system is so under-damped that coasting simply isn’t creating a fast enough change in velocity. It’s like the corrections are massively out of phase with the conditions that cause them.
But I’m also learning not to trust my ideas about PID loops, so I’m asking for input. If you see something glaring in my math above, I could use all the assistance I can get.
Thanks,
Tom