How to clear CAN buffer space?

Hi everyone,

I am working with Jetson TX2 for my thesis which requires an ultrasonic sensor (HC-SR04). For image identification and classification I am using the SSD MobileNet V2, a microcontroller (Olimexino STM-32) is used to process the data of the
ultrasonic sensor and the CAN module is used for communication between the TX2 and Olimexino.

I wrote separate codes to check the working of the image classification algorithm and working of CAN and ultrasonic sensor, and both the codes work perfectly. BUT, as soon as I combine both of them and make them work together in one algorithm, I started facing a 10-second delay in receiving the ultrasonic sensor reading.

After a long time of research as well as trial & error, I suspected that the delay is most probably in receiving the CAN message. Let me explain how basically when I run my code it takes time to load the pre-trained model and then it opens the display showing the video running detection algorithm and on the side, it shows the reading from the ultrasonic sensor. This reading is displayed from the beginning but as soon as I change the distance of an object in front of the ultrasonic sensor that change is displayed after 10 seconds. The terminal keeps printing messages even after I unplug the CAN module that means, it is displaying the old message that had already been sent via CAN.

My question is:

  1. Is there a way to reduce /remove this delay?
  2. How can I reduce the CAN buffer memory in Jetson?

Please find attached the image
of my setup and the code that I have been using so far.

Thank you for your time and consideration.

import can # For CAN commands
import jetson.inference # For importing Neural Network
import jetson.utils # For Image Processing and Display
import time

# Load the object detection model
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.6)

# Open and use the video
camera = jetson.utils.videoSource("traffic_video.mp4")  # using inbuilt MIPI CSI camera on Jetson
display = jetson.utils.glDisplay() # To display to the user on screen

#CAN Intiation
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=1000000)

while True:
m = bus.recv()
distance = int.from_bytes(m.data,"big")

if distance <= 54:
print('WARNING! STOP OPERATION! OBJECT WITHIN RANGE.', distance)
else:
print('ENVIRONMENT SAFE FOR OPERATION.', distance)

img = camera.Capture()
detections = net.Detect(img) # obtaining the detections and drawing box
display.RenderOnce(img) # Visualize the results
display.SetTitle("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))type or paste code here