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

    Join Date
    Jul 2012
    Rep Power

    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!):

    #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",
    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)
    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)
        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
            elif stat == name and d > boundary:
                stat = "leaving", name
                notifyFlag = 0
                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)):
        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"
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Rep Power

    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