| Author |
Message |
zbasicandy
Joined: 26 Jan 2006
|
|
Posted: 28 May 2006, 11:28 AM Post subject: PID R1 For ZX-xx microcontrollers |
|
|
PID Revision 1 For ZX-xx microcontrollers
'PID on a ZBasic microcontroller is very code efficient also is superfast. At ~ 3 ms. per PID calculation time, it
'is equal to programmable logic controllers costing tens of thousands of dollars! At this scan or cycle time it can
'do very fast flow, power and even motion control with plenty of real time left over!
'============================================[PID General]==================================================================
'
' •PID (Proportional, Integral & Derivative) The most dreaded three letter word in PLC Control!
'
' •PID is complex because a person would need to know software, hardware interfacing, and process control.
'
' •The reason why PID is not common in small microprocessors is the "floating point overhead" and instruction speed.
' ZBasic has both floating point math and instruction speed of ~172,000 Basic instructions/second.
'
' •PID control mimics an electrical opamp.
'
' •PID Control is a "close loop system" e.g. a feedback is provided to the control loop to monitor the process being
' controlled.
'
' •Considerable expertise is required to properly design a system to avoid oscillations or sluggish responses.
' This PID algorithm compares the feedback from the process sensor and the setpoint to determine the appropriate
' analog 0-100% output variable. This output could drive PWM (Pulse Width Modulation) 0-5 VDC output or a DAC normally
' 4-20 ma. or 0-10 VDC. Typical driven outputs could connect to thermal, motion, lighting, flow and power.
' The controller drive is automatically adjusted until the measured sensor signal matches the desired command.
'
' •The ZX-xx sends a setpoint signal to the PID subroutine. The PID subroutine then calls the PID function,
' which is made up of three elements: the Proportional, Integral, and Derivative routines. The Proportional routine
' creates an output signal proportional to the difference (error) between the measurement taken and the setpoint.
' The Integral routine produces an output proportional to the length and amount of time the error signal is present.
' The Derivative routine creates an output signal proportional to the rate of change of the error signal.
'
' •The input transducer generates an output signal from the process being controlled and feeds the measured value to
' the PID subroutine. Analog inputs (4-20ma) are normally used to acquire the feedback signal. The difference between
' the setpoint generated by the ZX-xx and the feedback "measured value" coming from the input transducer is the Error signal.
' Some sort of correcting device such as a motor control, valve control, or amplifier takes the error signal and
' uses it to control the correction sent to the process being controlled.
'
' Possible processes for PID control/simulation are:
' Note: All possible processes are not shown.
'
Process:
1 Power
2 Motion
3 Thermal
4 Lighting
5 Flow
Driver:
1a Bridge
2a Amplifier
3a FET (PWM)
4a Triac
5a Buffer
Speed:
1b Fast
2b Fast
3b Very Slow
4b Slow
5b Very Fast
Sensor:
1c Voltage
2c Position
3c Temperature
4c Intensity
5c Rate
Sensor Feedback:
1d Voltage to A/D
2d Encoder,Frequency(magnetic pickup, Hall sensor
3d RTD, Thermocuple,thermistor,Solid State Temp. Sensor
(LM34,AD590,AD592)
4d Light Sensor
5d Turbine flowmeter/counter
'===========================================[PID Design/Application Tips]====================================================
'
'
' Just having the PID algorithm software is not enough! Careful thought is needed in designing the control loop around
' the process - NOT the other way around!
'
' Items to consider when designing and engineering a "closed PID Loop" for control:
'
' #1. Is the control loop "control action" forward or reverse acting?
' A heater PID control action is forward acting - an increase in the control output will increase the feedback
' temperature. (If there was a cooler the action would be opposite - reverse acting control)
'
' #2. Range of feedback sensor?
' Too small of range will cause the control loop to become unstable and untunable.
' Extreme care should be taken to have the proper range for the process feedback.
'
' #3. Will the control output driver work properly with the process e.g. Pulse Width Modulation / Voltage / Current?
' PWM is well suited for heating control and other slow processes. For precision control you need a true
' precision current/voltage output DAC.
'
' #4. Is the driver output failsafe?
' A PID heater control is failsafe because if the output wire is opened or the driver fails the heater will not
' function - goes to a safe condition as oppose to "100% full control action"
' This PID control software adds an adjustable safety control shutdown. It makes the control output 0 % on failure.
'
' #5. How will the calibration be performed on the control loop driver and feedback?
'
' #6. How will you filter the feedback signal?
' Analog feedback signals are easier to filter than a digital "counter input" signal.
' This feedback signal should be converted from A/D counts to engineering units e.g. pressure/temperature etc.
' By employing "sample averaging" noise on this input variable can be greatly reduced.
'
' #7. How fast of integral scan rate do you need?
' In a heater PID control, the control action is extremely slow, so the scan rate can also be slow.
' The fixed scan time, for this control, is 20ms. This process scan rate can even run at a much lower/slower rate.
'
' #8. How will you "view the process" to tune it with the "P" "I" "D" variables?
' A serial FREE plotting interface from Selmware.com --> StampPlotPro version 3.6
' Use the support file I have created for PID testing. You need a extra serial RS232 port to use it.
' The feedback, setpoint, error and control outputs are all visible at the same time. This software will work
' for ten minutes and will quit. Just close the window out and restart the program via icon or buy the license for $20.
' (Have the icon on your window handy)
'
'
' #9. How fast is the process feedback?
' Don't violate Nyquest therom. The sample rate of the process feedback should be 2x.
' For example, if you are reading RPM and the hertz feedback is 365 hz for 8000 rpms then the minimum integral scan time
' should be 2 x 365 = 730 hz or 1/730hz = 1.3ms.
'
' #10. How will you read the feedback - by analog? or by digital?
' Analogs can be scanned and filtered easily.
' Digital counter inputs (Counter 0 or 1 inputs) are hard to filter in a fixed scanned timeslice RTOS.
'
' #11. On a feedback sensor failure what should you do to the control output?
' You should disable the control output to zero. (no control)
' Safety is the TOP PRIORITY in any PID close control loop design.
'
' #12. On a mis-tuned or faulty control loop how will you know if there is a problem?
' Use the deviation error setpoint along with the error flag LED.
'
' #13. What PID windup?
' The PID checks the difference between the feedback and setpoint and multiplies the "P" gain of this error for
' the control output. The intregral resets or reads this PID calculation every 20ms times the "I" tuning variable.
' This sum error plus the gain error is added together for the control output signal. If the control loop is
' un-responsive, mis-tuned or have a hardware failure, the control output will try increase the control output
' to bring the feedback and setpoint equal. This sum error will keep increasing in value.
' until it sees the feedback signal equal the setpoint. Since this delay is dependent on the scan rate, the
' response would be erratic or sluggest or hard to tune.
'
' #14. How do you slow down the output PID control action?
' Decrease the "P" gain value or decrease the PWM frequency or
' Decrease the "I" integral value.
'
' #15. How do you increase the output PID control action?
' Increase the "P" gain value or decrease the PWM frequency or
' Increase the "I" integral value.
'
'=================================[PID1 Subroutine General Notes]==========================================================
'
'
' The PID1 subroutine has the following:
'
' #1. All inputs and outputs to / from the PID are scaled from 0-100 %.
' #2. The feedback from the sensor (in this case a fixed number 0-100 units) is re-scaled to 0-100% for PID control.
' #3. The setpoint for the PID is rescaled to 0-100% for PID control.
' #4. Both feedback and setpoint are "clamped" and "limited" (For safety protection)
' #5. All PID "tuning constants" are "clamped" or "limited" between 0-1 (For safety protection and operator malfunction)
' #6. The PID Output is "clamped" or "limited" between 0-100%
' #7. There is a control enable bit to enable the output control. In case of "downscale" or "upscale" sensor burnout
' this enable/disable bit can prevent the PID output from controlling. (PID output equal to 0.0) There is no reason
' to control on a bad sensor input!
' #8. There is a PID error flag to indicate that the PID control loop is not tuned properly or just the failure
' of the control loop to make the feedback match the setpoint.(external problems in control loop e.g. wiring, bad
' actuators etc.) This "deviation setpoint error" is adjustable in constants. (Set at 5%)
' #9. The control output is from 0-100% for the forward actuator. If a heater was connected to this output, the
' more control e.g. 25,50,75 - 100% will cause the heater control to produce more heat directly proportional to
' the PID output signal. This is called "forward acting" control. Let's say that there was a device connected
' at full control e.g. 100% that the temperature decreased. This control action is call "reverse acting control"
' You have both options to choose from for the main actuator control output. ([0-5vdc or 5-0vdc using PWM] or
' [4-20ma or 20-4 ma using a milliamp DAC] or [0-10VDC or 10 VDC-0 using a voltage DAC]
' Keep in mind that a control output failure due to a driver problem, open wire etc. SHOULD cause the actuator
' to control less - not run wide open! e.g. Make it failsafe!
'#10. The code is modular. Replacing the suffix with a "2" instead of "1" will create another PID.
'
' Note: It's very important in the program user code to First read the "analogs in" (setpoint & feedback), Second do the PID
' calculations and Third output the signal to the Pwm, Dac/4-20ma last. <--- critical - must be in this order.
'
' Note: This code was made for a ZX-40 but it can be modified for use on any ZX-xx microcontroller.
'
'=========================================[PID1 Testing]===================================================================
'<Open Loop Testing>
'<Just download the program and observe the debug output>
'For the setpoint = feedback the forward output should be 0.0 - no change and no control action.
'Changing the feedback below the setpoint e.g. spt = 80.0 fb = 78.0 - the forward output should ramp to 100%
'Changing the feedback above the setpoint e.g. spt = 80.0 fb = 81.0 - the forward output should ramp or be at 0%
'Try changing the P or I values and observe the control action of the forward output.
'Note: Be sure to activate the PID plotting routines for better visualization of the process calculated PID variables.
'<Close Loop Testing>
'You need to "close the loop" and add a hardware "input" to the feedback variable and add a hardware control output
' e.g. PWM/voltage DAC/current DAC. Proper PID design techniques should be applied. - see above.
'
'==========================================================================================================================
| Description: |
| PID Revision 1 For ZX-xx microcontrollers |
|
 Download |
| Filename: |
PID_Timeslice_R1.bas |
| Filesize: |
42.98 KB |
| Downloaded: |
1102 Time(s) |
|
|
| Back to top |
|
 |
zbasicandy
Joined: 26 Jan 2006
|
|
Posted: 28 May 2006, 15:35 PM Post subject: PID |
|
|
Here is the macro support file for Selmaware's StampPlotPro.
The PID variables can be easily visualized using this plotting program.
'============================================================================================
' Plotting PID Data
'============================================================================================
' This program will plot 4 PID variable channels using Stamp Plot Pro.
'
' 1. Download the Free Stamp Plot Pro from Selmaware.com (version 3.6)
' Unlike the lite version, this version runs for only ten minutes before stopping.
' It can be restarted to resume plotting. Hey, its FREE! You can unlock it for $20.
' You can even connect this plot software to the internet!
'
' 2. You need 1 extra com port from ZX-xx using RS232 and a cable between PC and ZX-xx.
' You cannot use the programming port for this.
'
' 3. Use 1 empty serial port on the PC OR extend the PC serial ports by having a USB to
' serial converter)
' This way you can download to the ZX-xx at the same time while plotting
' (using side by side windows)
'
' 4. Copy the enclose file "ZX-xx PID Monitor" to StampPlotPro_V3 / Macro / V3R4_Samples
' folder under "Program Files"
' "Enable macro" using ZX-xx PID Monitor in the StampPlotPro software.
' Note: This macro file can be modified using notepad. Be very careful !!!!!
'
' 5. Make sure the setting for Stamp Plot Pro "9600 baud" match the ZX-xx serial port
' value of 9600 baud.
' Note: This can be changed by user.
'
'--------------------------------------------------------------------------------------------
| Description: |
| Support file for the PID R1 algorithm. This macro works with Selmaware's StampPlotPro |
|
 Download |
| Filename: |
ZX-xx PID Monitor.zip |
| Filesize: |
1022 Bytes |
| Downloaded: |
1098 Time(s) |
|
|
| Back to top |
|
 |
GTBecker
Joined: 18 Jan 2006
Location: Cape Coral
|
|
Posted: 28 May 2006, 15:58 PM Post subject: PID R1 For ZX-xx microcontrollers |
|
|
> ... At ~3ms per PID calculation time, it is equal to programmable
logic controllers costing tens of thousands of dollars!
Before you discovered the excitement of microprocessors, you wrote ad
copy for The Sharper Image, right? Or did you write and market
universal patent claims?
Please, Andy. What programmable logic controller, that costs tens of
thousands of dollars, is the equivalent of your code? If you can't find
two, cull the absurdities. Words mean things.
Tom
|
|
| Back to top |
|
 |
zbasicandy
Joined: 26 Jan 2006
|
|
Posted: 28 May 2006, 16:38 PM Post subject: PID |
|
|
| Quote: | Please, Andy. What programmable logic controller, that costs tens of
thousands of dollars, is the equivalent of your code? If you can't find
two, cull the absurdities. Words mean things. |
Go to the Allen Bradley Website AB.com and find the programmable automation controller system e.g. Contrologix 5000 series.
Please ask for a quote for this system! You know, you just can't buy just the CPU for this complete system. You have the buy the rack, CPU, I/O and the required $8000 software.
I have worked with this programmable logic controller for years. We use
this PLC for fuel control on a jet turbine, which required a 2 ms. scan time!
To be able to do a PID 3 ms. scan rate is very CPU intensive on any PLC. But having it done on a "single chip" ZBasic microcontroller is a great accomplishment.
Do you have a better/faster PID algorithm to share with the ZBasic community?
|
|
| Back to top |
|
 |
GTBecker
Joined: 18 Jan 2006
Location: Cape Coral
|
|
Posted: 28 May 2006, 18:00 PM Post subject: PID R1 For ZX-xx microcontrollers |
|
|
> ... AB.com and find the programmable automation controller system...
Thank you; I understand your words. "... equal to programmable logic
controllers..." means equal to "programmable automation controller
system[s]". I disagree. Surely your experience agrees that there is a
great distance between your code today and $20000 in an explosion-proof
NEMA enclosure on a job site.
I make no comment about your code; I'm sure it is all that you say it
is, Andy, except for the hyperbole.
Tom
|
|
| Back to top |
|
 |
zbasicandy
Joined: 26 Jan 2006
|
|
Posted: 28 May 2006, 19:36 PM Post subject: PID |
|
|
All I was trying to say, with hyperbole and all, is that having worked on several different types of PLC's and not be able to obtain a 3 ms. scan time and then be able to do it a single chip ZBasic microcontroller is somewhat amazing. Of course, you could use a PIC to do PID but you will not have any left over "CPU horsepower" to do anything else. With ZBasic, I believe, you can have several PIDs working at the same time and still have enough processing for other user functions/subroutines.
Note: AB Contrologix 5000 is Class I, Division II - no need for an explosion proof NEMA enclosure.
|
|
| Back to top |
|
 |
TheHammer
Joined: 05 Aug 2006
|
|
Posted: 06 August 2006, 14:34 PM Post subject: PID |
|
|
Finally found you aka "Batman" Have you ever used your PID in robotics?
I see on another forum you had several demos none of which had robotics in the application.
|
|
| Back to top |
|
 |
|