Skip to content

Commit cc0846a

Browse files
committed
Starting to read FIFO data
1 parent 75863db commit cc0846a

File tree

9 files changed

+720
-565
lines changed

9 files changed

+720
-565
lines changed

examples/Arduino/Example5_DMP/Example5_DMP.ino

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
* ** This example is based on the InvenSense Application Note "Programming Sequence for DMP Hardware Functions".
1111
* ** We are grateful to InvenSense for providing this.
1212
*
13-
* ** Important note: by default the DMP functionality is disabled in the library. This is to save program memory.
14-
* ** The DMP firmware takes up 14290 Bytes of program memory. To use the DMP, you will need to:
13+
* ** Important note: by default the DMP functionality is disabled in the library
14+
* ** as the DMP firmware takes up 14290 Bytes of program memory.
15+
* ** To use the DMP, you will need to:
1516
* ** Edit ICM_20948_C.h
1617
* ** Uncomment line 29: #define ICM_20948_USE_DMP
1718
* ** Save changes
18-
* ** If you are using Windows, you can find ICM_20948_C.h in Documents\Arduino\libraries\SparkFun_ICM-20948_ArduinoLibrary\src\util
19+
* ** If you are using Windows, you can find ICM_20948_C.h in:
20+
* ** Documents\Arduino\libraries\SparkFun_ICM-20948_ArduinoLibrary\src\util
1921
*
2022
* Please see License.md for the license information.
2123
*
@@ -169,21 +171,18 @@ void setup() {
169171
// Z = raw_x * CPASS_MTX_20 + raw_y * CPASS_MTX_21 + raw_z * CPASS_MTX_22
170172
// Magnetometer full scale is +/- 4900uT so _I think_ we need to multiply by 2^30 / 4900 = 0x000357FA
171173
// The magnetometer Y and Z axes are reversed compared to the accelerometer so we'll invert those
172-
// const unsigned char mountMultiplierZero[4] = {0x00, 0x00, 0x00, 0x00};
173-
// const unsigned char mountMultiplierPlus[4] = {0x00, 0x03, 0x57, 0xFA};
174-
// const unsigned char mountMultiplierMinus[4] = {0xFF, 0xFC, 0xA8, 0x05};
175174
const unsigned char mountMultiplierZero[4] = {0x00, 0x00, 0x00, 0x00};
176-
const unsigned char mountMultiplierPlus[4] = {0x40, 0x00, 0x00, 0x00};
177-
const unsigned char mountMultiplierMinus[4] = {0xC0, 0x00, 0x00, 0x00};
175+
const unsigned char mountMultiplierPlus[4] = {0x00, 0x03, 0x57, 0xFA};
176+
const unsigned char mountMultiplierMinus[4] = {0xFF, 0xFC, 0xA8, 0x05};
178177
success &= (myICM.writeDMPmems(CPASS_MTX_00, 4, &mountMultiplierPlus[0]) == ICM_20948_Stat_Ok);
179178
success &= (myICM.writeDMPmems(CPASS_MTX_01, 4, &mountMultiplierZero[0]) == ICM_20948_Stat_Ok);
180179
success &= (myICM.writeDMPmems(CPASS_MTX_02, 4, &mountMultiplierZero[0]) == ICM_20948_Stat_Ok);
181180
success &= (myICM.writeDMPmems(CPASS_MTX_10, 4, &mountMultiplierZero[0]) == ICM_20948_Stat_Ok);
182-
success &= (myICM.writeDMPmems(CPASS_MTX_11, 4, &mountMultiplierPlus[0]) == ICM_20948_Stat_Ok);
181+
success &= (myICM.writeDMPmems(CPASS_MTX_11, 4, &mountMultiplierMinus[0]) == ICM_20948_Stat_Ok);
183182
success &= (myICM.writeDMPmems(CPASS_MTX_12, 4, &mountMultiplierZero[0]) == ICM_20948_Stat_Ok);
184183
success &= (myICM.writeDMPmems(CPASS_MTX_20, 4, &mountMultiplierZero[0]) == ICM_20948_Stat_Ok);
185184
success &= (myICM.writeDMPmems(CPASS_MTX_21, 4, &mountMultiplierZero[0]) == ICM_20948_Stat_Ok);
186-
success &= (myICM.writeDMPmems(CPASS_MTX_22, 4, &mountMultiplierPlus[0]) == ICM_20948_Stat_Ok);
185+
success &= (myICM.writeDMPmems(CPASS_MTX_22, 4, &mountMultiplierMinus[0]) == ICM_20948_Stat_Ok);
187186

188187
// Enable the FIFO
189188
success &= (myICM.enableFIFO() == ICM_20948_Stat_Ok);
@@ -219,20 +218,32 @@ void setup() {
219218

220219
void loop()
221220
{
222-
uint16_t count;
223-
myICM.getFIFOcount(&count);
221+
icm_20948_DMP_data_t data;
222+
myICM.readDMPdataFromFIFO(&data);
224223
if( myICM.status == ICM_20948_Stat_Ok )
225224
{
226-
SERIAL_PORT.print("FIFO count is: ");
227-
SERIAL_PORT.println( count );
225+
SERIAL_PORT.print("Received data! Header: ");
226+
SERIAL_PORT.println( data.header );
227+
if ( data.header == DMP_header_bitmap_Quat9 ) // We have asked for orientation data so we should receive Quat9
228+
{
229+
SERIAL_PORT.print("Quat9 data is: 0x");
230+
for (int i = 0; i < 14; i++) // Quat9 data is 14 bytes long
231+
{
232+
if ( data.Quat9[i] < 16) SERIAL_PORT.print( "0" ); // Pad the zero
233+
SERIAL_PORT.print( data.Quat9[i], HEX );
234+
}
235+
SERIAL_PORT.println();
236+
}
228237
}
229-
else
238+
else if ( myICM.status != ICM_20948_Stat_FIFONoDataAvail )
230239
{
231-
SERIAL_PORT.print("getFIFOcount failed! Status is: ");
240+
SERIAL_PORT.print("readDMPdataFromFIFO failed! Status is: ");
232241
SERIAL_PORT.println( myICM.statusString() );
242+
SERIAL_PORT.print("Header is: 0x");
243+
SERIAL_PORT.println( data.header, HEX );
233244
}
234245

235-
delay(1000);
246+
delay(10);
236247
}
237248

238249

src/ICM_20948.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ ICM_20948_Status_e ICM_20948::enableDMP(bool enable)
953953
{
954954
if (_device._dmp_firmware_available == true) // Should we attempt to enable the DMP?
955955
{
956-
status = ICM_20948_enable_DMP(&_device, enable);
956+
status = ICM_20948_enable_DMP(&_device, enable == true ? 1 : 0 );
957957
return status;
958958
}
959959
return ICM_20948_Stat_DMPNotSupported;
@@ -1029,13 +1029,28 @@ ICM_20948_Status_e ICM_20948::setDMPODRrate(enum inv_icm20948_sensor sensor, int
10291029
{
10301030
if (_device._dmp_firmware_available == true) // Should we attempt to set the DMP ODR?
10311031
{
1032+
// In order to set an ODR for a given sensor data, write 2-byte value to DMP using key defined above for a particular sensor.
1033+
// Setting value can be calculated as follows:
1034+
// Value = (DMP running rate (225Hz) / ODR ) - 1
1035+
// E.g. For a 25Hz ODR rate, value= (225/25) - 1 = 8.
1036+
10321037
uint16_t period = (225 / rate) - 1;
10331038
status = inv_icm20948_set_dmp_sensor_period(&_device, sensor, period);
10341039
return status;
10351040
}
10361041
return ICM_20948_Stat_DMPNotSupported;
10371042
}
10381043

1044+
ICM_20948_Status_e ICM_20948::readDMPdataFromFIFO(icm_20948_DMP_data_t *data)
1045+
{
1046+
if (_device._dmp_firmware_available == true) // Should we attempt to set the data from the FIFO?
1047+
{
1048+
status = inv_icm20948_read_dmp_data(&_device, data);
1049+
return status;
1050+
}
1051+
return ICM_20948_Stat_DMPNotSupported;
1052+
}
1053+
10391054
// I2C
10401055
ICM_20948_I2C::ICM_20948_I2C()
10411056
{

src/ICM_20948.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ICM_20948
119119
ICM_20948_Status_e cfgIntLatch(bool latching); // If not latching then the interrupt is a 50 us pulse
120120
ICM_20948_Status_e cfgIntAnyReadToClear(bool enabled); // If enabled, *ANY* read will clear the INT_STATUS register. So if you have multiple interrupt sources enabled be sure to read INT_STATUS first
121121
ICM_20948_Status_e cfgFsyncActiveLow(bool active_low);
122-
ICM_20948_Status_e cfgFsyncIntMode(bool interrupt_mode); // Can ue FSYNC as an interrupt input that sets the I2C Master Status register's PASS_THROUGH bit
122+
ICM_20948_Status_e cfgFsyncIntMode(bool interrupt_mode); // Can use FSYNC as an interrupt input that sets the I2C Master Status register's PASS_THROUGH bit
123123

124124
ICM_20948_Status_e intEnableI2C(bool enable);
125125
ICM_20948_Status_e intEnableDMP(bool enable);
@@ -171,6 +171,33 @@ class ICM_20948
171171
ICM_20948_Status_e readFIFO(uint8_t *data);
172172

173173
//DMP
174+
175+
// Done:
176+
// Configure DMP start address through PRGM_STRT_ADDRH/PRGM_STRT_ADDRL
177+
// Load Firmware
178+
// Configure Accel scaling to DMP
179+
// Configure Compass mount matrix and scale to DMP
180+
// Reset FIFO
181+
// Reset DMP
182+
// Enable DMP interrupt
183+
// Configuring DMP to output data to FIFO: set DATA_OUT_CTL1 and DATA_INTR_CTL
184+
// Configuring DMP to output data at multiple ODRs
185+
186+
// To Do:
187+
// Configure DATA_RDY_STATUS
188+
// Additional FIFO output control: DATA_OUT_CTL2, FIFO_WATERMARK, BM_BATCH_MASK, BM_BATCH_CNTR, BM_BATCH_THLD
189+
// Configuring DMP features: MOTION_EVENT_CTL, PED_STD_STEPCTR, PED_STD_TIMECTR
190+
// Enabling Activity Recognition (BAC) feature
191+
// Enabling Significant Motion Detect (SMD) feature
192+
// Enabling Tilt Detector feature
193+
// Enabling Pick Up Gesture feature
194+
// Enabling Fsync detection feature
195+
// Configuring Accel calibration
196+
// Configuring Compass calibration
197+
// Configuring Gyro gain
198+
// Configuring Accel gain
199+
// Biases
200+
174201
ICM_20948_Status_e enableDMP(bool enable = true);
175202
ICM_20948_Status_e resetDMP(void);
176203
ICM_20948_Status_e loadDMPFirmware(void);
@@ -180,6 +207,9 @@ class ICM_20948
180207
ICM_20948_Status_e writeDMPmems(unsigned short reg, unsigned int length, const unsigned char *data);
181208
ICM_20948_Status_e readDMPmems(unsigned short reg, unsigned int length, unsigned char *data);
182209
ICM_20948_Status_e setDMPODRrate(enum inv_icm20948_sensor sensor, int rate);
210+
211+
ICM_20948_Status_e readDMPdataFromFIFO(icm_20948_DMP_data_t *data);
212+
183213
};
184214

185215
// I2C

0 commit comments

Comments
 (0)