|
1 | | -/* |
2 | | - This sketch shows to get the axis projections from the orientation estimate. It also shows how to project a vector in the global or local reference frame. |
3 | | - */ |
4 | | - |
5 | | -#include <imuFilter.h> |
6 | | -#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050 |
7 | | - |
8 | | -// Sensor fusion |
9 | | -constexpr float GAIN = 0.1; // Fusion gain, value between 0 and 1 - Determines response of heading correction with respect to gravity. |
10 | | -imuFilter <&GAIN> fusion; |
11 | | - |
12 | | -// Imu sensor |
13 | | -basicMPU6050<> imu; |
14 | | - |
15 | | -// Display functions: |
16 | | -void printVector( vec3_t r ) { |
17 | | - Serial.print( r.x, 2 ); |
18 | | - Serial.print( "," ); |
19 | | - Serial.print( r.y, 2 ); |
20 | | - Serial.print( "," ); |
21 | | - Serial.print( r.z, 2 ); |
22 | | -} |
23 | | - |
24 | | -void printQuat( quat_t q ) { |
25 | | - Serial.print( q.w ); |
26 | | - Serial.print( "," ); |
27 | | - printVector( q.v ); |
28 | | -} |
29 | | - |
30 | | -void setup() { |
31 | | - // Initialize filter: |
32 | | - fusion.setup( imu.ax(), imu.ay(), imu.az() ); |
33 | | - |
34 | | - // Calibrate imu |
35 | | - imu.setup(); |
36 | | - imu.setBias(); |
37 | | - |
38 | | - Serial.begin(38400); |
39 | | -} |
40 | | - |
41 | | -void loop() { |
42 | | - // Update filter: |
43 | | - fusion.update( imu.gx(), imu.gy(), imu.gz(), imu.ax(), imu.ay(), imu.az() ); |
44 | | - |
45 | | - // Unit vectors of rectangular coodinates [Choose between GLOBAL_FRAME and LOCAL_FRAME] |
46 | | - vec3_t x = fusion.getXaxis(GLOBAL_FRAME); |
47 | | - vec3_t y = fusion.getYaxis(GLOBAL_FRAME); |
48 | | - vec3_t z = fusion.getZaxis(GLOBAL_FRAME); |
49 | | - |
50 | | - const vec3_t VEC = {1, 1, 0}; |
51 | | - vec3_t v = fusion.projectVector(VEC, GLOBAL_FRAME); |
52 | | - |
53 | | - // Display vectors: |
54 | | - Serial.print( " x = " ); |
55 | | - printVector( x ); |
56 | | - Serial.print( " | y = " ); |
57 | | - printVector( y ); |
58 | | - Serial.print( " | z = " ); |
59 | | - printVector( z ); |
60 | | - Serial.print( " | v = " ); |
61 | | - printVector( v ); |
62 | | - |
63 | | - // Display quaternion |
64 | | - Serial.print( " | q = " ); |
65 | | - printQuat( fusion.getQuat() ); |
66 | | - Serial.println(); |
67 | | -} |
| 1 | +/* |
| 2 | + This sketch shows to get the axis projections from the orientation estimate. It also shows how to project a vector in the global or local reference frame. |
| 3 | + */ |
| 4 | + |
| 5 | +#include <imuFilter.h> |
| 6 | +#include <basicMPU6050.h> // Library for IMU sensor. See this link: https://github.com/RCmags/basicMPU6050 |
| 7 | + |
| 8 | +// Sensor fusion |
| 9 | +constexpr float GAIN = 0.1; // Fusion gain, value between 0 and 1 - Determines response of heading correction with respect to gravity. |
| 10 | +imuFilter <&GAIN> fusion; |
| 11 | + |
| 12 | +// Imu sensor |
| 13 | +basicMPU6050<> imu; |
| 14 | + |
| 15 | +// Display functions: |
| 16 | +void printVector( float r[] ) { |
| 17 | + Serial.print( r[0], 2 ); |
| 18 | + Serial.print( "," ); |
| 19 | + Serial.print( r[1], 2 ); |
| 20 | + Serial.print( "," ); |
| 21 | + Serial.print( r[2], 2 ); |
| 22 | +} |
| 23 | + |
| 24 | +void printQuat( float q[] ) { |
| 25 | + Serial.print( q[0] ); |
| 26 | + Serial.print( "," ); |
| 27 | + printVector( q + 1 ); |
| 28 | +} |
| 29 | + |
| 30 | +void setup() { |
| 31 | + // Initialize filter: |
| 32 | + fusion.setup( imu.ax(), imu.ay(), imu.az() ); |
| 33 | + |
| 34 | + // Calibrate imu |
| 35 | + imu.setup(); |
| 36 | + imu.setBias(); |
| 37 | + |
| 38 | + Serial.begin(38400); |
| 39 | +} |
| 40 | + |
| 41 | +void loop() { |
| 42 | + // Update filter: |
| 43 | + fusion.update( imu.gx(), imu.gy(), imu.gz(), imu.ax(), imu.ay(), imu.az() ); |
| 44 | + |
| 45 | + float v[3] = { 1, 1, 0 }; |
| 46 | + float x[3], y[3], z[3]; |
| 47 | + |
| 48 | + // Unit vectors of rectangular coodinates |
| 49 | + #define TO_WORLD false |
| 50 | + // Project local axis to global reference frame = true |
| 51 | + // Project global axis to local reference frame = false |
| 52 | + |
| 53 | + fusion.getXaxis( TO_WORLD, x ); |
| 54 | + fusion.getYaxis( TO_WORLD, y ); |
| 55 | + fusion.getZaxis( TO_WORLD, z ); |
| 56 | + fusion.projectVector( TO_WORLD, v ); |
| 57 | + |
| 58 | + // Display vectors: |
| 59 | + Serial.print( " x = " ); |
| 60 | + printVector( x ); |
| 61 | + Serial.print( " | y = " ); |
| 62 | + printVector( y ); |
| 63 | + Serial.print( " | z = " ); |
| 64 | + printVector( z ); |
| 65 | + Serial.print( " | v = " ); |
| 66 | + printVector( v ); |
| 67 | + |
| 68 | + // Display quaternion |
| 69 | + float q[4]; |
| 70 | + fusion.getQuat(q); |
| 71 | + |
| 72 | + Serial.print( " | q = " ); |
| 73 | + printQuat( q ); |
| 74 | + Serial.println(); |
| 75 | +} |
0 commit comments