forked from jumellet/XY-Plotter-2.0-experimentation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcharacterization.py
144 lines (114 loc) · 3.94 KB
/
characterization.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
import glob
import serial
import platform
import time
import processing
import kalman
import numpy as np
from io import StringIO
###############################################################################
# TODO: implement an interactive terminal with getch()?
# https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user
def main():
rx = processing.init_position()
#port = serial_init()
# Number of step on stepper motor
d = 1314
# Distance in centimeter
x = 30
buffer = StringIO()
# For IMU
I_Accelero = np.zeros(3)
data_accel = np.zeros((3,3))
s_acc = 0.0423639918
# For Kalman Filter
I_True = np.zeros(3)
s_True = np.zeros(3)
for i in range(3):
s_True[i] = 0.00001
start = time.time()
print(start)
duration = 0
while duration < 15: # not sine_done(port):
# Measurements
print("1")
I_LH, data_accel, s_acc = processing.get_position(rx)
I_Accelero = data_accel[0]
I_LH = [(I_LH[0][0] + I_LH[3][0]) / 2, (I_LH[0][1] + I_LH[3][1]) / 2, (I_LH[0][2] + I_LH[3][2]) / 2]
print("2")
# Kalman Filter of the position
# Measurement of variances in static
s_Accelero = s_acc[0]
for i in range(3):
s_Accelero[i] = s_Accelero[i]**2
#print(s_Accelero)
s_LH = [1.02121*10**(-6), 8.667*10**(-7), 9.6482*10**(-7)]
# Update position calculated by Kalman filter
#I_True, s_True = kalman.linear_kalman(data_accel, s_Accelero, I_LH, s_LH, I_True, s_True)
# Madgwick fusion
I_True = kalman.madgwick(data_accel, s_Accelero, I_LH, s_LH)
duration = time.time() - start
print(duration)
# Print file
# [duration, I_LH, I_Accelero, I_True]
buffer.write("%s," % duration)
for i in range(3):
buffer.write("%s," % I_LH[i])
for i in range(3):
buffer.write("%s," % I_Accelero[i])
for i in range(2):
buffer.write("%s," % I_True[i])
buffer.write("%s \n" % I_True[2])
BUF = buffer.getvalue()
file = open("data_2_IMU05s_Sin2.txt","w")
file.write(BUF)
file.close()
while True:
pass
###############################################################################
def serial_init():
PLATFORM = platform.system()
if "Linux" in PLATFORM:
SERIAL_PATH = "/dev/ttyUSB*"
elif "Darwin" in PLATFORM:
SERIAL_PATH = "/dev/tty.usb*"
else: # Windows
port = serial.Serial('COM6', 115200, timeout = 0) # TODO ADAPT COM NUMBER !!
SERIAL_PATH = 'WIN_WORKARAOUND'
if SERIAL_PATH != 'WIN_WORKARAOUND':
devices = glob.glob(SERIAL_PATH)
port = serial.Serial(devices[0], 115200)
success = port.isOpen()
if success:
print("Port open, waiting for calibration...\n")
line = port.readline()
while ("start".encode(encoding="utf-8") not in line):
line = port.readline()
else:
print("\n!!! Error: serial device not found !!!")
exit(-1)
return port
###############################################################################
def sine_x(port):
command = "M5" # L1 R" + str(int(d/3)) # 10cm
port.write(command.encode(encoding="utf-8"))
print(command)
def sine_done(port):
line = port.readline()
if ("ok".encode(encoding="utf-8") in line):
return True
else:
return False
###############################################################################
def go_to(x, port):
command = "X" + str(x)+ "\n"
port.write(command.encode(encoding="utf-8"))
print(command)
# wait for acknowledgment
line = port.readline()
while ("ok".encode(encoding="utf-8") not in line):
line = port.readline()
###############################################################################
if __name__ == "__main__":
main()