problem in writing PID with C program

This is the place for any magazine-related discussions that don't fit in any of the column discussion boards below.
choisoso
Posts: 10
Joined: Sat Mar 15, 2003 1:01 am
Location: hk
Contact:

problem in writing PID with C program

Post by choisoso » Sun Mar 16, 2003 7:53 pm

hi, i am writing a PID program that can control the car to have lane keeping function.
i am using six Sharp GP2D12 IR sensors to collect the data then by using C program to write PID.
so my problem now is how to write this one??

Chris Foley
Posts: 146
Joined: Mon Jan 13, 2003 1:01 am
Location: Chicago IL
Contact:

Re: problem in writing PID with C program

Post by Chris Foley » Mon Mar 17, 2003 6:10 am

Motorola PID loop routines for 68HC11 processors with PWM output
http://e-www.motorola.com/brdata/PDFDB/docs/AN1215.pdf<p>Engineering School assignment on PID loops
http://godiva.eos.uoguelph.ca/~raudette ... lerlab.pdf<p>Happy hunting.

choisoso
Posts: 10
Joined: Sat Mar 15, 2003 1:01 am
Location: hk
Contact:

Re: problem in writing PID with C program

Post by choisoso » Mon Mar 17, 2003 7:56 pm

hi
thank you Chris Foley
but i am using 8255 I/O card that contains 8254,8255 mirco-controller.<p>
ref PWM
distance output sharpGP2D12
------->+ >--PID -->car -->IR sensor
| data |
|----<----------------- |
here is the flow chart<p>so how can i write PID with C program in the above chart.
i am soso

Chris Foley
Posts: 146
Joined: Mon Jan 13, 2003 1:01 am
Location: Chicago IL
Contact:

Re: problem in writing PID with C program

Post by Chris Foley » Mon Mar 17, 2003 10:07 pm

Your project hardware spec still isn't clear -- I think you're talking about using a PC with one of those multi-I/O board setups (8255 is a programmable peripheral interface, 8254 is an 8-bit timer/counter IC).<p>If you're looking for something you can just plug in to do your job, sorry -- neither of the links will help you too much. I'm assuming this is for an Engineering class, here. What you need to do are the following two simultaneous projects:<p>* Project 1 is bottom-up -- look at your hardware, and figure out how to program it in C. For the two Intel chips you mentioned, you basically have two jobs -- how to initialize it so as to program it to do the things you want, and then the commands necessary to make the programmed chip do the things you want. Not knowing your board setup, I can't tell you specifically how to do that, but it will be either outportb bytes to an output port or writing a memory location, and then reading/writing other memory locations or ports to send/get the data. There's no better way to figure out how to do that then by memorizing the data sheets, which I'm sure you have access to. Bury these functionalities in the C functions which you will call and never have to touch or modify again. The 8254 is somewhat harder to figure out than the 8255, but neither is really a difficult job. The Intel/Harris/Whoever data sheets on these do a pretty good job of telling someone who takes the time and effort to understand them how to use the chip (after all, that's what they're made for).<p>* Project two is the standard top-down programming setup itself. Again, assuming you're doing this for a class, you probably really just need to learn how to do this. Looking at, modifying, and trying on successful code is the best way to learn how to slam successful code. An algorithm that works well in 6811 assembler code will probably also work in C on an IBM PC, and vice versa. The two PID loop programs are examples of how to do it.<p>A couple of small pieces of free advice:<p>Be careful about what happens to the 8255 IC, as well as your interface circuitry, in the period of time before the IC is initialized. It can take a minute or two from the time you've turned on the PC until you've initialized the chips. That can mean going way too far into the "groan zone" for either the PPI chip, or your prototype. Buy a couple of spares of both chips just because (all this type of board have sockets for these ICs, for a good reason). Especially if you have 82C55 ICs, be <VERY> careful about ESD -- the 82C55 will die easy. An anti-static jihad is definitely in order. Also, if you're using the 8254 to pick up outside signals instead of just using it as an external timer, be careful about noise on the input signal lines. It's very difficult to debug that type of hardware problem.<p>Happy hunting.

Will
Posts: 310
Joined: Tue Feb 11, 2003 1:01 am
Location: Katy Texas
Contact:

Re: problem in writing PID with C program

Post by Will » Tue Mar 18, 2003 11:17 am

When you are using PID you need a setpoint (Desired Value ) and Process Variable (Measured Value) The error between them has to be represented as a proportion of some scale - What is your scale/s and how do you determine error magnitude with these (Sharp) sensors and an 8255 ? Don't you need some analog variables and an ADC ?
Why would you want Derivative (D) on this application ? I'm sure you are aware that, to implement the Derivative function you have to be aware of the rate of Change of the error !
Will
BB

Chris Foley
Posts: 146
Joined: Mon Jan 13, 2003 1:01 am
Location: Chicago IL
Contact:

Re: problem in writing PID with C program

Post by Chris Foley » Tue Mar 18, 2003 4:10 pm

The Sharp GP2D12 is a distance sensor with an analog output -- cool chip, I want one! It senses distance, and outputs an analog voltage proportional to how close the object is, from 10cm. to 80cm. From the data sheet, using a +5V supply, the output voltage will be between 0.4V and Vcc-0.3V. I would assume you could put a line of six of these down on the bottom of a motorized platform, and measure distance from a guide rail travelling between the wheels? That's one idea, anyway.<p>I would assume at this point the project would include either some kind of multiple input A/D converter to be controlled by bit-banging the 8255, or an analog multiplexer going to a V/F converter, which would be read by the 8254. The second seems at first like it might be a little slow, but it should be more than fast enough for this application (the GP2 updates only 25 times a second, and I wouldn't expect more than several adjustments in wheel direction per second. I would assume, since there is no way to synchronize with the conversion rate of the GP2, you are just going to get data as it comes once every 38 ms). Obviously, the data would be used to calculate an error integer which would indicate how far from centered the go-cart is.<p>There are a lot of possibilities on this project, but we're going on pure guesswork here as far as your hardware base. Need more information. Happy hunting.<p>[ March 18, 2003: Message edited by: Chris Foley ]</p>

choisoso
Posts: 10
Joined: Sat Mar 15, 2003 1:01 am
Location: hk
Contact:

Re: problem in writing PID with C program

Post by choisoso » Wed Mar 19, 2003 3:18 am

hi
thans everybody. Yes. i have a A/D converter that convert the sensors data into digital form. so the problem is how to write PID with C progam?

Chris Foley
Posts: 146
Joined: Mon Jan 13, 2003 1:01 am
Location: Chicago IL
Contact:

Re: problem in writing PID with C program

Post by Chris Foley » Wed Mar 19, 2003 6:39 am

If you are unable to download the second link, you should install Adobe Acrobat Reader. This free software will allow you to read .pdf files. This free software is available at:<p>http://www.adobe.com/products/acrobat/readstep.html<p>[ March 19, 2003: Message edited by: Chris Foley ]</p>

Will
Posts: 310
Joined: Tue Feb 11, 2003 1:01 am
Location: Katy Texas
Contact:

Re: problem in writing PID with C program

Post by Will » Wed Mar 19, 2003 8:42 am

I don't know how to write anything in C but I could write (And have written before) such a program in GWBasic (Or FORTRAN in the dim and distant past.
I had the impression that you wanted to attach this device to a car so that what you would have to read would be the white line - and it would not be between the sensors it would be off to one side. Given that and assuming that you know how to convert all of these sensor outputs into a given distance then you would set up a Setpoint (Desired Value) of distance - Let us say 3 ft. which is 91.44 cm - that's out of range of an 80 cm sensor - It could work if you made the setpoint, say 60 cm but that seems a little too close for a car. Then - You establish a SPAN i.e. range of motion - let us say +/- 20 cm. You then continuously measure the distance from the line - when it is 60 cm you do nothing i.e. do not make any steering adjustments. When there is an error i.e. difference between Measured Value (MV) and setpoint then you need a steering correction. Before you can do that you have to determine (a) The proportional (Gain) term of the PID. Let us say you decide that the gain term is set to 2.0 then, with a 40 cm span a 2 cm error will produce a 10% change in output to the steering. Such an arrangement (Proportional only) will not reduce the error to zero except at one unique point on the scale so you have to have an Integral (Reset) term - this works so that, whenever an error (Difference between MV and Setpoint ) exists the Integral term continuously adds into the output a component proportional to the error and changing with time in the correct direction to guide the car back to the setpoint. This (Integral) term is usually calibrated in Repeats per Minute i.e. if an error produced a gain output change of 10% then the integral term would continuously add into the output (In the corrct direction) 10% each minute, reducing in proportion to the error until, when the error becomes zero, so does the Integral term output change and then the outoput freezes at it's new value until another error is detected. In calculus terms the Integral term adds into the output an element which is the time related INTEGRAL of the error - hence it's name.
If you wished to add in a Derivative term then that also is a calculus definition i.e. it adds into the output an element which, calibrated in a similar fashion to the Integral term, is directly proportional to the Rate of Change of the error i.e. k*(dE/dt) As I previoualy suggested I don't know why you would wish to have a derivative term on such an application - it is mostly used to compensate for time lags, distance/velocity lags and capacity lags none of which would, I think, appear in this application.
Will
BB

Chris Foley
Posts: 146
Joined: Mon Jan 13, 2003 1:01 am
Location: Chicago IL
Contact:

Re: problem in writing PID with C program

Post by Chris Foley » Wed Mar 19, 2003 3:16 pm

It's all yours if you want it, Will. The correspondent seems to need someone to help him. Free advice and $2.50 will get you a cup of coffee at Starbucks, so here goes...<p>If I were doing this, I would make an electronics go-cart without a drive motor, and put it on an exercise treadmill. By affixing the cart to a guide bar (or even a good piece of nylon twine tied to the handlebars) suspended over the belt, you could still have all the electronics in a PC and on an adjacent workbench (except sensors and actuator), and keep the PC stationary, just having an umbilical with the wires running from the cart to the hardware table to the PC. I would look into an RC airplane servo setup for front wheel steering (PWM TTL-level output to determine direction -- another good use for the 8254 timer/counter IC), and then you could just use a piece of white cardboard on the side of the belt to simulate a wall you're guiding from and avoiding crashing into. You could move the white cardboard wall by hand, or place one piece of cardboard in front of another (step change), and see how the car moves. As the wheels turn, the car will get closer to or farther from the position of the white cardboard laterally, while being constrained by the guide bar to keep at the same position horizontally relative to the ground. Put collars at the ends of the guide bar to prevent the car from driving off the treadmill, at least during development and setting of PID constants. Also, rather than using a chart recorder to measure temp. (the most common use of PID loops), you could have the PC store the data from the sensor, which should give you data for a chart. Part of the paper he will have to do will involve describing how he tuned it, and why he did what he did. This data being already available (having been stored at run-time), will give him what he needs there. Different treadmill speeds and different step changes should be sufficient to generate tons of data.<p>All of the advice above is still valid. You especially want to check the second link above. It looks like a much leaner version of something I did a number of years ago, and it inspects like it would work. By the way, I originally learned C by transcribing some old GWBASIC programs at the insistence of an Engineer I worked for at the time (the Cardinal learned it that way, too), then taking a class. Try heating up the old DOS machine, and download Turbo C 2.0 from the Borland museum<p>http://bdn.borland.com/museum/ <p>For a textbook, try the Waite Group's Turbo C for the PC by Robert Lafore, available from Amazon.com for $2.50 and up. It's the best real-world start to learning C, and has a good section on Rocks&Shoals, otherwise known as pointers. C is an incredibly versatile instrument, and you should be able to modify a program you write (or a transcribed version of your old GWBASIC or FORTRAN program) easily to run in a DOS window in Win98 with Visual C (probably the format he'll insist on). E-mail me and let me know if you take the job, and how it goes. Enjoy your coffee, and happy hunting, sir.<p>[ March 19, 2003: Message edited by: Chris Foley ]<p>[ March 19, 2003: Message edited by: Chris Foley ]<p>[ March 19, 2003: Message edited by: Chris Foley ]<p>[ March 19, 2003: Message edited by: Chris Foley ]</p>

Will
Posts: 310
Joined: Tue Feb 11, 2003 1:01 am
Location: Katy Texas
Contact:

Re: problem in writing PID with C program

Post by Will » Thu Mar 20, 2003 9:37 am

Thank you Chris Foley - I'm not being competitive and I don't want to take anything over from you. Choisoso has only requested stuff on PID and no one else including you seems to have mentioned it so I did - trying to help.
I got the impression, particularly since his post mentions `the car', that the guy is following up on an article that appeared in this forum actually discussing how to build such a system on a car i.e. an automobile. Such being the case then advice on building things on treadmills etc would be of little or no help. If it isn't for this project then he needs more hardware help than I can give him.
I have explicitly shown how the components of PID i.e. P, I & D relate to each other and what the function of them is. If the guy can't understand it from that then he doesn't understand enough to be able to do it. He needs someone to do it for him and that won't be me because it looks to me like an academic project.
I have tried `C' but it seemed a little arcane to me. Perhaps I will get the $2.50 book you mentioned and have another go at it
Will
BB

Will
Posts: 310
Joined: Tue Feb 11, 2003 1:01 am
Location: Katy Texas
Contact:

Re: problem in writing PID with C program

Post by Will » Thu Mar 20, 2003 9:43 am

Chris Foley,
I forgot to mention above that I have absolutely no idea what you mean when refer to the principal use of PID loops being with Chart Recorders for temperature measurement - Is that what you said ? Perhaps you can explain it to me ? I'm always open to learning - perhaps you could send me a copy of the paper you wrote on it ?
Thanks Will
BB

Chris Foley
Posts: 146
Joined: Mon Jan 13, 2003 1:01 am
Location: Chicago IL
Contact:

Re: problem in writing PID with C program

Post by Chris Foley » Thu Mar 20, 2003 5:14 pm

Hi, Will. I'm sorry -- I didn't mean to get on your nerves. All I meant to say was I don't think I can help this guy, and he looks like he needs somebody to do this for him. I was wondering if you wanted to offer your services -- I'm definitely not interested at this point. I'm an Engineering Tech (though I've worked for a number of good Engineers over the years, and one or two very good ones), and haven't written any papers recently, except for helping a couple of co-workers with their Senior EE projects over the years. Most of my experience with PID loops has been with temperature controllers, and the proof of a good PID setup on a temp controller for me has been to chart a step change (e.g. open the furnace door for a minute, or temporarily change the setpoint by 50 degrees), and look at overshoot / undershoot / number of oscillations / response time to 90, 95, or 99 percent of delta t. My understanding was that the majority of the electronic devices out there that use PID loops are temperature controllers, not that the majority of ideas about PID loops have to do with temp., or that chart recorders use PID loops (although I once used and repaired a 60s vintage HP X-Y chart recorder that used a fairly elegant servo setup to control the pens).<p>The suggestion with the treadmill has to do with one of the inherent difficulties with a Senior Project of this type -- the DISASTER. I can see setting up the thing on a go-cart, watching it roll, and then watching it lose control and drive off a cliff or into traffic and under a semi, taking the grade with it (along with the data). By having a free-rolling "pan with 4 wheels" tethered to a treadmill, and using the control electronics to just sense how close it is to the wall/cardboard and steer the thing as it's being dragged along the belt, he can keep all of the electronics except the sensors and the servo controlling the front wheel direction off the pan and stable on the table, including power supplies. He can also use that PC without having to work up some kind of wireless comm setup, with the software/hardware overhead or expense that entails (or use a million foot extension cord, or have the PC on the go-cart, with associated hazard to the hard drive). If he uses a treadmill, he can program its speed easily external to his experiment, have his experiment do just what it's supposed to do, and the worst that can happen is that the pan tips over and falls on the floor (if the student is too slow to catch it). He is also isolating the variable he is experimenting with, which is usually a good thing. And, by storing the successive readings from the sensor to file, he can plug the data into an Excel spreadsheet, and be able to graph (chart) how the PID loop responded to a step change.<p>Choisoso has gotten some advice and ideas on hardware, a few references on the software (one of which happens to be the answer to his original question, just different hardware and a different thing to be controlled), and a good, detailed explanation of how the PID loop he was asking about works, and how it should be tuned, courtesy of you. By the way, the second reference above<p>http://godiva.eos.uoguelph.ca/~raudette ... lerlab.pdf<p>has a C program doing pretty much what Choisoso wants, and also has a detailed explanation of PID loops and their math on page 4 of the 24 page paper, which Choisoso apparently wasn't interested in downloading or reading. Take care, and thanks for joining the talk -- I'm done. By the way, good luck with the C.<p>[ March 20, 2003: Message edited by: Chris Foley ]<p>[ March 20, 2003: Message edited by: Chris Foley ]<p>[ March 21, 2003: Message edited by: Chris Foley ]</p>

choisoso
Posts: 10
Joined: Sat Mar 15, 2003 1:01 am
Location: hk
Contact:

Re: problem in writing PID with C program

Post by choisoso » Fri Mar 21, 2003 7:23 am

ok thans everybody.<p>but my problem now is i want to generate a PWM signal by using 8255 programmable peripheral interface card. i short out0 and Gate1 so that out1 can output PWM.
but i use CRO to look at it. But just a constant voltage appears.
my program is follows:<p>#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#define control 0x30B
#define count1 0x309
#define count2 0x30A
#define count0 0x308<p>void main()
{
int c,x,sec;
outportb(control,0x34);
outportb(control,0x72);
outportb(control,0xb4);
outportb(count0,128);// LSB
outportb(count0,312); //MSB with output 50Hz<p>x=3600;
c=40000-x;
sec=c%/256;
outportb(count1,sec);//LSB
sec=c/256;
outport(count1,sec);//MSB
}<p>so what is the problem???
i am soso

Will
Posts: 310
Joined: Tue Feb 11, 2003 1:01 am
Location: Katy Texas
Contact:

Re: problem in writing PID with C program

Post by Will » Fri Mar 21, 2003 10:07 am

I'm sorry Choisoso - I haven't got a clue about C - or what your C program is doing _ - I don't see how you could be implementing a PID loop in such a short program. As far as I can see you are taking a counter output, dividing it by a constant and sending it as an output. Is that not so ? Shouldn't you have some inputs somewhere or is that inferred in COUNT ?
I also can't see how you could send a PWM signal to a steering wheel or similar device unless your were able to state that a particular level of signal would position the steering device at a particular point - Then, if you had a capacitor in the output the system would work in a fashion. But not in any way that could be described as a PID loop.
Do you need this for an education problem - or are you just having fun?
If you are prepared to send me a commented copy of your C program - so that I may understand it<p>Will
BB

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests