Skip to content

Instantly share code, notes, and snippets.

@tuna-f1sh
Last active January 8, 2023 15:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tuna-f1sh/2abe421c2547d41e638253e04e509331 to your computer and use it in GitHub Desktop.
Delta time comparison of Socketcan gs_usb hardware and software timestamps
import datetime
import statistics
import plotly.express as px
import pandas as pd
from cantools.logreader import Parser
# path to comparison files
software_file = "/Users/john/Desktop/software-timestamps.log"
hardware_file = "/Users/john/Desktop/hardware-timestamps.log"
def calc(file: str):
deltas = []
micros_delta = []
with open(file) as fd:
log = Parser(fd).iterlines()
last = next(log)[1]
for _, frame in log:
if last and frame:
deltas.append(frame.timestamp - last.timestamp)
last = frame
micros = [x.microseconds for x in deltas]
micros_delta = [abs(2000 - x) for x in micros]
max_percent = (max(micros_delta) / 2000) * 100
print(f"Max: {max(micros)} µs")
print(f"Min: {min(micros)} µs")
print("Stddev: {:.1f} µs".format(statistics.stdev(micros)))
print("Variance: {:.1f} µs".format(statistics.variance(micros)))
print(f"Largest percentage slip: {max_percent}%")
print(f"Median: {statistics.median(deltas)}")
return pd.DataFrame({
'deltatime': deltas,
'micros': micros,
})
# process
hardware = calc(hardware_file)
software = calc(software_file)
# plot
if len(hardware) > len(software):
df = pd.DataFrame({'Hardware': hardware[0:len(software)]['micros'], 'Software': software['micros']})
else:
df = pd.DataFrame({'Hardware': hardware['micros'], 'Software': software[0:len(hardware)]['micros']})
fig = px.scatter({'Software': df['Software'], 'Hardware': df['Hardware']}, labels={'value': 'delta µs', 'variable': 'Timestamping'}, title='Delta time comparison of Socketcan gs_usb hardware and software timestamps for 2 ms cycle CAN message')
fig.show()
fig.write_html("timestamping-scatter.html")
hist = px.histogram(df, nbins=800, labels={'value':'delta us', 'variable': 'Timestamping'}, title='Delta time comparison of Socketcan gs_usb hardware and software timestamps for 2 ms cycle CAN message')
hist.show()
hist.write_html("timestamping-hist.html")
box = px.box(df, points='all', labels={'value':'delta µs', 'variable':'Timestamping'}, title='Delta time comparison of Socketcan gs_usb hardware and software timestamps for 2 ms cycle CAN message')
box.show()
box.write_html("timestamping-box.html")
@tuna-f1sh
Copy link
Author

tuna-f1sh commented Jan 8, 2023

Test files from a candleLight_fw (Entree) interface candump can0 -l -H (hardware) and candump can0 -l (software) receiving messages from a STM32F4 sending every 2 ms in a SysTick ISR (ext osc) using kernel 6.1 gs_usb module:

https://www.dropbox.com/s/phoksassj8jzgx8/hardware-timestamps.log?dl=0
https://www.dropbox.com/s/bn6s6qd5bydsvuw/software-timestamps.log?dl=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment