-
Notifications
You must be signed in to change notification settings - Fork 4
/
serial_connect.py
209 lines (170 loc) · 6.45 KB
/
serial_connect.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
import struct as st
import serial as sp
import params as p
import time as t
class serialPort:
#private
_ser = []
_connected = False
_changed = False
_version = []
rxBuffer = ()
paramName = []
subParamName = []
param_count = 0
_subparam_count = 0
_private_flag = []
_mode = 1 # 0:CHIBIOS USB mode
# 1:CHIBIOS UART mode
#public
portName = ''
def __init__(self,version):
self._connected = False
self._changed = False
self._version = version
def checkConnection(self):
self._changed = False
return self._connected
def checkConnectionChange(self):
return self._changed
def connect(self):
try:
self._ser = sp.Serial(self.portName, 115200, timeout=0.5)
except:
print 'E:Cannot find port '+ self.portName
return
print 'Connected to '+self._ser.name
self._connected = True
self._changed = True
def disconnect(self):
self._ser.close()
del self._ser
self._connected = False
self._changed = True
def sendScale(self, index, sub_index, power):
#byte = st.pack('i',power)
data = chr(ord('0') + power)
if self._mode == 0:
self._ser.write('\r')
t.sleep(0.05)
param_index = chr(ord('0')+index)
subparam_index = chr(ord('0')+sub_index)
self._ser.write('\xFD '+ param_index + subparam_index)
self._ser.write(data)
self._ser.write('-------\r')
#print 's '+str(index)+' '+str(sub_index)+' '+str(power)
def sendUpdateCommand(self):
if self._mode == 0:
self._ser.write('\r')
t.sleep(0.05)
self._ser.write('\xFB update----\r')
t.sleep(1)
print 'parameters saved'
def sendParam(self, value, index, sub_index):
data = hex(st.unpack('I',(st.pack('f',value)))[0])[2:]
#convert floating point number to hex value , then to a character string
#E.g. 3.141592653 -> 0x40490fdb -> '40490fdb' -> '40490?=;'
while len(data) < 8:
data = '0'+ data
if self._mode == 0:
self._ser.write('\r')
t.sleep(0.05)
param_index = chr(ord('0')+index)
subparam_index = chr(ord('0')+sub_index)
self._ser.write('\xF9 '+ param_index + subparam_index)
self._ser.write(data)
self._ser.write('\r')
#print 'p '+str(index)+' '+str(sub_index)+' '+data
def clearRxBuffer(self):
del self.paramName[:]
del self.subParamName[:]
paramName = []
subParamName = []
self.rxBuffer = ()
def reset(self):
del self._private_flag[:]
self._private_flag = []
self.param_count = 0
self._subparam_count = 0
self.clearRxBuffer()
def getParam(self, params):
#TODO:getParameters
self.reset()
self._ser.write('\r-----------\r') #flush the shell serial port
t.sleep(0.05)
self._ser.write('\xFA getPID----\r')
t.sleep(0.05)
self._ser.reset_input_buffer()
byte1 = self._ser.read(1)
if len(byte1) == 0:
print 'E:Unable to retrieve parameters from board 1!'
return False;
byte2 = self._ser.read(1)
byte_private = self._ser.read(4)
byte_mode = self._ser.read(1)
self._mode = (st.unpack('B',byte_mode))[0]
if self._mode == 0:
print 'Device in USB mode'
elif self._mode == 1:
print 'Device in UART mode'
else:
print 'E:Unsupported connection mode!'
ver = self._ser.readline().split('.')
ver[len(ver)-1]=ver[len(ver)-1][0:len(ver[len(ver)-1])-1]
ver_min = self._version.split('.')
ver_num_min = 0
for i in range(0,3):
if i < len(ver_min):
ver_num_min += int(ver_min[i])
ver_num_min *= 1000
ver_num = 0
try:
for i in range(0,3):
if i < len(ver):
ver_num += int(ver[i])
ver_num *= 1000
except ValueError:
print 'E:Your on-board param driver appeared to be obsolete, namely param.c and param.h'
print 'Please download the latest param driver from Github, and reload your embedded program'
return False
if ver_num < ver_num_min:
print 'E:Your on-board param driver appeared to be obsolete, namely param.c and param.h'
print 'Please download the latest param driver from Github, and reload your embedded program'
return False
private = st.unpack('I',byte_private)[0]
for i in range(0,32):
self._private_flag.append((private & pow(2,i) > 0))
self.param_count = (st.unpack('B',byte1))[0]
self._subparam_count = (st.unpack('B',byte2))[0]
print 'Retrieved '+str(self.param_count)+' parameters and ' +\
str(self._subparam_count)+' sub-parameters'
byteSubparams = self._ser.read(self.param_count * 9)
for byte in byteSubparams:
self.rxBuffer = self.rxBuffer + st.unpack('b',byte)
param_count = 0;
for i in range(0, self.param_count):
#print 'Param '+ str(i)+' has '+ str(self.rxBuffer[9*i])+ ' subparams'
for j in range(0, self.rxBuffer[9*i]):
byteparams = self._ser.read(4)
self.rxBuffer = self.rxBuffer + st.unpack('f',byteparams)
param_count += self.rxBuffer[9*i]
for i in range(0, self.param_count):
p = str(self._ser.readline())
subp = str(self._ser.readline())
if p[0] == '*':
print 'W:unknown parameter name at position '+str(i)
p = 'Controller '+str(i)
p = p[0:len(p)-1]
self.paramName.append(p)
print self.paramName[i]
self.subParamName.append(subp.split())
if len(self.subParamName[i]) != self.rxBuffer[9*i]:
print 'W:incorrect subparameter name at position '+str(i)
self.subParamName[i] = []
for j in range(0, self.rxBuffer[9*i]):
self.subParamName[i].append('p'+str(j))
self._ser.flush()
if len(byteSubparams) != self.param_count*9 or param_count != self._subparam_count:
print 'E:GODDAMNIT!!Fucking incorrect parameters on board!'
return False;
return True