PID algorithm in C

Hello all! Well, my partners and I are implementing the control scheme on an autonomous robot. We are using a laser ranger and infrared for obstacle avoidance, and a GPS plus digital compass for navigation.

We plan on using the compass to keep our robot heading straight. I figured (after some reading) that a PID algorithm would probably be the best way to keep it in line without oscillation. Previous groups had such a problem, with their bot continuously overshooting its setpoint. Our plan is to use the compass heading as our sensor reading, and to use this algorithm to control the motors in order to bring the robot smoothly back onto its proper path.

Can anyone point me in the direction of some C source code? We are running this on Linux (CentOS) laptop as well. I understand that there will be much testing and fine-tuning necessary, but I'm hoping having some code on hand will at least help us get a place to start.

Thank you, I appreciate it.

Reply to
TenmaNeko
Loading thread data ...

There's a link to my Embedded Systems Programming article here:

formatting link

You can still get overshoot (and stranger things) even with a PID controller -- the PID controller is just the standard way to get things done and is well understood.

If you want to delve deeper, look at my other articles,

formatting link
or buy my book:
formatting link

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

Posting from Google?  See http://cfaj.freeshell.org/google/

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
Reply to
Tim Wescott

Which can be a very good deal, as you can pepper the author with your questions! Very hard to beat a book that can sensibly reply. ;)

Jon

Reply to
Jonathan Kirwan

Check this out

formatting link

Reply to
sdeyoreo

And in so doing, help to improve future printings of the book. Perhaps even future editions as well, assuming I go crazy enough to want to do it over again, yet remain sane enough to do a good job.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

Posting from Google?  See http://cfaj.freeshell.org/google/

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
Reply to
Tim Wescott

The PID is extremely trivial. If you didn't understand the theory behind it, it won't be going to work anyway.

Rene

--
Ing.Buero R.Tschaggelar - http://www.ibrtses.com
& commercial newsgroups - http://www.talkto.net
Reply to
Rene Tschaggelar

This robot is travelling over what distances?

GPS is not much use for small distances (a few metres or less).

What are you going to use as the error term, GPS or compass? If the robot is trying to travel 100's of metres then it should be use GPS to calculate what heading you should be on, THEN use the compass to work out required heading error.

It is amazing how many get this wrong.

Needing to know what distance accuracy and resolution is your first step. This will help to determine how much fine tuning capability you need, to determine your software algorithm and its implementation. As well as the resolution of the sensors required.

--
Paul Carpenter          | paul@pcserviceselectronics.co.uk
    PC Services
              GNU H8 & mailing list info
             For those web sites you hate
Reply to
Paul Carpenter

Good article, thanks for that.

You might have made a sale here.

Reply to
Tom Lucas

Rene - that is terribly, terribly wrong. At it's most basic level, sure, a PID controller can be fairly simple to implement and tweak. But PID controllers can also be incredibly complicated. For example - how simple do you think an auto-tuning PID controller is? Not simple! Or when you need to design a PID controller to have specific overshoots, rise times, and steady state times? Not terribly simple.

-Mike

Reply to
Mike Noone

I completely agree with Rene. If someone don't know what is PID, it is too early for him to think about robots.

There is no need to tweak anything. Once the system response is determined, the analytical design of PID is very trivial and straightforward.

It usually happens if somebody is trying to make PID to work by tweaking.

This is a basic problem from any entry-level textbook.

Yes, it is little bit more complicated then 2 x 2 = 4

Vladimir Vassilevsky

DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

So - a complete knowledge of PID is necessary to do work in robotics?! Right...

No.

And determining system response is incredibly easy, let me tell you...

*rolls eyes*

Which often is the only viable route unless you want to spend half your life doing simulation.

Ummmmm... No, what textbook would this be? People write their theses on stuff like this. PID can be as simple or as complicated as you want it to be.

Good one, really.

Please back up your statements. I think you're pulling them out of a place where the sun don't shine.

-Mike

Reply to
Mike Noone

When used by somebody that doesn't know what they're doing, sure. Implement DGPS, or add in accelerometers and do kalman filtering, or do many other things and you can get accuracy in the cm range.

-Mike

Reply to
Mike Noone

PID is the very very basics of the automatic control. If one doesn't know what is PID, there is nothing to talk about.

Yes, it is not too complex. You just have to think a little bit and then put the numbers together. It requires no more then the basic science from high school to estimate the responce of a mechanical or electrical system with the required accuracy.

Big mistake. How about tweaking on the space rocket? As for simulation, is a brute force approach. As Wiener said, the computer can be used as the amplifier for the head, but not as the substitute. Simulation is useful to check the fine aspects of the nonlinear and time variant systems; however the simulation should be based on the analysis, not vice versa.

Probably, Mr. Tim Wescott has that. Hey, Tim? I like "Advances in the PID control" book, there is also a number of books on the robust control.

People write their theses

Ziegler and Nichols solved the basic problem of PID design in 1940. After that, there were few small refinements and elaborations.

I suggest you to read books instead of tweaking the parameters. You will be surprised; it is really helpfull.

Vladimir Vassilevsky

DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

If you're just using plug and chug formulas - sure. If you actually want to understand where those formulas come from, not as simple.

Of course sometimes simulation is the only viable route. However, in most situations that I've run across simulation has been only good enough to get a starting point for gain values, and to get perfection those have to be hand tweaked.

Not familiar with that one - will take a look some time. I'll mention that any book that covers that in the beginning is glossing over all the nuts and bolts of control systems, which in my humble opinion is not proper form, unless it is assumed that the reader already has a solid foundation in control systems.

I guess that's why there's about 6000 papers on auto tuning PID:

formatting link

Not willing to back up anything? Guess that isn't too surprising.

I think you're arguing that using other's work and formulas and whatnot to implement PIDs is easy. This is true. However, if you actually want to know what is going on, you'll need a foundation in control systems.

-Mike

Reply to
Mike Noone

This will provide for the resolution, not for the accuracy.

However, there is GPS with better accuracy, too. But there is one problem with it: the GPS with 10m guaranteed accuracy is $$, and the GPS with 10cm accuracy is $$$$. Perhaps, it can do even better for $$$$$$, but it doesn't seem to be a very good option...

Vladimir Vassilevsky

DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

Carrier phase correction will get down to 10cm. It is doable for $1000. You need a stable clock source, dro and pll, precision delays (ns), high speed dividers (GHz). It will be a lunch box size and takes about 5 Watts.

Reply to
linnix

I would suggest doing some more research on that topic then. You are misinformed. There are countless papers on this topic so I won't go into much depth on it. But if you were to combine kalman filtering with DGPS you can get some very, very good accuracy as well as resolution.

Cleverness can overcome cost. You may find this website interesting:

formatting link

-Mike

Reply to
Mike Noone

interesting:

formatting link

This is based on the idea that the you have a stable reference source at both places (a fixed and rover location) and that the receiver clocks does not drift apart. Yes, you have a narrow processing window after calibration, but the clock phases will drift randomly afterward. Namely, error rate increase in time after calibration.

My proposal is to eliminate the clock drift with a stable laser tuned Cesium clock.

Reply to
linnix

Even better than that.

Best regards, Spehro Pefhany

--
"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

Aren't they related?

A few hundred $ for an OEM board with Carrier Phase info. Good enough if you can constantly recalibrate the drift. Not good if you don't have a fixed reference.

The receiver has to be in both places at the same time for calibration. I guess that the non-physics version of Uncertainty Principle.

Reply to
linnix

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.