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

Math Issue

Earl FosterEarl Foster Posts: 185
edited 2008-08-24 13:50 in Propeller 1
I have a procedure that is not producing the results I was expecting.· The procedure steps through a string of characters, convert the ones I want to decimal, and stores that value to a variable called "number".

An example:
I read a string in is as "a456d" and I only care about the number portion. If that number is less then 500 I want to do something
number := 0
repeat i from 1 to 3
   number := number * 10 + string[noparse][[/noparse]i] - "0" 
if number < 500
   Do something


The results I am expecting is that something will be done since the "if" statement is true

if 456 < 500
·· Do something

Can someone explain to me why this shouldn't work?

Thanks



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET

"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman

Comments

  • scanlimescanlime Posts: 106
    edited 2008-08-23 01:25
    This snippet looks fine to me, but there could be problems elsewhere...

    Some questions:

    - How is "string" declared? For this to work, it needs to be a byte array.
    - Are you certain that "string" contains "a456d"? (How did you verify this?)
    - Can you post the complete program?

    Also, if you only care about checking that the number is less than 500, why not just test that the digit in string is < "5"?

    --Micah
  • RaymanRayman Posts: 14,242
    edited 2008-08-23 01:38
    I think the "string" function returns an address to the generated string and so won't work there...

    Try doing "repeat i from "1" to "3"", "number:= number*10 + i-"0""
  • Earl FosterEarl Foster Posts: 185
    edited 2008-08-23 02:16
    I shouldn't have use the word "string" in my example code.· Sorry about that I was trying to keep it simple for explanation sake.

    The actual string is a byte array of 82 characters with the numbers always changing.· Using just a "5" will not work because the number could be 510 or 5000 so looking for 500 is important.

    I am thinking it might be the way the propeller handles run time variables.· The manuals seems to reference run time variables·as being handled differently then contants.· If I declare a contant or assign a value to the variable (num := 350)··the procedure works, however, as soon I as put the run time variable back in place it no longer works.

    The actually number is a float (ex. 234.5) but I am not concerned with the fractional portion of the number so I use the period as my delimiter.· Assume that holder[noparse][[/noparse]82] is populated correctly.
    var
      byte index, holder[noparse][[/noparse]82]
      long num
     
    pub main 
      dira[noparse][[/noparse]1..5]~~                                  'Make Pins 1 thru 5 outputs
      repeat
         repeat while holder[noparse][[/noparse]index] <> "."          'Store only the integer value
            num := num * 10 + (holder[noparse][[/noparse]index++] - "0")
         repeat                                     'Timing to set to have the lights
            if num < 500                            'cycle 40 times per minute
               !outa[noparse][[/noparse]1]                 
               waitcnt(clkfreq/4 + cnt)
               !outa[noparse][[/noparse]1]
               !outa[noparse][[/noparse]2]
               waitcnt(clkfreq/4 + cnt)
               !outa[noparse][[/noparse]2]
               !outa[noparse][[/noparse]3]
               waitcnt(clkfreq/4 + cnt)
               !outa[noparse][[/noparse]3]
               !outa[noparse][[/noparse]4]
               waitcnt(clkfreq/4 + cnt)
               !outa[noparse][[/noparse]4]
               !outa[noparse][[/noparse]5]
               waitcnt(clkfreq/4 + cnt)
               !outa[noparse][[/noparse]5]
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman

    Post Edited (Earl Foster) : 8/23/2008 2:21:22 AM GMT
  • mparkmpark Posts: 1,305
    edited 2008-08-23 04:56
    Maybe set num and index to 0 at the top of the loop?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Michael Park

    PS, BTW, and FYI:
    To search the forum, use search.parallax.com (do not use the Search button).
    Check out the Propeller Wiki: propeller.wikispaces.com/
  • BEEPBEEP Posts: 58
    edited 2008-08-23 07:55
    Earl Foster said...
    number := 0
    repeat i from 1 to 3
       number := number * 10 + string[noparse][[/noparse] i ] - "0"
    if number < 500
       Do something
    
    


    number := number * 10 + byte[noparse][[/noparse] myString ][noparse][[/noparse] i ] - "0"
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-08-23 12:00
    Somebody said...

    repeat 'Timing to set to have the lights
    if num < 500 'cycle 40 times per minute
    !outa
    waitcnt(clkfreq/4 + cnt)

    This will become an endless loop.


    One thing that is not clear to me in some of these examples is,

    If there are letters within this string, how are you separating them from the numbers?
  • Earl FosterEarl Foster Posts: 185
    edited 2008-08-23 12:13
    I appreciate the help but I don’t want to get bogged down in code. I think it comes down to a store and process question.

    Usually the propeller would store the string in ascii format. So if I have a string that is 123 and want to store that as a number I should be able to convert and store it.

    Number := 0
    Repeat 1 to 3 times
    Number := number * 10 + (holder[noparse][[/noparse]index]-“0&#8221[noparse];)[/noparse]

    This would step through as
    Number := 0 + (49 – 48) = 1
    Number := 10 + (50 – 48) = 12
    Number := 120 + (51 – 48) = 123

    Now I have stored the actual integer value and should be able to use directly with any math expressions.
    Number < 200 = True, therefore do something
    Number := number + 50 = 173
    Etc

    Is my understanding correct?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
  • hippyhippy Posts: 1,981
    edited 2008-08-23 12:31
    repeat index from 1 to 3
    number := number * 10 + charArray[noparse][[/noparse]index] - "0"


    Your index most probably needs to start at zero as mpark suggested earlier, so "repeat index from 0 to 2", and, as also suggested, make sure number is zeroed before starting.
  • Earl FosterEarl Foster Posts: 185
    edited 2008-08-23 12:51
    I did all that.· So I can assume that my understanding is correct and the problem is somewhere else.·

    Here is the actual code.· Its rather large that is why I didn't want to get bogged down in it.· Everything works except my altitude check which is suppose to flash external lights below a 1000 meters.· It is currently set to 200 for testing.· I will have to step through the code again this weekend.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
  • hippyhippy Posts: 1,981
    edited 2008-08-23 13:37
    Two ways to go then - Add some debug reporting code to send data to a TV or serial port and display what data and results you are getting, or write some shorter test code to check that the algorithm works without all the clutter of everything else around it. You have debug support already, so I'd use that.

    The code you're having problems with is this ... ?

    repeat index from 40 to 41           'Satellite position in GGA sentence
        satellites := satellites * 10 + (gga[noparse][[/noparse]index] - "0") 'Store the satellite value
      altitude := 0                        'Reset counters to zero
      index := 0
      comma := 0
      repeat until comma == 9               'Start counting the commas in the string.
        j := gga[noparse][[/noparse]index]                     'Altitude starts after the 9th comma 
        if j == ","                         'if you include the GPGGA which I do
          comma ++
        index++
      repeat while gga[noparse][[/noparse]index] <> "."        'Store only the integer value
        altitude := altitude * 10 + (gga[noparse][[/noparse]index++] - "0")
    
    



    For 'satellites' check the characters are at 40 to 41, not 39 to 40 or similar. Likewise for 'altitude' check that 'index' does point to the first character of the number you want to read. Is it after the ninth comma ? Check that your earlier code which moves the received data into the 'gga' array is doing as you want it to.
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-08-23 14:17
    Here is some code I know works for altitude. It is based off of the parallax gps decoder. It returns the altitude in tmp1 in feet if I recall correctly. You may be one comma off or something small like that.
  • Earl FosterEarl Foster Posts: 185
    edited 2008-08-23 15:07
    Hippy
    Since I store the GPS value starting with GPGGA the ninth comma is the correct location for altitude. I already display the code to an LCD so the character placement is correct.

    I have already been using Debug.DEC(Altitude) for troubleshooting and it shows that I am capturing and manipulating the data correctly. Like I said earlier, I will step through the code this weekend. I wanted to make sure my understanding was correct before do that. It is probably something simple lost amongst the code. Perhaps I am toggling the altitude between 0 and the actual value when it is being read by the other cog.

    Erik
    I haven’t seen that version of gps code before. I will take a look at for my next version. I noticed that you had GGA set to 64. GGA can be up to 82 characters although I haven’t seen 82 characters yet with the GPS units I use. I like the fact that it is using the simple serial object which frees up a cog. My code uses 2 cogs for gps functions.

    I will keep troubeshooting. Thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
  • Earl FosterEarl Foster Posts: 185
    edited 2008-08-24 13:50
    I figured it out.· I WAS stepping on my altitude value.· In my procedure I was setting altitude to zero right before the new altitude was being recorded.· That might work in serial processing but not parallel processing.· Introduced a temporary variable “height” which gets reset to zero before each iteration then the new value is assigned to the altitude variable.

    Works like a champ - Code complete.· Launch is scheduled for next weekend.

    Thanks for the help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
Sign In or Register to comment.