forked from DavidEGrayson/minimu9-ahrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimu9-ahrs.cpp
302 lines (262 loc) · 9.24 KB
/
minimu9-ahrs.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "vector.h"
#include "MinIMU9.h"
#include "version.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <system_error>
#include <boost/program_options.hpp>
namespace opts = boost::program_options;
// TODO: print warning if accelerometer magnitude is not close to 1 when starting up
// An Euler angle could take 8 chars: -234.678, but usually we only need 6.
float field_width = 6;
#define FLOAT_FORMAT std::fixed << std::setprecision(3) << std::setw(field_width)
std::ostream & operator << (std::ostream & os, const vector & vector)
{
return os << FLOAT_FORMAT << vector(0) << ' '
<< FLOAT_FORMAT << vector(1) << ' '
<< FLOAT_FORMAT << vector(2);
}
std::ostream & operator << (std::ostream & os, const matrix & matrix)
{
return os << (vector)matrix.row(0) << ' '
<< (vector)matrix.row(1) << ' '
<< (vector)matrix.row(2);
}
std::ostream & operator << (std::ostream & os, const quaternion & quat)
{
return os << FLOAT_FORMAT << quat.w() << ' '
<< FLOAT_FORMAT << quat.x() << ' '
<< FLOAT_FORMAT << quat.y() << ' '
<< FLOAT_FORMAT << quat.z();
}
typedef void rotation_output_function(quaternion& rotation);
void output_quaternion(quaternion & rotation)
{
std::cout << rotation;
}
void output_matrix(quaternion & rotation)
{
std::cout << rotation.toRotationMatrix();
}
void output_euler(quaternion & rotation)
{
std::cout << (vector)(rotation.toRotationMatrix().eulerAngles(2, 1, 0) * (180 / M_PI));
}
int millis()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec) * 1000 + (tv.tv_usec)/1000;
}
void streamRawValues(IMU& imu)
{
imu.enable();
while(1)
{
int t = millis();
imu.read();
printf("%10i %7d %7d %7d %7d %7d %7d %7d %7d %7d\n",
t, imu.raw_m[0], imu.raw_m[1], imu.raw_m[2],
imu.raw_a[0], imu.raw_a[1], imu.raw_a[2],
imu.raw_g[0], imu.raw_g[1], imu.raw_g[2]
);
usleep(20*1000);
}
}
//! Uses the acceleration and magnetic field readings from the compass
// to get a noisy estimate of the current rotation matrix.
// This function is where we define the coordinate system we are using
// for the ground coords: North, East, Down.
matrix rotationFromCompass(const vector& acceleration, const vector& magnetic_field)
{
vector down = -acceleration; // usually true
vector east = down.cross(magnetic_field); // actually it's magnetic east
vector north = east.cross(down);
east.normalize();
north.normalize();
down.normalize();
matrix r;
r.row(0) = north;
r.row(1) = east;
r.row(2) = down;
return r;
}
typedef void fuse_function(quaternion& rotation, float dt, const vector& angular_velocity,
const vector& acceleration, const vector& magnetic_field);
void fuse_compass_only(quaternion& rotation, float dt, const vector& angular_velocity,
const vector& acceleration, const vector& magnetic_field)
{
// Implicit conversion of rotation matrix to quaternion.
rotation = rotationFromCompass(acceleration, magnetic_field);
}
// Uses the given angular velocity and time interval to calculate
// a rotation and applies that rotation to the given quaternion.
// w is angular velocity in radians per second.
// dt is the time.
void rotate(quaternion& rotation, const vector& w, float dt)
{
// Multiply by first order approximation of the
// quaternion representing this rotation.
rotation *= quaternion(1, w(0)*dt/2, w(1)*dt/2, w(2)*dt/2);
rotation.normalize();
}
void fuse_gyro_only(quaternion& rotation, float dt, const vector& angular_velocity,
const vector& acceleration, const vector& magnetic_field)
{
rotate(rotation, angular_velocity, dt);
}
void fuse_default(quaternion& rotation, float dt, const vector& angular_velocity,
const vector& acceleration, const vector& magnetic_field)
{
vector correction = vector(0, 0, 0);
if (abs(acceleration.norm() - 1) <= 0.3)
{
// The magnetidude of acceleration is close to 1 g, so
// it might be pointing up and we can do drift correction.
const float correction_strength = 1;
matrix rotationCompass = rotationFromCompass(acceleration, magnetic_field);
matrix rotationMatrix = rotation.toRotationMatrix();
correction = (
rotationCompass.row(0).cross(rotationMatrix.row(0)) +
rotationCompass.row(1).cross(rotationMatrix.row(1)) +
rotationCompass.row(2).cross(rotationMatrix.row(2))
) * correction_strength;
}
rotate(rotation, angular_velocity + correction, dt);
}
void ahrs(IMU & imu, fuse_function * fuse, rotation_output_function * output)
{
imu.loadCalibration();
imu.enable();
imu.measureOffsets();
// The quaternion that can convert a vector in body coordinates
// to ground coordinates when it its changed to a matrix.
quaternion rotation = quaternion::Identity();
int start = millis(); // truncate 64-bit return value
while(1)
{
int last_start = start;
start = millis();
float dt = (start-last_start)/1000.0;
if (dt < 0){ throw std::runtime_error("Time went backwards."); }
vector angular_velocity = imu.readGyro();
vector acceleration = imu.readAcc();
vector magnetic_field = imu.readMag();
fuse(rotation, dt, angular_velocity, acceleration, magnetic_field);
std::cout << start << " ";
output(rotation);
std::cout << " " << acceleration << " ";
std::cout << angular_velocity << " ";
std::cout << magnetic_field << std::endl << std::flush;
// Ensure that each iteration of the loop takes at least 20 ms.
while(millis() - start < 20)
{
usleep(1000);
}
}
}
int main(int argc, char *argv[])
{
try
{
// Define what all the command-line parameters are.
std::string mode, output_mode, i2cDevice;
opts::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("version,v", "print version number")
("i2c-bus,b", opts::value<std::string>(&i2cDevice)->default_value("/dev/i2c-0"),
"i2c-bus the IMU is connected to")
("mode", opts::value<std::string>(&mode)->default_value("normal"),
"specifies what algorithm to use.\n"
"normal: Fuse compass and gyro.\n"
"gyro-only: Use only gyro (drifts).\n"
"compass-only: Use only compass (noisy).\n"
"raw: Just print raw values from sensors.")
("output", opts::value<std::string>(&output_mode)->default_value("matrix"),
"specifies how to output the orientation.\n"
"matrix: Direction Cosine Matrix.\n"
"quaternion: Quaternion.\n"
"euler: Euler angles (yaw, pitch, roll).\n")
;
opts::variables_map options;
opts::store(opts::command_line_parser(argc, argv).options(desc).run(), options);
opts::notify(options);
if(options.count("help"))
{
std::cout << desc << std::endl;
std::cout << "For more information, run: man minimu9-ahrs" << std::endl;
return 0;
}
if (options.count("version"))
{
std::cout << VERSION << std::endl;
return 0;
}
MinIMU9 imu(i2cDevice.c_str());
rotation_output_function * output;
// Figure out the output mode.
if (output_mode == "matrix")
{
output = &output_matrix;
}
else if (output_mode == "quaternion")
{
output = &output_quaternion;
}
else if (output_mode == "euler")
{
field_width += 2; // See comment above for field_width.
output = &output_euler;
}
else
{
std::cerr << "Unknown output mode '" << output_mode << "'" << std::endl;
return 1;
}
// Figure out the basic operating mode and start running.
if (mode == "raw")
{
streamRawValues(imu);
}
else if (mode == "gyro-only")
{
ahrs(imu, &fuse_gyro_only, output);
}
else if (mode == "compass-only")
{
ahrs(imu, &fuse_compass_only, output);
}
else if (mode == "normal")
{
ahrs(imu, &fuse_default, output);
}
else
{
std::cerr << "Unknown mode '" << mode << "'" << std::endl;
return 1;
}
return 0;
}
catch(const std::system_error & error)
{
std::string what = error.what();
const std::error_code & code = error.code();
std::cerr << "Error: " << what << " " << code.message() << " (" << code << ")" << std::endl;
return 2;
}
catch(const opts::multiple_occurrences & error)
{
std::cerr << "Error: " << error.what() << " of " << error.get_option_name() << " option." << std::endl;
return 1;
}
catch(const std::exception & error)
{
std::cerr << "Error: " << error.what() << std::endl;
return 9;
}
}