-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucamii.py
85 lines (59 loc) · 2.14 KB
/
ucamii.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
import serial
import time
class UCamIICommands(object):
def SYNC(self):
return b'\xAA\x0D\x00\x00\x00\x00'
def RESET(self):
return b'\xAA\x0E\xFF\x00\x00\x00'
class UCamIICommunication(object):
def DEFAULT_BAUDRATE(self):
return 57600
def FRAME_LENGTH(self):
return 12
def connect(self, port):
self.ser = serial.Serial(port, self.DEFAULT_BAUDRATE())
def disconnect(self):
self.ser.close()
def send_command(self, command):
self.ser.write(command)
def received_bytes(self):
return self.ser.in_waiting
def a_frame_received(self):
if self.received_bytes()==self.FRAME_LENGTH():
return True
return False
def clear_buffers(self):
self.ser.reset_input_buffer()
self.ser.reset_output_buffer()
class UCamII(object):
def __init__(self, port):
self.commands = UCamIICommands()
self.communication = UCamIICommunication()
self.communication.connect(port)
def MAX_SYNCS(self):
return 60
def DELAY_RESET(self):
return 0.005
def DELAY_BASE(self):
return 0.02
def DELAY_MULTIPLIER(self):
return 0.001
def ERROR_NOT_SYNCED(self):
return -1
def disconnect(self):
self.communication.disconnect()
def get_sync_delay(self, sync_index):
return self.DELAY_BASE() + self.DELAY_MULTIPLIER() * sync_index
def sync_once(self):
self.communication.send_command(self.commands.SYNC())
def sync(self):
self.communication.clear_buffers();
for sync_index in range(1, self.MAX_SYNCS()):
self.sync_once()
time.sleep(self.get_sync_delay(sync_index))
if(self.communication.received_bytes()>0):
self.communication.send_command(self.commands.RESET())
time.sleep(self.DELAY_RESET())
if self.communication.a_frame_received():
return sync_index
return self.ERROR_NOT_SYNCED()