- Created `python_can.py`: - Implements a CAN message receiver that processes angle, velocity, and enable/disable flags. - Added `python_enable_motor.py`: - Sends enable/disable commands to the motor via CAN. - Added `python_send_angle.py`: - Sends target angle commands over the CAN bus. - Added `python_send_velocity.py`: - Sends target velocity commands over the CAN bus. - Configured all scripts to use `python-can` library with `socketcan` interface for CAN communication.
34 lines
1 KiB
Python
34 lines
1 KiB
Python
import can
|
|
import struct
|
|
import time
|
|
|
|
# Function to send the target angle
|
|
def send_target_angle(bus, target_angle):
|
|
msg = can.Message()
|
|
msg.arbitration_id = 1 # Message ID
|
|
msg.is_extended_id = False
|
|
msg.dlc = 5 # Message length
|
|
msg.data = [ord('A')] + list(struct.pack('<f', target_angle)) # 'A' for the command identifier, followed by the angle in float format
|
|
|
|
try:
|
|
bus.send(msg)
|
|
print(f"Sent message with target angle: {target_angle} degrees")
|
|
print(f"Message data: {msg.data}")
|
|
except can.CanError:
|
|
print("Message failed to send")
|
|
|
|
def main():
|
|
# CAN interface setup
|
|
bus = can.interface.Bus(channel='can0', bustype='socketcan', bitrate=1000000) # Ensure the bitrate matches the microcontroller settings
|
|
print("CAN bus initialized, sending target angles...")
|
|
|
|
# Target angle
|
|
target_angle = 0.0
|
|
|
|
# Loop to send messages
|
|
while True:
|
|
send_target_angle(bus, target_angle)
|
|
time.sleep(1) # Delay before sending the next message
|
|
|
|
if __name__ == '__main__':
|
|
main()
|