Files
ODrive/tools/can_example.py
thatcomputerguy0101 8b37784e8c Fix current_state in can_example.py
The documentation, CAN doc file, and firmware all indicate that the current state is only one byte of the heartbeat message. This file used to read four bytes as the state on line 21, but only one byte on line 49. This PR adjusts line 21 so that it only uses one byte, making this example work. Otherwise, it incorrectly reads the flags that occupy the next three bytes as part of the state, which can cause the idle state to never be recognized.
2022-03-21 21:28:38 -04:00

54 lines
1.7 KiB
Python

import can
bus = can.Bus("can0", bustype="socketcan")
axisID = 0x1
print("Requesting AXIS_STATE_FULL_CALIBRATION_SEQUENCE (0x03) on axisID: " + str(axisID))
msg = can.Message(arbitration_id=axisID << 5 | 0x07, data=[3, 0, 0, 0, 0, 0, 0, 0], dlc=8, is_extended_id=False)
print(msg)
try:
bus.send(msg)
print("Message sent on {}".format(bus.channel_info))
except can.CanError:
print("Message NOT sent! Please verify can0 is working first")
print("Waiting for calibration to finish...")
# Read messages infinitely and wait for the right ID to show up
while True:
msg = bus.recv()
if msg.arbitration_id == (axisID << 5 | 0x01):
current_state = msg.data[4]
if current_state == 0x1:
print("\nAxis has returned to Idle state.")
break
for msg in bus:
if(msg.arbitration_id == (axisID << 5 | 0x01)):
errorCode = msg.data[0] | msg.data[1] << 8 | msg.data[2] << 16 | msg.data[3] << 24
print("\nReceived Axis heartbeat message:")
if errorCode == 0x0:
print("No errors")
else:
print("Axis error! Error code: "+str(hex(errorCode)))
break
print("\nPutting axis",axisID,"into AXIS_STATE_CLOSED_LOOP_CONTROL (0x08)...")
msg = can.Message(arbitration_id=axisID << 5 | 0x07, data=[8, 0, 0, 0, 0, 0, 0, 0], dlc=8, is_extended_id=False)
print(msg)
try:
bus.send(msg)
print("Message sent on {}".format(bus.channel_info))
except can.CanError:
print("Message NOT sent!")
for msg in bus:
if msg.arbitration_id == (axisID << 5 | 0x01):
print("\nReceived Axis heartbeat message:")
if msg.data[4] == 0x8:
print("Axis has entered closed loop")
else:
print("Axis failed to enter closed loop")
break