Software > Software

PID trajectory control

(1/1)

**mdav**:

I'm trying to implement PID control for a two wheeled car. The car has two DC motors with optical encoders and an H-bridge. The device needs to move along a straight-line path with predetermined acc/dec ramps on both ends. I can determine the target position of the wheels by calculating the area under the velocity/time curve at each sampling interval.

My question concerns how to use the PID error to keep the car on track and on schedule.

My guess is that I should determine the current error by subtracting the current encoder position from the previous target position (where I should be now). Then, for each wheel, by adding Kp*error + Ki*errSum + Kd*dErr to the new setpoint (next target position), I can calculate the speed of each wheel necessary to get to this new position in the time interval (speed = pulses x delta_t). From there I get the new pwm value. Etc.

For instance if my current error for the left wheel is +6 pulses I subtract whatever I get by running this through the PID algorithm from the change in setpoint (new target - previous target) and divide this by the sampling interval to get my new speed. From this I can determine the new pwm duty cycle.

Is my thinking correct?

Thanks

**jwatte**:

Yes, that is how these systems work. Usually, the "sumError" is actually a "leaky integrator" -- either the sum of the error for the last N data points, or a simple "new = old * alpha + sample * (1 - alpha)" single-pole filter. This is so that very old errors don't affect the integrator for the current situation.

Getting good PID values can be a time-consuming trial-and-error process. While you can put in "theoretically correct" numbers, all the various non-ideal behaviors of the real world may make the best set of parameters significantly different. Sometimes, jumping one of the values by a factor of 2, or 4, just to get a good sense of what the right order of magnitude should be, is the fastest way to the optimal solution. It's like doing manual simulated annealing on the PID values with your eyes and senses as the "fitness function" :-)

**mdav**:

Thanks for the confirmation

**Robot Attack**:

A good rule of thumb when beginning to tune a PID system is to start I and D gains at zero. Increase P until your system becomes unstable, then back it off, maybe by 1/2.

It is very likely that P gain is all that you will need

**mdav**:

Thanks for the tip. I understand that tuning is largely a matter of trial and error.

Navigation

[0] Message Index

Go to full version