-
Notifications
You must be signed in to change notification settings - Fork 0
/
polecam_play.py
executable file
·283 lines (220 loc) · 8.41 KB
/
polecam_play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
## Daniel Buscombe, August 2016
#================================================
COMNUM=4
BAUDRATE = 38000
cs2cs_args = 'epsg:26949'
# ===============================================
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.accordion import *
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import *
from kivy.graphics import Canvas, Translate, Fbo, ClearColor, ClearBuffers
from kivy.core.clipboard import Clipboard
from kivy.clock import Clock
from kivy.uix.textinput import TextInput
import subprocess
import serial
from pynmea import nmea
import pyproj
import time, os
# get the transformation matrix of desired output coordinates
try:
trans = pyproj.Proj(init=cs2cs_args)
except:
trans = pyproj.Proj(cs2cs_args)
#=========================
def init_serial(self,portnum):
'''initialise serial port for gps
'''
comnum = 'COM' + str(portnum)
now = time.asctime().replace(' ','_').replace(':','_')
try:
global BAUDRATE
self.ser = serial.Serial()
self.ser.baudrate = BAUDRATE
self.ser.port = comnum
self.ser.timeout = 0.5
self.ser.open()
if self.ser.isOpen():
print '==========================='
print 'GPS is open'
print '==========================='
self.gpsopen = 1
except:
self.ser = 0
print '==========================='
print "GPS failed to open"
print '==========================='
self.gpsopen = 0
return self
#=========================
def get_nmea(self):
'''get easting/northing from serial port gps
'''
gotpos = 0; counter = 0
while (gotpos==0) &(counter<1):
line = self.ser.read(500) # read 400 bytes
parts = line.split('\r\n') # split by line return
gpgga_parts = []; gprmc_parts = [];
newparts = [] # create new variable which contains cleaned strings
for k in range(len(parts)):
if parts[k].startswith('$'): #select if starts with $
if len(parts[k].split('*'))>1: #select if contains *
if parts[k].count('$')==1: # select if only contains 1 $
newparts.append(parts[k])
for k in range(len(newparts)):
if "GPGGA" in newparts[k]:
gpgga_parts.append(newparts[k])
elif "GPRMC" in newparts[k]:
gprmc_parts.append(newparts[k])
if gpgga_parts:
gotpos=1
gpgga_parts = gpgga_parts[-1];
if gprmc_parts:
gprmc_parts = gprmc_parts[-1];
counter += 1
gpgga = nmea.GPGGA();
gprmc = nmea.GPRMC();
# GPGGA
try:
gpgga.parse(gpgga_parts)
lats = gpgga.latitude
longs= gpgga.longitude
#convert degrees,decimal minutes to decimal degrees
lat1 = (float(lats[2]+lats[3]+lats[4]+lats[5]+lats[6]+lats[7]+lats[8]))/60
lat = (float(lats[0]+lats[1])+lat1)
long1 = (float(longs[3]+longs[4]+longs[5]+longs[6]+longs[7]+longs[8]+longs[9]))/60
long = (float(longs[0]+longs[1]+longs[2])+long1)
#convert to az central state plane east/north
e,n = trans(-long,lat)
except:
# GPRMC
try:
gprmc.parse(gprmc_parts)
lats = gprmc.lat; longs= gprmc.lon
#convert degrees,decimal minutes to decimal degrees
lat1 = (float(lats[2]+lats[3]+lats[4]+lats[5]+lats[6]+lats[7]+lats[8]))/60
lat = (float(lats[0]+lats[1])+lat1)
long1 = (float(longs[3]+longs[4]+longs[5]+longs[6]+longs[7]+longs[8]+longs[9]))/60
long = (float(longs[0]+longs[1]+longs[2])+long1)
#convert to az central state plane east/north
e,n = trans(-long,lat)
except:
n = self.n_txt.text
e = self.e_txt.text
long = 0
lat = 0
return str(e), str(n), -long, lat
#=========================
def export_to_png(self, filename, *args):
'''Saves an image of the widget and its children in png format at the
specified filename. Works by removing the widget canvas from its
parent, rendering to an :class:`~kivy.graphics.fbo.Fbo`, and calling
:meth:`~kivy.graphics.texture.Texture.save`.
'''
if self.parent is not None:
canvas_parent_index = self.parent.canvas.indexof(self.canvas)
self.parent.canvas.remove(self.canvas)
fbo = Fbo(size=self.size, with_stencilbuffer=True)
with fbo:
ClearColor(0, 0, 0, 1)
ClearBuffers()
Translate(-self.x, -self.y, 0)
fbo.add(self.canvas)
fbo.draw()
fbo.texture.save(filename, flipped=False)
fbo.remove(self.canvas)
if self.parent is not None:
self.parent.canvas.insert(canvas_parent_index, self.canvas)
return True
#===========================================================================================
#============================================================================================
# start app
#=========================
## kv markup for building the app
Builder.load_file('polecam_play.kv')
#=========================
#=========================
class CameraWidget(BoxLayout):
#=========================
def TakePicture(self):
'''takes a picture and saves it to the folder
'''
self.export_to_png = export_to_png
tmp = Clipboard.paste()
n_txt = tmp.split(':')[0]
e_txt = tmp.split(':')[1]
now = time.asctime().replace(' ','_').replace(':','_')
filename = 'im_'+now+'_'+e_txt+'_'+n_txt+'.png'
#filename = 'im_'+now+'.png'
self.export_to_png(self.ids.camera, filename=filename)
subprocess.Popen("python move_polecam_im.py -i "+filename+" -o GPSCam_images", shell=True)
#=========================
#=========================
class GPSCam(App):
font_size8 = NumericProperty(8)
font_size20 = NumericProperty(20)
#=========================
def _update_time(self, dt):
self.item.title = 'Time is '+time.asctime()+'. '+'Position is [N: '+str(self.n_txt.text)+', E: '+str(self.e_txt.text)+']'+'\n'
#=========================
def _update_pos(self, dt):
'''
get and update position
'''
e, n, lon, lat = get_nmea(self)
try:
self.e_txt.text = e[:10]#self.dat['e'][:10]
self.n_txt.text = n[:10]#self.dat['n'][:10]
except:
pass
Clipboard.copy(self.n_txt.text+':'+self.e_txt.text)
tmp = Clipboard.paste()
self.n_txt.text = tmp.split(':')[0]
self.e_txt.text = tmp.split(':')[1]
#=========================
def build(self):
'''
build the app
'''
#text field for easting
self.e_txt = TextInput(multiline=False)
self.e_txt.text = ''
#text field for northing
self.n_txt = TextInput(multiline=False)
self.n_txt.text = ''
#sets the accordion panel for the timestamp
root = Accordion(orientation='horizontal')
self.item = AccordionItem(title='Current time is '+time.asctime())
image = CameraWidget(size_hint = (3.5, 1.0))
#initiate serial port for gps
self = init_serial(self,COMNUM) # is the com number this needs to read a config file or something
# add image to AccordionItem
self.item.add_widget(image)
#set clock to poll time and posotion on different threads
Clock.schedule_interval(self._update_time, 1) #update time
Clock.schedule_interval(self._update_pos, 1) #1.6) #update position
root.add_widget(self.item)
return root
#=========================
def on_stop(self):
'''close ports, etc
'''
# close the serial port for gps
if self.ser!=0:
self.ser.close()
print "================="
print "GPS is closed"
print "================="
# ==============================================================================
# ============ run app =========================================================
#==============================================================================
#=========================
#=========================
if __name__ == '__main__':
try:
os.mkdir('GPSCam_images')
except:
pass
GPSCam().run()