-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pushing up initial virtual CAN testing
- Loading branch information
Showing
4 changed files
with
55 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,35 @@ | ||
#!/usr/bin/env python | ||
import socket | ||
import time | ||
import can | ||
import cantools | ||
from pprint import pprint | ||
import os | ||
|
||
from hytech_np_proto_py import hytech_pb2 | ||
|
||
# Define the IP and port for the UDP socket | ||
UDP_IP = "127.0.0.1" | ||
UDP_PORT = 12345 | ||
|
||
bus1 = can.interface.Bus('can0', bustype='virtual') | ||
|
||
def main(): | ||
# Create an instance of your message | ||
my_message = hytech_pb2.id_dashboard_status() | ||
my_message.start_button = True | ||
my_message.buzzer_active = False | ||
my_message.ssok_above_threshold = True | ||
my_message.shutdown_h_above_threshold = True | ||
my_message.mark_button = True | ||
my_message.mode_button = True | ||
my_message.motor_controller_cycle_button = True | ||
my_message.launch_ctrl_button_ = True | ||
my_message.torque_mode_button = True | ||
my_message.led_dimmer_button = True | ||
|
||
my_message.dial_state = "yo" | ||
my_message.ams_led = "yo" | ||
my_message.imd_led = "yo" | ||
my_message.mode_led = "yo" | ||
my_message.motor_controller_error_led = "yo" | ||
my_message.start_status_led = "yo" | ||
my_message.inertia_status_led = "yo" | ||
my_message.mechanical_brake_led = "yo" | ||
my_message.gen_purp_led = "yo" | ||
my_message.bots_led = "yo" | ||
my_message.cockpit_brb_led = "yo" | ||
my_message.crit_charge_led = "yo" | ||
my_message.glv_led = "yo" | ||
my_message.launch_control_led = "yo" | ||
|
||
path_to_dbc = os.environ.get('DBC_PATH') | ||
full_path = os.path.join(path_to_dbc, "hytech.dbc") | ||
# Serialize the message to bytes | ||
serialized_message = my_message.SerializeToString() | ||
db = cantools.database.load_file(full_path) | ||
msg = db.get_message_by_name("ID_MC1_TORQUE_COMMAND") | ||
print(msg.signals) | ||
data = msg.encode({'torque_command': 100}) | ||
|
||
# Create a UDP socket | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
msg = can.Message(arbitration_id=msg.frame_id, is_extended_id=False, data=data) | ||
print(msg) | ||
while(1): | ||
|
||
while 1: | ||
time.sleep(0.2) | ||
try: | ||
# Send the serialized message over the UDP socket | ||
serialized_message = my_message.SerializeToString() | ||
sock.sendto(serialized_message, (UDP_IP, UDP_PORT)) | ||
print(f"Message sent to {UDP_IP}:{UDP_PORT}") | ||
except KeyboardInterrupt: | ||
# Handle Ctrl+C to exit the loop gracefully | ||
sock.close() | ||
break | ||
except Exception as e: | ||
print(f"Error sending message: {e}") | ||
# finally: | ||
# sock.close() | ||
|
||
bus1.send(msg) | ||
print("Message sent on {}".format(bus1.channel_info)) | ||
except can.CanError: | ||
print("Message NOT sent! Please verify can0 is working first") | ||
time.sleep(0.2) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters