44 lines
1.1 KiB
Python

import subprocess
import time
import matplotlib.pyplot as plt
ITERATIONS = 300
ITERATION_DURATION = 1 #second(s)
def set_rt_priority():
subprocess.run(["schedtool", "-F", "-p", "99"], check=True)
def rt_task():
new_time = time.time()
jitter_record = []
for i in range(ITERATIONS + 1):
old_time = new_time
new_time = time.time()
print("--------------------------------------------------")
print(f"system time:\t{new_time} s")
print(f"time diff:\t{new_time - old_time} s")
# jitter
jitter = new_time - old_time - ITERATION_DURATION
jitter_record.append(jitter)
print(f"jitter:\t{jitter} s")
# sleep for the rest of one second
time.sleep(new_time + ITERATION_DURATION - time.time())
#remove the first measurement
jitter_record.pop(0)
#plot and store the jitter record
plt.plot(jitter_record)
plt.title(f"Jitter at {ITERATION_DURATION} s interval")
plt.xlabel("Iteration")
plt.ylabel("Jitter (in s)")
plt.savefig("out.svg")
if __name__ == "__main__":
set_rt_priority()
rt_task()