-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAhrsManager.py
271 lines (227 loc) · 11 KB
/
AhrsManager.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from SoftRealtimeLoop import SoftRealtimeLoop
import time
import csv
import sys, time
import numpy as np
from os.path import realpath
sys.path.append(r'/usr/share/python3-mscl/') # Path of the MSCL)
import traceback
import mscl
from StatProfiler import SSProfile
class AhrsManager():
def __init__(self, csv_file_name=None, dt=0.01, port="/dev/ttyACM0", baud = 921600, communication_protocol = "USB"):
self.port = realpath(port) # dereference symlinks
self.save_csv = not (csv_file_name is None)
self.csv_file_name = csv_file_name
self.csv_file = None
self.csv_writer = None
self.prevTime = 0.0
self.R = np.eye(3)
self.init_R = None
self.R_prime = None
self.dt = dt
self.xd = np.zeros((3,1))
self.x = np.zeros((3,1))
self.xd_forget = 0.0
self.x_forget = 0.0
self.acc_bias = np.zeros((3,1))
self.lp_xdd = 0.0
self.baud = baud
self.communication_protocol = communication_protocol # Allowable arguments are "USB" or "RS232"
def __enter__(self):
if self.save_csv:
with open(self.csv_file_name,'w') as fd:
writer = csv.writer(fd)
writer.writerow(["pi_time",
"r00", "r01", "r02",
"r10", "r11", "r12",
"r20", "r21", "r22"])
self.csv_file = open(self.csv_file_name,'a').__enter__()
self.csv_writer = csv.writer(self.csv_file)
self.connection = mscl.Connection.Serial(self.port, self.baud)
self.node = mscl.InertialNode(self.connection)
if self.communication_protocol == "USB":
self.node.setToIdle()
time.sleep(0.05)
# self.deltaTime = 0
# self.sampleRate = mscl.SampleRate(1,500)
#Resume node for streaming
# self.node.resume()
#if the node supports AHRS/IMU
if self.communication_protocol == "USB":
if self.node.features().supportsCategory(mscl.MipTypes.CLASS_AHRS_IMU):
self.node.enableDataStream(mscl.MipTypes.CLASS_AHRS_IMU, True)
#if the self.node supports Estimation Filter
if self.node.features().supportsCategory(mscl.MipTypes.CLASS_ESTFILTER):
self.node.enableDataStream(mscl.MipTypes.CLASS_ESTFILTER)
#if the self.node supports GNSS
if self.node.features().supportsCategory(mscl.MipTypes.CLASS_GNSS):
self.node.enableDataStream(mscl.MipTypes.CLASS_GNSS)
# Clean the internal circular buffer. Select timeout to be 500ms
# self.packets = self.node.getDataPackets(0)
packets = self.node.getDataPackets(0)
return self
def __exit__(self, etype, value, tb):
""" Closes the file properly """
if self.save_csv:
self.csv_file.__exit__(etype, value, tb)
if self.communication_protocol == "USB":
self.node.setToIdle()
if not (etype is None):
traceback.print_exception(etype, value, tb)
def get_sagittal_angle(self):
# this is the X-Y plane angle on the device, relative to initial position.
return 180/np.pi*np.arctan2(self.R_prime[1,0],self.R_prime[0,0])
return "%.2f degrees"%(180/np.pi*np.arctan2(self.R_prime[1,0],self.R_prime[0,0]))
return "\n%.2f, %.2f, %.2f\n%.2f, %.2f, %.2f\n%.2f, %.2f, %.2f\n\n"%(
self.R_prime[0,0], self.R_prime[0,1], self.R_prime[0,2],
self.R_prime[1,0], self.R_prime[1,1], self.R_prime[1,2],
self.R_prime[2,0], self.R_prime[2,1], self.R_prime[2,2],
)
def update(self):
t0=time.monotonic()
microstrainData = self.readIMUnode(timeout=0)# 0ms
# print([microstrainDatum.keys() for microstrainDatum in microstrainData ])
for datum in microstrainData:
if 'orientMatrix' in datum.keys():
self.R = datum['orientMatrix']
if self.init_R is None:
self.init_R = np.array(self.R)
self.R_prime = [email protected]_R.T
if 'deltaVelX' in datum.keys():
self.xdd = [email protected]([[datum['deltaVelX'], datum['deltaVelY'], datum['deltaVelZ']]]).T*9.81/self.dt
self.lp_xdd += 0.4*(self.xdd-self.lp_xdd)
self.xd += (self.xdd - self.xd_forget * self.xd + self.acc_bias) * self.dt
if np.linalg.norm(self.xdd - self.lp_xdd)<1e-1:
self.xd*=0
self.acc_bias = -self.lp_xdd
self.x += (self.xd - self.x_forget * self.x)* self.dt
# self.R = self.readIMUnode()['orientMatrix']
# self.R= np.eye(3)
dur = time.monotonic()-t0
if self.save_csv:
self.csv_writer.writerow([time.monotonic()
, self.R[0,0], self.R[0,1], self.R[0,2]
, self.R[1,0], self.R[1,1], self.R[1,2]
, self.R[2,0], self.R[2,1], self.R[2,2]
])
#print(self.R[0,0], self.R[1,1], self.R[2,2])
return 1
def start_cal(self):
self.t0=time.monotonic()
self.xd = np.zeros((3,1))
self.x = np.zeros((3,1))
print('start cal')
def stop_cal(self):
cal_time = time.monotonic()-self.t0
self.acc_bias = -self.xd/cal_time
self.xd = np.zeros((3,1))
self.x = np.zeros((3,1))
self.xd_forget = .01
self.x_forget = .01
print('stop cal', self.acc_bias.T)
def readIMUnode(self, timeout = 0, maxPackets = 0, last_packet_only = False):
# SSProfile("Get IMU Data Packets").tic()
packets = self.node.getDataPackets(timeout, maxPackets)
# SSProfile("Get IMU Data Packets").toc()
# SSProfile("Read Data Packets").tic()
microstrainData = []
if len(packets) > 0 and last_packet_only == True:
packet = packets[-1]
microstrainDatum = dict()
# SSProfile("Loop Through Datapoints").tic()
for dataPoint in packet.data():
# print(dataPoint.channelName())
if dataPoint.storedAs() == 0:
# SSProfile("Datapoint 0").tic()
microstrainDatum[dataPoint.channelName()] = dataPoint.as_float()
# SSProfile("Datapoint 0").toc()
elif dataPoint.storedAs() == 3:
# SSProfile("Datapoint 3").tic()
# print(dir(dataPoint))
# ts = dataPoint.as_Timestamp()
microstrainDatum[dataPoint.channelName()] = None
# SSProfile("Datapoint 3").toc()
elif dataPoint.storedAs() == 1:
# SSProfile("Datapoint 1").tic()
# print(dir(dataPoint))
ts = dataPoint.as_double()
microstrainDatum[dataPoint.channelName()] = ts
# SSProfile("Datapoint 1").toc()
elif dataPoint.storedAs() == 9:
# SSProfile("Datapoint 9").tic()
mat = dataPoint.as_Matrix()
npmat = np.array([[mat.as_floatAt(i,j) for j in range(3)] for i in range(3)])
microstrainDatum[dataPoint.channelName()] = npmat
# SSProfile("Datapoint 9").toc()
else:
print("no solution for datapoint stored as", dataPoint.storedAs(), dataPoint.channelName())
microstrainDatum[dataPoint.channelName()] = None
# SSProfile("Loop Through Datapoints").toc()
microstrainData.append(microstrainDatum)
else:
for packet in packets:
microstrainDatum = dict()
# SSProfile("Loop Through Datapoints").tic()
for dataPoint in packet.data():
# print(dataPoint.channelName())
if dataPoint.storedAs() == 0:
# SSProfile("Datapoint 0").tic()
microstrainDatum[dataPoint.channelName()] = dataPoint.as_float()
# SSProfile("Datapoint 0").toc()
elif dataPoint.storedAs() == 3:
# SSProfile("Datapoint 3").tic()
# print(dir(dataPoint))
# ts = dataPoint.as_Timestamp()
microstrainDatum[dataPoint.channelName()] = None
# SSProfile("Datapoint 3").toc()
elif dataPoint.storedAs() == 1:
# SSProfile("Datapoint 1").tic()
# print(dir(dataPoint))
ts = dataPoint.as_double()
microstrainDatum[dataPoint.channelName()] = ts
# SSProfile("Datapoint 1").toc()
elif dataPoint.storedAs() == 9:
# SSProfile("Datapoint 9").tic()
mat = dataPoint.as_Matrix()
npmat = np.array([[mat.as_floatAt(i,j) for j in range(3)] for i in range(3)])
microstrainDatum[dataPoint.channelName()] = npmat
# SSProfile("Datapoint 9").toc()
else:
print("no solution for datapoint stored as", dataPoint.storedAs(), dataPoint.channelName())
microstrainDatum[dataPoint.channelName()] = None
# SSProfile("Loop Through Datapoints").toc()
microstrainData.append(microstrainDatum)
# SSProfile("Read Data Packets").toc()
return microstrainData
def getTotalPackets(self):
return self.node.totalPackets()
def get_data(self):
init_time = time.perf_counter_ns()
#get the data in first packet from the node, with a timeout of 500 milliseconds
data = self.readIMUnode(timeout = 500)
delta_time = (time.perf_counter_ns() - init_time)*1e-6
self.grav_x = data['grav_x'][0]/9.81
self.grav_y = data['grav_y'][0]/9.81
self.grav_z = data['grav_z'][0]/9.81
return data, delta_time
def get_euler_angles(self):
self.roll_usedef = -np.arccos(np.dot( np.array([0,1,0]) , np.array([self.grav_x,self.grav_y,self.grav_z]) )) + np.pi/2
self.pitch_usedef = np.arctan2(self.grav_y, self.grav_x)
eulerAngles = (self.roll_usedef,self.pitch_usedef,self.yaw_usedef)
# eulerAngles = extractEulerAngles(R_update)
return eulerAngles
def main():
with AhrsManager(csv_file_name="test_ahrs.csv", port="/dev/ttyAhrsShank_L") as am:
cal=False
for i,t in enumerate(SoftRealtimeLoop(dt=1.0/200, report=True)):
am.update()
# if t<.01:
# am.start_cal()
# if t>1.5 and not cal:
# cal=True
# am.stop_cal()
if i%20==0: print(am.x)
# print(am.get_sagittal_angle())
if __name__ == '__main__':
main()