MertensToon
Posts: 12
Joined: Mon Jul 04, 2016 6:45 am

GPS GUI how to start?

Mon Jul 04, 2016 7:00 am

Dear,

I have a raspberry pi 3 B with a touchscreen setup. Now want to integrate a GPS. I want to make an application where I can make a black background and when I move with the gps that it show the way I did.

Now It's the first time I going to try to program graphically. Can somebody give me some first directions to go to?

-Can I do this with Tkinter?
-Are there some thing I need to watch out for?

I would like to use the Ultimate GPS.

Kind regards,
Toon

User avatar
topguy
Posts: 6466
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: GPS GUI how to start?

Mon Jul 04, 2016 2:23 pm

You don't really describe well what you want to show on the screen.

You dont specify language but Python I assume, maybe take a look at Kivy or PyGame and see if it is a better match for what your want to do.

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPS GUI how to start?

Mon Jul 04, 2016 2:50 pm

Sounds like you are just looking for a black screen with a trace of where you have been ? No maps etc ?

Pygame will work OK for that.

Assume you just show your start position as the center of the screen and scale the screen to suit how far you plan to move.

Something like this ?
Attachments
screenshot.jpg
screenshot.jpg (17.39 KiB) Viewed 5990 times

DavidGMX
Posts: 9
Joined: Sun Jun 19, 2016 2:30 pm

Re: GPS GUI how to start?

Wed Jul 06, 2016 9:04 am

Hi,guys,

You can click this link to get more info about how to start the GPS: http://wiki.dragino.com/index.php?title ... spberry_Pi
There ,you can also get some info about a powerful LoRa/GPS HAT from the link :)

User avatar
topguy
Posts: 6466
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: GPS GUI how to start?

Wed Jul 06, 2016 9:41 am

DavidGMX wrote:Hi,guys,

You can click this link to get more info about how to start the GPS: http://wiki.dragino.com/index.php?title ... spberry_Pi
There ,you can also get some info about a powerful LoRa/GPS HAT from the link :)
That is enough spamming from you today I think.

fruitoftheloom
Posts: 23132
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: GPS GUI how to start?

Wed Jul 06, 2016 10:18 am

Rather than negativity think outside the box !
RPi 4B 4GB (SSD Boot)..
Asus ChromeBox 3 Celeron is my other computer...

MertensToon
Posts: 12
Joined: Mon Jul 04, 2016 6:45 am

Re: GPS GUI how to start?

Mon Aug 01, 2016 7:49 pm

gordon77 wrote:Sounds like you are just looking for a black screen with a trace of where you have been ? No maps etc ?

Pygame will work OK for that.

Assume you just show your start position as the center of the screen and scale the screen to suit how far you plan to move.

Something like this ?
Sorry for the late reply. But yes I mean something like this.
At the moment I'm still struggling with reading the GPS data. When that's solved I will have a look at Pygame.
Thanks!

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPS GUI how to start?

Tue Aug 02, 2016 8:34 am

Here's what I did for the screenshot above..using a USB GPS dongle

Code: Select all

#!/usr/bin/env python
import os
import pygame, sys
import datetime
import time
import math
import shutil
from pygame.locals import *
import serial
from decimal import *
getcontext().prec = 8

# initial values
Frame = 0
fix = 0
gps_con = 0
start = 0
lat = 0
lon = 0
speed = 0
angle = 0
oldX = 0
oldY = 0

# set screen size
width = 640
height = 480
Frame = 1

# set SCALE
J = 100000

#move old log.txt if exists
try:
   if os.path.exists('/run/shm/log.txt') == True:
      now = datetime.datetime.now()
      timestamp = now.strftime("%y%m%d%H%M%S")
      shutil.copy('/run/shm/log.txt','/home/pi/' + str(timestamp) + '_log.txt')
      os.remove('/run/shm/log.txt')
except OSError:
   pass
            
pygame.init()
blackColor =  pygame.Color(  0,  0, 0)
yellowColor = pygame.Color(255,255, 0) 
redColor =    pygame.Color(255,  0, 0)
greenColor =  pygame.Color(  0,255, 0)

if not Frame:
   windowSurfaceObj = pygame.display.set_mode((width,height), pygame.NOFRAME, 24)
else: 
   windowSurfaceObj = pygame.display.set_mode((width,height), 1,             24)
pygame.display.set_caption('Pi-GPSPLot')

def print_text (msg,color,width,height):
    fontObj =  pygame.font.Font('freesansbold.ttf', height / 32)
    blackColor =  pygame.Color(  0,   0,   0)
    pygame.draw.rect(windowSurfaceObj, blackColor, Rect(0,height - (height/10),width,height - (height/10)))
    msgSurfaceObj = fontObj.render(msg, False, color)
    msgRectobj =    msgSurfaceObj.get_rect()
    msgRectobj.topleft = (width / 20, height - 30)
    windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)

def fileplot(J):
   # replot coordinates when zooming in or out.
   q = 0
   oldX = 0
   try:
      pygame.draw.rect(windowSurfaceObj, blackColor, Rect(0,0,width,height))
      pygame.display.update()
      with open("/run/shm/log.txt", "r") as file:
         while q < 9999:
            inputx = file.readline()
            inp = inputx.split(',',10)
            if q == 0:
               A = float(inp[3])
               B = float(inp[5])
            if q > 0:
               X = float(inp[3])
               Y = float(inp[5])
               pygame.draw.rect(windowSurfaceObj, yellowColor,Rect(((A-X)* J)+(width/2),((B-Y)*J)+ (height/2),3,3))
               if oldX != 0:
                  pygame.draw.line(windowSurfaceObj, yellowColor,  (((A-oldX)* J)+(width/2),((B-oldY)*J)+ (height/2)), (((A-X)* J)+(width/2),((B-Y)*J)+ (height/2)))
               oldX = X
               oldY = Y
               pygame.draw.rect(windowSurfaceObj, redColor,Rect((width/2)- 4,(height/2) - 4,10,10),1)
               pygame.display.update()
            q +=1
   except:
      pass

   
while  True:
    # check for gps connected on USB0 or USB1
   if gps_con == 0 and os.path.exists('/dev/ttyUSB0') == True:
      ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)
      gps_con = 1
      print "connected on USB0"
   elif gps_con == 0 and os.path.exists('/dev/ttyUSB1') == True:
      ser = serial.Serial('/dev/ttyUSB1',4800,timeout = 10)
      gps_con = 1
      print "connected on USB1"

   if gps_con == 1:
      gps = ser.readline()
      if gps[1 : 6] == "GPGGA":
         gps1 = gps.split(',',14)
      if gps[1:6] == "GPGSA":
         fix = int(gps[9:10])
      if gps[1 : 6] == "GPGGA" and len(gps) > 68 and (gps1[3] == "N" or gps1[3] == "S")and fix > 1:
         lat = int(gps[18:20]) + (Decimal(int(gps[20:22]))/(Decimal(60))) + (Decimal(int(gps[23:27]))/(Decimal(360000)))
         if gps[28:29] == "S":
            lat = 0 - lat
         lon = int(gps[30:33]) + (Decimal(int(gps[33:35]))/(Decimal(60))) + (Decimal(int(gps[36:40]))/(Decimal(360000)))
         if gps[41:42] == "W":
            lon = 0 - lon
         if start < 4:
             A = lat
             B = lon
             pygame.draw.rect(windowSurfaceObj, redColor,Rect((width/2)- 4,(height/2) - 4,10,10),1)
             start += 1
         if start == 4:
            now = datetime.datetime.now()
            timestamp = now.strftime("%y/%m/%d-%H:%M:%S")
            timp = "TIME:, " + str(timestamp) + ", LAT:, " + str(lat) + ", LON:, " + str(lon) + ", SPEED:, " + str(speed) + ", ANGLE:, " + str(angle)  + "\n"
            with open("/run/shm/log.txt", "a") as file:
               file.write(timp)
            start +=1
         else:
             start += 1
             X = lat
             Y = lon
             pygame.draw.rect(windowSurfaceObj, redColor,Rect(((A-X)* J)+(width/2),((B-Y)*J)+ (height/2),3,3))
             pygame.display.update()
             time.sleep(.25)
             pygame.draw.rect(windowSurfaceObj, yellowColor,Rect(((A-X)* J)+(width/2),((B-Y)*J)+ (height/2),3,3))
             if oldX != 0:
                pygame.draw.line(windowSurfaceObj, yellowColor,  (((A-oldX)* J)+(width/2),((B-oldY)*J)+ (height/2)), (((A-X)* J)+(width/2),((B-Y)*J)+ (height/2)))
         msg = "LAT: " + str(lat) + " LON: " + str(lon) + "  SPEED: " + str(speed) + " ANGLE: " + str(angle) + " SCALE: " + str(((Decimal(1)/Decimal(J))*Decimal(10000)))[:4]  + " Km/Px"
         print_text(msg,greenColor,width,height)
         pygame.display.update()
         now = datetime.datetime.now()
         timestamp = now.strftime("%y/%m/%d-%H:%M:%S")
         timp = "TIME:, " + str(timestamp) + ", LAT:, " + str(lat) + ", LON:, " + str(lon) + ", SPEED:, " + str(speed) + ", ANGLE:, " + str(angle)  + "\n"
         with open("/run/shm/log.txt", "a") as file:
            file.write(timp)
         if start > 4:
            oldX = X
            oldY = Y
      if gps[1 : 6] == "GPRMC" and fix > 1:
         gps2 = gps.split(',',14)
         speed = gps2[7]
         angle = gps2[8]
         msg = "LAT: " + str(lat) + " LON: " + str(lon) + "  SPEED: " + str(speed) + " ANGLE: " + str(angle) + " SCALE: " + str(((Decimal(1)/Decimal(J))*Decimal(10000)))[:4] + " Km/Px"
         print_text(msg,greenColor,width,height)
         pygame.display.update()
      if fix <= 1:
         msg = "LAT: " + str(lat) + " LON: " + str(lon) + "  SPEED: " + str(speed) + " ANGLE: " + str(angle) + " SCALE: " + str(((Decimal(1)/Decimal(J))*Decimal(10000)))[:4]  + " Km/Px"
         print_text(msg,redColor,width,height)
         pygame.display.update()
         print gps

   # mouse and keyboard
   for event in pygame.event.get():
       if event.type == QUIT:
          pygame.quit()
          sys.exit()

       elif event.type == MOUSEBUTTONUP or event.type == KEYDOWN:
          z =  0
          kz = 0
          if event.type == KEYDOWN:
             kz = event.key
             # screen capture
             if kz == K_s:
                now = datetime.datetime.now()
                timestamp = now.strftime("%y%m%d%H%M%S")
                pygame.image.save(windowSurfaceObj, '/home/pi/scr' + str(timestamp) + '.bmp')

          if event.type == MOUSEBUTTONUP:
             mousex, mousey = event.pos
             # click bottom right to zoom in
             if mousex > width * 0.9 and mousey > height * 0.9:
                J = J * 2
                fileplot(J)
             # click bottom left to zoom out
             if mousex < width * 0.1 and mousey > height * 0.9:
                J = J / 2
                fileplot(J)
      

MertensToon
Posts: 12
Joined: Mon Jul 04, 2016 6:45 am

Re: GPS GUI how to start?

Tue Aug 02, 2016 2:39 pm

Thanks!

I would like to do more then only showing the lines but for this I think I quickly need to explain for what I want to use it.

I would like to put this gps on my fathers tractor. so when I'm working on a field and I drive 1 time around it that it calculates the size of the aera.
After the pi calculated the aera I want him to devide the field in lines parrallel to one of the contours.
I want to do this to so I can increase the accuracy of working on the field.

Do you guys think that this is possible with pygame?

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: GPS GUI how to start?

Tue Aug 02, 2016 3:15 pm

MertensToon wrote:After the pi calculated the aera I want him to devide the field in lines parrallel to one of the contours. I want to do this to so I can increase the accuracy of working on the field.
Have you done all this manually using the GPS and a calculator to see if the accuracy is acceptable? If it is, you are just asking for data logging and automatic calculation, so it should be the same but without the tedious part.

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPS GUI how to start?

Tue Aug 02, 2016 6:59 pm

MertensToon wrote:Thanks!

I would like to do more then only showing the lines but for this I think I quickly need to explain for what I want to use it.

I would like to put this gps on my fathers tractor. so when I'm working on a field and I drive 1 time around it that it calculates the size of the aera.
After the pi calculated the aera I want him to devide the field in lines parrallel to one of the contours.
I want to do this to so I can increase the accuracy of working on the field.

Do you guys think that this is possible with pygame?
Sounds an interesting project. Pygame is mainly for displaying graphics, python will be able to do the maths required.
I would try out the gps part and see how accurate you can get first.

Then this looks promising for the area once you have the coordinates.. https://www.mathsisfun.com/geometry/are ... ygons.html

MertensToon
Posts: 12
Joined: Mon Jul 04, 2016 6:45 am

Re: GPS GUI how to start?

Tue Aug 02, 2016 9:36 pm

On the website of adafruit they say the accuracy is longitude and latitude is 1,8 meters. Now this isn't the accuracy I want but I just want to try it with a gps that cost 40 dollars begore going to a gps that cost 200 dollars ;) . I have vacation now and it is bad in belgium as always so i will continue tomorrow and keep you guys up to date. ;)

MertensToon
Posts: 12
Joined: Mon Jul 04, 2016 6:45 am

Re: GPS GUI how to start?

Thu Aug 04, 2016 6:46 pm

Hi everybody!

I did some programming. Now the First thing I have written is code to save data into a local database. For this I have used sqlite3.

The following thing quickly did was making an 'interface' with tkinter where you can choose between different machines. When you have chosen a machine there will be some labels to enter data that I need to do the math.

Now the following I have written is a piece of code in pyGame to display the path. (Not so much only the basics)

Now I need to connect these 2. With this I have some problems. I have a button GPS and I want to call with this button the code I have written in pyGame.

this is the code.


Code: Select all

import time
import Tkinter as tk
from Tkinter import PhotoImage
import pygame

import gps

LARGE_FONT = ('Verdana',12)
display_width = 800
display_height = 600

STN_background = (206,206,206)
black = (0,0,0)
white = (255,255,255)

tractorImage = pygame.image.load('TractorGPS.png')


class Tractor(tk.Tk):

        def __init__(self,*args, **kwargs):

                tk.Tk.__init__(self, *args, **kwargs)
                tk.Tk.wm_title(self,"Farmer")

                container = tk.Frame(self)
                container.pack(side='top', fill='both', expand='true')
                container.grid_rowconfigure(0, weight=1)
                container.grid_columnconfigure(0, weight=1)

                self.frames= {}

                for F in (StartPage, Settings):
                        frame = F(container,self)
                        self.frames[F] = frame
                        frame.grid(row=0, column=0, sticky="nsew")


                self.show_frame(StartPage)



        def show_frame(self,cont):

                frame = self.frames[cont]
                frame.tkraise()


## different pages

class StartPage(tk.Frame):

        def __init__(self, parent, controller):
                tk.Frame.__init__(self, parent)
                label = tk.Label(self, text='StartPage', font=LARGE_FONT)
                label.pack( padx=50, pady=10)


                button1 = tk.Button(self, text='maaien', command=lambda: controller.show_frame(Settings))
                button1.pack(side='left', expand ='true')

                button2 = tk.Button(self, text='keren', command=lambda: controller.show_frame(Settings))
                button2.pack(side='left', expand ='true')

                button3 = tk.Button(self, text='harken', command=lambda: controller.show_frame(Settings))
                button3.pack(side='left', expand ='true')

                button4 = tk.Button(self, text='frezen', command=lambda: controller.show_frame(Settings))
                button4.pack(side='right', expand ='true')

                button5 = tk.Button(self, text='breken', command=lambda: controller.show_frame(Settings))
                button5.pack(side='right', expand ='true')

                button6 = tk.Button(self, text='eggen', command=lambda: controller.show_frame(Settings))
                button6.pack(side='right', expand ='true')




class Settings(tk.Frame):
        def __init__(self, parent, controller):
                tk.Frame.__init__(self, parent)
                label = tk.Label(self, text='Maaien', font=LARGE_FONT)
                label.pack( padx=50, pady=10)


                settingsTractor = PhotoImage(file='TractorGPSbig.png')
                label = tk.Label(self,image=settingsTractor)
                label.pack()


                buttonHome = tk.Button(self, text='Home', command=lambda: controller.show_frame(StartPage))
                buttonHome.pack()


                buttonGPS = tk.Button(self, text='GPS',command=app.destroy)
                buttonGPS.pack()


app = Tractor()
app.mainloop()

def tractor(x,y):
        gameDisplay.blit(tractorImage,(x,y))

x = (display_width * 0.45)
y = (display_height * 0.45)

pygame.init()
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('GPS')
clock = pygame.time.Clock()


runGPS = False

while not runGPS:

        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        runScript = True

                print(event)

        gameDisplay.fill(STN_background)

        tractor(x,y)
        pygame.display.update()
        clock.tick(60) # 60 frames a second

pygame.quit()
now I found allready to to the next thing
buttonGPS = tk.Button(self, text='GPS',command=app.destroy)

this gives me an error. I think because Is say after this code app = Tractor() and not before. Aswell this isn't the way to do it because I want also to be able to go from pyGame to tkinter.

Anybody an Idea how to solve this?
Kind Regards

Return to “Graphics programming”