-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyMagModel.m
193 lines (154 loc) · 8.78 KB
/
polyMagModel.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
classdef polyMagModel
% The class for handling polynomial model fitting and updates
properties
A
pinvA
B
J1J2
end
properties (Access = private)
points
theta
dim
order
dpsi
db_dpsi
phi
pinvphi
end
methods (Access = public)
function obj = polyMagModel(order)
obj.points = polyMagModel.get_points(order);
obj.dim = order^2 + 4*order+3;
obj.A = [];
% points
for i = 1 : size(obj.points, 1)
obj.A = [obj.A; polyMagModel.get_ABnull(obj.points(i, :), order)];
end
obj.pinvA = pinv(obj.A);
obj.order = order;
% choose db_dpsi function based on the order
if order == 1
obj.db_dpsi = @dbtheta_dpsi1;
elseif order == 2
obj.db_dpsi = @dbtheta_dpsi2;
elseif order == 3
obj.db_dpsi = @dbtheta_dpsi3;
end
obj.theta = zeros(obj.dim, 1);
end
function obj = set_theta(obj, theta)
obj.theta = theta;
end
function obj = init_theta(obj, mag)
[obj.theta, ~] = obj.LS_coeff(mag);
end
function theta = get_theta(obj)
theta = obj.theta;
end
function obj = update_theta(obj, dpsi)
% keep record of dpsi
obj.dpsi = dpsi;
% calculate B
obj.B = obj.getB(dpsi);
% calculate d(B*theta)/dpsi
obj.J1J2 = zeros(3 * size(obj.points, 1), 6);
for i = 1 : size(obj.points, 1)
obj.J1J2(3 * (i - 1) + 1 : 3 * i, :) = obj.db_dpsi(dpsi, obj.points(i, :)', obj.theta);
end
% update theta
obj.theta = obj.pinvA * obj.B * obj.theta;
end
function obj = set_phi(obj, sensor_pos)
obj.phi = [];
for i = 1 : size(sensor_pos, 2)
obj.phi = [obj.phi; polyMagModel.get_ABnull(sensor_pos(:, i), obj.order)];
end
obj.pinvphi = pinv(obj.phi);
end
function [coeff, var_res] = LS_coeff(obj, mag)
% calculate LS estimate of theta based on magnetic field data
% Input
% mag: magnetic field data --- (dim of the magnetic field data) x 1 vector
% Output
% coeff: estimated theta --- (dim of theta) x 1 vector
% var_res: fitting residuals --- (dim of the magnetic field data) x 1 vector
coeff = obj.pinvphi*mag;
var_res = mean((mag - obj.phi * coeff).^2);
end
end
methods (Access = private)
function B = getB(obj, dpsi)
% dpsi: 3 x 1
dp = dpsi(1:3);
dphi = dpsi(4:6);
R=eye(3)+vect2skew(dphi); % small angle approximation
B=zeros(3*size(obj.points,1), obj.dim);
for ii=1:size(obj.points, 1)
r=R*obj.points(ii,:)'+dp;
B(3*(ii-1)+1:3*ii,:) = R.'*polyMagModel.get_ABnull(r,obj.order);
end
end
end
methods (Static)
function points = get_points(order)
% Pre-defined point sets for determine theta's dynamics
% Input
% order: the order of the polynomial model --- scalar
% Output
% points: the selected points where the magnetic field is expressed --- ceil(kappa/3) x 3 vector (row)
switch order
case 1
points = [0 0 1;...
0 1 0;...
1 0 0;...
];
case 2
points = [0 0 1;...
0 1 0;...
1 0 0;...
1 1 1;...
0 0 0];
case 3
points = [0 0 1;...
0 1 0;...
1 0 0;...
1 1 1;...
0 0 0;...
1 1 0;...
1 0 1;...
-1 0 1];
otherwise
disp('Not supported!')
end
end
function ABnull=get_ABnull(r, order)
% calculate the basis for representing magnetic field
% r: location --- 1 x 3 or 3 x 1 vector
% order: the order of the polynomial model --- scalar
rx=r(1);
ry=r(2);
rz=r(3);
switch order
case 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x, 0, -2*rz, ry^2 - rz^2, -6*ry*rz, rx*ry, -2*rx*rz, rx^2 - rz^2, -2*ry*rz, -6*rx*rz, ry^3 - 3*ry*rz^2, 4*rz^3 - 12*ry^2*rz, rx*ry^2 - rx*rz^2, -6*rx*ry*rz, ry*rx^2 - ry*rz^2, - 2*rx^2*rz - 2*ry^2*rz + (4*rz^3)/3, rx^3 - 3*rx*rz^2, -6*rx*ry*rz, 4*rz^3 - 12*rx^2*rz, ry^4 - 6*ry^2*rz^2 + rz^4, - 20*ry^3*rz + 20*ry*rz^3, rx*ry^3 - 3*rx*ry*rz^2, - 12*rx*ry^2*rz + 4*rx*rz^3, rx^2*ry^2 - rx^2*rz^2 - ry^2*rz^2 + rz^4/3, - 6*rx^2*ry*rz - 2*ry^3*rz + 4*ry*rz^3, ry*rx^3 - 3*ry*rx*rz^2, - 2*rx^3*rz - 6*rx*ry^2*rz + 4*rx*rz^3, rx^4 - 6*rx^2*rz^2 + rz^4, - 12*ry*rx^2*rz + 4*ry*rz^3, - 20*rx^3*rz + 20*rx*rz^3];
otherwise
error('Only model orders 1-4 supported');
end
end
end
end