Skip to content

Commit

Permalink
got socketcan working with detection of can device
Browse files Browse the repository at this point in the history
  • Loading branch information
RCMast3r committed Feb 19, 2024
1 parent 02c47ac commit 5e4a914
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
13 changes: 13 additions & 0 deletions down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Check if can0 is already down
if ! ip link show up | grep -q can0; then
echo "can0 is already down."
else
echo "Bringing down can0..."

# Bring down can0 interface
sudo ip link set down can0

echo "can0 has been brought down."
fi
43 changes: 23 additions & 20 deletions py_data_acq/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"local_can_usb_KV": [0, 'kvaser'],
"local_debug": ["vcan0", 'socketcan']
}
def find_can_interface():
"""Find a CAN interface by checking /sys/class/net/."""
for interface in os.listdir('/sys/class/net/'):
if interface.startswith('can'):
return interface
return None

async def continuous_can_receiver(can_msg_decoder: cantools.db.Database, message_classes, queue, q2, can_bus):
loop = asyncio.get_event_loop()
Expand All @@ -53,25 +59,6 @@ async def continuous_can_receiver(can_msg_decoder: cantools.db.Database, message
notifier.stop()


# with can.Bus(
# interface='socketcan', channel='vcan0', receive_own_messages=True
# ) as bus:
# # bus = can.Bus(interface='socketcan', channel='vcan0', receive_own_messages=True)
# reader = can.AsyncBufferedReader()
# loop = asyncio.get_running_loop()
# notifier = can.Notifier(bus, [reader], loop=loop)
# while True:
# msg = await reader.get_message()
# 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)
# print("received new messgae")
# data = QueueData(msg.DESCRIPTOR.name, msg)
# await queue.put(data)
# await q2.put(data)

# notifier.stop()

async def write_data_to_mcap(queue, mcap_writer):
async with mcap_writer as mcw:
while True:
Expand All @@ -86,7 +73,23 @@ async def run(logger):

# for example, we will have CAN as our only input as of right now but we may need to add in
# a sensor that inputs over UART or ethernet
bus = can.Bus(interface='socketcan', channel='vcan0', receive_own_messages=True)

can_interface = find_can_interface()

if can_interface:
print(f"Found CAN interface: {can_interface}")
try:
# Attempt to initialize the CAN bus
bus = can.interface.Bus(channel=can_interface, bustype='socketcan')
print(f"Successfully initialized CAN bus on {can_interface}")
# Interface exists and bus is initialized, but this doesn't ensure the interface is 'up'
except can.CanError as e:
print(f"Failed to initialize CAN bus on {can_interface}: {e}")
else:

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

queue = asyncio.Queue()
queue2 = asyncio.Queue()
path_to_bin = ""
Expand Down
20 changes: 20 additions & 0 deletions up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Check if can0 is up
if ! ip link show up | grep -q can0; then
echo "Bringing up can0..."

# Load CAN related modules
sudo modprobe can
sudo modprobe can_raw

# Set the bitrate for can0
sudo ip link set can0 type can bitrate 5000000

# Bring up can0 interface
sudo ip link set up can0

echo "can0 setup complete."
else
echo "can0 is already up."
fi

0 comments on commit 5e4a914

Please sign in to comment.