Author Options:

PID control hardware? Answered

This is for a college project, I'm not asking for help so much as advice. I need to write a PID algorithm and have it control an electromagnet. The only experience I have with this kind of stuff is C++ and arduino serial communications (with processing, not C++). Obviously the goal is to make the whole system as fast as possible, so could anybody recommend a programming language and hardware interface I might look into? Arduino on the whole seems a little slow, and I'm not  sure how fast the serial communications are or if something like USB might be better.

Thanks in advance.



7 years ago

The reason for Derivative in Proportional controllers is anticipating sudden
very fast changing events like in thermal controls dt/dt ;-)
And the reason for the Integral is to home in on slow longer action,
my favorite is the Boxcar Running Average in color sampling controllers.

Education of PID is all too often just a math exercise taught by people
who were never in a real industrial situation where PID was invented
for actual critical process control.



Answer 7 years ago

Learning that for many processess the D term is turned right OFF.


Answer 7 years ago

Hi everyone;

Thanks for all your help, but I'd just like to stress a couple of things:

I know quite a bit about the PID algorithm, including the purpose of each term. The project title we were given requires PID (I've asked), we can't miss out any terms; even if we choose to make their constants tiny, all terms must be there.

What I'm asking, essentially:
Firstly, would the algorithm, plus the sampling of sensors, plus pulse width modulation of the signal be too much for an arduino to keep up with?
Secondly, since this is a shared project, we'd rather run it from a laptop, so that we can both have the software without shelling out for two arduinos (student budgets...) So could somebody recommend a fast, maths capable programming language that makes interfacing with hardware not particularly difficult?

I really do appreciate the help, but I just wanted to be a bit clearer as to what I was actually asking :)


Answer 7 years ago

Look, you're studying engineering PUT SOME NUMBERS ON IT. Handwaving "about would the algorithm, plus the sampling of sensors, plus pulse width modulation of the signal be too much for an arduino to keep up with?" doesn't help you or us. If you can't measure it, you can't begin to engineer it.

As for language; what is "fast" what is "maths capable" ? All you need is something that can service a loop (speed to be SPECIFIED by you) and basically do little more than add or subtract three constants and three variables. Nearly ANY language I can think of can do that.

You haven't even said what you mean by "controlling an electromagnet", nor why it needs to be "controlled". Are you provided with the magnet and the amplifiers to drive it OR WHAT.

You are training in a field where we don't deal in handwaving. Now's a really good time to get to grips with a proper specification. I often find writing the spec clarifies and crystallises the design and approach to the problem.



Answer 7 years ago

Steve is correct that D term can and will destroy a controlled device
Some controllers let you set limits which helps in keeping expensive
hardware from bankrupting a project.  I make a soft Bollinger type
process tracking limit in addition to the never exceed just to give
the normal process math time to keep up.

A real world example of D control is taught to every instrument pilot ;
When instrument flying a pilot monitors a rate of climb / descent
an average pressure based meter.  Understanding when the needle
stops its increase or decrease is the moment of craft is actually level
along its axis prevents a  usually increasing dangerous porpoise 
action in untrained individuals.


Jack A Lopez

7 years ago

In discrete time
y(t) becomes y(n*Ts) becomes y[n] where Ts is your sample period (and fs=1/Ts is the sample frequency) and n is time as an integer (e.g. now =0 , one sample ago is -1, two samples ago is -2, etc)

Also in PID (proportional, integral, derivative) becomes PSD (proportional, sum, difference), or at least that's the easy way to do it.

P[0] := y[0]

S[0] :=   y[-inf] + .... + y[-3] + y[-2] + ... y[-1]

D[0]:=  y[-2] - y[-1]

Then you calculate the next output u[0] as:
u[0] = p*P[0] + s*S[0] + d*D[0]
where p,s, and d are the constants controlling the magnitude of the controllers response to P, S, D, respectively.

I'm not sure why you are worried about the speed of USB communications.  I mean you can program the Arduino to do math (like the equations above) all by itself, um right?  It doesn't have to convert samples to numbers, send the samples to a PC over a USB cable, then wait for the PC to send an answer back? Does it?   Those are sincere questions on my part, because I am not really familiar with Arduino at all.

Guessing the sample period,  Ts, is simply going to be the amount of time it takes the Arduino to run through its little program loop once.  You might even want to pad it with a counting loop to make Ts longer?  Again, here I am assuming the Arduino can do the math all by itself.

alexhalfordJack A Lopez

Answer 7 years ago

Well, no, usually it wouldn't. But the project is structured in such a way that those assessing it would rather we did this on a computer, just using the micro controller to read sensors and control a transistor. I can do the PID algorithm, I'm asking about hardware and programming language. The project, by the way, is PID stabilised electromagnetic levitation.

Thanks for your help :)

steveastroukJack A Lopez

Answer 7 years ago

Bang-Bang, with hysteresis, looking at it.
Nicely commented, clear code,


steveastroukJack A Lopez

Answer 7 years ago

I'd have reckoned that he'll get away with an Arduino too, unless the loop bandwidth is outrageous.

Jack A Lopezsteveastrouk

Answer 7 years ago

I think it has been done before.  Looking at this 'ible:

in the zip file attached to the intro
a file named "Floating_Magnets.cpp" includes a reference to a header named "wprogram.h" which, I think, is an Arduino library. 

The point is that 'ible is using an ATmega168, with an Arduino library in its code, so essentially it is Arduino.  The point here is, I think that demonstrates that Arduino is fast enough, for that control problem, which is also a magnetic levitator.

I am not totally sure what the code is actually doing,  although it looks like on-off control. 

Jack A LopezJack A Lopez

Answer 7 years ago

Dang! I messed up the equation for the difference term. It should be the most recent sample minus the sample before that:

D[0]:= y[-1] - y[-2]


7 years ago

The speed you need depends on the mechanical bandwidth of your system, and the computational requirements to meet the update rate. What's the system ?

Try the ARM based MBED system. That uses Ethernet or USB for comms.