readadc accuracy

alband

Senior Member
Well, this is another post in the context of my ongoing struggle to get that tank barrel to self level.

Brief outline of the setup:
Accelerometer is now attached to a gun barrel which is controlled by a servo, which in turn, is controlled by a PICAXE.
See the picture for (hopefully) a clearer idea of the set-up.
The servo can go to a position between 129 and 177 without breaking the tank's gun barrel.

I really have come to the conclusion that I need to be able to convert the accelerometer value into the corresponding servo value. This will clearly need a formula of some sort where the input the the accel's reading and the output is the servo value that would give that reading (if the tank were level).
That said, I only think that because I think it would be impossible to have 50 if statement along the lines of "if accel reading = 730 then: let servo = 145".

This is where it all goes wrong :( :
The servo positions for the gun ranges from 129 to 177.
The readings are measured using "readadc". The readings range from roughly 187 to 174. As you can see though, it only uses 13 values with lots of repeats (see the attached excel file, yellow column).
This is bad for two reasons:
1. It means there is effectively 13 steps spread between about 50 so that means each step is about 4deg which is rubbish.
2. It makes making a formula to convert accel reading into servo reading even harder.

So; is there any way to make the accel readings cover more adc range and therefore have more steps? Also, if there is a way to change the range, is there a way of precisely choosing the upper and lower limits, thus, perhaps, making constructing a formula easier?

I also wondered if it was possible to do the same thing with the servo units:
A normal servo runs from 75-225, by turning the arm until it gets the desired position from the POT. My tank's gun barrel only covers about 50deg and thus only about 50 of the 75-225 steps. So is it possible to change the POT readings so that the 50deg that the gun barrel covers takes up more units of the POT?

This is the code used to get the results in the attached excel file (needs changing to ".xls").
Code:
#PICAXE28X2

symbol reading = b1
symbol err = b2
symbol pos = b3

adcsetup = 5
pause 10
high B.5
'high B.4
setfreq em32
servo B.7,150
'let pos = 180

pause 5000

let b5 = 129

pause 8000
sertxd ("start,")
main:		servopos B.7,b5
		pause 20000
		readadc 1,w0
		sertxd (#w0,",")
		let b5 = b5 + 1
		if b5 = 178 then end
		endif
		goto main
Thanks in advance as always,
David
 

Attachments

Dippy

Moderator
Your Servo pot.
What are the actual values you've used?
Its all about ratios of your pot/div.
Simple algybra.
Post the circuit of you pot part. Changing values may help.

Alternatively, an op-amp circuit to change gain+offset.

Can't comment on rest of it.
 

BeanieBots

Moderator
Yes, you can do that with an op-amp.
Have a look at Doc's op-amp tutorial.

With just a few resistors, a reference voltage (could be your 5v regulator output) and an op-amp you can do "Y=mx+C" on the analogue side.
That could then be scaled and offset so that your ReadADC see's a full 0v to 5v over the working range.

Re-scaling the servo is not so easy. It requires changing the mechanics of the servo.
 

alband

Senior Member
@Dippy: I wasn't aware that the value of the POT mattered?

Surely half way one a 1K POT gives the same voltage as half way on a 10K POT?

Thus, I have changed the POT countless times, though since you asked I assume there is some significance so it seems to be a 4.2K POT. The last one I used was 10K but it seemed to be giving very sluggish reading to the servo, give the way it was "crawling" to position so I changed it.

I'm afraid I don't know what the original was or which one is the original, the original could well be the one in there now!

I'm afraid I'm not sure what you meant by "changing values" :(.

AS for the circuit for the POT just "+" "-" and wiper? Or do you mean the circuitry of the servo's PCB? - that will take a while.

I'm also not sure about what you mean with POT/div? (not doing well here am I?) What is the "div"?

Thanks though, I'll try and get those answeres to you as soon as I know what they are :eek:

@BB: I though it might be op-amps :)(), I'll remind myself of his tutorial and see what I can do. I have a few op-amps around the place and the good part is I get to make another PCB! :D

As for re-scaling the servo (I knew the must be a word for doing that, thanks) surely it would depend on the POT e.g. if I got rid of the resistive track on the extremities that the wiper never reaches it would be much more precise?

Thanks for the quick replys,
David.
 

BeanieBots

Moderator
In your reply to me you have answered the question you posed to Dippy!

Keep thinking track ends and how you might do that.
Adding a resistor at either end effects the overall response of the POT.
(what value of course depends on the value of the POT).

If you were to add a resistor to either end of the pot in your servo, it would have to travel further to achieve the same change in voltage. Hence, a smaller change in position demand would be required.

To go the other way, (a larger change in demand for the same movement) requires a different mechanical ratio between moving object and feedback pot.
Op-amps of course can do that electronically.
Have a read of the tutorial and come back if anything is not clear.
 

Dippy

Moderator
I had assumed you'd pot/divved your POt with another resistor.
In which case all the values will have an effect.
Just having a pot (on it's own) rubbing over a tiny area is not very good design electrically.
If its a tuppenny Ebay carbon track pot then .... oooh.
 

alband

Senior Member
@BB: Now I understand. I actually had the idea of just coating a portion of the carbon track with solder, but it was not to be.

@Dippy: Yep, just a POT on its own :D You'll be pleaseed to know though that this time eBay was not my source (for once). All the ones I've used and am using are from servos.

I've been going through Dr.'s tutorial and I'm already stuck!

I've got to the bit where the second circuit is set up with a gain of 2.2, where you are supposed to get 3.16V out.
The problem is with those "voltage followers". I'm using a spare LM339N instead of the LM324 used in all the diagrams, but I assume this will make no difference? I've got 2V and 2.3V as it says, but the output of each "follower op-amp" is vertually nothing :confused:

The positive has 2.29V going in and 0.06V going out.
The negative has 2.01V going in and 0.03V going out.
The reference POT is set at 5V but only 0.06V is coming out of the voltage follower.
The over-all output of this differential amplifier is 0.04V.

Hopefully you can see my set-up in the picture.
 

Attachments

Dippy

Moderator
I thought the DOc's post was for diff amps???

You just need a simple gain setup.
Just go to Farnell and get a Data Sheet on something like an LPC662 for exaple.
http://www.farnell.com/datasheets/78429.pdf

Then whizz down and look at page 10 which shows some basic inv and non-inv layouts.
Obv you'll have to translate to your op-amp pinout (or do we call it 'legout' on this Forum?:) )
Sorry, I can't follow your breadboard, my kettle is boiling.
Schematics are SSSSOO much easier.

It should be a piece of cake.
 

BeanieBots

Moderator
Oh David, we've done this one before with you.
The 339 is a COMPARITOR (with open collector outputs) and NOT an op-amp.
However, you make it pretend to be one.

It has open collector outputs. To make it "play like" an op-amp, you need to give it the ability to SOURCE current.
You can do that by adding a pull-up resistor to the output.
The value depends on what currents are involved within the feedback loops but a few k should work for most cases.

Get yourself some LM324 quad packs. Very nice (almost rail to rail) general purpose op-amp. I use them for just about everything unless something particularly more specialised is required. For 8/10 bit accuracy and gains <50, they will cover just about everything you could ever need when working with PICAXE and analogue voltages.
 

alband

Senior Member
Oh David, we've done this one before with you.
The 339 is a COMPARITOR (with open collector outputs) and NOT an op-amp.
However, you make it pretend to be one.
Oh yeah, I remember that... now. Well that'll be why they were lying around then. I've found some lovely SMT LM324's on eBay :)rolleyes:). I'll go the LM324 route because by the looks of Dr.'s tutorial, I will need four op-amps and it will be smaller to have them in one package.

Incidently, adding the resistors did work but it seems to have affected the voltages slightly, plus in the final version, I am trying to keep size down so I could do without those pull-up's.

Right, I'll finish Dr.'s tutorial (alas without a live demo on my breadboard) and see if I can put together a schematic of what I think I need to get my system to work.

Thanks,
David.
 

BeanieBots

Moderator
It should be possible to do what you want with just one op-amp.
Using more just makes the maths easier and the resistor values less critical.

If you have a negative power rail available it would make things even easier.
 

boriz

Senior Member
I don't see how an accelerometer can be made to work properly. How do you filter out unwanted responses to vertical and horizontal acceleration? When you go from sitting still, to moving forwards, you don’t want the barrel to tilt do you? Similarly, this thing will I assume be used on uneven ground, maybe bouncing a little? Can the vertical component be eliminated?

A gyro would ignore everything but the tilt. Which is what you want.
 

BeanieBots

Moderator
Why buy a gyro when you can have so much fun learning about op-amps so that you can make your own from an accelerometer!
 

alband

Senior Member
I never thought I'd see the day when you defended my choice of using an accelerometer BB.

Seriously though, I know it can't elliminate for the starting and stopping effect, but that would be the only cause of unwanted noise (except for the natural tendancy of it to fluctuate a bit when it is static, but that can be programmed out. The bumpy surfaces are what it is designed for; tank pitches forward and the barrel raises. Any vertical bumps are read on the vertical axis so they aren't a problem.
It would he nice to be Able to use an inclinometer or gyro but they all cost so much.
Now I need to get back to that tutorial, ah it is quarter to midnight though. I may get back to it in the morning.
 

BeanieBots

Moderator
I never thought I'd see the day when you defended my choice of using an accelerometer BB.
Maybe I was being sarcastic;)

Either way, you will learn from the experience which is a good thing.
I gave you a hard enough time last time around so now it's time to get you using and understanding op-amps. (don't forget to refresh on PID).
 

alband

Senior Member
I don't have a negative rail available, although, I believe you can get chips to produce and artificial one? This just seems like it it's overcomplicating things though.

I did actually have a really good go at a PID program but I just doesn't work unless I can translate the reading from the accel into a servo position.
 

Attachments

BeanieBots

Moderator
Negative is easy. Either a chip (eg 7660) or use PICAXE PWM to drive a capacitor/diode pump.
PICAXE will not be fast enough for the PID.
Use the op-amp to do the PID stuff.

Accelerometer + setpoint + PID loop = gyro.
Think we've been here before!
 

alband

Senior Member
Attached is a schematic of what I think I need.

The accel output currently swings from 3.41V to 3.66V
So gain is 1.35 (27k/(10k+10k)) Which brings 3.66 to 4.941V.
Negative dif input is set as close as possible to 3.5383V as possible (midway between 3.41 and 3.66).
Refference is set to 2.5V.

Now the problem is that bit at the end of Dr.'s post about LM324 not being rail-rail. So how do I overcome this? Lower gain?

As for negative supply: I have a hunch that this will be the solution for the above problem? Another chip just makes this whole thing seem even bigger, so I like the idea of using the PWMOUT but I know nothing about a capacitor diode pump, so I'll go look that up now and see if I understand it.

PID: The PICAXE was running at 32Mhz and was capable of doing the calculations in time but the result was awful because the number going in was not the same as the number that I was comparing it to. Also, the 20X2 will be running at 64Mhz so I would have though it would be fast enough? This is base on my attempts at a PID algorithm though, so a PICAXE may not be fast enough for a proper one.
Now... getting the op-amp to do the PID?! :eek: I've only just got my head round the idea that an op-amp is effectively y=mx+c (m = gain, c = reference, quite like that). But how on earth can an op-amp do PID?

PID = proportion + integration + differentiation.
So proportion could be the gain of the op-amp, that's easy enough. Would integration (i.e. sum of previous errors) be the reference? what about the differentiation (i.e. difference between previous errors)?
:confused:
I'll go see if I can have more luck with the capacitor/diode pump thing.
 

Attachments

Dippy

Moderator
Haven't the time to help you with your PID, but differentiation and integration are two very basic functions of simple Op-Amp circuits. Compare with filtering techniques.

These would be 'My First Circuit' in op-amp classes at college.

Has this thread moved on from simple gaining-up a pot to accelerometers and trying to PID?
.. I can't keep up. Must be me age :)
 

BeanieBots

Moderator
Alband,
Not gone through your circuit in detail but I'm sure it can all be done with one op-amp including the PID.

Remind me, so that we have it all on one page again.
The signal range of your sensor. 3.41V to 3.66V yes?
You want it to be 0v to 5v, yes?
Your supply is a good quality 5v (ie can be used as a reference).

I have to go now and won't be able to dedicate any time until tommorrow but I'm sure it can be done with 1/4 LM324.

Do you know the source impedance of the sensor?
If it gives out say 3.500v, what does that voltage become if you put a 1k load on it. (any starting voltage will do, just let me know the numbers).
If you don't know or can't work it out, don't worry, we'll just shove in another op-amp to buffer it.
 

alband

Senior Member
Sounds a bit stupid, but I'm not sure what you mean by a 1k load? 1k to ground or 1K in series with the multimeter?

Staring voltage at accel output = 2.95V
Voltage with 1k resistor to ground = 11.2mV
Voltage with 1k in series with multimeter = 2.95

Hope that helps.

Edit: oh and yes that is all summed up correctly.
 

Dippy

Moderator
Things like sensors output a voltage, but that voltage varies (as you have discovered) depending on the load (usually down to the ground reference 0).

I assume you are measuring the sensor end and NOT the ground end? (Please, please say yes)
By doing the test down to ground with a resistor or two you can determine the approx source impedance. If, for example, it had a SI of zero then it wouldn't have been affected by your 1K down to ground.
Many things can affect the results but it gives a good 'first estimate'.

You obv know about putting resistors in series and the voltage drops associated and the reasons and the calculations? (real basic stuff).

Putting a 1k in series with your Multimeter is bound to have b-all effect. Your DMM probably rated at several MOhms, so an extra 1K will have little effect, as you have discovered.

Now BB knows that your output is feeble (high SI) he can use that info to calculate things including buffering.
You're lucky you've found someone with a big brain to do the hard work for you :)
 

alband

Senior Member
I assume you are measuring the sensor end and NOT the ground end? (Please, please say yes)
Yes. "I may be incredibly think, but I'm not stupid" ;)

I knew it would have little affect, but I thought it's be better to give as much info as I could.

By the sound of things, it does have a high impedance although the datasheet holds no clues (that I can find).

I also had a google on op-amp differentiation and integration.
Integration = http://www.visionics.ee/curriculum/Experiments/Opamp integrator/Opamp Integrator1.html I think I understand that one, basically, the feedback capacitor adds on the previous error to the present error?

Differentiation = http://commons.wikimedia.org/wiki/File:Op-Amp_Differentiating_Amplifier.svg I'm not so sure about that but I'm pretty sure it is mainly to do with the capacitor again.
 

Dippy

Moderator
Can't you I2C or SPI it and save the hassle and extra components?
I only had a quick squiz so you'll know more about it than me.

Or is SPI and I2C a bit daunting?
(I know looking at timing diagrams can be frightening, believe me if you saw what i just had to do with SPI I can agree with that!)
 

BeanieBots

Moderator
Only got a few minutes for a quick reply right now.

Had a quick look at the datasheet.
Output impedance is typically 32k. (min 24k, max 40k).
It's been "designed" high so that only a single cap is required for filtering.
Talking of which, bandwidth is 1kHz. So not VERY fast.

As Dippy points out, it supports SPI and I2C.
Not only does that negate the need for any analogue processing PICAXE side, it is also 12-bit resolution. That's four times better than the PICAXE could do using ReadADC10.
Could save you a lot of bother on the analogue side.

The links you posted.
The integrator is WRONG.
Didn't check the maths but the diagram is WRONG.
If the diagram is wrong, I'd not trust the maths (but it did look OK at a glance).
Just goes to show, you can't trust everything you read on the internet.
The differentiator looked OK.
 

alband

Senior Member
I wondered about using I2C in fact is was one reason why I bought it, but I decided that learning I2C or SPI in amongst everything else would make using analogue easier. However, since you understood the 12bit acuracy (which I didn't) is does sound like the easier option now.
I just need to learn how to use I2C or SPI. Any recomsndations which? I'll have a look at that section of the datasheet and the I2C stuff in the various manuals in the mean time.
 

SilentScreamer

Senior Member
I wondered about using I2C in fact is was one reason why I bought it, but I decided that learning I2C or SPI in amongst everything else would make using analogue easier. However, since you understood the 12bit acuracy (which I didn't) is does sound like the easier option now.
I just need to learn how to use I2C or SPI. Any recomsndations which? I'll have a look at that section of the datasheet and the I2C stuff in the various manuals in the mean time.
I don't know if its any good but this claims to be a tutorial - http://www.rev-ed.co.uk/docs/axe110_i2c.pdf

Try searching around the forum if you need it, I'm sure someone must have written one.
 

nbw

Senior Member
Did someone here write an SPI tutorial? That would be a useful addition to the dox indeed, given the newer PAXs feature SPI.
 

alband

Senior Member
I found that I2C tutorial and have decided (temporerally at least) to go with I2C because I'm a tad more familiar with it. However, I'm finding the accel'e datasheet as easy as mud for fishing for information in; in particular the 4 things that the I2C tutorial lists as needed.
Anyway, I'll keep going and see if I can get it talking to a PICAXE.
Thanks,
David.
 

Dippy

Moderator
Keep trying with the serial David. If you can do it then it'll be more reliable.

But I know how tricky it can be just looking at the description and timing diagrams - thats why most people always start their posts with a request asking if someone has already written something.

Just recently I had to write an SPI routine to talk between PIC 3 wire SPI and a 2 wire SPI device. I spent nearly a day just doing that because the other device required some very odd logic to get it going.

If you manage it you will learn lots more than just cutting&pasting someone else's code.
 

BeanieBots

Moderator
As you keep going about component count and size, I2C is possibly your best bet. (just another two resistors).
If you do want to follow the analogue route (potentially not even using a PICAXE at all), here's a link to a PID circuit.
http://www.ecircuitcenter.com/Circuits/op_pid/op_pid.htm

It actually uses no less than 5 op-amps, but just like complex logic circuits, it is possible to reduce down. It can be done with just one op-amp. Even the buffer amp gets eleiminated by taking into account the source impedance in your gain calculations.

Which would be the best route?
Well, that's really up to you. There are pro's and con's either way.
If I was doing it (short of buying a gyro;)) I'd choose the analogue method largely because I'm more comfortable with that type of design, no need to worry about quantising issues and integer maths. Also, I already have all the required kit, decade component boxes for fine tuning, scope etc, etc.
Also, analogue methods don't have the speed problems likely to be encountered with a micro. I'd also hazard a guess that the COMPLETE solution would also be smaller.

Digital on the other hand means you can play with the numbers via your PC rather than soldering iron (or decade box). Easier to change later if anything on your tank changes. (eg heavier or longer barrel would require a change to the PID terms.)

Good like either way and keep us posted on your progress.
 

alband

Senior Member
Suprise suprise, I need help

The titles says it all really. This is the code I have so far:
Code:
		#PICAXE28X2

		hi2csetup i2cmaster,%00110010,i2cfast,i2cbyte


main:		'hi2cout 0,(0x80)
		hi2cin %10000000,(b0)
		hi2cin %10000010,(b1)
		hi2cin %10000100,(b2)
		sertxd (#b0,",",#b1,",",#b2,",")
		goto main
I am pretty sure I have the hardware set up correctly. SCL on the accel to pin14 on the 28X2, SDA on the accel to pin15 on the 28X2; both will pull-up 4k7's. Also with all the relevant pins on the accel high and low, to enable I2C coms.

I'll provide the datasheet again for ease: http://www.kionix.com/Product-Specs/KXPS5-3157 Specifications Rev 1.pdf
The relevant pages are:
6 = pinout
13 = general I2C intro
14,15 = read/write sequences
19 = what I would call commands but surely have a real name

I'm obviously trying to read from it. This is the relevent passage for reading from it via I2C:
When reading data from a KXPS5 8-bit register on the I2C bus, as shown in Sequence 3 on the next page,
the following protocol must be observed: The Master first transmits a start condition (S) and the
appropriate Slave Address (SAD) with the LSB set at &#8216;0&#8217; to write. The KXPS5 acknowledges and the
Master transmits the 8-bit RA of the register it wants to read. The KXPS5 again acknowledges, and the
Master transmits a repeated start condition (Sr). After the repeated start condition, the Master addresses
the KXPS5 with a &#8216;1&#8217; in the LSB (SAD+R) to read from the previously selected register. The Slave then
acknowledges and transmits the data from the requested register. The Master does not acknowledge
(NACK) it received the transmitted data, but transmits a stop condition to end the data transfer. Note that
the KXPS5 automatically increments through its sequential registers, allowing data reads from multiple
registers following a single SAD+R command as shown below in Sequence 4 on the following page.
If you have page 15 open also it will just be easier to talk in their terms.
What does the PICAXE compiler "fill in" for the user, in terms of I2C com's? When reading from the device, it states that the PICAXE should:
first send a "Start Condition",
then the slave's address along with the write bit of "1".
The device then acknowledges this.
The PICAXE then sends the register address of what it wants,
then the device acknowledges again.
Then the PICAXE sends a "Start condition" again and the register address, this time with the read bit of "0".
The device then acknowledges a third time, sends the data requested,
then the PICAXE sends a stop condition.
So basically, how much of that is the standard I2C protocol? And, how much of it is included in the "hi2cin" command? You can see that my current understanding is that most of it is included in the command, but it isn't working, it just returns 255 repeatedly (255 being the default number?)

Also, I want to read the x, Y, and Z positions. On page 19 it gives the registers for each of these, but it gives a LSB and MSB option for each, which should I use?

The last thing is, the I2C guide from Rev Ed states I need to know the address length, either word or byte. I'm not quite sure what this is and haven't found it in the datasheet, so I've just kept guessing.

Oh and also, the slaves address is set at "001100xy". "x" is either "1" or "0" depending on whether the "CS" pin is high or low (I have it high, so x=1). In the I2C guide the last bit is said to define whether, the PICAXE wishes to read or write to the device. In general, I want to read from it, so I have set it to "0", but as clearly stated in the acce's manual, during the sequence of reading from it, the PICAXE needs to transmit to it, at one point with this particular bit needing to be "1" to signify writing. Is the "hi2csetup" line in my code incorrect, or even unnecessary.

Hope this "little" post wasn't too uncomprehending! :eek:
Thanks,
David.

Addit: one other, other thing: I have currently chosen the I2C route, but give your post BB, I'm not too sure. If you sincerely think it will be impossible to set up a PID algorithm on the PICAXE even after obtaining a presumable "wobble" free, accurate reading form the accel, then I will revert back to the analogue method with op-amps, those LM324's have arrived BTW.
 
Last edited:

BeanieBots

Moderator
I'll leave it to someone better at explaining than myself about the I2C issue but you need to read up on again.
The read/write bit (y in your terminology) is set accordingly by the PICAXE. You do not need to worry about.

IMHO you WILL struggle to get a PICAXE running a PID algorythm fast enough but the proof will be in the trying. Maybe some members far better at coding than myself can help you out converting your PID algorythm into integer maths and possibly suggest ways of getting the timings right and fast.

I have tried and failed to get fast PID working with PICAXE so I am NOT the person to advise you on the coding. Not knowing any of the mechanical parameters of your setup (nor having the time) I can't comment on how fast your iteration would need to be.

The real killer doing it in code is the integer maths and 16 bit resolution.
In a PID system I did for a water temp controller (nice and slow response) the loop was too fast for the "I" and too slow for the "D" when only using 16 bit. The two options were to either go to 32 bit resolution or split the loop into two levels of nesting. Either way, very maths intensive and hence slow.

Just because I couldn't get a good response doesn't mean it's not possible. I'd love to be proved wrong.
One option might be to build to an analogue PID loop, prove it works, reverse engineer to get the PID terms, then see if a PICAXE can replicate the response of the (known working) analogue circuit. That way you would not be fighting with two unknown variables. Namely:-
1) are your PID terms correct.
2) can the PICAXE do it.
 

alband

Senior Member
Hmmm. I can't actually do much at the moment because I'm away from home, but I can make decisions.
When Reading the variable from the accel I've always experianced a lot of fluctuation. This has of course made the gun jitter unless I decreased the acuracy. Thus I am quite keen to try the I2C method to see if it was due to the PICAXE's readadc abilities of whether that was the true value being given by the accel. Therefore I think it is worth trying the I2C just to see if it solves the fluctuation, so I'll have a re-read of the datasheet and see I I can better-understand it. If the I2C didn't cure the problem then obviously there s no as much advantage of using it and the op-amps would be the way to go. However if I do end up getting some nice crisp, constantnumbers, this will leave PID looming on the horizon. As far as my attempts have taken me PID is this:
P= multiply the error by a adjustable constant
I= make a total of a number of previous errors (how many should he used though, or is this also an adjustable constant?)
D= difference between the last to errors (I.e. If the error is getting bigger then more correction is needed and vice versa.
So far the main drawback for me has been the lack of negative numbers. So:
Is it possible to get a chip that can handle negative numbers and make it do the calculations?
Is it possible to get a dedicated "PID chip" where I just send it the error and the setpoints and the gain values and it will work out the correction needed?
Basically can we think of any easier wY together a PID algorytuem once wemght have an accurate reading from the I2C coms?

Thanks,
David.

P.S. Sorry ifthere are more than the usual number of typos and miss-spellings; this has beentyped out on my iPod so ithas graciously taken to task amending poor attempts at spelling "pod" and taken a distinct liking for extra capital letters! Despite this, there is no spell check:eek:
 

BeanieBots

Moderator
Alband,
You're getting there.
"Is it possible to get a dedicated "PID chip" where I just send it the error and the setpoints and the gain values and it will work out the correction needed?
"
Not a chip but you can programmable PID controllers.
They cost $$$ and are big. You still need to tell them all the numbers you need for YOUR PID loop. Some will try and work it out for you. They cost $$$$.

The PICAXE ADC should not be responsible for any instability on the read values. The issue is that it is only 10-bit. If it is seen to vary then either your circuit is really bad OR the signal really does vary.

The sensor output is quite high impedance so care needs to be taken with connections. A bit like wiring up a microphone, do it wrong and you get lots of hiss and hum.

Negative numbers.
The way I solve this is to have two loops. One for positive errors and the other for negative errors.
There is of course the uFPU. (it uses I2C).
It will do 23-bit floating point maths but driving it will be something else to learn. Might be an option though as it also has built-in 12-bit ADC and could be 'programmed' to do the whole PID bit as a 'user function' all in full 23-bit floats. Download the data, have a read and then decide if it's a route you want to take.

PID.
P=error * Kp

do
I = I + (Ki * error)
pause It
loop

D=(oldposition-newposition)*Kd*Dt

PID=P+I+D
Where:-
Kp=Proportional gain
Ki=Integral gain,Kt=Integral time constant.
Kd=derivative gain,Dt=derivative time constant.

The problem with integer maths and limited resolution is that the PID loop might not be able satisfy both time constants within the same loop.

Imagine that your loop needs to run every 1mS to keep the derivative and overall bandwidth happy but the integral value neds to climb slowly. The only way would be have a very small value added to "I" with each itteration. The smallest value you can have is 1. If you start putting in things such extra variables to accumulate "I" and then roll-over at a set value, it makes the loop take too long to run and makes things difficult to tune for different "K" values.

The values added, gains used and time between each evaluation are ALL CRITICAL.
What those values are depend on the mass/inertia of your 'load', the torgue/gear ration of your servo, sensitivity of your accellerometer, speed of your PICAXE A2D resolution and required accuracy.
 

boriz

Senior Member
All this talk of integrals and derivatives gives me a head ache. Never was very good at maths.

Can’t a PID be simplified to something like this:

-Measure difference between current position and required position
-If less than (a), stop.
-If less than (b), go very slow.
-If less than (c), go medium speed.
-Otherwise go fast.

And tune a,b,c to your app?
 

BeanieBots

Moderator
In a nutshell, NO.
What you describe is a very crude "P" controller.

With "P" only, there will ALWAYS be an error.
The amount of error is inversely proportional to the gain.
If you could turn the gain right up to infinity, you would have an on/off controller. (if it's to far this way, drive it that way and vice-versa).
However, in any system which has delay (eg a motor driving an inertial load), the drive is out of phase with postion. Infinite (or just very high) gain would result in oscillations.

It really isn't all that complex. However, the terms can be a little daunting.
Get to know and understand what those terms mean. They are only words used to describe a function.

PID is one of the most BASIC controll funtions. You should get to know it if you ever want to controll anything. It's applicable to almost everything.
 
Top