Shop OBEX P1 Docs P2 Docs Learn Events
get float ? — Parallax Forums

get float ?

Tesla trooperTesla trooper Posts: 4
edited 2008-05-21 13:28 in Propeller 1
Hi!

I have been trying to read values from a digital mutimeter. The values comes in scientific form, eg. 3,2E+3. Cant find any suitable way to do this...
Is there a "getfloat" command, or something similar?


agi.get[color=#990000] ???[/color](agiVal)                             

agiVal := (flo.FloatToString(agiVal))
agiVal := (vfd.StrToDec(agiVal))

vfd.dec(agiVal)         






Thanks in advance!

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-05-01 16:58
    Hello, when you say you receive values in scientific form, do you mean you get a 32 (or 64) bit value that is IEEE single (double) floating point, or are you receiving the ASCII version of the number ("3" "," "2" "E" "+" "3")?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Tesla trooperTesla trooper Posts: 4
    edited 2008-05-01 20:13
    Hi! I can read the values in hyperterminal, so it should be ASCII
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-05-01 23:07
    Ok, do you do any mathmatical operations on the data, or are you only looking to display the value?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Tesla trooperTesla trooper Posts: 4
    edited 2008-05-02 08:55
    Yes. We collect the data from a digital multimeter. The instrument is sending a value every ~.5 seconds. The idea is that I store the floating point number in a variable ei. "agiVal" in the code I posted. I then use the command "floattostring" to transform the floating point number to a string, which I in turn transform into a decimal number using the command "strtodec". I then intend to use this variable in some simple mathematical operations and display the result on a VFD display.

    However, I can't seem to find any command to collect this floating point number from the multimeter.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-05-06 18:15
    You will need to implement a str2float command. To do this you will need to perform the following psuedo-code:
    Str2Float(str_ptr) : float_n
      'test BYTE[noparse][[/noparse]str_ptr]
        'if == '-' set negative flag for mantissa then increment str_ptr
      'set mantissa equal to BYTE[noparse][[/noparse]str_ptr] - "0" then increment str_ptr by 2 (skipping decimal place)
      'repeat until BYTE[noparse][[/noparse]str_ptr] == "E"
        'mantissa := (mantissa * 10) + BYTE[noparse][[/noparse]str_ptr] - "0"  (shift mantissa and add next decimal place)
        'increment str_ptr
      'str_ptr++ (skip past E)
      'if BYTE[noparse][[/noparse]str_ptr] == '-' set negative flag for exponent, increment str_ptr
      'exponent := BYTE[noparse][[/noparse]str_ptr] - "0"
      'str_ptr++
      'if BYTE[noparse][[/noparse]str_ptr] <> 0
        'exponent := (exponent * 10) + BYTE[noparse][[/noparse]str_ptr] - "0"
      'Now you have 4 variables filled: mantissa, exponent and the negative flags for each
      'if a negative flag is set, perform the two's compliment of the corresponding value
      'pack a 32 bit integer (variable float_n) with the exponent and mantissa fields according to IEEE-754 standard 
      
     
    

    After the routine is performed you will have a floating point representation of the string

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 5/6/2008 6:20:58 PM GMT
  • Tesla trooperTesla trooper Posts: 4
    edited 2008-05-21 11:07
    I actually found a similar program to yours in an other post which I modified to serve my purposes. It turned out that the problem was that the digital multimeter sent a linefeed in the end of each string, however, rxflush made it all work out the way it it was supposed to. But of course, by solving one problem a new one appeared. This is an extraction of my current code:


      repeat                                                                                                
          agi.getstr(agiVal)                                   
           
          stf.StringToFloat(agiVal, @vfdVal)
    
          vfd.str(string($0C))
    
          vfdVal := fmath.FSub(vfdVal, tara)
    
          vfd.str(fstr.FloatToString(vfdVal)) 
    
           agi.rxflush
    
    



    Problem is, "tara" is supposed to be a "calibration-value". I am writing this program for someone, the idea is that he can tune the sufficient calibration and then turn the propeller off. And when he switches it on again the tara-value is still the same as it was when he turned the chip off, and hence he doesn't have to recalibrate it every time he starts up propeller chip. My spontaneous idea was to store this tara-value in the EEPROM, it's just that I can't seem to find any simple way to do that... Is there a way to achieve this?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-21 12:58
    Download "Simple_I2C_Driver" from the Propeller Object Exchange and read the object's comments. If you're using a Protoboard or other board with an EEPROM > 32K, you can store the value at a location above 32K. If there's only 32K of EEPROM, you can still store the value at the "end" of EEPROM which normally isn't used, but, when you download a new program, the value will be erased.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-21 13:28
    If you receive values in the form "x,yEz" where x is the integral part, y is the fractional part, z is exponent,
    you can use the Format object function bscanf

    OBJ
    · fmt: "Format"
    k var long
    buf var byte[noparse][[/noparse]10] 'assumed to hold received value string
    i var long
    f var long
    e var long

    k := fmt.bscanf(@buf,0,string("%d"),@i)· 'convert integer part, reads up to the decimal point (comma)
    k := fmt.bscanf(@buf,k,string(",%d"),@f)· 'convert fractional part, skips the decimal point·(comma)
    k := fmt.bscanf(@buf,k,string("E%d"),@e)· 'convert exponent, skips the E

    Both i and e can be positive, zero or negative, f can be zero or positive.
    Now you just need to combine i, f and e into a floating point number.

    You will find the Format object in the object exchange under Tools.

    regards peter
Sign In or Register to comment.