Skip to content

Commit 802d542

Browse files
committed
imu example NED orientation
1 parent f720336 commit 802d542

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

examples/imu/imu.ino

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "motion_fx.h"
2+
#include "LSM6DSOSensor.h"
3+
4+
#define ALGO_FREQ 100U /* Algorithm frequency 100Hz */
5+
#define ALGO_PERIOD (1000U / ALGO_FREQ) /* Algorithm period [ms] */
6+
#define MOTION_FX_ENGINE_DELTATIME 0.01f
7+
#define FROM_MG_TO_G 0.001f
8+
#define FROM_G_TO_MG 1000.0f
9+
#define FROM_MDPS_TO_DPS 0.001f
10+
#define FROM_DPS_TO_MDPS 1000.0f
11+
12+
#define STATE_SIZE (size_t)(2432)
13+
14+
#define SAMPLETODISCARD 15
15+
16+
#define GBIAS_ACC_TH_SC (2.0f*0.000765f)
17+
#define GBIAS_GYRO_TH_SC (2.0f*0.002f)
18+
19+
#define DECIMATION 1U
20+
21+
/* Private variables ---------------------------------------------------------*/
22+
23+
static MFX_knobs_t iKnobs;
24+
static MFX_knobs_t *ipKnobs = &iKnobs;
25+
static uint8_t mfxstate[STATE_SIZE];
26+
27+
static volatile int sampleToDiscard = SAMPLETODISCARD;
28+
static int discardedCount = 0;
29+
30+
char LibVersion[35];
31+
int LibVersionLen;
32+
33+
static volatile uint32_t TimeStamp = 0;
34+
35+
int32_t accelerometer[3];
36+
int32_t gyroscope[3];
37+
int32_t magnetometer[3];
38+
int32_t MagOffset[3];
39+
40+
LSM6DSOSensor AccGyr(&Wire, LSM6DSO_I2C_ADD_L);
41+
42+
HardwareTimer *MyTim;
43+
44+
volatile uint8_t fusion_flag;
45+
46+
void fusion_update(void)
47+
{
48+
fusion_flag = 1;
49+
}
50+
51+
void setup() {
52+
/* Initialize Serial */
53+
Serial.begin(115200);
54+
while (!Serial) yield();
55+
56+
/* Initialize I2C bus */
57+
Wire.begin();
58+
Wire.setClock(400000);
59+
60+
/* Start communication with IMU */
61+
AccGyr.begin();
62+
AccGyr.Set_X_ODR((float)ALGO_FREQ);
63+
AccGyr.Set_X_FS(4);
64+
AccGyr.Set_G_ODR((float)ALGO_FREQ);
65+
AccGyr.Set_G_FS(2000);
66+
AccGyr.Enable_X();
67+
AccGyr.Enable_G();
68+
delay(10);
69+
70+
/* Initialize sensor fusion library */
71+
MotionFX_initialize((MFXState_t *)mfxstate);
72+
73+
MotionFX_getKnobs(mfxstate, ipKnobs);
74+
75+
ipKnobs->acc_orientation[0] = 'n';
76+
ipKnobs->acc_orientation[1] = 'e';
77+
ipKnobs->acc_orientation[2] = 'd';
78+
ipKnobs->gyro_orientation[0] = 'n';
79+
ipKnobs->gyro_orientation[1] = 'e';
80+
ipKnobs->gyro_orientation[2] = 'd';
81+
82+
ipKnobs->gbias_acc_th_sc = GBIAS_ACC_TH_SC;
83+
ipKnobs->gbias_gyro_th_sc = GBIAS_GYRO_TH_SC;
84+
85+
ipKnobs->output_type = MFX_ENGINE_OUTPUT_ENU;
86+
ipKnobs->LMode = 1;
87+
ipKnobs->modx = DECIMATION;
88+
89+
MotionFX_setKnobs(mfxstate, ipKnobs);
90+
MotionFX_enable_6X(mfxstate, MFX_ENGINE_ENABLE);
91+
MotionFX_enable_9X(mfxstate, MFX_ENGINE_DISABLE);
92+
93+
/* OPTIONAL */
94+
/* Get library version */
95+
LibVersionLen = (int)MotionFX_GetLibVersion(LibVersion);
96+
97+
MyTim = new HardwareTimer(TIM3);
98+
MyTim->setOverflow(ALGO_FREQ, HERTZ_FORMAT);
99+
MyTim->attachInterrupt(fusion_update);
100+
MyTim->resume();
101+
}
102+
103+
void loop() {
104+
105+
if(fusion_flag)
106+
{
107+
108+
MFX_input_t data_in;
109+
MFX_output_t data_out;
110+
111+
float delta_time = MOTION_FX_ENGINE_DELTATIME;
112+
fusion_flag = 0;
113+
AccGyr.Get_X_Axes(accelerometer);
114+
AccGyr.Get_G_Axes(gyroscope);
115+
116+
/* Convert angular velocity from [mdps] to [dps] */
117+
data_in.gyro[0] = (float)gyroscope[0] * FROM_MDPS_TO_DPS;
118+
data_in.gyro[1] = (float)gyroscope[1] * FROM_MDPS_TO_DPS;
119+
data_in.gyro[2] = (float)gyroscope[2] * FROM_MDPS_TO_DPS;
120+
121+
/* Convert acceleration from [mg] to [g] */
122+
data_in.acc[0] = (float)accelerometer[0] * FROM_MG_TO_G;
123+
data_in.acc[1] = (float)accelerometer[1] * FROM_MG_TO_G;
124+
data_in.acc[2] = (float)accelerometer[2] * FROM_MG_TO_G;
125+
126+
/* Don't set mag values because we use only acc and gyro */
127+
data_in.mag[0] = 0.0f;
128+
data_in.mag[1] = 0.0f;
129+
data_in.mag[2] = 0.0f;
130+
131+
if (discardedCount == sampleToDiscard)
132+
{
133+
134+
MotionFX_propagate(mfxstate, &data_out, &data_in, &delta_time);
135+
MotionFX_update(mfxstate, &data_out, &data_in, &delta_time, NULL);
136+
137+
Serial.print("Yaw: ");
138+
Serial.print(data_out.rotation[0]);
139+
Serial.print(" Pitch: ");
140+
Serial.print(data_out.rotation[1]);
141+
Serial.print(" Roll: ");
142+
Serial.print(data_out.rotation[2]);
143+
Serial.println("");
144+
145+
}
146+
else
147+
{
148+
discardedCount++;
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)