2025-01-18 18:34:05 +03:00
|
|
|
import can
|
|
|
|
import struct
|
2025-02-04 13:41:05 +03:00
|
|
|
import argparse
|
2025-01-18 18:34:05 +03:00
|
|
|
|
|
|
|
# Function to send the target speed
|
|
|
|
def send_target_speed(bus, target_speed):
|
|
|
|
msg = can.Message()
|
|
|
|
msg.arbitration_id = 1 # Message ID
|
|
|
|
msg.is_extended_id = False
|
|
|
|
msg.dlc = 5 # Message length
|
|
|
|
msg.data = [ord('V')] + list(struct.pack('<f', target_speed)) # 'V' for the command identifier, followed by the speed in float format
|
|
|
|
|
|
|
|
try:
|
|
|
|
bus.send(msg)
|
|
|
|
print(f"Sent message with target speed: {target_speed} m/s")
|
|
|
|
print(f"Message data: {msg.data}")
|
|
|
|
except can.CanError:
|
|
|
|
print("Message failed to send")
|
|
|
|
|
2025-02-04 13:41:05 +03:00
|
|
|
# Main function
|
2025-01-18 18:34:05 +03:00
|
|
|
def main():
|
2025-02-04 13:41:05 +03:00
|
|
|
parser = argparse.ArgumentParser(description="Send target speed over CAN bus.")
|
|
|
|
parser.add_argument("--speed", type=float, required=True, help="Target speed to send over the CAN bus (in m/s)")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
target_speed = args.speed
|
|
|
|
|
2025-01-18 18:34:05 +03:00
|
|
|
# CAN interface setup
|
|
|
|
bus = can.interface.Bus(channel='can0', bustype='socketcan', bitrate=1000000) # Ensure the bitrate matches the microcontroller settings
|
2025-02-04 13:41:05 +03:00
|
|
|
print("CAN bus initialized, sending target speed...")
|
2025-01-18 18:34:05 +03:00
|
|
|
|
2025-02-04 13:41:05 +03:00
|
|
|
# Send the message once
|
|
|
|
send_target_speed(bus, target_speed)
|
2025-01-18 18:34:05 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|