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

Math problem

MoskogMoskog Posts: 554
edited 2007-12-29 19:53 in BASIC Stamp
Hello

I have this little math problem:

I want to make a NIB out of numbers from 27 and up to 228.
27 should be the same as 0 and 228 should be 15.

I have been studying the scaling description in What's a microcontroller, around page 155, but having a hard time figuring out how to solve my own problem.

Background information to the problem:
I have this MLX90316 mag. rotation sensor that outputs an analog voltage from 0,5 to 4,5 volts depending on the angel of the magnet. Through a AD-converter I get the values 27 to 228 from those voltages as the magnet rotates one revolution.
The resolution of one revolution does not have to be more then 16 levels, thats why I only need a NIB-variable.
The whole thing is for detecting wind direction!

KjellO

·

Comments

  • danieldaniel Posts: 231
    edited 2007-12-29 15:13
    You might find the shift and scale explanation at www.jmckell.com/shiftscale.html to be useful.

    In your case, the sensor would be the X and the nibble's range the Y.

    If you calculate the nibble's value each time, be sure to pay attention to the fact of integer math on the Stamp. You might wish to multiply the D by 100 and then divide the result of the multiply & divide by 100 before adding the C.

    If you want to use the Stamp's operators for multiplying by a fraction, take a look at Tracy Allen's page (http://www.emesystems.com/BS2math1.htm) that describes them in the context of scaling the result from a temperature sensor.

    Why don't you work out the math in a spreadsheet for the range of your sensor's input. You will start to see a pattern in the computed result.

    Once you have done the spreadsheet analysis of the scaling, you might then decide to instead use a LOOKDOWN function to fit the sensor's input into your nibble.

    Daniel
  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-29 15:20
    First, I'd subtract the 27. That would give you numbers in the range of 0 to 201. Now, 201 / 15 is 13.4, so I'd use 13 as a divisor because 201 / 13 is 15 plus a remainder which you can discard. The overall formula is: result = (original - 27) / 13.

    A LOOKDOWN statement is another way to handle this if you want a non-linear conversion.
  • Andy FoxAndy Fox Posts: 46
    edited 2007-12-29 15:24
    Would something as simple as this work for you?

    a = (x - 27) / 13

    With the stamp's integer math this should truncate to 0 <= a <= 15.

    Or you could use the /* to divide by a more accurate scaler... $0D55 I think it would be?
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-12-29 18:27
    After you have subtracted 27, you are left with numbers from 0 to 201.
    x = x MIN 27 - 27  MAX 201 ' use MIN to avoid possible negative number, MAX for definite range 0 to 201
    


    Not only do you want 201-->15, but also any number in the range {189--201}-->15. You can lay it out as
    {0--12}-->0
    {13--25}-->1
    {26--37}-->2
    ...
    {189--201}-->15

    The best divisor is 202/16 = 12.625. The easiest way to deal with the fraction is to multiply everything times 8, so 12.625 * 8 becomes 101:
    x = x MIN 27 - 27  MAX 201 ' use MIN to avoid possible negative number, MAX for definite range 0 to 201
     azimuth = x * 8 / 101  ' multiply by 8 here
    DEBUG ? azimuth   ' from 0 to 15
    



    Another refinement with azimuth is that you might want 0 to be centered around North, say, for example
    {196--0--6} --> 0
    {7--18} --> 1
    etc. and in that case you can do an offset in software.
    x = x MIN 27 - 27  MAX 201 + 6 // 202   ' offset
    



    That kind of offset in software may also be necessary if you for some reason you need to correct the direction reading after the wind vane is installed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MoskogMoskog Posts: 554
    edited 2007-12-29 19:53
    Thank you to everybody for all the good ideas. Now I look forward to check out those things tomorrow.

    KjellO
Sign In or Register to comment.