forked from cogan/RU-AUVSI-ImageStation
/
SerialInterface.py
executable file
·382 lines (344 loc) · 13.9 KB
/
SerialInterface.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
###############################################################################
#
# file: SerialInterface.py
# author: Cogan Noll
# email: colgate360@gmail.com
# last modified: 2010
#
###############################################################################
from Interface import *
from NmeaEncoder import *
from NmeaDecoder import *
import serial
import time
class SerialInterface(Interface):
"""interface class to use when there is no connection selected."""
def __init__(self, port="/dev/ttyUSB0", baud=9600):
"""constructor"""
#indicates whether there is a connection on the selected port
self.enabled = False
#create a connection on the selected port
self.initialize_connection(port, baud)
#try to open on ttyUSB1 if ttyUSB0 fails
if self.enabled == False:
self.initialize_connection("/dev/ttyUSB1", baud)
#talker identifier = IS (ImageStation)
self.identifier = "IS"
#set encoder and decoder
self.encoder = NmeaEncoder(self.identifier)
self.decoder = NmeaDecoder()
# FOR REFERENCE: Dictionary of sentence identifiers
#
# "toggle_power" : "CPW"
# "set_mode" : CMO"
# "take_picture" : "CPT"
# "toggle_record: "CRC"
# "pan" : "CPA"
# "tilt" : "CTI"
# "resume_search" : ""
# "lock_target" : "",
# "acknowledge download_to_flc" : "FLC"
# "download to FLC complete" : "FLD"
# "generate_crop" : "CRP"
# "request_info" : "INF"
# "request_size" : "PSZ"
# "download_segment" : "DPC"
# "ping" : "PNG"
#*
#* Define Abstract functions
#*
def toggle_power(self):
"""toggle power on and off for the camera"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CPW")
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def set_mode(self, mode):
"""set the mode for the camera (0 for storage, 1 for camera mode)"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CMO", mode)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def take_picture(self, picture_num):
"""snap a picture."""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CPT", picture_num)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def toggle_record(self):
"""toggle recording on and off for the camera"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CRC")
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def pan(self, value):
"""set the pan servo to position (-180 to 180)"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CPA", value)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def tilt(self, value):
"""set the pan servo to position (-45 to 45)"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CTI", value)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
#def resume_search(self):
# """have the camera resume it's search pattern."""
# if self.enabled == True:
# try:
# msg_to_send = self.encoder.encode("")
# return self.tx_rx_decode(msg_to_send)
# except InterfaceError as e:
# raise InterfaceError(e.value)
# else:
# raise InterfaceError("no serial connection")
#def lock_target(self, xa, ya):
# """lock onto a target given pixels xa and ya."""
# if self.enabled == True:
# try:
# msg_to_send = self.encoder.encode("", xa, ya)
# return self.tx_rx_decode(msg_to_send)
# except InterfaceError as e:
# raise InterfaceError(e.value)
# else:
# raise InterfaceError("no serial connection")
def download_to_flc(self):
"""download pictures form the camera memory onto the
flight linux computer."""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("FLC")
self.tx_rx_decode(msg_to_send)
return self.rx_decode()
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def generate_crop(self, picture_num, xa, ya, xb, yb):
"""generate a crop of an image given the image number,
xa, ya, xb, and yb. which represent the top left and bottom right
corners of a rectangle."""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("CRP", picture_num, \
xa, ya, xb, yb)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def request_info(self, picture_num):
"""get all positional info about a picture
returns gps_x, gps_y, pan, tilt, yaw, pitch, roll, orientation, altitude"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("INF", picture_num)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def request_size(self, picture_num, crop_num):
"""get the size of a crop"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("PSZ", picture_num, crop_num)
return self.tx_rx_decode(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
def download_segment(self, picture_num, crop_num, segment_num):
"""download a segment of an image"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("DPC", picture_num, \
crop_num, segment_num)
return self.tx_rx_decode_bin(msg_to_send)
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
#def camera_zoom_in(self, increment):
# """have the camera zoom in."""
# if self.enabled == True:
# try:
# msg_to_send = self.encoder.encode("CZI", increment)
# return self.tx_rx_decode(msg_to_send)
# except InterfaceError as e:
# raise InterfaceError(e.value)
# else:
# raise InterfaceError("no serial connection")
#def camera_zoom_out(self, increment):
# """have the camera zoom out."""
# if self.enabled == True:
# try:
# msg_to_send = self.encoder.encode("CZO", increment)
# return self.tx_rx_decode(msg_to_send)
# except InterfaceError as e:
# raise InterfaceError(e.value)
# else:
# raise InterfaceError("no serial connection")
def ping(self):
"""ping the plane"""
if self.enabled == True:
try:
msg_to_send = self.encoder.encode("PNG")
t1 = time.time()
self.tx_rx_decode(msg_to_send)
t2 = time.time()
return (t2-t1)*1000.0
except InterfaceError as e:
raise InterfaceError(e.value)
else:
raise InterfaceError("no serial connection")
#*
#* Non-Abstract functions
#*
def initialize_connection(self, port, baud):
"""try to create a connection on the specified serial port."""
try:
self.ser = serial.Serial(port)
self.ser.baudrate = baud
self.ser.bytesize = 8
self.ser.stopbits = 1
self.ser.rtscts = 1
self.ser.timeout = 1
self.enabled = True
print "serial connection successful on port %s at %d baud" % (port, baud,)
except serial.serialutil.SerialException as e:
self.enabled = False
print "serial connection failed on port %s" % (port,)
print e
def tx_rx_decode(self, msg_to_send):
"""transmit to the plane, recieve a response, and decode it."""
attempts = 0
while (attempts < 3):
self.ser.write(msg_to_send)
### DEBUGGING ###
print "sending msg: %s" % (msg_to_send,)
### /DEBUGGING ###
response = self.ser.readline()
if response:
### DEBUGGING ###
print "received msg: %s" % (response,)
### /DEBUGGING ###
try:
### DEBUGGING ###
print "returned args array: %s" % (self.decoder.decode(response),)
### /DEBUGGING ###
return self.decoder.decode(response)
except DecodeError as e:
attempts += 1
error_msg = e.value
#flush the serial line
else: #timeout
attempts += 1
error_msg = "waiting for response timed out"
#flush the serial line
# attempts > 3, failure
raise InterfaceError(error_msg)
def tx_rx_decode_bin(self, msg_to_send):
"""transmit to the plane, recieve a response, and decode it.
This should be used when decoding responses with binary arguments,
in this case a random /r/n could appear, so a different decoding
method must be used"""
attempts = 0
while (attempts < 3):
length = 0
# get and decode the message containing the length
self.ser.write(msg_to_send)
### DEBUGGING ###
print "sending msg: %s" % (msg_to_send,)
### /DEBUGGING ###
response1 = self.ser.readline()
if response1:
### DEBUGGING ###
print "received msg1: %s" % (response1,)
### /DEBUGGING ###
try:
### DEBUGGING ###
print "returned args array: %s" % (self.decoder.decode(response1),)
### /DEBUGGING ###
(length_str,) = self.decoder.decode(response1)
length = int(length_str)
except DecodeError as e:
attempts += 1
error_msg = e.value
#flush the serial line
else: #timeout
attempts += 1
error_msg = "waiting for response timed out"
#flush the serial line
# get and decode the message containing image data
response2 = self.ser.read(length)
if response2:
### DEBUGGING ###
print "received msg2: %s" % (response2,)
### /DEBUGGING ###
try:
### DEBUGGING ###
print "returned args array: %s" % (repr(self.decoder.decode_bin(response2)),)
### /DEBUGGING ###
return self.decoder.decode_bin(response2)
except DecodeError as e:
attempts += 1
error_msg = e.value
#flush the serial line
else: #timeout
attempts += 1
error_msg = "waiting for response timed out"
#flush the serial line
# attempts > 3, failure
raise InterfaceError(error_msg)
def rx_decode(self):
"""recieve a response and decode it."""
print "in rx_decode"
attempts = 0
MAX_ATTEMPTS = 999
while (attempts < MAX_ATTEMPTS):
response = self.ser.readline()
if response:
### DEBUGGING ###
print "received msg: %s" % (response,)
### /DEBUGGING ###
try:
### DEBUGGING ###
print "returned args array: %s" % (self.decoder.decode(response),)
### /DEBUGGING ###
return self.decoder.decode(response)
except DecodeError as e:
attempts += 1
error_msg = e.value
#flush the serial line
else: #timeout
attempts += 1
error_msg = "waiting for response timed out"
#flush the serial line
# attempts > 3, failure
raise InterfaceError(error_msg)