Shop OBEX P1 Docs P2 Docs Learn Events
Floats??? — Parallax Forums

Floats???

lboucherlboucher Posts: 139
edited 2009-05-13 19:58 in General Discussion
Hi All

I am building a backpack GPS system.
To keep cost/weight down i will not be using a tablet PC.
I will probably use this data logger.
http://www.acumeninstruments.com/Products/SDR2-CF/index.shtml

The other side of things is that i want to split the serial coming out of the GPS.
1 will go to the logger, the other i need to monitor to ensure certain variables stay within the right range.
I was thinking i would use an SX, read in the serial, look at the numbers then light up various LED's based upon the results.

The question is.
The data i need to look at is a 4 Byte Float 32.
I need to know when the value is greater than 0.02.
(If it matters, the value will never be less than 0)

Can i do this with the SX???

Thanks
Lucas

Comments

  • PJMontyPJMonty Posts: 983
    edited 2009-05-09 05:47
    Lucas,

    Your GPS is putting out floating point data?

    If you really need a floating point comparison, you'll first need to find out what floating point format the data is stored in. If it's IEEE-754, then you're ahead of the game. Check out this page:

    www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    In particular, look down the page under the heading Comparing using integers. This describes a features of IEEE-754 floats which says that if you're just doing a comparison, you can treat the data as 32 bit integers and then simply compare tham. For this you'll need to find out the value of .02 as an IEEE-754 format float, and then do a 32 bit integer compare of it to your incoming data.

    Thanks,
    PeterM
  • lboucherlboucher Posts: 139
    edited 2009-05-09 14:35
    Thanks for the info.

    Yes my GPS unit is putting out floats. (Trying to control a 10,000 dollar GPS unit with 50 bucks worth of parts.)

    Would this be easier with a propeller or stamp?




    PJMonty said...
    Lucas,

    Your GPS is putting out floating point data?

    If you really need a floating point comparison, you'll first need to find out what floating point format the data is stored in. If it's IEEE-754, then you're ahead of the game. Check out this page:

    www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    In particular, look down the page under the heading Comparing using integers. This describes a features of IEEE-754 floats which says that if you're just doing a comparison, you can treat the data as 32 bit integers and then simply compare tham. For this you'll need to find out the value of .02 as an IEEE-754 format float, and then do a 32 bit integer compare of it to your incoming data.

    Thanks,
    PeterM
  • lboucherlboucher Posts: 139
    edited 2009-05-09 17:59
    So how would one do a 32 bit int compare in an SX microcontroller?

    Lucas

    Guess thats a stupid question.

    I just need to compare byte by byte.
    PJMonty said...
    Lucas,

    Your GPS is putting out floating point data?

    If you really need a floating point comparison, you'll first need to find out what floating point format the data is stored in. If it's IEEE-754, then you're ahead of the game. Check out this page:

    www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    In particular, look down the page under the heading Comparing using integers. This describes a features of IEEE-754 floats which says that if you're just doing a comparison, you can treat the data as 32 bit integers and then simply compare tham. For this you'll need to find out the value of .02 as an IEEE-754 format float, and then do a 32 bit integer compare of it to your incoming data.

    Thanks,
    PeterM
    Post Edited (lboucher) : 5/10/2009 2:41:11 PM GMT
  • PJMontyPJMonty Posts: 983
    edited 2009-05-10 21:09
    Lucas,

    No, it's not a stupid question. You're on the right track in that you do the work a byte at a time. However, since you're trying to compare to a value, you need to do a 32 bit signed compare. This means that basically you're doing a 32 bit subtraction and seeing if the result is 0 (they're the same) or one is bigger than the other. So, if you're subtracting your new value from the constant .02, if the result is negative than you're new value is greater than .02, and vice versa if the result is positive.

    In order to do a multibyte subtraction, you need to have the carry flag factor into your results. Go to this site...

    www.SxList.com

    ..and look for the math routines on the site. They don't seem to have a 32 bit subtraction, but a 16 bit subtraction can be extended by simply doing two more bytes worth of work.

    Thanks,
    PeterM
  • lboucherlboucher Posts: 139
    edited 2009-05-11 00:00
    Cool

    Thanks for the site and the info.

    I actually don't think i need to worry about the negative.

    I am monitoring the standard deviation of the GPS solution, and that will never go negative.

    I think as long as i know the 4 int representation of the number i want to compare with I can simply have 4 if statements and make it work.

    Thanks.
  • lboucherlboucher Posts: 139
    edited 2009-05-11 01:29
    So wrote up a quick test in matlab.

    See attached.

    Works fine as long as there are no negative numbers.

    So now the question is, how many nested if functions can an SX handle.
    Guess i could always find a way around this issue using GOTO's.
  • BeanBean Posts: 8,129
    edited 2009-05-11 15:34
    Lucus,
    · Here is a program that will convert a IEEE754 32-bit float to a WORD value of trunc(abs(float * 65536))
    · With this program the value 0.02 will be converted to 1310.
    · You can then compare this WORD variable to see if the float value is greater than 1310.
    DEVICE SX28,OSC4MHZ
    FREQ 4_000_000
     
    PROGRAM Start NOSTARTUP
     
    float     VAR BYTE (4) ' Holds IEEE754 32-bit floating point value
    negative  VAR BIT      ' 1=float value is negative
    value     VAR WORD     ' Holds trunc(abs(value * 65536))
     
    Start:
      ' Convert IEEE754 32-bit floating value to the truncated value of (value * 65536)
      PUT float, $3c, $A3, $D7, $0A ' 0.02 = $3C $A3 $D7 $0A
     
      ' Check if value is negative
      negative = float(0).7
     
      ' Shift the float to the left 1 bit, float(0) = Exponent; float(1) = Mantissa_MSB; float(3) = Mantissa_LSB
      float(0) = float(0) SHL 1
      float(0).0 = float(1).7
      float(1).7 = 1 ' Implied 1 as MSB of Mantissa
     
      value = float(2),float(1) ' Set value as 16 most significant bits of the mantissa
      
      IF float(0) > 126 THEN
        value = $FFFF
      ELSE
        IF float(0) < 110 THEN
          value = 0
        ELSE
          DO UNTIL float(0) = 126
            value = value SHR 1
            INC float(0)
          LOOP
        ENDIF
      ENDIF
     
      ' 0.02 * 675536 = 1310 (when truncated)
      ' The variable "value" now holds the value of trunc(abs(float * 65536))
      ' If the variable "negative" is 1, then the value is negative
    
    END
    
    

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...



    Post Edited (Bean (Hitt Consulting)) : 5/11/2009 5:09:20 PM GMT
  • lboucherlboucher Posts: 139
    edited 2009-05-13 19:58
    Awesome

    Thanks alot
Sign In or Register to comment.