using teensy 3 in a precision PID controller

Status
Not open for further replies.

guyst

Member
hello to all makers,


this is my 1st post here so please bear with me as im not femiliar with the place...
as a part of a robotic system, Im interested in making a high precision PID controller using teensy/arduino. the feedback input is an analog voltage from a position sensor(actually 2), which i need to sample at a 14bit resolution or higher. sampling rate must be high - above about 10ksps(before accounting for oversampling that will multiply it). obviously the MCU must be able to cope with the fast calculations.

Im trying to decide between 2 options(more will be welcomed!):
1. using teensy 3.0 and its onboard ADC: as i understand, thru carfull configuration its possible to reach my resolution needs.
2. using arduino 8bit MCU and an external ADC breakout such as PCM1803(https://www.sparkfun.com/products/9365).

the teensy way apeals to me because of:
1. hardware simplicity
2. no need for communication to external module that will complicate things(software wise)
3. a powerfull platform for fast calcs.

the arduino way apeals to me because of:
1. a familiar(to me) and well supported platform. as i will encounter many difficulties and am not a software wiz, this is very important to me.
2. freeing system resources by outsourcing ADC(although with the teensy the ADC will be very fast, so maybe no advantage there)
3. as i gather, the external module has a better chanse of achiving my resolution needs

i should point out that i have made a similar control loop in the past with arduino, but with much lower requirements and the native 10bit res so many problems didnt surface.
also, im only familiar with the arduino environment at this point, and would like to stick with it.


any directions/tips/help will be very appreciatad!
thanks
 
Teensy is programmed with the same Arduino environment. Also, the level of support with Teensy (in terms of rapid and correct forum responses, primary developer involvement, and bugfixes for reported issues) is considerably better than you would find with Arduino.
 
From what little I understand of your project, Teensy3 is probably about right. However, the ADC has about 13 bits of effective resolution in single ended mode, if you use 4X or 8X averaging. It's capable of at least 40 kHz sampling in those modes.

But I can't understand why you're considering an audio converter? If this is a PID control loop, presumably you want an ADC with good DC accuracy. Sure, you can get about 16 bits (THD+N is -95 dB) for AC coupled audio signals. Those audio converters are great where things like gain error, offsets and absolute accuracy don't matter. Who cares if you're off by 1% or 2%. Offsets are just DC that gets filtered out. Scaling errors are just the volume being adjusted a tiny bit. Audio converters are meant for audio. They aren't meant for precisely measuring physical sensors for closed loop control.

You might also take a quick look at Sparkfun's example code. Does it look useful?
 
Last edited:
If you do try that PCM1800, be aware its default mode has a high pass filter. Obviously your PID control loop needs DC, unless your control strategy involve always making something change at 20 Hz or faster! Fortunately the routed the BYPAS signal to a pin, so you can disable the DC filter.
 
Here is a link to a Wikipedia article explaining what a PID controller is. It is very common in industrial controls. It is also commonly used in robotics applications e.g. It's used to stabilize quad copters.

The Teensy3 through Tensyduino is fully Arduiuno software compatible, with the exception that some of the libraries are better optimized and up to date than what you can find elsewhere. Hardware wise there are obviously differences. Try to find a cheaper smaller faster micro with more features. If you want fast PID control implemented I'd stay away from an 8-bit microcontroller as there is some math involved.
 
The ARM math library has optimized functions for PID control. I haven't personally used them or even read much about them yet, only just seen them while using the filters, fft and matrix math stuff.
 
There is a good Arduino PID library out there from Brett Beauregard- the rocketscream reflow oven controller uses it, for example. No need to reinvent what's already out there. The teensy should be able to use it, no problem. The primary consideration of Teensy 3 vs. Arduino, IMO, is the voltage - i.e. do you need a micro controller that can put out 20mA at 5V or is 5mA at 3.3V enough? The ADC for the Teensy is better than the one found inside the AVR too. However, for any application above 10 bits, i'd consider the use of a buffer chip (i.e. opamp) or dedicated ADC, just to keep the costs down and the accuracy up.
 
Bretts PID library, however, was mainly developed not for real time PID control of fast processes, as he's written in his blog, or on the Rocktscream forum himself. It's first use was for PID control of the Reflow Oven Controller Shield and while it works very well there (i have one on my Toaster oven rig) that process obviously does not require a very fast PID loop. That does not mean of course that it could not work any faster but...
 
The PID math is certainly no problem for the Teensy 3, but be careful with the ADC. I would use an external SPI ADC with 16 bits resolution, set the SPI frequency to the lowest value that allows 10ksps on 2 channels, and carefully design the board for the ADC. That might result in 14 useful bits.
 
From what little I understand of your project, Teensy3 is probably about right. However, the ADC has about 13 bits of effective resolution in single ended mode, if you use 4X or 8X averaging. It's capable of at least 40 kHz sampling in those modes.

But I can't understand why you're considering an audio converter? If this is a PID control loop, presumably you want an ADC with good DC accuracy. Sure, you can get about 16 bits (THD+N is -95 dB) for AC coupled audio signals. Those audio converters are great where things like gain error, offsets and absolute accuracy don't matter. Who cares if you're off by 1% or 2%. Offsets are just DC that gets filtered out. Scaling errors are just the volume being adjusted a tiny bit. Audio converters are meant for audio. They aren't meant for precisely measuring physical sensors for closed loop control.

You might also take a quick look at Sparkfun's example code. Does it look useful?

hi paul, thank you for the teensy! i havnt got one yet but soon enough i will.
regarding resolution - are your numbers taken from the ARM datasheet(pg38), or from real world results with the teensy? in any event, if X8 oversampling(i assume its the same as averaging) gets us 13bit res, X32 should get us to 14bit, assuming uncorrelated noise. am i wrong?
regarding the PCM1803 - you'r right, i have overlooked the DC presicion parameters(new to this..). they seem to be about an order of magnitude worse than for presicion devices.
however, im beginning to think that for my level of presicion i will have to calibrate the complete system against an accurate optical encoder, and create a calibration map that correlates ADC output to real movement. assuming the errors are constant(hence - repeatable), i believe this has the potential to shrink the errors to quantization error only(and less with oversampling).
i had no idea this kind of presicion is so hard to achieve...
 
There is a good Arduino PID library out there from Brett Beauregard- the rocketscream reflow oven controller uses it, for example. No need to reinvent what's already out there. The teensy should be able to use it, no problem. The primary consideration of Teensy 3 vs. Arduino, IMO, is the voltage - i.e. do you need a micro controller that can put out 20mA at 5V or is 5mA at 3.3V enough? The ADC for the Teensy is better than the one found inside the AVR too. However, for any application above 10 bits, i'd consider the use of a buffer chip (i.e. opamp) or dedicated ADC, just to keep the costs down and the accuracy up.

i have used this PID lib with good results, at 10bit res and about 1-2ksps. however, i understand that there isnt such a library written for the teensy 3.0, how can i be sure the arduino one will work here?
regarding the voltage differences - i can use a level shifter like this i guess. annoying, but the resolution is a must.
why do you suggest the buffer?
can you recommend an external presicion ADC which is also fast enough and available in PDIP package or a breakout board(cant solder SMD)?

thaks!
 
The IDE is the same. The library doesn't call anything specific to the AVR microprocessor, as far as I know (i.e. assembly subroutine). In fact, I bet the PID controller could be sped up substantially on the Teensy 3 if it could be re-programmed with the ARM-specific built-in PID hardware in mind. However, that is far beyond my level of expertise. Perhaps you could convince Brett to take a stab by sending him a Teensy and a beer (or two)? :)
 
Status
Not open for further replies.
Back
Top