-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_esc_latency.py
89 lines (77 loc) · 1.9 KB
/
i2c_esc_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
# Desctiption: simialr to esc_latency.py, but latency calculation is done
# in adafruit_pwm_lat.ino, and not just the python script (see README).
# Prints result to terminal.
import serial
import numpy as np
import matplotlib.pyplot as plt
import time
latency_end = 0
ser = serial.Serial('/dev/cu.usbmodem14101',9600)
time.sleep(3)
ser.flushInput()
ser.flushOutput()
data = []
latency_flag = 1
ser.flushInput()
ser.flushOutput()
command = 20
print('write spin')
ser.write((str(command)+'\n').encode()) #spin motor
# test below
input_byte = ser.readline()
string = input_byte.decode()
print('string_spin = ' + string);
# test above
input("Press Enter to begin reading")
#read serial for 5 seconds
start = time.time()
while (time.time() - start) < 5:
ser.flushInput()
ser.flushOutput()
input_byte = ser.readline()
string = input_byte.decode()
string = string.rstrip()
RPM = float(string)
data.append(RPM)
print(RPM)
# spin motor at second speed
ser.flushInput()
ser.flushOutput()
command+=20
ser.write((str(command)+'\n').encode()) #spin motor
latency_begin = time.time()
start = time.time()
while (time.time() - start) < 5:
ser.flushInput()
ser.flushOutput()
input_byte = ser.readline()
string = input_byte.decode()
string = string.rstrip()
RPM = float(string)
data.append(RPM)
if (RPM >= 2500 and RPM < 5000):
if latency_flag == 1:
latency_end = time.time()
latency_flag = 0
print(RPM)
#stop motor
ser.flushInput()
ser.flushOutput()
command = 0
print(command)
print('\n')
ser.write((str(command)+'\n').encode()) #spin motor
time.sleep(0.25)
filtered_data = []
for x in data:
if(x > 500 and x < 4000):
filtered_data.append(x)
plt.plot(filtered_data)
plt.xlabel('Time')
plt.ylabel('RPM')
plt.title('I2C Data')
plt.show()
print("Latency = ", latency_end-latency_begin)
print(latency_begin)
print(latency_end)
ser.close()