If you'd like to learn how to caculate this yourself, research the statistics calculation called regression. There are a number of variants (including non-linear ones). Regression is a class of formulas that can be used to find the algebraic equation that closely matches a set of x and y values. Linear regression finds the best straight line through a series of points in a 2 dimensional plane. Excel has formulas (check out LINEST) for doing this calculation on a series of numbers. Some TI calculators can also do the calculation. You can find a wealth of information on Wikipedia about it as well. Note, that unless your number series is a perfect straight line, there will be some error. Linest in excel gives you this error information (deviation) to allow you to decide if the fit is close enough. Basically, the smaller the deviation number, the better the fit. The nonlinear regressions available (like log regression and power regression) can fit data onto curves and usually a better fits for realworld scenarios. For instance, if you are measuring the resistance of a thermistor and want to convert that resistance to a temperature rating, you could place the thermistor it in a cup of icy water with a thermometor and just log the resistance value at a number of temperatures as it warms up. Then you plug the series of numbers in a power regression formula and come up with a power algebra equation that very closely approximates the observed values.

deSilva

09-16-2007, 09:49 PM

@mcstar: I should consider this a quite unfair remark http://forums.parallax.com/images/smilies/smile.gif

Edit: But on the other hand most microcontrollers (I think) do nothing but straighten out some device's characteristic curve....

Post Edited (deSilva) : 9/16/2007 2:55:05 PM GMT

Mark Bramwell

09-16-2007, 10:15 PM

Hi Skogsgurra,

I think you were on the right track but this gives closer results:

result := 1000 + ( ( a / 127 ) * 1000 )

if a = 63 then result = 496

With the multiplication factor, I only got 492
Maybe you had a typo because: 1000 / 127 = 7.874

Fred Hawkins

09-16-2007, 10:21 PM

In case you don't follow Skogsgurra

larger value range is 1000 (1000 to 2000)
index range is 128. (add one to prevent divide by zero)
min point is 0/1
Mid point is 500/64
Max point is 1000/128

each input value is worth 7.8125 Stated as a fraction: 125/16

Gross integer approximation (which in some cases might be good enough): input*8+1000

Fine integer approximation for using the accuracy of decimal values in the intermediate calculations without floating point
(input*78)/10+1000 'for first decimal
(input*781)/100+1000 'for first two decimals
(input*7812)/1000+1000 'for three decimals accuracy.
and so on

Alternatively, you can skip the decimalization by using the original fraction:
(input*125)/16+1000

All of these approximations assume that an integer return value is sufficient to your purposes.

_______
Edit: hot air arithmetic above. Note behavior at inputs, 0,1,127,128.
I was·extending·to the range of input·values to 128 by adding one. Probably because I like the even numbers 64 and 128.

As others have pointed out, the correct step size is 1000/127 = 7.8740
Thus the damn fraction is unfactorable.
So the third calculation should be
(input*1000)/127+1000

Post Edited (Fred Hawkins) : 9/16/2007 3:40:27 PM GMT

deSilva

09-16-2007, 10:29 PM

This is an approximation rather than a true interpolation, as
+63 -> + 500
+127 -> + 1000

so the "mean factor" is 500/126.5 = 7.905 which undershoots 63 and overshoots 127. It meets 0 which is always a good thing.

An exact fit needs a quadratic function.

Post Edited (deSilva) : 9/16/2007 3:34:41 PM GMT

Mark Bramwell

09-16-2007, 10:38 PM

I don't get the '128' part. The highest value needed/quoted is 127, not 128.

If a = 127, the formula would be 127/127 * 1000 which equals 1000, we then add 1000 which gives us --> 2000 Bang on!
If a = 0, the formula would be 0/127 * 1000 which equals 0, we then add 1000 which gives us --> 1000 also bang on!
If a = 63, the formula would be 63/127 * 1000 which equals 496, we then add 1000 which gives us --> 1496 Slight undershoot but closer than the other guys

Using the method I quoted, you will never get a divide by zero error because I am never dividing by the input number.

Re-read your math books:

0/127 is ok

127/0 is bad

Fred Hawkins

09-16-2007, 10:40 PM

Mark, if your message was directed to me, see my hot air footnote above.

Mark Bramwell

09-16-2007, 10:44 PM

Yes Fred, I see your comment now.

Oh well. I think we have beaten this one up.
I am sure Computer Guy now has lots of samples to follow from us and can see how we debated and derived the answers.

deSilva said...
@mcstar: I should consider this a quite unfair remark http://forums.parallax.com/images/smilies/smile.gif

Edit: But on the other hand most microcontrollers (I think) do nothing but straighten out some device's characteristic curve....
I'm not sure what's unfair about it, I was just pointing out the knowledge needed to determine these formulas for one's self.· However, I'm sorry for· the offense.

Fred Hawkins

09-16-2007, 11:13 PM

Mcstar, you did offer advice for those of us who did wonder what the hell to do if the relation was not linear. Thanks for that. Though I have to admit that Wiki on most math becomes Greek to me just as soon as I tab down the page.

deSilva

09-16-2007, 11:23 PM

@mcstar: Please see my remark as a consequence of what computer guy said: "..However I am not very mathematically minded...".

You are absolutely right, as - in fact - this is a non-linear problem, and already people start argueing http://forums.parallax.com/images/smilies/smile.gif

mcstar

09-16-2007, 11:27 PM

LOL, yes interpretting wiki's use of calculus and pretentious assumption that we all intuitively know what all the greek symbol's are supposed to represent is sometimes a problem for me as well. The help files in Excel do a descent job of simplifying the concepts for LINEST and LOGEST. Also there is an abundance of information from other internet sources geared towards teaching college students regression and statistics that might be helpful. I've been enjoying this course of late... http://home.ubalt.edu/ntsbarsh/Business-stat/opre504online.htm
It's a college course on statistics but it starts with the conceptual and then moves into the expressive (formulas symbols and such). I've found it most helpful.

Mark Bramwell

09-16-2007, 11:35 PM

Why is it non-linear? His original example shows that it is linear. His only mistake (and it was a very small mistake, AKA approximation) was guessing that 63 is the mid-point. But 64 is also not the mid-point. His guess is close enough although we know that 63.5 would have been the real mid-point. The constant of '1000' doesn't change the slope or 'curve', it simply offsets the position on a linear graph.

deSilva

09-16-2007, 11:46 PM

Mark, this an interpretation of yours. When someone says he needs a function with
0-> 1000
63 -> 1500
127 -> 2000
I have to assume he wants that http://forums.parallax.com/images/smilies/smile.gif

There is a well known and perfect linear approximation for this which is
Y = 1000 + 7.905 * X

There is another well known and perfekt quadratic interpolation, I have no intention to compute though EXCEL can do that within 2 minutes (plot a graph, press the right mouse key and say: smooth, show parameters, duh, duh..)

An interesting point is the acceptable error-range, caused by approximation and integer arithmetic (which can become unexpectedly high in case of divisions!)

I stress this a little bit, as it is one of the focal points of professional microcontroller programming http://forums.parallax.com/images/smilies/smile.gif

Graham Stabler

09-17-2007, 03:06 AM

CP,

Can I ask what this is for and where the values came from?

Graham

computer guy

09-17-2007, 08:31 AM

The values come from an adc0834 and the values need to be converted to control a servo.

The adc0834 is connected to a joystick from an xbox controller and 0 is fully down 63 is in the middle and 127 is full up.

That's why I said limit it, it would just mean that you got to full scale a little before the stick hit the stop (at 126.008).

Graham

computer guy

09-19-2007, 05:07 AM

Yes but the servo values are used to control motor controllers.
I don't know what would happen If they got a value higher than that.
It is working perfectly with your previous method anyway with 63 being 1498 or something close to that.

When I say limit it I mean make any number greater than 2000, 2000 and any number less than 1000, 1000. You would loose only a tiny bit of travel on the stick.

Anyway if its working then fine I just wanted you to understand what I meant.

Graham

t2nt2n

09-19-2007, 04:12 PM

Your problem is not well asked.

What do you want ?

If you want a line curve, there are no exact solutions ( a line needs only 2 points to be draw , and 3 can be advisable if (and only if)....they are on the same line http://forums.parallax.com/images/smilies/tongue.gif ).

A regression line can give a solution, the line which is nearest the 3 points.

But only the NEAREST line.
Any spreadsheet has this kind of features.

If you want a parabole (a curve polynomic with some x²), you have only one solution.
The polynom with squares is the only one which gives an unique solution.

Otherwise, if you want polynoms of greater degrees, you have an infinity of each ones.

But perhaps you don't want polynomic curves ?
So what you want to do ?

You have to know what you have to say before learning the first greek word, in fact.

Post Edited (t2nt2n) : 9/19/2007 9:18:18 AM GMT

Graham Stabler

09-19-2007, 04:17 PM

Its actually more or less linear its just that manufacturing tolerances mean that the joystick probably moves a little further from the center in one direction from the other or that the center is actually not quite the center.

I don't think he knows what a polynomial curve is so you are talking to yourself on that one.

Graham

t2nt2n

09-19-2007, 08:02 PM

Graham Stabler said...
I don't think he knows what a polynomial curve is so you are talking to yourself on that one.

Graham

You read my post and reply to it.
So, we are at least 2 around the table http://forums.parallax.com/images/smilies/smurf.gif