Math Problem
computer guy
Posts: 1,113
I need to turn
0 into 1000
63 into 1500
127 into 2000
using the same mathematical calculation for each.
However I am not very mathematically minded.
Some help would be great.
Thank you
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au
0 into 1000
63 into 1500
127 into 2000
using the same mathematical calculation for each.
However I am not very mathematically minded.
Some help would be great.
Thank you
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au
Comments
B = 1000 + 7.8125*A
You can use the floating point functions to do that. Or scale it up a bit and then down again.
B := (10000000 + 78125*A)/10000
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
B := LOOKDOWN(A:0, 63, 127)
B := LOOKUP(B:1000, 1500, 2000)
Thank you
That should do the job perfectly.
deSilva
Thank you
However I need all the numbers in the middle.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au
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
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
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
+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
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
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.
Next problem please....
You are absolutely right, as - in fact - this is a non-linear problem, and already people start argueing
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.
0-> 1000
63 -> 1500
127 -> 2000
I have to assume he wants that
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
Can I ask what this is for and where the values came from?
Graham
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.
The servo needs a value between 1000 and 2000.
Thank you
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au
Post Edited (computer guy) : 9/17/2007 1:36:04 AM GMT
B = 1000 + 7.936*A
Because that will ensure you get 1500 at neutral, well at least after some tweaking and that is what's important I should think.
You may just want to limit the values to the range as well.
Graham
Post Edited (Graham Stabler) : 9/17/2007 2:28:27 PM GMT
That results in 127 being over 2000 so that won't work.
Am happy with your first answer though.
Hopefully with a bit of tweaking it will work perfectly.
Thank you for your assistance.
You have helped greatly with my robot.
I am about to start work on redesigning my site.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au
Graham
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.
Thank you
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au
Anyway if its working then fine I just wanted you to understand what I meant.
Graham
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 ).
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
I don't think he knows what a polynomial curve is so you are talking to yourself on that one.
Graham
So, we are at least 2 around the table