#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    My first attempt at Python coding


    I discovered Telit's line of GSM/GPS modules that have a built in Python interpreter. This peaked my curiousity so I decided to learn Python. Below is my first attempt at coding. I'm sure it looks sloppy but it works. It takes a list of GPS RMC sentences (to simulate a GPS serial input stream) and compares the "current position" with a list of geofences and reports the results (i.e. "300SD is at home", or "300SD is leaving home. Heading 309.62 degrees", etc.) It also reports stops and when the car is traveling over a predefined speed. Here's the code, comments welcome (remember, this is my very first attempt at programming!):

    Code:
    #!/usr/local/bin/python
    #monitors the current position of the gps unit and reports when the unit
    #enters/exits predefined geofences ("zones")
    
    import math
    import time
    import re    #used for the stopTimer functionality
    
    tracklog = []
    fences = []
    rmclist = ["$GPRMC,161229.000,A,3857.9760,N,07723.4240,W,0.13,309.62,121111,,*10",
               "$GPRMC,161431.000,A,3857.9810,N,07723.3390,W,20.1,309.62,121111,,*10",
               "$GPRMC,161633.000,A,3858.1200,N,07723.2700,W,21.5,309.62,121111,,*10",
               "$GPRMC,161835.000,A,3858.1280,N,07723.1740,W,24.4,309.62,121111,,*10",
               "$GPRMC,161936.000,A,3858.8400,N,07723.0340,W,24.4,309.62,121111,,*10",
               "$GPRMC,162037.000,A,3857.8420,N,07723.0370,W,0.00,309.62,121111,,*10",
               "$GPRMC,162637.000,A,3857.8420,N,07723.0370,W,0.00,309.62,121111,,*10",
               "$GPRMC,162739.000,A,3857.8620,N,07722.9940,W,14.8,309.62,121111,,*10",
               "$GPRMC,162941.000,A,3857.7420,N,07722.9080,W,27.0,309.62,121111,,*10",
               "$GPRMC,163343.000,A,3857.6500,N,07722.7390,W,32.8,309.62,121111,,*10",
               "$GPRMC,163545.000,A,3857.5360,N,07722.4720,W,38.3,309.62,121111,,*10",
               "$GPRMC,163747.000,A,3857.4420,N,07722.2770,W,37.8,309.62,121111,,*10",
               "$GPRMC,163949.000,A,3857.2840,N,07722.0840,W,0.00,309.62,121111,,*10",
               "$GPRMC,164151.000,A,3857.2330,N,07721.7880,W,18.6,309.62,121111,,*10",
               "$GPRMC,164353.000,A,3857.2500,N,07721.5490,W,27.5,309.62,121111,,*10",
               "$GPRMC,164555.000,A,3857.2720,N,07721.5530,W,0.00,309.62,121111,,*10",
               "$GPRMC,164757.000,A,3857.3420,N,07721.5540,W,0.18,309.62,121111,,*10"]
    rlat = 0.0
    rlon = 0.0
    stat = ""
    stopCounter = 0
    notifyFlag = 0
    
    data = open("fences.txt")
    for each_line in data:
        (name, lat, lon, boundary) = each_line.split(",")
        lat = float(float(lat)/57.29577951)
        lon = float(float(lon)/57.29577951)
        boundary = float(boundary)
        fence = (name, lat, lon, boundary)
        fences.append(fence)
    data.close
    
    def gps():
        #formats and parses incoming gps sentences 
        global ctime, rlat, rlon, lat, lon, speed, bearing, tracklog
        nmea = rmclist.pop(0)
        (header, utm, status, lat, NS, lon, EW, speed, bearing, date, magvar, checksum) = nmea.split(",")
        ctime = utm[:2]+":"+ utm[2:4] + ":" + utm[4:6]
        latd = int(lat[:2])
        latm = float(lat[2:9])
        lond = int(lon[:3])
        lonm = float(lon[3:10])
        lat = round((latd + latm/60), 4)
        lon = round((lond + lonm/60), 4)
        rlat,rlon = map(math.radians, [lat, lon])
        #rlon = float(lon/57.29577951)
        speed = int(float(speed)*1.15077945)
        new_entry = (ctime, lat, lon)
        tracklog.append(new_entry)
        return rlat, rlon, ctime, lat, lon, speed, bearing
    
    def status():
        #get current position and compare it with all geofences, reporting results
        global stat, notifyFlag
        for fence in fences:
            name = fence[0]
            glat = fence[1]
            glon = fence[2]
            boundary = fence[3]
            x = (glon-rlon)*math.cos((glat+rlat)/2)
            y = (glat-rlat)
            d = math.sqrt(x*x+y*y) * 3958.756
            if d < boundary:
                stat = name
                notifyFlag += 1
                break
            elif stat == name and d > boundary:
                stat = "leaving", name
                notifyFlag = 0
                break
            else:
                stat = "roaming"
        if stat == name and notifyFlag < 2:
            print "\n", ctime, "300SD is at", name
        elif stat == ("leaving", name):
            print "\n", ctime + " 300SD is leaving", name + "; traveling at", speed, "mph; heading", bearing, "degrees"
            
    
    for i in range(len(rmclist)):
        gps()
        status()
        if speed > 35:
            print "\n", ctime, "300SD is speeding! Currently travelling at", speed, "mph"
        if speed != 0:
            stopCounter = 0
        if speed == 0 and stopCounter == 0:
            stopCounter += 1
            stopTimer = int(re.sub("\D", "", ctime))
            startTime = stopTimer
        if speed == 0 and stopCounter != 0 and (stopTimer - startTime) > 510:
            print "\n", ctime, "300SD has stopped at", lat, "N", lon, "W"
        time.sleep(2)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    plans for version 2


    I forgot to mention that in the next version I'm going to attempt to modularize the program.

IMN logo majestic logo threadwatch logo seochat tools logo