Quote:
|
Originally Posted by billbo911
Besides, I know many of you will have input as to why it will or will not work and, I'm hoping, ways to improve it. So here goes:
|
Your computed values for a PID system are best declared as int to avoid problems with overflow when assigning variables. For instance, in the assignment:
PosError3 = (p3_y - PosFdbk1);
a value larger than 127 can produce a negative result when stored in PosError3, providing for undesired behavior of the control system.
The same goes for DriveCmd3, in the assignment
DrvCmd3 = (127 + PosError3);
in general, although this instance might not be producing undesired results.
Your code implements a P control system, providing for two discrete
signals for smaller values of the error and an upper bound for large values of the error. It is suitable for a system that has a lot of friction, for instance something driven by a screw. The mechanical friction provides any required damping in this case. Our robot, this year, had this kind of drive system for the arm, and as a result we used a P control system with a one stage minimum for small errors. The Fisher Price motors were prone to smoke and it was better to have them "jump into the window," so to speak, and then be cleanly shut off. If they sat stalled they could burn up, and we did smoke one of the 6 volt motors this way.
You shift the value returned by Get_Analog_Value down by two bits, losing two bits of precision in the deal. This loss of precision can be a problem when implementing the arithmetic in a full PID control system, especially when doing subtraction involved in the D and I signals. It is better practice to shift the 8 bit values returned from the OI up by two bits, although for your specific code it is of no concern.
If your system does not have enough mechanical friction to damp oscillation, you will need to implement at least the D control signal. You can also scale and apply limits to the P, I and D signals imdependently before adding them. This allows you to easily tune each signal, without affecting the others. You will know if this is an issue when you try your code out on a given mechanical system. If you can't get rid of oscillation you will need to add a D signal, back down on the gain of the P signal, or both.
It is best to take care of the dead band of a given victor, and the conversion to an 8 bit unsigned value for the pwm controller, somewhat separately from the PID code after the full blown PID signal has been developed. The dead band of a victor can change when swapping a part, or if "recalibration" should occur (students can do that sort of thing). You can easily measure the minimum and maximum pwm signals that do not activate the motor, and update them in this section of the code without changing the PID control code above it.
Ultimately, every system is different, and the things that you need to do in order to control it can be, as a result, different. There is no limit to the phase space for being creative.
Have fun,
Eugene