-
Notifications
You must be signed in to change notification settings - Fork 0
/
brett_sysid_yaw_vicon.m
180 lines (137 loc) · 6.23 KB
/
brett_sysid_yaw_vicon.m
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
%% load bag files: one bag for estimating the TF, one for validating that TF
clear;
close all
clc;
path(path,'../read_bags');
path(path,'../helper_functions');
bag_name1 = '2018-07-06-14-59-56.bag';
bag_name2 = '2018-07-06-15-00-38.bag';
bag1 = ros.Bag(bag_name1);
bag2 = ros.Bag(bag_name2);
%% read topics
attitude_cmd1 = readAttitudeTarget(bag1, '/mavros/setpoint_raw/target_attitude');
attitude_cmd2 = readAttitudeTarget(bag2, '/mavros/setpoint_raw/target_attitude');
attitude_cmd1.rpy = quat2rpy([attitude_cmd1.q(4,:)', attitude_cmd1.q(1:3,:)']');
attitude_cmd2.rpy = quat2rpy([attitude_cmd2.q(4,:)', attitude_cmd2.q(1:3,:)']');
vicon_odometry1 = readOdometry(bag1, '/brett2/vrpn_client/estimated_odometry');
vicon_odometry2 = readOdometry(bag2, '/brett2/vrpn_client/estimated_odometry');
vicon_odometry1.rpy = quat2rpy([vicon_odometry1.q(4,:)', vicon_odometry1.q(1:3,:)']');
vicon_odometry2.rpy = quat2rpy([vicon_odometry2.q(4,:)', vicon_odometry2.q(1:3,:)']');
vicon_odometry1.t = vicon_odometry1.t - vicon_odometry1.t(1);
vicon_odometry2.t = vicon_odometry2.t - vicon_odometry2.t(1);
attitude_cmd1.t = attitude_cmd1.t - attitude_cmd1.t(1);
attitude_cmd2.t = attitude_cmd2.t - attitude_cmd2.t(1);
%% sysid begins
attitude_cmd1.y_interp = zeros(length(vicon_odometry1.rpy));
attitude_cmd2.y_interp = zeros(length(vicon_odometry2.rpy));
%interp yaw
attitude_cmd1.y_interp = interp1(attitude_cmd1.t, attitude_cmd1.rpy(3,:), vicon_odometry1.t, 'spline');
attitude_cmd2.y_interp = interp1(attitude_cmd2.t, attitude_cmd2.rpy(3,:), vicon_odometry2.t, 'spline');
attitude_cmd1.t = vicon_odometry1.t;
attitude_cmd2.t = vicon_odometry2.t;
%get rid of first and last x seconds (to remove ground and transient effects)
t0 = 10;
t1 = 0;
vicon_odometry1.t_clip = vicon_odometry1.t(vicon_odometry1.t > t0 & vicon_odometry1.t < vicon_odometry1.t(end)-t1);
vicon_odometry2.t_clip = vicon_odometry2.t(vicon_odometry2.t > t0 & vicon_odometry2.t < vicon_odometry2.t(end)-t1);
vicon_odometry1.rpy = vicon_odometry1.rpy(:, vicon_odometry1.t > t0 & vicon_odometry1.t < vicon_odometry1.t(end) - t1);
vicon_odometry2.rpy = vicon_odometry2.rpy(:, vicon_odometry2.t > t0 & vicon_odometry2.t < vicon_odometry2.t(end) - t1);
attitude_cmd1.t_clip = attitude_cmd1.t(attitude_cmd1.t > t0 & attitude_cmd1.t < attitude_cmd1.t(end) - t1);
attitude_cmd2.t_clip = attitude_cmd2.t(attitude_cmd2.t > t0 & attitude_cmd2.t < attitude_cmd2.t(end) - t1);
attitude_cmd1.y_interp = attitude_cmd1.y_interp(:, attitude_cmd1.t > t0 & attitude_cmd1.t < attitude_cmd1.t(end) - t1);
attitude_cmd2.y_interp = attitude_cmd2.y_interp(:, attitude_cmd2.t > t0 & attitude_cmd2.t < attitude_cmd2.t(end) - t1);
%% ID of yaw system
np = 2; %1 for 1st order system, 2 for 2nd order system
%experiment 1
Experiment1.u1 = attitude_cmd1.y_interp;
Experiment1.y1 = vicon_odometry1.rpy(3,:);
dt1 = mean(diff(vicon_odometry1.t_clip));
yaw_data1 = iddata(Experiment1.y1',Experiment1.u1',dt1, ...
'ExperimentName', 'SysID_1', 'InputName','yaw_{cmd}', ...
'OutputName','yaw', 'InputUnit','rad', 'OutputUnit','rad', ...
'TimeUnit','Second');
yaw_data1 = detrend(yaw_data1);
%experiment 2
Experiment2.u1 = attitude_cmd2.y_interp;
Experiment2.y1 = vicon_odometry2.rpy(3,:);
dt2 = mean(diff(vicon_odometry2.t_clip));
yaw_data2 = iddata(Experiment2.y1',Experiment2.u1',dt2,...
'ExperimentName', 'SysID_2', 'InputName','yaw_{cmd}',...
'OutputName','yaw', 'InputUnit','rad', 'OutputUnit','rad',...
'TimeUnit','Second');
yaw_data2 = detrend(yaw_data2);
% *At this point we have 3 options!*
%
% # Estimate a model from both experiments - but cannot validate it on independent dataset
% # Estimate a model from Exp1 and validate it on data from Exp2
% # Estimate a model from Exp2 and validate it on data from Exp1
%For now we choose the best model from options 2 and 3
%Assume 1st order system
%np = 2;
nz = 0;
%Generate model using Experiment1 and validate the model with Experiment2
yaw_estimated_tf1 = tfest(yaw_data1,np, nz);
[~, fit1, ~] = compare(yaw_data2, yaw_estimated_tf1);
%Generate model using Experiment2 and validate the model with Experiment1
yaw_estimated_tf2 = tfest(yaw_data2,np, nz);
[~, fit2, ~] = compare(yaw_data1, yaw_estimated_tf2);
if fit1>fit2
%We pick the first Identification
yaw_estimated_tf = yaw_estimated_tf1;
disp('The yaw is estimated using experiment 1 and validated on data from experiment 2');
figure;
compare(yaw_data2, yaw_estimated_tf1);
disp(strcat('The yaw model fits the validation data with **',...
num2str(fit1), '** %'));
else
%We pick the second Identification
yaw_estimated_tf = yaw_estimated_tf2;
disp('The yaw model is estimated using experiment 2 and validated on data from experiment 1');
figure;
compare(yaw_data1, yaw_estimated_tf2);
disp(strcat('The yaw model fits the validation data with **',...
num2str(fit2), '** %'));
end
%%% Estimated Transfer Functions
disp('yawrate estimated transfer function is: ');
tf(yaw_estimated_tf)
yawrate_params=getpvec(yaw_estimated_tf);
if(np==1)
yawrate_gain=yawrate_params(1)/yawrate_params(2);
yawrate_tau=1/yawrate_params(2);
fprintf('yawrate gain=%.3f, tau=%.3f\n',yawrate_gain,yawrate_tau);
elseif(np==2)
yawrate_omega=sqrt(yawrate_params(3));
yawrate_gain=yawrate_params(1)/yawrate_params(3);
yawrate_damping=yawrate_params(2)/(2*yawrate_omega);
fprintf('yawrate omega=%.3f, gain=%.3f damping=%.3f\n',yawrate_omega,yawrate_gain,yawrate_damping);
end
figure('Name','System analysis (yawrate)');
subplot(311);
bode(yaw_estimated_tf); grid;
title('yawrate bode plot');
subplot(312);
rlocusplot(yaw_estimated_tf); grid;
title('yawrate RootLucas plot');
subplot(313);
step(yaw_estimated_tf); grid;
title('yawrate step response plot');
%% plot
% *Plot attitude*
figure();
title('Experiment 1 and 2 Data');
subplot(2,1,1);
plot(vicon_odometry1.t_clip, vicon_odometry1.rpy(3,:)*180/pi, ...
attitude_cmd1.t_clip, attitude_cmd1.y_interp*180/pi, ...
'g--', 'linewidth', 2);
xlabel('time');
legend('y','y_{ref}');
ylabel('yaw [deg]');
title('yaw from IMU, exp 1');
subplot(2,1,2);
plot(vicon_odometry2.t_clip, vicon_odometry2.rpy(3,:)*180/pi, ...
attitude_cmd2.t_clip, attitude_cmd2.y_interp*180/pi, ...
'g--', 'linewidth', 2);
xlabel('time');
ylabel('yaw [deg]');
title('yaw from IMU, exp 2');