Hi,
Just as Darrel says it can be used to control "anything". Basicly you feed it the Error and it returns the amount of Drive it thinks is neccesary to reduce the Error to zero based on the P- I- & D-gains and a couple of other paramters. It doesn't know, or care, if it's position, speed or whatever.
Code:
pidError = Setpoint - CurrentSpeed 'Calculate error
Gosub PID 'Call PID. Result returned in pid_Out
There are a couple of things you need to concider. Since you're driving the motor in one quadrant only. There may be occations when the output of the PID routine is "negative" ie. it want's to actually reverse the motor. Obviously you can't do that with only one transistor so you need to clamp the output from the PID routine to positive drive only, perhaps something like this:
Code:
pid_Error = Setpoint - CurrentSpeed 'Calculate error
Gosub PID 'Result returned in pid_Out
If pid_out.15 then 'If "negative drive" is called for...
pid_out = 0 '..set output to 0...
pid_Ei = 0 'And keep the I-term from accumulating..
Else 'and if positve drive is called for....
pid_Out = ABS pid_Out '...set it to absoulte value of pid_Out
Endif
Since you want to controll the speed from zero all the way up I think you should concider more than a single a pulse per rev. Otherwise it can get really hard to get good control in the lower speedrange.
Another thing that is important in digital PID control is constant sampling rate. Ideally you set up a timer interrupt to run at whatever rate you need. What rate to run at depends on the timeconstant of the load and you may need to experiment with a bit but for a basic motorcontrol I'd say somewhere between 100-1000Hz.
/Henrik Olsson.
Bookmarks