|
39 | 39 | import numpy as np |
40 | 40 | from scipy import signal |
41 | 41 | from pyulog import ULog |
42 | | -from scipy.signal import resample |
| 42 | +from scipy.interpolate import make_interp_spline |
43 | 43 |
|
44 | 44 | def getInputOutputData(logfile, axis, t_start=0.0, t_stop=0.0, instance=0): |
45 | 45 | log = ULog(logfile) |
@@ -85,57 +85,48 @@ def get_delta_mean(data_list): |
85 | 85 | dx = dx/(length-1) |
86 | 86 | return dx |
87 | 87 |
|
| 88 | +def resample_interp(t, u, t_new): |
| 89 | + interp = make_interp_spline(t, u, k=1) |
| 90 | + return interp(t_new) |
| 91 | + |
88 | 92 | def extract_identification_data(log, t_u_data, u_data, t_y_data, y_data, axis, t_v_data, v_data, t_start, t_stop): |
89 | | - status_data = get_data(log, 'autotune_attitude_control_status', 'state') |
90 | | - t_status = us2s(get_data(log, 'autotune_attitude_control_status', 'timestamp')) |
91 | | - |
92 | | - len_y = len(t_y_data) |
93 | | - len_s = len(t_status) |
94 | | - len_v = len(t_v_data) |
95 | | - i_y = 0 |
96 | | - i_s = 0 |
97 | | - i_v = 0 |
98 | | - u_aligned = [] |
99 | | - y_aligned = [] |
100 | | - v_aligned = [] |
101 | | - t_aligned = [] |
102 | | - axis_to_state = [2, 4, 6] # roll, pitch, yaw states |
| 93 | + if t_start == 0.0 and t_stop == 0.0: |
| 94 | + # Find autotune sequence |
| 95 | + status_data = get_data(log, 'autotune_attitude_control_status', 'state') |
| 96 | + t_status = us2s(get_data(log, 'autotune_attitude_control_status', 'timestamp')) |
| 97 | + axis_to_state = [2, 4, 6] # roll, pitch, yaw states |
103 | 98 |
|
104 | | - if t_start == 0.0: |
105 | | - t_start = t_u_data[0] |
| 99 | + status_prev = 0 |
106 | 100 |
|
107 | | - if t_stop == 0.0: |
108 | | - t_stop = t_u_data[-1] |
| 101 | + for i_s in range(len(t_status)): |
| 102 | + if status_data[i_s] == axis_to_state[axis]: |
| 103 | + if status_prev != axis_to_state[axis]: |
| 104 | + t_start = t_status[i_s] |
109 | 105 |
|
110 | | - for i_u in range(len(t_u_data)): |
111 | | - t_u = t_u_data[i_u] |
112 | | - while t_y_data[i_y] <= t_u and i_y < len_y-1: |
113 | | - i_y += 1 |
| 106 | + else: |
| 107 | + if status_prev == axis_to_state[axis]: |
| 108 | + t_stop = t_status[i_s] |
| 109 | + break |
114 | 110 |
|
115 | | - while i_v < len_v-1 and t_v_data[i_v] <= t_u: |
116 | | - i_v += 1 |
| 111 | + status_prev = status_data[i_s] |
117 | 112 |
|
118 | | - if len_s > 0: |
119 | | - while t_status[i_s] <= t_u and i_s < len_s-1: |
120 | | - i_s += 1 |
| 113 | + if t_start == 0.0: |
| 114 | + t_start = t_u_data[0] |
121 | 115 |
|
122 | | - status_aligned = status_data[i_s-1] |
| 116 | + if t_stop == 0.0: |
| 117 | + t_stop = t_u_data[-1] |
123 | 118 |
|
124 | | - if status_aligned == axis_to_state[axis] and t_u >= t_start and t_u <= t_stop: |
125 | | - u_aligned.append(u_data[i_u]) |
126 | | - y_aligned.append(y_data[i_y-1]) |
127 | | - t_aligned.append(t_u) |
| 119 | + dt = get_delta_mean(t_y_data) |
| 120 | + t_aligned = np.arange(t_start, t_stop, dt) |
128 | 121 |
|
129 | | - if i_v > 0: |
130 | | - v_aligned.append(v_data[i_v-1]) |
| 122 | + # Resample series to the common index |
| 123 | + u_aligned = resample_interp(t_u_data, u_data, t_aligned) |
| 124 | + y_aligned = resample_interp(t_y_data, y_data, t_aligned) |
131 | 125 |
|
132 | | - elif t_u >= t_start and t_u <= t_stop: |
133 | | - u_aligned.append(u_data[i_u]) |
134 | | - y_aligned.append(y_data[i_y-1]) |
135 | | - t_aligned.append(t_u) |
| 126 | + v_aligned = [] |
136 | 127 |
|
137 | | - if i_v > 0: |
138 | | - v_aligned.append(v_data[i_v-1]) |
| 128 | + if len(v_data) > 0: |
| 129 | + v_aligned = resample_interp(t_v_data, v_data, t_aligned) |
139 | 130 |
|
140 | 131 | return (t_aligned, u_aligned, y_aligned, v_aligned) |
141 | 132 |
|
|
0 commit comments