-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_latency.py
94 lines (71 loc) · 2.71 KB
/
time_latency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import can
import math
import time
import matplotlib.pyplot as plt
from datetime import datetime
from pyinstrument import Profiler
from CanJointMotor import CanJointMotor
from CanScrewMotor import CanScrewMotor
from CanUtils import CanUtils
def init():
os.system('sudo ifconfig can0 down')
os.system('sudo ip link set can0 type can bitrate 1000000')
os.system('sudo ifconfig can0 up')
def cleanup():
os.system('sudo ifconfig can0 down')
def profile(motor):
profiler = Profiler()
profiler.start()
for i in range(1000):
motor.pos_ctrl(0x00, 0x41, 0x00, 0x00)
motor.read_encoder()
profiler.stop()
print(profiler.output_text(unicode=True, color=True))
def avg(l):
if (len(l) == 0):
return 0
return sum(l)/len(l)
if __name__ == "__main__":
init()
# make the can bus
canBus = can.ThreadSafeBus(channel='can0', bustype='socketcan_ctypes')
# motor initialization
joint1 = CanJointMotor(canBus, 0x141)
screw = CanScrewMotor(canBus, 0x143)
utils = CanUtils()
start = datetime.now()
send_cmds_joint = []
recv_cmds_joint = []
send_cmds_screw = []
recv_cmds_screw = []
for i in range(200):
try:
time_since_start = datetime.now() - start
to_vel = (math.pi**2) * math.sin(time_since_start.total_seconds() * 0.2 * math.pi)
to_pos = (math.pi**2) * math.sin(time_since_start.total_seconds() * 0.2 * math.pi)
before_send = datetime.now()
joint1.pos_ctrl(to_pos)
send_cmds_joint.append((before_send - datetime.now()).total_seconds())
before_recv = datetime.now()
joint_position = joint1.read_position()
recv_cmds_joint.append((before_send - datetime.now()).total_seconds())
before_send = datetime.now()
screw.speed_ctrl(to_vel)
send_cmds_screw.append((before_send - datetime.now()).total_seconds())
before_recv = datetime.now()
screw_speed = screw.read_speed()
recv_cmds_screw.append((before_send - datetime.now()).total_seconds())
loop_dur = datetime.now() - start - time_since_start
# 10ms for each loop
time.sleep(max(0, 0.01 - loop_dur.total_seconds()))
except (KeyboardInterrupt, ValueError) as e:
print(e)
break
joint1.motor_stop()
screw.motor_stop()
print("avg joint send latency = %d" % (avg(send_cmds_joint)))
print("avg joint receive latency = %d" % (avg(recv_cmds_joint)))
print("avg screw send latency = %d" % (avg(send_cmds_screw)))
print("avg screw receive latency = %d" % (avg(recv_cmds_screw)))
cleanup()