summaryrefslogtreecommitdiff
path: root/src/body.cpp
blob: caa7cb90ea2a7bc25f4a94098bad5fb6fce7af8e (plain)
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
/*
 * =====================================================================================
 *
 *       Filename:  body.cpp
 *
 *    Description:  Method definitions for body class.
 *
 *        Version:  1.0
 *        Created:  03/17/2017 08:07:35 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Martin Miller (MHM), miller7@illinois.edu
 *   Organization:  Aerospace Robotics and Controls Lab (ARC)
 *
 * =====================================================================================
 */
#include "body.h"

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: update
 * Description:  Increments the state by dx after a Kalman update.
 *--------------------------------------------------------------------------------------
 */
void
Body::update ( const Matrix<double,9,1> &dx )
{
    X += dx;
    return ;
}		/* -----  end of method Body::update  ----- */

void
Body::vel ( const Matrix<double,3,1> &v )
{
    X.segment(3,3) = v;
    return ;
}		/* -----  end of method Body::vel  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: S
 * Description:  Returns the matrix S=HPH.T+R. HPH.T is trivial, so Body::H() is
 * not called explicitly.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,1,1>
Body::S ( const Matrix<double,9,9> &P )
{
    Matrix<double,1,1> S;
    S << P(2,2);
    return S+R();
}		/* -----  end of method Body::S  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: enu
 * Description:  Stores the position input as ENU. Internally stored as NED.
 *--------------------------------------------------------------------------------------
 */
void
Body::enu ( const UTM &utm )
{
    utm_c = utm.zone_c;
    utm_i = utm.zone_i;
    X[0] = utm.northing;
    X[1] = utm.easting;
    X[2] = -utm.up;
    return ;
}		/* -----  end of method Body::enu  ----- */


/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: R
 * Description:  Returns R matrix of measurement noise for height measurement.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,1,1> 
Body::R()
{
    Matrix<double,1,1> R;
    R << 1e-3;
    return R;
}

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: Q
 * Description:  Returns Q matrix of process noise for the body.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,9,9>
Body::Q (double dt)
{
    Matrix<double,9,9> Q;
    Q = Matrix<double,9,9>::Zero();
    Q.block<3,3>(0,0) = 0.25*dt*dt*dt*dt*Matrix<double,3,3>::Identity();
    Q.block<3,3>(3,0) = 0.5*dt*dt*dt*Matrix<double,3,3>::Identity();
    Q.block<3,3>(0,3) = 0.5*dt*dt*dt*Matrix<double,3,3>::Identity();
    Q.block<3,3>(3,3) = dt*dt*Matrix<double,3,3>::Identity();
    Q *= 800e-5;
    Q.block<3,3>(6,6) = dt*dt*5e-6*Matrix<double,3,3>::Identity();
    return Q;
}		/* -----  end of method Body::q  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: H
 * Description:  Returns the Jacobian of the measurement model, H.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,1,9>
Body::H ( )
{
    Matrix<double,1,9> H;
    H = Matrix<double,1,9>::Zero();
    H[0,2] = 1;
    return H ;
}		/* -----  end of method Body::H  ----- */


/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: h
 * Description:  Returns the predicted measurement vector, h.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,1,1>
Body::h ( )
{
    Matrix<double,1,1> h;
    h[0] = X[2];
    return h;
}		/* -----  end of method Body::h_hat  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: F
 * Description:  Returns the Jacobian of the motion model of the body.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,9,9>
Body::F ( const Vector3d &ang, const Quaterniond &q, double dt )
{
    Matrix<double,9,9> F = Matrix<double,9,9>::Zero();
    Matrix<double,3,3> Rbw(q.toRotationMatrix());
    Matrix<double,3,3> W;
    W = skewSymmetric(ang);
    F.block<3,3>(0,3) = Rbw;
    F.block<3,3>(3,3) = -W;
    F.block<3,3>(3,6) = -Matrix<double,3,3>::Identity();
    F *= dt;
    F += Matrix<double,9,9>::Identity();
    return F;
}		/* -----  end of method Body::F  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: motionModel
 * Description:  Propagates the motion model of the body into vector X.
 *--------------------------------------------------------------------------------------
 */
void
Body::motionModel ( const Vector3d &acc, const Vector3d &ang, const Quaterniond &q, const double dt)
{
    Vector3d bias(X.segment(6,3));
    Vector3d gravity_world(0.,0.,9.80655);

    Matrix<double,6,3> A;
    Matrix<double,6,1> b;
    A = Matrix<double,6,3>::Zero();
    b = Matrix<double,6,1>::Zero();

    Matrix<double,3,3> Rbw(q.toRotationMatrix());
    A.block<3,3>(0,0) = Rbw;
    Matrix<double,3,3> W;
    W = skewSymmetric(ang);
    A.block<3,3>(3,0) = -W;
    b.segment(3,3) = acc-bias+Rbw.transpose()*gravity_world;

    X.segment(0,6) += (A*X.segment(3,3)+b)*dt;
    return ;
}		/* -----  end of method Body::motionModel  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: skewSymmetric
 * Description:  Create the skew symmetric matrix y from the vector x.
 *--------------------------------------------------------------------------------------
 */
Matrix<double,3,3>
Body::skewSymmetric ( const Vector3d &x )
{
    Matrix<double,3,3> y;
    y << 0.,-x[2], x[1], x[2],0.,-x[0],-x[1],x[0],0.;
    return y;
}		/* -----  end of method Body::skewSymmetric  ----- */

void
Body::unicsv ( )
{
    printf("%d,%c,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", utm_i, utm_c,
            X[0], X[1], -X[2],
            X[3], X[4], X[5],
            X[6], X[7], X[8]);
    return ;
}		/* -----  end of method Body::unicsv  ----- */

    void
Body::accelerometer_bias ( const Vector3d &b )
{
    X.segment(6,3) = b;
    return ;
}		/* -----  end of method State::accelerometer_bias  ----- */

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Body
 *      Method:  Body :: ned
 * Description:  Returns the body position in NED coordinates.
 *--------------------------------------------------------------------------------------
 */
Vector3d
Body::ned (  )
{
    return X.segment(0,3) ;
}		/* -----  end of method Body::ned  ----- */