-
Notifications
You must be signed in to change notification settings - Fork 0
/
esc_latency.py
84 lines (72 loc) · 1.79 KB
/
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
# Description: sends commands to arduino (adafruit_pwm.ino or adafruit_pwm_pat.ino)
# to spin motor, and reads in tachometer RPM data.
import serial
import numpy as np
import matplotlib.pyplot as plt
import time
ser = serial.Serial('/dev/cu.usbmodem14101',9600)
time.sleep(3)
ser.flushInput()
ser.flushOutput()
command = 700
data = []
latency_flag = 1
print('write arm')
ser.write((str(command)+'\n').encode()) #arm motor
time.sleep(2.75)
ser.flushInput()
ser.flushOutput()
command = 1000
print('write spin')
ser.write((str(command)+'\n').encode()) #spin motor
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+=10
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 >= 3000 and RPM < 5000):
if latency_flag:
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('PWM Data')
plt.show()
print("Latency = ", latency_end-latency_begin)
ser.close()