25
25
#! MOTOR CONFIGURATION REGISTER !# 这里的LENGTH是指可读写单个参数列表中的字节数
26
26
"MOTOR_OUTPUT" : (0x00 , 0x12 , None , 0x7004 , 1 ),
27
27
"MOTOR_MODE" : (0x01 , 0x12 , None , 0x7005 , 1 ),
28
- "MOTOR_ORP" : (0x0A , 0x0C , None , None ), # 0x0C:ENABLE,0x0D:DISABLE
28
+ "MOTOR_ORP" : (0x0A , None , None , None ), # 0x0C:ENABLE,0x0D:DISABLE
29
29
"RELEASE_STALL_PROTECT" : (0x0B , 0x12 , None , 0x7003 , 1 ),
30
30
"MOTOR_STATUS" : (0x0C , None , None , None , 22 ),
31
31
"MOTOR_ERROR_CODE" : (0x0D , None , None , None ),
32
32
"BTN_SWITCH_MODE_ENABLE" : (0x0E , None , None , None ),
33
- "MOTOR_STALL_PROTRCT" : (0x0F , None , None , None ),
34
- "MOTOR_ID" : (0x10 , 0x00 , 16 , None , 8 ), # REA CMD ID为0x00,WRITE CMD ID为0x07
33
+ "MOTOR_STALL_PROTRCT" : (0x0F , 0x0C , None , None ),
34
+ "MOTOR_STALL_PROTRCT_REMOVE" : (0x0F , 0x0D , None , None ),
35
+ "MOTOR_ID" : (0x10 , 0x00 , 16 , None , 8 ), # REA CMD ID为0x00
36
+ "SET_MOTOR_ID" : (0x10 , 0x07 , 16 , None , 8 ), # WRITE CMD ID为0x07
35
37
"CAN_BSP" : (0x11 , 0x0B , 16 , None ),
36
38
"RGB_BRIGHT" : (0x12 , 0x12 , None , 0x7052 , 1 ),
37
39
#! SPEED CONTROL REGISTER !#
@@ -195,7 +197,9 @@ def set_motor_jam_protect_state(self, state: int = 1) -> None:
195
197
196
198
@param state: Protection state value (1 to enable, 0 to disable).
197
199
"""
198
- self .write ("MOTOR_STALL_PROTRCT" , bytes ([state ]))
200
+ self .write (
201
+ "MOTOR_STALL_PROTRCT" if state else "MOTOR_STALL_PROTRCT_REMOVE" , bytes ([state ])
202
+ )
199
203
200
204
def get_motor_jam_protect_state (self ) -> bool :
201
205
"""! Get the motor jam protection status.
@@ -209,7 +213,9 @@ def set_motor_id(self, id: int = 0) -> None:
209
213
210
214
@param id: The ID to assign to the motor.
211
215
"""
212
- self .write ("MOTOR_ID" , bytes ([id ]))
216
+ self .write (
217
+ "SET_MOTOR_ID" if self ._mode != RollerCANUnit ._I2C_MODE else "MOTOR_ID" , bytes ([id ])
218
+ )
213
219
214
220
def get_motor_id (self ) -> int :
215
221
"""! Get the motor ID.
@@ -218,17 +224,17 @@ def get_motor_id(self) -> int:
218
224
"""
219
225
return self .read ("MOTOR_ID" , 1 )[0 ]
220
226
221
- def set_485_baudrate (self , bps : int = 0 ) -> None :
222
- """! Set the 485 baudrate.
227
+ def set_can_baudrate (self , bps : int = 0 ) -> None :
228
+ """! Set the can baudrate.
223
229
224
230
@param bps: Baud rate value.
225
231
"""
226
232
self .write ("CAN_BSP" , bytes ([bps ]))
227
233
228
- def get_485_baudrate (self ) -> int :
229
- """! Get the 485 baudrate.
234
+ def get_can_baudrate (self ) -> int :
235
+ """! Get the can baudrate.
230
236
231
- @return: The current 485 baudrate.
237
+ @return: The current can baudrate.
232
238
"""
233
239
return self .read ("CAN_BSP" , 1 )[0 ]
234
240
@@ -563,41 +569,63 @@ def __init__(self, bus, address=_ROLLERCAN_CAN_ADDR, mode=None) -> None:
563
569
def create_frame (self , register , option , data , is_read = False ):
564
570
if len (data ) < 4 :
565
571
data = data [:4 ] + b"\x00 " * (4 - len (data ))
566
- identifier = (
567
- 0x00000000
568
- | (( _COMMON_REG [ register ][ 1 ] if not is_read else _COMMON_REG [ register ][ 1 ] - 1 ) << 24 )
569
- | ( option << 16 )
570
- | self . _can_addr
571
- )
572
- can_data = (
573
- bytes (
574
- [ _COMMON_REG [ register ][ 3 ] & 0xFF , ( _COMMON_REG [ register ][ 3 ] >> 8 ) & 0xFF , 0x0 , 0x0 ]
575
- )
576
- + data
577
- )
572
+ cmd_id = _COMMON_REG [ register ][ 1 ]
573
+ if is_read :
574
+ if cmd_id == 0x12 :
575
+ cmd_id -= 1
576
+ elif cmd_id in range ( 0x0C , 0x0D + 1 ):
577
+ cmd_id = 0x02
578
+ else :
579
+ if cmd_id == 0x07 or cmd_id == 0x0B :
580
+ option = data [ 0 ]
581
+ index_id = _COMMON_REG [ register ][ 3 ] or 0
582
+ identifier = 0x00000000 | ( cmd_id << 24 ) | ( option << 16 ) | self . _can_addr
583
+ can_data = bytes ([ index_id & 0xFF , ( index_id >> 8 ) & 0xFF , 0x0 , 0x0 ]) + data
578
584
return identifier , can_data
579
585
580
586
def read (self , register , length ):
581
- identifier , can_data = self .create_frame (register , 0 , bytes (0 ), is_read = True )
582
- self ._can_bus .send (can_data , id = identifier , timeout = 0 , rtr = False , extframe = True )
587
+ if _COMMON_REG [register ][1 ] in {0x12 , 0x00 }:
588
+ print ("0x00 or 0x12" )
589
+ if _COMMON_REG [register ][1 ] == 0x12 :
590
+ self ._can_bus .recv (0 , timeout = 100 ) # 清除写入后的返回数据,调整超时为 100ms
591
+ identifier , can_data = self .create_frame (register , 0 , bytes (0 ), is_read = True )
592
+ self ._can_bus .send (can_data , id = identifier , timeout = 0 , rtr = False , extframe = True )
583
593
return self .read_response ()
584
594
585
- def write (self , register , bytes ):
595
+ def write (self , register , data ):
586
596
try :
587
- identifier , can_data = self .create_frame (register , 0 , bytes )
597
+ print (data )
598
+ identifier , can_data = self .create_frame (register , 0 , data )
588
599
self ._can_bus .send (can_data , id = identifier , timeout = 0 , rtr = False , extframe = True )
589
- self ._can_bus .recv (0 , timeout = 100 ) # 清除写入后的返回数据,调整超时为 100ms
590
- buf = self .read (register , bytes )
591
- # if (can_data[4:8], _COMMON_REG[register][3]) == buf:
592
- return (can_data [4 :8 ]) == buf
600
+ buf = self .read (register , data )
601
+ if _COMMON_REG [register ][1 ] == 0x12 :
602
+ if (can_data [4 :8 ], _COMMON_REG [register ][3 ]) == buf :
603
+ return (can_data [4 :8 ]) == buf
604
+ elif _COMMON_REG [register ][1 ] == 0x0B :
605
+ return data == buf
606
+ else :
607
+ return True
593
608
except Exception as e :
594
609
print (f"Error in write: { e } " )
595
610
return False
596
611
597
612
def read_response (self ):
598
613
receive_data = self ._can_bus .recv (0 , timeout = 200 )
599
614
cmd_id = (receive_data [0 ] >> 24 ) & 0x1F # 24~28 bit
600
- master_can_id = (receive_data [0 ] >> 8 ) & 0xFFFF # 8~23 bit
615
+ if cmd_id == 0x02 :
616
+ print ("Feedback data" )
617
+ error_info = (receive_data [0 ] >> 16 ) & 0x07
618
+ over_range = (receive_data [0 ] >> 18 ) & 0x01
619
+ jam_motor = (receive_data [0 ] >> 17 ) & 0x01
620
+ over_voltage = (receive_data [0 ] >> 16 ) & 0x01
621
+ print (
622
+ f"Error Info: { error_info } , Over Range: { over_range } , Jam Motor: { jam_motor } , Over Voltage: { over_voltage } "
623
+ )
624
+ elif cmd_id == 0x0B :
625
+ print ("CAN BPS" )
626
+ can_bps = (receive_data [0 ] >> 16 ) & 0xFF # 16~23bit
627
+ print (f"CAN BPS:{ can_bps } " )
628
+ master_can_id = (receive_data [0 ] >> 8 ) & 0xFF # 8~15 bit
601
629
motor_can_id = (receive_data [0 ] >> 0 ) & 0xFF # 0~7 bit
602
630
index = (receive_data [4 ][1 ] << 8 ) | receive_data [4 ][0 ]
603
631
data = receive_data [4 ][4 :8 ]
@@ -609,7 +637,12 @@ def read_response(self):
609
637
print (f"Index: 0x{ index :04x} " )
610
638
print (f"Data: 0x{ data .hex ()} " )
611
639
# return data, index
612
- return data
640
+ if cmd_id == 0x11 :
641
+ return data
642
+ elif cmd_id == 0x00 :
643
+ return bytes ([master_can_id ])
644
+ elif cmd_id == 0x0B :
645
+ return bytes ([can_bps ])
613
646
614
647
615
648
class RollerCANUnit :
0 commit comments