-
Notifications
You must be signed in to change notification settings - Fork 0
/
X_arm.py
179 lines (160 loc) · 7.42 KB
/
X_arm.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
#!/home/lab/.conda/envs/lab502/bin/python
# -*- coding: UTF-8
import serial
import time
import struct
import keyboard
class X_arm:
def __init__(self):
self.ser = None
self.head = b'\xf5\x5f'
self.motorId = b'\x01'
self.instruction = b'\x01'
self.speedPercent = 0.5
self.andCrc = 255
self.xorCrc = 255
self.angle = 0.0
self.calibration_manual_on=0
def __del__(self):
self.ser.close()
def ConvertFixedIntegerToComplement(self, fixedInterger):
return bin(fixedInterger)[2:]
def ConvertFixedDecimalToComplement(self, fixedDecimal):
fixedpoint = int(fixedDecimal) / (10.0 ** len(fixedDecimal))
s = ''
while fixedDecimal != 1.0 and len(s) < 23:
fixedpoint = fixedpoint * 2.0
s += str(fixedpoint)[0]
fixedpoint = fixedpoint if str(fixedpoint)[0] == '0' else fixedpoint - 1.0
return s
def ConvertToExponentMarker(self, number):
return bin(number + 127)[2:].zfill(8)
def ConvertToFloat(self, floatingPoint):
floatingPoint = float(floatingPoint)
floatingPointString = str(floatingPoint)
if floatingPointString.find('-') != -1:
sign = '1'
floatingPointString = floatingPointString[1:]
else:
sign = '0'
l = floatingPointString.split('.')
front = self.ConvertFixedIntegerToComplement(int(l[0]))
rear = self.ConvertFixedDecimalToComplement(l[1])
floatingPointString = front + '.' + rear
relativePos = floatingPointString.find('.') - floatingPointString.find('1')
if relativePos > 0:
exponet = self.ConvertToExponentMarker(relativePos - 1)
mantissa = floatingPointString[
floatingPointString.find('1') + 1: floatingPointString.find('.')] + floatingPointString[
floatingPointString.find(
'.') + 1:]
else:
exponet = self.ConvertToExponentMarker(relativePos)
mantissa = floatingPointString[floatingPointString.find('1') + 1:]
mantissa = mantissa[:23] + '0' * (23 - len(mantissa))
floatingPointString = '0b' + sign + exponet + mantissa
return int(int(floatingPointString, 2)).to_bytes(length=4, byteorder='little', signed=False)
# print(ConvertToFloat(-5.56))
def print_hex(self, bytes):
l = [hex(int(i)) for i in bytes]
print(" ".join(l))
def setJointAngle(self, id, targetAngle, speedPercent):
self.angle = targetAngle
self.speedPercent = speedPercent
self.instruction = b'\x01'
data = self.head + int(id).to_bytes(length=1, byteorder='little', signed=False) \
+ self.instruction \
+ self.ConvertToFloat(self.angle) \
+ self.ConvertToFloat(self.speedPercent)
for iterating_var in data:
self.andCrc = int(self.andCrc) and iterating_var
self.xorCrc = int(self.xorCrc) ^ iterating_var
data = data + self.andCrc.to_bytes(length=1, byteorder='little', signed=False) + self.xorCrc.to_bytes(length=1,
byteorder='little',
signed=False)
# self.print_hex(data)
self.andCrc = 255
self.xorCrc = 255
if self.ser.is_open and self.ser is not None:
self.ser.write(data)
time.sleep(0.5)
def powerOff(self,isOn):
self.instruction = b'\x02'
data = self.head + b'\x08' \
+ self.instruction \
+ int(isOn).to_bytes(length=1, byteorder='little', signed=False) \
+ b'\xff' + b'\xff' + b'\xff' + b'\xff' + b'\xff' + b'\xff' + b'\xff'
for iterating_var in data:
self.andCrc = int(self.andCrc) and iterating_var
self.xorCrc = int(self.xorCrc) ^ iterating_var
data = data + self.andCrc.to_bytes(length=1, byteorder='little', signed=False) + self.xorCrc.to_bytes(length=1,
byteorder='little',
signed=False)
self.print_hex(data)
self.andCrc = 255
self.xorCrc = 255
if self.ser.is_open and self.ser is not None:
self.ser.write(data)
time.sleep(0.5)
if isOn==1:
print("entering lose power mode ")
elif isOn==0:
print("quitting lose power mode")
def Open_port(self, comNumber):
self.ser = serial.Serial(comNumber, 115200)
if self.ser.is_open:
print("open success")
def getStates(self):
is_exit = False
motor_states={
'J1': 0.0,
'J2': 0.0,
'J3': 0.0,
'J4': 0.0,
'J5': 0.0,
'J6': 0.0,
}
self.instruction = b'\x03'
data = self.head + b'\x09' \
+ self.instruction \
+ b'\x01' \
+ b'\xff' + b'\xff' + b'\xff' + b'\xff' + b'\xff' + b'\xff' + b'\xff'
for iterating_var in data:
self.andCrc = int(self.andCrc) and iterating_var
self.xorCrc = int(self.xorCrc) ^ iterating_var
data = data + self.andCrc.to_bytes(length=1, byteorder='little', signed=False) + self.xorCrc.to_bytes(length=1,
byteorder='little',
signed=False)
self.print_hex(data)
self.andCrc = 255
self.xorCrc = 255
if self.ser.is_open and self.ser is not None:
self.ser.write(data)
time.sleep(1)
while not is_exit:
count = self.ser.inWaiting()
if count > 20:
rec_str = self.ser.read(count)
#data_bytes = data_bytes + rec_str
presentPosition=struct.unpack('<ffffff',rec_str)
# for i in range(0,6):
# print(int(presentPosition[i]))
motor_states['J1'] = int(presentPosition[0])
motor_states['J2']=int(presentPosition[1])
motor_states['J3'] = int(presentPosition[2])
motor_states['J4'] = int(presentPosition[3])
motor_states['J5']=int(presentPosition[4])
motor_states['J6'] = int(presentPosition[5])
for motorID in motor_states:
print(motorID+":"+str(motor_states[motorID]))
is_exit=True
time.sleep(1)
def calibration_manual(self):
self.powerOff(1)
print("entering calibration mode")
print("1 manual mode")
print("2 press m ")
keyboard.add_hotkey('m', self.getStates)
keyboard.wait('ctrl+enter')
self.powerOff(0)
print("over")