Skip to content

Commit 49814b4

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/unit: RollerCAN support W/R PID.
Signed-off-by: Tinyu <[email protected]>
1 parent 1d13d40 commit 49814b4

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

m5stack/libs/unit/rollercan.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,28 @@
3939
"SPEED_MAX_CURRENT": (0x50, 0x12, None, 0x7018, 4),
4040
"SPEED_READBACK": (0x60, 0x12, None, 0x7030, 4),
4141
"SPEED_PID": (0x70, 0x12, None, 0x7020, 4), # 特例SPEED ,P,I,D被分为0x7020~0x7022三个写入地址
42+
"SPEED_PID_I": (
43+
None,
44+
0x12,
45+
None,
46+
0x7021,
47+
4,
48+
), # 特例SPEED ,P,I,D被分为0x7020~0x7022三个写入地址
49+
"SPEED_PID_D": (
50+
None,
51+
0x12,
52+
None,
53+
0x7022,
54+
4,
55+
), # 特例SPEED ,P,I,D被分为0x7020~0x7022三个写入地址
4256
#! POSITION CONTROL REGISTER !#
4357
"POSITION_SETTING": (0x80, 0x12, None, 0x7016, 4),
4458
"POSITION_MAX_CURRENT": (0x20, 0x12, None, 0x7017, 4),
4559
"POSITION_READBACK": (0x90, 0x12, None, 0x7031, 4),
46-
"POSITION_PID": (
47-
0xA0,
48-
0x12,
49-
None,
50-
0x7023,
51-
4,
52-
), # POSITION ,P,I,D被分为0x7023~0x7025三个写入地址
60+
"POSITION_PID": (0xA0, 0x12, None, 0x7023, 4),
61+
"POSITION_PID_I": (None, 0x12, None, 0x7024, 4),
62+
"POSITION_PID_D": (None, 0x12, None, 0x7025, 4),
63+
# POSITION ,P,I,D被分为0x7023~0x7025三个写入地址
5364
#! CURRENT CONTROL REGISTER !#
5465
"MAX_CURRENT": (0xB0, 0x12, None, 0x7006, 4),
5566
"CURRENT_READBACK": (0xC0, 0x12, None, 0x7032, 4),
@@ -286,15 +297,27 @@ def set_motor_speed_pid(self, p: float, i: float, d: float) -> None:
286297
p *= 100000
287298
i *= 10000000
288299
d *= 100000
289-
self.write("SPEED_PID", struct.pack("<iii", int(p), int(i), int(d)))
300+
if self._mode == 1:
301+
self.write("SPEED_PID", struct.pack("<i", int(p)))
302+
self.write("SPEED_PID_I", struct.pack("<i", int(i)))
303+
self.write("SPEED_PID_D", struct.pack("<i", int(d)))
304+
else:
305+
self.write("SPEED_PID", struct.pack("<iii", int(p), int(i), int(d)))
290306

291307
def get_motor_speed_pid(self) -> tuple:
292308
"""! Get the motor speed PID.
293309
294310
@return: A tuple containing the PID values.
295311
"""
296-
buf = self.read("SPEED_PID", 12)
297-
return tuple(a / b for a, b in zip(struct.unpack("<iii", buf), (100000, 10000000, 100000)))
312+
if self._mode == 1:
313+
buf = bytearray()
314+
buf.extend(self.read("SPEED_PID", 4))
315+
buf.extend(self.read("SPEED_PID_I", 4))
316+
buf.extend(self.read("SPEED_PID_D", 4))
317+
print(f"buf:{buf}")
318+
else:
319+
buf = self.read("SPEED_PID", 12)
320+
return struct.unpack("<iii", buf)
298321

299322
def set_motor_position(self, position: int) -> None:
300323
"""! Set the motor position and max current setting.
@@ -341,7 +364,14 @@ def get_motor_position_pid(self) -> tuple:
341364
342365
@return: A tuple containing the PID values for position.
343366
"""
344-
buf = self.read("POSITION_PID", 12)
367+
if self._mode == 1:
368+
buf = bytearray()
369+
buf.extend(self.read("POSITION_PID", 4))
370+
buf.extend(self.read("POSITION_PID_I", 4))
371+
buf.extend(self.read("POSITION_PID_D", 4))
372+
print(f"buf:{buf}")
373+
else:
374+
buf = self.read("POSITION_PID", 12)
345375
return struct.unpack("<iii", buf)
346376

347377
def set_motor_position_pid(self, p: float, i: float, d: float) -> None:
@@ -354,9 +384,12 @@ def set_motor_position_pid(self, p: float, i: float, d: float) -> None:
354384
p *= 100000
355385
i *= 10000000
356386
d *= 100000
357-
buf = struct.pack("<iii", int(p), int(i), int(d))
358-
print(f"buf:{buf}")
359-
self.write("POSITION_PID", buf)
387+
if self._mode == 1:
388+
self.write("POSITION_PID", struct.pack("<i", int(p)))
389+
self.write("POSITION_PID_I", struct.pack("<i", int(i)))
390+
self.write("POSITION_PID_D", struct.pack("<i", int(d)))
391+
else:
392+
self.write("POSITION_PID", struct.pack("<iii", int(p), int(i), int(d)))
360393

361394
def set_motor_max_current(self, current: int) -> None:
362395
"""! Set the motor max current.
@@ -489,6 +522,7 @@ def __init__(
489522
"""
490523
self._i2c_bus = i2c
491524
self._i2c_addr = address
525+
self._mode = mode
492526
super().__init__()
493527

494528
def read(self, register, length) -> bytes:
@@ -518,6 +552,8 @@ def __init__(self, bus, address=_ROLLERCAN_CAN_ADDR, mode=None) -> None:
518552
"""
519553
self._can_bus = bus
520554
self._can_addr = address # motor id == address
555+
self._mode = mode
556+
print(f"mode: {mode}")
521557
super().__init__()
522558
self._obuffer = bytearray(15) # Output buffer for sending commands.
523559
self._ibuffer = bytearray(17) # Input buffer for receiving responses.

0 commit comments

Comments
 (0)