Skip to content

Commit

Permalink
Merge pull request #5 from RCMast3r/TPMS
Browse files Browse the repository at this point in the history
TPMS
  • Loading branch information
SreeDan authored Mar 1, 2024
2 parents 594da70 + 2e8b7e1 commit 1913d82
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/data_acq.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
mcap.url = "github:RCMast3r/py_mcap_nix";
foxglove-websocket.url = "github:RCMast3r/py_foxglove_webserver_nix";
asyncudp.url = "github:RCMast3r/asyncudp_nix";
ht_can_pkg_flake.url = "github:hytech-racing/ht_can";
ht_can_pkg_flake.url = "github:hytech-racing/ht_can/TPMS_add";
nix-proto = { url = "github:notalltim/nix-proto"; };
};

Expand Down
64 changes: 64 additions & 0 deletions py_data_acq/broadcast-test-TPMS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python
import socket
import time
import can
from can.interfaces.udp_multicast import UdpMulticastBus
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
# bus1 = can.interface.Bus('can0', bustype='virtual')
bus1 = can.Bus(channel=UdpMulticastBus.DEFAULT_GROUP_IPv6, interface='udp_multicast')
def main():
path_to_dbc = os.environ.get('DBC_PATH')
full_path = os.path.join(path_to_dbc, "hytech.dbc")
# Serialize the message to bytes
db = cantools.database.load_file(full_path)

msg = db.get_message_by_name("MC1_TORQUE_COMMAND")
rpm = db.get_message_by_name("MC4_SETPOINTS_COMMAND")
tpmsLF_msg = db.get_message_by_name("LF_TTPMS_1")
tpmsLR_msg = db.get_message_by_name("LR_TTPMS_1")
tpmsRF_msg = db.get_message_by_name("RF_TTPMS_1")
tpmsRR_msg = db.get_message_by_name("RR_TTPMS_1")

data = msg.encode({'torque_command': 100})
msg = can.Message(arbitration_id=msg.frame_id, is_extended_id=False, data=data)

tpmsLF_data = tpmsLF_msg.encode({'LF_TTPMS_SN': 100, 'LF_TTPMS_BAT_V': 100, 'LF_TTPMS_P': 20, 'LF_TTPMS_P_GAUGE': 20})
tpmsLF_msg = can.Message(arbitration_id=tpmsLF_msg.frame_id, is_extended_id=False, data=tpmsLF_data)

tpmsLR_data = tpmsLR_msg.encode({'LR_TTPMS_SN': 100, 'LR_TTPMS_BAT_V': 100, 'LR_TTPMS_P': 20, 'LR_TTPMS_P_GAUGE': 20})
tpmsLR_msg = can.Message(arbitration_id=tpmsLR_msg.frame_id, is_extended_id=False, data=tpmsLR_data)

tpmsRF_data = tpmsRF_msg.encode({'RF_TTPMS_SN': 100, 'RF_TTPMS_BAT_V': 100, 'RF_TTPMS_P': 20, 'RF_TTPMS_P_GAUGE': 20})
tpmsRF_msg = can.Message(arbitration_id=tpmsRF_msg.frame_id, is_extended_id=False, data=tpmsRF_data)

tpmsRR_data = tpmsRR_msg.encode({'RR_TTPMS_SN': 100, 'RR_TTPMS_BAT_V': 100, 'RR_TTPMS_P': 20, 'RR_TTPMS_P_GAUGE': 20})
tpmsRR_msg = can.Message(arbitration_id=tpmsRR_msg.frame_id, is_extended_id=False, data=tpmsRR_data)


rpm_set = 100
while(1):
try:
rpm_set= rpm_set+1
bus1.send(msg)
rpm_data = rpm.encode({'negative_torque_limit': 1, 'positive_torque_limit': 1, 'speed_setpoint_rpm': rpm_set, 'remove_error': 1, 'driver_enable': 1, 'hv_enable': 1, 'inverter_enable': 1})
rpm_msg = can.Message(arbitration_id=rpm.frame_id, is_extended_id=False, data=rpm_data)
bus1.send(rpm_msg)

bus1.send(tpmsLF_msg)
bus1.send(tpmsLR_msg)
bus1.send(tpmsRF_msg)
bus1.send(tpmsRR_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.1)

if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion py_data_acq/py_data_acq/web_server/mcap_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ async def start_mcap_generation(self):

async def stop_mcap_generation(self):
if self.mcap_writer is not None:
await self.mcap_writer.__aexit__(None, None, None)
# await self.mcap_writer.__aexit__(None, None, None)
self.mcap_writer.finish()
self.mcap_writer.writing_file.close()
self.mcap_status_message = "No MCAP file is being written."
self.mcap_writer = None

Expand Down
7 changes: 3 additions & 4 deletions py_data_acq/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# - config to inform io handler (say for different CAN baudrates)

can_methods = {
"debug": [UdpMulticastBus.DEFAULT_GROUP_IPv6, 'udp_multicast'],
"debug": [UdpMulticastBus.DEFAULT_GROUP_IPv4, 'udp_multicast'],
"local_can_usb_KV": [0, 'kvaser'],
"local_debug": ["vcan0", 'socketcan']
}
Expand All @@ -43,7 +43,6 @@ async def continuous_can_receiver(can_msg_decoder: cantools.db.Database, message
# Wait for the next message from the buffer
msg = await reader.get_message()
try:

decoded_msg = can_msg_decoder.decode_message(msg.arbitration_id, msg.data, decode_containers=True)
msg = can_msg_decoder.get_message_by_frame_id(msg.arbitration_id)
msg = pb_helpers.pack_protobuf_msg(decoded_msg, msg.name.lower(), message_classes)
Expand Down Expand Up @@ -87,7 +86,7 @@ async def run(logger):
else:

print("defaulting to using virtual can interface vcan0")
bus = can.Bus(interface='socketcan', channel='vcan0', receive_own_messages=True)
bus = can.Bus(channel=UdpMulticastBus.DEFAULT_GROUP_IPv6, interface='udp_multicast')

queue = asyncio.Queue()
queue2 = asyncio.Queue()
Expand All @@ -114,7 +113,7 @@ async def run(logger):
path_to_mcap = "/home/nixos/recordings"

mcap_writer = HTPBMcapWriter(path_to_mcap, list_of_msg_names, True)
mcap_server = MCAPServer(mcap_writer=mcap_writer, path=path_to_mcap)
mcap_server = MCAPServer(mcap_writer=mcap_writer, path=path_to_mcap, )
receiver_task = asyncio.create_task(continuous_can_receiver(db, msg_pb_classes, queue, queue2, bus))
fx_task = asyncio.create_task(fxglv_websocket_consume_data(queue, fx_s))
mcap_task = asyncio.create_task(write_data_to_mcap(queue2, mcap_writer))
Expand Down

0 comments on commit 1913d82

Please sign in to comment.