caramoan tour package

caramoan tour package

Author Topic: Python Script Problem for Telit GM862-GPS  (Read 495 times)

Offline genexide

  • LR44 Battery
  • *
  • Posts: 0
  • Pogi/Ganda Points: 0
Python Script Problem for Telit GM862-GPS
« on: February 26, 2011, 12:14:41 AM »
Good day!

I'm trying to create a python script for a Telit GM862-GPS that would retrieve GPS information every 1 second, split the gpsacp information and store the speed in km/h gps information to a variable.

then would call a method that would perform an If..Else statement that would check if the speed retrieved is over the speed limit i have set.

now here's the problem, it calls the method, perform If..else BUT produces a wrong answer.

For example, the speed i retrieved is 10.25 i would compare it to the speed limit that is set to 30

if currentspeed > speedlimit:
     print ('over the limit')
else:
     print ('not over the limit')

it always prints "over the limit", meaning the currentspeed is > speedlimit.

i did a simple test to the currentspeed variable.

i tried:
     a = currentspeed * 10
when i print a, instead of displaying 102.5 (if the currentspeed is 10.25), it displays, : "10.2510.2510.2510.2510.2510.2510.2510.2510.2510.25"

i think theres the problem.

i tried explicitly casting it to either float or int by:
    int(currentspeed) or float(currentspeed)

it compiles with no problem (.pyo)

but when i run it at the Telit GM862-GPS, whenever it reaches the point the it would cast the currentspeed to either a int or float, it hangs.


any solutions?

thanks :)
Quote
##### Constants #####

TRUE = 1
FALSE = 0
LOOP = 1




##### Modules #####

#Use serial
import SER

#Use build in module
import MOD

#Use AT command interface
import MDM

#Use GPS
import GPS



###### General Functions ######

#Debug message
def debugmsg(msgtext):
    msgtext = msgtext.replace('\r', '\\r')
    msgtext = msgtext.replace('\n', '\\n')
    print msgtext
    SER.send(msgtext + '\r\n')
    #f = open('log.txt','ab')
    #f.write(msgtext + '\n')
    #f.close()

#GPS status
def gps_status(gpspos):
    debugmsg('Retrieving GPS status')

    gpspos_parts = gpspos.split(',')

    if ( (gpspos_parts[5] == '2') or (gpspos_parts[5] == '3') ): #2D or 3D fix
        #debugmsg('GPS fix "' + gpspos_parts[5] + '" ie valid');
        status = TRUE
    else:
        #debugmsg('GPS fix "' + gpspos_parts[5] + '" ie not valid');
        status = FALSE

    return status

def check_if_over(currentspeed):
   
   limit = 30
   debugmsg('set limit to 10')
   tempspeed = int(currentspeed)
   
   if tempspeed > limit:
      debugmsg('NOT OVER LIMIT')
   else:
      debugmsg('OVER THE LIMIT')
   

def testspeed(gpspos):
   
   debugmsg('Test if speed is over limit')
   
   if (gps_status(gpspos) == TRUE):
      debugmsg('Has GPS Fix')
      gpsdataparts = gpspos.split(',')
      currentspeed = gpsdataparts[7]
      debugmsg('Speed is ' + gpsdataparts[7])
      check_if_over(currentspeed)
   else:
      debugmsg('Has NO GPS Fix to be tested for overspeeding')



##################################################################################


###### Init ######

SER.set_speed('115200','8N1')
SER.send('\r\n--------------------\r\n\r\n')

debugmsg('Running...');

#Set verbose error reporting
MDM.send('AT+CMEE=2\r', 0)
MDM.receive(50)#5 sec
MOD.sleep(1)#wait 0.1sec

#Main loop
while (LOOP==1):

   
    debugmsg('Entering loop')

    #Retrieve current position
    gpspos = GPS.getActualPosition()

    debugmsg('Position: %s' % gpspos)

    #Retrieve GPS fix status
    gps_statusnow = gps_status(gpspos)

    #Save last valid position
    #If position fix valid, or none recorded already, use last retrieved
    if ( (gps_statusnow == TRUE) or (gps_statusnow == FALSE) ):
        testspeed(gpspos)
   
    MOD.powerSaving(1)

Philippine Electronics Forum

Python Script Problem for Telit GM862-GPS
« on: February 26, 2011, 12:14:41 AM »

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Python Script Problem for Telit GM862-GPS
« Reply #1 on: February 26, 2011, 09:41:06 AM »
...
i tried:
     a = currentspeed * 10
when i print a, instead of displaying 102.5 (if the currentspeed is 10.25), it displays, : "10.2510.2510.2510.2510.2510.2510.2510.2510.2510.25"
....
it's because "currentspeed" is a string type.

 this is what happens when you "multiply" a string and an int:
(example) "genexide" * 2 = "genexidegenexide"

Quote
whenever it reaches the point the it would cast the currentspeed to either a int or float, it hangs
I think, the string "currentspeed" contains invalid characters like "space" (0x20).
try stripping off these characters first before casting.
example (strip white spaces):
int( currentspeed.strip() )

you can also try implementing error handling to avoid the script from hanging.
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Re: Python Script Problem for Telit GM862-GPS
« Reply #1 on: February 26, 2011, 09:41:06 AM »

Offline genexide

  • LR44 Battery
  • *
  • Posts: 0
  • Pogi/Ganda Points: 0
Re: Python Script Problem for Telit GM862-GPS
« Reply #2 on: February 26, 2011, 11:09:56 AM »
thanks for the reply.

btw, how can i remove the space on a string type variable in python?

i'm a newb in writing programs in python. :)

Philippine Electronics Forum

Re: Python Script Problem for Telit GM862-GPS
« Reply #2 on: February 26, 2011, 11:09:56 AM »

Offline 0b00000111

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 6129
  • Pogi/Ganda Points: 398
  • There is no delight in owning anything unshared.
Re: Python Script Problem for Telit GM862-GPS
« Reply #3 on: February 26, 2011, 12:54:55 PM »
^^ check for the methods ng included sa string variable mo.

for example:
myStr="hehe"

para makita mo yung mga methods na available para sa myStr variable mo, type mo sa interpreter:
dir(myStr)

kung kelangan mong tanggalin spaces sa dulo ng string, use strip. kung substring replacement, use replace.
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Philippine Electronics Forum

Re: Python Script Problem for Telit GM862-GPS
« Reply #3 on: February 26, 2011, 12:54:55 PM »

Offline enriz622

  • Hydroelectric
  • ***
  • Posts: 3553
  • Pogi/Ganda Points: 289
  • Gender: Male
Re: Python Script Problem for Telit GM862-GPS
« Reply #4 on: February 26, 2011, 01:55:49 PM »
makinuod nga po

Philippine Electronics Forum

Re: Python Script Problem for Telit GM862-GPS
« Reply #4 on: February 26, 2011, 01:55:49 PM »

Offline genexide

  • LR44 Battery
  • *
  • Posts: 0
  • Pogi/Ganda Points: 0
Re: Python Script Problem for Telit GM862-GPS
« Reply #5 on: March 01, 2011, 07:30:52 PM »
thanks for the help guys.

the device is now fully working.

i used the strip() to remove the whitespaces and no more casting needed

Philippine Electronics Forum

Re: Python Script Problem for Telit GM862-GPS
« Reply #5 on: March 01, 2011, 07:30:52 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com