|
3 | 3 | import can |
4 | 4 | import threading |
5 | 5 | from enum import Enum |
| 6 | +import numpy as np |
6 | 7 |
|
7 | 8 | class FrameProperty(Enum): |
8 | 9 | INVALID_FRAME_PROPERTY = 0x00 # Invalid CAN frame property | No return |
@@ -33,6 +34,28 @@ def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28): |
33 | 34 | self.can_id = can_id |
34 | 35 | self.running = True |
35 | 36 | self.x05, self.x06, self.x07 = [],[],[] |
| 37 | + # New pressure sensors |
| 38 | + self.xb0,self.xb1,self.xb2,self.xb3,self.xb4,self.xb5 = [-1] * 5,[-1] * 5,[-1] * 5,[-1] * 5,[-1] * 5,[-1] * 5 |
| 39 | + |
| 40 | + self.thumb_matrix = np.full((12, 6), -1) |
| 41 | + self.index_matrix = np.full((12, 6), -1) |
| 42 | + self.middle_matrix = np.full((12, 6), -1) |
| 43 | + self.ring_matrix = np.full((12, 6), -1) |
| 44 | + self.little_matrix = np.full((12, 6), -1) |
| 45 | + self.matrix_map = { |
| 46 | + 0: 0, |
| 47 | + 16: 1, |
| 48 | + 32: 2, |
| 49 | + 48: 3, |
| 50 | + 64: 4, |
| 51 | + 80: 5, |
| 52 | + 96: 6, |
| 53 | + 112: 7, |
| 54 | + 128: 8, |
| 55 | + 144: 9, |
| 56 | + 160: 10, |
| 57 | + 176: 11, |
| 58 | + } |
36 | 59 |
|
37 | 60 | # Initialize CAN bus according to operating system |
38 | 61 | if sys.platform == "linux": |
@@ -186,6 +209,48 @@ def process_response(self, msg): |
186 | 209 | elif frame_type == 0x23: |
187 | 210 | d = list(response_data) |
188 | 211 | self.approach_inc = [float(i) for i in d] |
| 212 | + elif frame_type == 0xb0: |
| 213 | + self.xb0 = list(response_data) |
| 214 | + elif frame_type == 0xb1: |
| 215 | + d = list(response_data) |
| 216 | + if len(d) == 2: |
| 217 | + self.xb1 = d |
| 218 | + elif len(d) == 7: |
| 219 | + index = self.matrix_map.get(d[0]) |
| 220 | + if index is not None: |
| 221 | + self.thumb_matrix[index] = d[1:] # Remove the first flag bit |
| 222 | + elif frame_type == 0xb2: |
| 223 | + d = list(response_data) |
| 224 | + if len(d) == 2: |
| 225 | + self.xb2 = d |
| 226 | + elif len(d) == 7: |
| 227 | + index = self.matrix_map.get(d[0]) |
| 228 | + if index is not None: |
| 229 | + self.index_matrix[index] = d[1:] # Remove the first flag bit |
| 230 | + elif frame_type == 0xb3: |
| 231 | + d = list(response_data) |
| 232 | + if len(d) == 2: |
| 233 | + self.xb3 = d |
| 234 | + elif len(d) == 7: |
| 235 | + index = self.matrix_map.get(d[0]) |
| 236 | + if index is not None: |
| 237 | + self.middle_matrix[index] = d[1:] # Remove the first flag bit |
| 238 | + elif frame_type == 0xb4: |
| 239 | + d = list(response_data) |
| 240 | + if len(d) == 2: |
| 241 | + self.xb4 = d |
| 242 | + elif len(d) == 7: |
| 243 | + index = self.matrix_map.get(d[0]) |
| 244 | + if index is not None: |
| 245 | + self.ring_matrix[index] = d[1:] # Remove the first flag bit |
| 246 | + elif frame_type == 0xb5: |
| 247 | + d = list(response_data) |
| 248 | + if len(d) == 2: |
| 249 | + self.xb5 = d |
| 250 | + elif len(d) == 7: |
| 251 | + index = self.matrix_map.get(d[0]) |
| 252 | + if index is not None: |
| 253 | + self.little_matrix[index] = d[1:] # Remove the first flag bit |
189 | 254 | def pose_slice(self, p): |
190 | 255 | """Slice the joint array into finger action arrays""" |
191 | 256 | try: |
@@ -243,14 +308,36 @@ def get_temperature(self): |
243 | 308 | def clear_faults(self): |
244 | 309 | '''Clear motor faults''' |
245 | 310 | self.send_command(0x07, [1, 1, 1, 1, 1]) |
246 | | - |
247 | 311 | def get_touch_type(self): |
248 | | - '''Get touch type, not supported''' |
249 | | - return [-1] * 5 |
| 312 | + '''Get touch type''' |
| 313 | + self.send_command(0xb0,[],sleep=0.03) |
| 314 | + self.send_command(0xb1,[],sleep=0.03) |
| 315 | + t = [] |
| 316 | + for i in range(3): |
| 317 | + t = self.xb1 |
| 318 | + time.sleep(0.01) |
| 319 | + if len(t) == 2: |
| 320 | + return 2 |
| 321 | + else: |
| 322 | + return -1 |
250 | 323 |
|
251 | 324 | def get_touch(self): |
252 | | - '''Get touch data, not supported''' |
253 | | - return [-1] * 6 |
| 325 | + '''Get touch data''' |
| 326 | + self.send_command(0xb1,[],sleep=0.03) |
| 327 | + self.send_command(0xb2,[],sleep=0.03) |
| 328 | + self.send_command(0xb3,[],sleep=0.03) |
| 329 | + self.send_command(0xb4,[],sleep=0.03) |
| 330 | + self.send_command(0xb5,[],sleep=0.03) |
| 331 | + return [self.xb1[1],self.xb2[1],self.xb3[1],self.xb4[1],self.xb5[1],0] # The last digit is palm, currently not available |
| 332 | + |
| 333 | + def get_matrix_touch(self): |
| 334 | + self.send_command(0xb1,[0xc6],sleep=0.04) |
| 335 | + self.send_command(0xb2,[0xc6],sleep=0.04) |
| 336 | + self.send_command(0xb3,[0xc6],sleep=0.04) |
| 337 | + self.send_command(0xb4,[0xc6],sleep=0.04) |
| 338 | + self.send_command(0xb5,[0xc6],sleep=0.04) |
| 339 | + return self.thumb_matrix , self.index_matrix , self.middle_matrix , self.ring_matrix , self.little_matrix |
| 340 | + |
254 | 341 |
|
255 | 342 | def get_faults(self): |
256 | 343 | '''Get motor fault codes''' |
|
0 commit comments