39
39
"SPEED_MAX_CURRENT" : (0x50 , 0x12 , None , 0x7018 , 4 ),
40
40
"SPEED_READBACK" : (0x60 , 0x12 , None , 0x7030 , 4 ),
41
41
"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三个写入地址
42
56
#! POSITION CONTROL REGISTER !#
43
57
"POSITION_SETTING" : (0x80 , 0x12 , None , 0x7016 , 4 ),
44
58
"POSITION_MAX_CURRENT" : (0x20 , 0x12 , None , 0x7017 , 4 ),
45
59
"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三个写入地址
53
64
#! CURRENT CONTROL REGISTER !#
54
65
"MAX_CURRENT" : (0xB0 , 0x12 , None , 0x7006 , 4 ),
55
66
"CURRENT_READBACK" : (0xC0 , 0x12 , None , 0x7032 , 4 ),
@@ -286,15 +297,27 @@ def set_motor_speed_pid(self, p: float, i: float, d: float) -> None:
286
297
p *= 100000
287
298
i *= 10000000
288
299
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 )))
290
306
291
307
def get_motor_speed_pid (self ) -> tuple :
292
308
"""! Get the motor speed PID.
293
309
294
310
@return: A tuple containing the PID values.
295
311
"""
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 )
298
321
299
322
def set_motor_position (self , position : int ) -> None :
300
323
"""! Set the motor position and max current setting.
@@ -341,7 +364,14 @@ def get_motor_position_pid(self) -> tuple:
341
364
342
365
@return: A tuple containing the PID values for position.
343
366
"""
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 )
345
375
return struct .unpack ("<iii" , buf )
346
376
347
377
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:
354
384
p *= 100000
355
385
i *= 10000000
356
386
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 )))
360
393
361
394
def set_motor_max_current (self , current : int ) -> None :
362
395
"""! Set the motor max current.
@@ -489,6 +522,7 @@ def __init__(
489
522
"""
490
523
self ._i2c_bus = i2c
491
524
self ._i2c_addr = address
525
+ self ._mode = mode
492
526
super ().__init__ ()
493
527
494
528
def read (self , register , length ) -> bytes :
@@ -518,6 +552,8 @@ def __init__(self, bus, address=_ROLLERCAN_CAN_ADDR, mode=None) -> None:
518
552
"""
519
553
self ._can_bus = bus
520
554
self ._can_addr = address # motor id == address
555
+ self ._mode = mode
556
+ print (f"mode: { mode } " )
521
557
super ().__init__ ()
522
558
self ._obuffer = bytearray (15 ) # Output buffer for sending commands.
523
559
self ._ibuffer = bytearray (17 ) # Input buffer for receiving responses.
0 commit comments