Shop OBEX P1 Docs P2 Docs Learn Events
Math Problem — Parallax Forums

Math Problem

computer guycomputer guy Posts: 1,113
edited 2007-09-19 13:02 in Propeller 1
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 smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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

  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-09-16 12:50
    If your input is A and your output is B, then you have a linear relationship with an offset. Like this:

    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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 13:03
    If you need nothing but the three values you gave, you can also use SPIN's lookup/lookdown operation[noparse][[/noparse]code]
    B := LOOKDOWN(A:0, 63, 127)
    B := LOOKUP(B:1000, 1500, 2000)
  • computer guycomputer guy Posts: 1,113
    edited 2007-09-16 13:17
    Skogsgurra

    Thank you smile.gif

    That should do the job perfectly.

    deSilva

    Thank you smile.gif

    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
  • mcstarmcstar Posts: 144
    edited 2007-09-16 14:46
    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.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 14:49
    @mcstar: I should consider this a quite unfair remark 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 BramwellMark Bramwell Posts: 56
    edited 2007-09-16 15:15
    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 HawkinsFred Hawkins Posts: 997
    edited 2007-09-16 15:21
    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
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 15:29
    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 BramwellMark Bramwell Posts: 56
    edited 2007-09-16 15:38
    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 HawkinsFred Hawkins Posts: 997
    edited 2007-09-16 15:40
    Mark, if your message was directed to me, see my hot air footnote above.
  • Mark BramwellMark Bramwell Posts: 56
    edited 2007-09-16 15:44
    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.

    Next problem please.... smile.gif
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 15:46
    This is so funny smile.gif
  • mcstarmcstar Posts: 144
    edited 2007-09-16 16:05
    deSilva said...
    @mcstar: I should consider this a quite unfair remark 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 HawkinsFred Hawkins Posts: 997
    edited 2007-09-16 16:13
    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.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 16:23
    @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 smile.gif
  • mcstarmcstar Posts: 144
    edited 2007-09-16 16:27
    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 BramwellMark Bramwell Posts: 56
    edited 2007-09-16 16:35
    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.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 16:46
    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 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 smile.gif
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-09-16 20:06
    CP,

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

    Graham
  • computer guycomputer guy Posts: 1,113
    edited 2007-09-17 01:31
    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.

    The servo needs a value between 1000 and 2000.

    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-09-17 10:12
    I thought that might have been it, in that case

    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
  • computer guycomputer guy Posts: 1,113
    edited 2007-09-17 11:58
    Graham

    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. smile.gif
    You have helped greatly with my robot.

    I am about to start work on redesigning my site. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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 StablerGraham Stabler Posts: 2,510
    edited 2007-09-17 12:34
    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 guycomputer guy Posts: 1,113
    edited 2007-09-18 22:07
    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.

    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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 StablerGraham Stabler Posts: 2,510
    edited 2007-09-19 08:29
    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
  • t2nt2nt2nt2n Posts: 21
    edited 2007-09-19 09:12
    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 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 StablerGraham Stabler Posts: 2,510
    edited 2007-09-19 09:17
    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
  • t2nt2nt2nt2n Posts: 21
    edited 2007-09-19 13:02
    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 smurf.gif
Sign In or Register to comment.