Skip to content

Commit 572f841

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/unit: RollerCAN complete support CAN Mode.
Signed-off-by: Tinyu <[email protected]>
1 parent 49814b4 commit 572f841

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

m5stack/cmodules/m5can/modcan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static mp_obj_t pyb_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
356356
for (int i = 0; i < bufinfo.len; i++) {
357357
mp_printf(&mp_plat_print, "0x%02X ", tx_msg.data[i]);
358358
}
359-
mp_printf(&mp_plat_print, "\n");
359+
mp_printf(&mp_plat_print, "\n\n");
360360

361361
check_esp_err(twai_transmit(&tx_msg, args[ARG_timeout].u_int));
362362
return mp_const_none;
@@ -381,7 +381,7 @@ static mp_obj_t pyb_can_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
381381
twai_message_t rx_msg;
382382
esp_err_t ret = twai_receive(&rx_msg, args[ARG_timeout].u_int);
383383

384-
mp_printf(&mp_plat_print, "Received identifier: 0x%02X\n", rx_msg.identifier);
384+
mp_printf(&mp_plat_print, "Received identifier: 0x%08X\n", rx_msg.identifier);
385385
mp_printf(&mp_plat_print, "received data: ");
386386
for (int i = 0; i < rx_msg.data_length_code; i++) {
387387
mp_printf(&mp_plat_print, "0x%02X ", rx_msg.data[i]);

m5stack/libs/unit/rollercan.py

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
#! MOTOR CONFIGURATION REGISTER !# 这里的LENGTH是指可读写单个参数列表中的字节数
2626
"MOTOR_OUTPUT": (0x00, 0x12, None, 0x7004, 1),
2727
"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
2929
"RELEASE_STALL_PROTECT": (0x0B, 0x12, None, 0x7003, 1),
3030
"MOTOR_STATUS": (0x0C, None, None, None, 22),
3131
"MOTOR_ERROR_CODE": (0x0D, None, None, None),
3232
"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
3537
"CAN_BSP": (0x11, 0x0B, 16, None),
3638
"RGB_BRIGHT": (0x12, 0x12, None, 0x7052, 1),
3739
#! SPEED CONTROL REGISTER !#
@@ -195,7 +197,9 @@ def set_motor_jam_protect_state(self, state: int = 1) -> None:
195197
196198
@param state: Protection state value (1 to enable, 0 to disable).
197199
"""
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+
)
199203

200204
def get_motor_jam_protect_state(self) -> bool:
201205
"""! Get the motor jam protection status.
@@ -209,7 +213,9 @@ def set_motor_id(self, id: int = 0) -> None:
209213
210214
@param id: The ID to assign to the motor.
211215
"""
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+
)
213219

214220
def get_motor_id(self) -> int:
215221
"""! Get the motor ID.
@@ -218,17 +224,17 @@ def get_motor_id(self) -> int:
218224
"""
219225
return self.read("MOTOR_ID", 1)[0]
220226

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.
223229
224230
@param bps: Baud rate value.
225231
"""
226232
self.write("CAN_BSP", bytes([bps]))
227233

228-
def get_485_baudrate(self) -> int:
229-
"""! Get the 485 baudrate.
234+
def get_can_baudrate(self) -> int:
235+
"""! Get the can baudrate.
230236
231-
@return: The current 485 baudrate.
237+
@return: The current can baudrate.
232238
"""
233239
return self.read("CAN_BSP", 1)[0]
234240

@@ -563,41 +569,63 @@ def __init__(self, bus, address=_ROLLERCAN_CAN_ADDR, mode=None) -> None:
563569
def create_frame(self, register, option, data, is_read=False):
564570
if len(data) < 4:
565571
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
578584
return identifier, can_data
579585

580586
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)
583593
return self.read_response()
584594

585-
def write(self, register, bytes):
595+
def write(self, register, data):
586596
try:
587-
identifier, can_data = self.create_frame(register, 0, bytes)
597+
print(data)
598+
identifier, can_data = self.create_frame(register, 0, data)
588599
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
593608
except Exception as e:
594609
print(f"Error in write: {e}")
595610
return False
596611

597612
def read_response(self):
598613
receive_data = self._can_bus.recv(0, timeout=200)
599614
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
601629
motor_can_id = (receive_data[0] >> 0) & 0xFF # 0~7 bit
602630
index = (receive_data[4][1] << 8) | receive_data[4][0]
603631
data = receive_data[4][4:8]
@@ -609,7 +637,12 @@ def read_response(self):
609637
print(f"Index: 0x{index:04x}")
610638
print(f"Data: 0x{data.hex()}")
611639
# 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])
613646

614647

615648
class RollerCANUnit:

0 commit comments

Comments
 (0)