From 80f37f71223deed638d9e981d01f5608c407c4eb Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Thu, 3 Dec 2020 16:31:11 +0100 Subject: [PATCH 01/10] Add files via upload Wake On Motion functions added --- src/ICM_20948.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++ src/ICM_20948.h | 2 ++ 2 files changed, 50 insertions(+) diff --git a/src/ICM_20948.cpp b/src/ICM_20948.cpp index a7ee4d0..e076e35 100644 --- a/src/ICM_20948.cpp +++ b/src/ICM_20948.cpp @@ -570,6 +570,31 @@ ICM_20948_Status_e ICM_20948::intEnableWatermarkFIFO(uint8_t bm_enable) return status; } + +ICM_20948_Status_e ICM_20948::WOMThreshold(uint8_t threshold) +{ + ICM_20948_ACCEL_WOM_THR_t thr; // storage + status = ICM_20948_wom_threshold(&_device, NULL, &thr); // read phase + if (status != ICM_20948_Stat_Ok) + { + return status; + } + thr.WOM_THRESHOLD = threshold; // change the setting + status = ICM_20948_wom_threshold(&_device, &thr, &thr); // write phase w/ readback + if (status != ICM_20948_Stat_Ok) + { + return status; + } + if (thr.WOM_THRESHOLD != threshold) + { + status = ICM_20948_Stat_Err; + return status; + } + return status; +} + + + // Interface Options ICM_20948_Status_e ICM_20948::i2cMasterPassthrough(bool passthrough) { @@ -1041,3 +1066,26 @@ ICM_20948_Status_e ICM_20948_read_SPI(uint8_t reg, uint8_t *buff, uint32_t len, return ICM_20948_Stat_Ok; } +/* +ICM_20948_Status_e ICM_20948::WOMThreshold(uint8_t threshold) +{ + ICM_20948_ACCEL_WOM_THR_t thr; // storage + status = ICM_20948_wom_threshold(&_device, NULL, &thr); // read phase + if (status != ICM_20948_Stat_Ok) + { + return status; + } + thr.WOM_THRESHOLD = threshold; // change the setting + status = ICM_20948_wom_threshold(&_device, &thr, &thr); // write phase w/ readback + if (status != ICM_20948_Stat_Ok) + { + return status; + } + if (thr.WOM_THRESHOLD != threshold) + { + status = ICM_20948_Stat_Err; + return status; + } + return status; +} +*/ \ No newline at end of file diff --git a/src/ICM_20948.h b/src/ICM_20948.h index df5357e..6fe0d4d 100644 --- a/src/ICM_20948.h +++ b/src/ICM_20948.h @@ -89,6 +89,8 @@ class ICM_20948 ICM_20948_Status_e intEnableOverflowFIFO(uint8_t bm_enable); ICM_20948_Status_e intEnableWatermarkFIFO(uint8_t bm_enable); + ICM_20948_Status_e ICM_20948::WOMThreshold(uint8_t threshold); + // Interface Options ICM_20948_Status_e i2cMasterPassthrough(bool passthrough = true); ICM_20948_Status_e i2cMasterEnable(bool enable = true); From b42341b89f839f911d15de37a8ba4f26d611989a Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Thu, 3 Dec 2020 16:32:36 +0100 Subject: [PATCH 02/10] Add files via upload Wake On Motion threshold function added --- src/util/ICM_20948_C.c | 39 +++++++++++++++++++++++++++++++++++++++ src/util/ICM_20948_C.h | 3 +++ 2 files changed, 42 insertions(+) diff --git a/src/util/ICM_20948_C.c b/src/util/ICM_20948_C.c index 85ceb40..f988121 100644 --- a/src/util/ICM_20948_C.c +++ b/src/util/ICM_20948_C.c @@ -425,6 +425,45 @@ ICM_20948_Status_e ICM_20948_int_enable(ICM_20948_Device_t *pdev, ICM_20948_INT_ return retval; } + + + + + + +ICM_20948_Status_e ICM_20948_wom_threshold(ICM_20948_Device_t *pdev, ICM_20948_ACCEL_WOM_THR_t *write, ICM_20948_ACCEL_WOM_THR_t *read) +{ + ICM_20948_Status_e retval = ICM_20948_Stat_Ok; + + ICM_20948_ACCEL_WOM_THR_t thr; + + retval = ICM_20948_set_bank(pdev, 2); // Must be in the right bank + + if (write != NULL) + { // If the write pointer is not NULL then write to the registers BEFORE reading + thr.WOM_THRESHOLD = write->WOM_THRESHOLD; + + retval = ICM_20948_execute_w(pdev, AGB2_REG_ACCEL_WOM_THR, (uint8_t *)&thr, sizeof(ICM_20948_ACCEL_WOM_THR_t)); + if (retval != ICM_20948_Stat_Ok) + { + return retval; + } + } + + if (read != NULL) + { // If read pointer is not NULL then read the registers (if write is not NULL then this should read back the results of write into read) + retval = ICM_20948_execute_r(pdev, AGB2_REG_ACCEL_WOM_THR, (uint8_t *)&thr, sizeof(ICM_20948_ACCEL_WOM_THR_t)); + if (retval != ICM_20948_Stat_Ok) + { + return retval; + } + + read->WOM_THRESHOLD = thr.WOM_THRESHOLD; + } + + return retval; +} + ICM_20948_Status_e ICM_20948_set_sample_mode(ICM_20948_Device_t *pdev, ICM_20948_InternalSensorID_bm sensors, ICM_20948_LP_CONFIG_CYCLE_e mode) { ICM_20948_Status_e retval = ICM_20948_Stat_Ok; diff --git a/src/util/ICM_20948_C.h b/src/util/ICM_20948_C.h index 402ece2..8e6b924 100644 --- a/src/util/ICM_20948_C.h +++ b/src/util/ICM_20948_C.h @@ -192,6 +192,9 @@ callbacks for the user to respond to interrupt events ICM_20948_Status_e ICM_20948_int_pin_cfg(ICM_20948_Device_t *pdev, ICM_20948_INT_PIN_CFG_t *write, ICM_20948_INT_PIN_CFG_t *read); // Set the INT pin configuration ICM_20948_Status_e ICM_20948_int_enable(ICM_20948_Device_t *pdev, ICM_20948_INT_enable_t *write, ICM_20948_INT_enable_t *read); // Write and or read the interrupt enable information. If non-null the write operation occurs before the read, so as to verify that the write was successful + // WoM Threshold Level Configuration + ICM_20948_Status_e ICM_20948_wom_threshold(ICM_20948_Device_t *pdev, ICM_20948_ACCEL_WOM_THR_t *write, ICM_20948_ACCEL_WOM_THR_t *read); // Write and or read the Wake on Motion threshold. If non-null the write operation occurs before the read, so as to verify that the write was successful + // Internal Sensor Options ICM_20948_Status_e ICM_20948_set_sample_mode(ICM_20948_Device_t *pdev, ICM_20948_InternalSensorID_bm sensors, ICM_20948_LP_CONFIG_CYCLE_e mode); // Use to set accel, gyro, and I2C master into cycled or continuous modes ICM_20948_Status_e ICM_20948_set_full_scale(ICM_20948_Device_t *pdev, ICM_20948_InternalSensorID_bm sensors, ICM_20948_fss_t fss); From fc4cf85eb57d97efa82641423b9f0dabbcc9ee3d Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Thu, 3 Dec 2020 16:44:15 +0100 Subject: [PATCH 03/10] Create Example4_WakeOnMotion --- examples/Arduino/Example4_WakeOnMotion | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/Arduino/Example4_WakeOnMotion diff --git a/examples/Arduino/Example4_WakeOnMotion b/examples/Arduino/Example4_WakeOnMotion new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/Arduino/Example4_WakeOnMotion @@ -0,0 +1 @@ + From 8e100f8f170dab76e0d883b6392f933ad1efb8e6 Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Thu, 3 Dec 2020 16:45:18 +0100 Subject: [PATCH 04/10] Delete Example4_WakeOnMotion --- examples/Arduino/Example4_WakeOnMotion | 1 - 1 file changed, 1 deletion(-) delete mode 100644 examples/Arduino/Example4_WakeOnMotion diff --git a/examples/Arduino/Example4_WakeOnMotion b/examples/Arduino/Example4_WakeOnMotion deleted file mode 100644 index 8b13789..0000000 --- a/examples/Arduino/Example4_WakeOnMotion +++ /dev/null @@ -1 +0,0 @@ - From 252a610a75fae3d701f48039f8d39ee2abd14e86 Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Thu, 3 Dec 2020 16:46:03 +0100 Subject: [PATCH 05/10] Create Example4_WakeOnMotion.ino --- examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino diff --git a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino @@ -0,0 +1 @@ + From 7f1f050d4d0471498502c4cc07e99932bc69b3cf Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Thu, 3 Dec 2020 16:46:32 +0100 Subject: [PATCH 06/10] Add files via upload --- .../Example4_WakeOnMotion.ino | 340 +++++++++++++++++- 1 file changed, 339 insertions(+), 1 deletion(-) diff --git a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino index 8b13789..8df97c4 100644 --- a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino +++ b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino @@ -1 +1,339 @@ - +/**************************************************************** + * Example3_Interrupts.ino + * ICM 20948 Arduino Library Demo + * Builds on Example2_Advanced.ino to set up interrupts when data is ready + * Owen Lyke @ SparkFun Electronics + * Original Creation Date: June 5 2019 + * + * For this example you must connect the interrupt pin "INT" on the breakout + * board to the pin specified by "INT_PIN" on your microcontroller. + * + * This code is beerware; if you see me (or any other SparkFun employee) at the + * local, and you've found our code helpful, please buy us a round! + * + * Distributed as-is; no warranty is given. + ***************************************************************/ +#include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU + +//#define USE_SPI // Uncomment this to use SPI + +#define SERIAL_PORT Serial + +#define INT_PIN 2 // Make sure to connect this pin on your uC to the "INT" pin on the ICM-20948 breakout +//#define LED_PIN 13 +#define LED_PIN LED_BUILTIN +#define BUFFER_SAMPLE_NUM 32 + +#define SPI_PORT SPI // Your desired SPI port. Used only when "USE_SPI" is defined +#define SPI_FREQ 5000000// You can override the default SPI frequency +#define CS_PIN 2 // Which pin you connect CS to. Used only when "USE_SPI" is defined + +#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined +#define AD0_VAL 1 // The value of the last bit of the I2C address. + // On the SparkFun 9DoF IMU breakout the default is 1, and when + // the ADR jumper is closed the value becomes 0 + +#ifdef USE_SPI + ICM_20948_SPI myICM; // If using SPI create an ICM_20948_SPI object +#else + ICM_20948_I2C myICM; // Otherwise create an ICM_20948_I2C object +#endif + +// Some vars to control or respond to interrupts +volatile bool isrFired = false; +volatile bool sensorSleep = false; +volatile bool canToggle = false; +unsigned int WOM_threshold=255; +ICM_20948_ACCEL_WOM_THR_t pdata; + +void setup() { + + pinMode(INT_PIN, INPUT_PULLUP); // Using a pullup b/c ICM-20948 Breakout board has an onboard pullup as well and we don't want them to compete + attachInterrupt(digitalPinToInterrupt(INT_PIN), icmISR, FALLING); // Set up a falling interrupt + + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, !sensorSleep); + + SERIAL_PORT.begin(115200); + while(!SERIAL_PORT){}; + +#ifdef USE_SPI + SPI_PORT.begin(); +#else + WIRE_PORT.begin(); + WIRE_PORT.setClock(400000); +#endif + + bool initialized = false; + while( !initialized ){ + +#ifdef USE_SPI + myICM.begin( CS_PIN, SPI_PORT, SPI_FREQ ); // Here we are using the user-defined SPI_FREQ as the clock speed of the SPI bus +#else + myICM.begin( WIRE_PORT, AD0_VAL ); +#endif + + SERIAL_PORT.print( F("Initialization of the sensor returned: ") ); + SERIAL_PORT.println( myICM.statusString() ); + if( myICM.status != ICM_20948_Stat_Ok ){ + SERIAL_PORT.println( "Trying again..." ); + delay(500); + }else{ + initialized = true; + } + } + + // In this advanced example we'll cover how to do WakeOn Motion action using interrupts + SERIAL_PORT.println("Device connected!"); + + // Here we are doing a SW reset to make sure the device starts in a known state + myICM.swReset( ); + if( myICM.status != ICM_20948_Stat_Ok){ + SERIAL_PORT.print(F("Software Reset returned: ")); + SERIAL_PORT.println(myICM.statusString()); + } + delay(250); + + // Now wake the sensor up + myICM.sleep( sensorSleep ); + myICM.lowPower( false ); + + // The next few configuration functions accept a bit-mask of sensors for which the settings should be applied. + + // Set Gyro and Accelerometer to a particular sample mode + // options: ICM_20948_Sample_Mode_Continuous + // ICM_20948_Sample_Mode_Cycled + myICM.setSampleMode( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), ICM_20948_Sample_Mode_Cycled ); + SERIAL_PORT.print(F("setSampleMode returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + + ICM_20948_smplrt_t mySmplrt; + mySmplrt.g = 54; + myICM.setSampleRate( ICM_20948_Internal_Gyr, mySmplrt ); + SERIAL_PORT.print(F("setSampleRate returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + // Set full scale ranges for both acc and gyr + ICM_20948_fss_t myFSS; // This uses a "Full Scale Settings" structure that can contain values for all configurable sensors + + myFSS.a = gpm2; // (ICM_20948_ACCEL_CONFIG_FS_SEL_e) + // gpm2 + // gpm4 + // gpm8 + // gpm16 + + myFSS.g = dps250; // (ICM_20948_GYRO_CONFIG_1_FS_SEL_e) + // dps250 + // dps500 + // dps1000 + // dps2000 + + myICM.setFullScale( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myFSS ); + if( myICM.status != ICM_20948_Stat_Ok){ + SERIAL_PORT.print(F("setFullScale returned: ")); + SERIAL_PORT.println(myICM.statusString()); + } + + + // Set up Digital Low-Pass Filter configuration + ICM_20948_dlpcfg_t myDLPcfg; // Similar to FSS, this uses a configuration structure for the desired sensors + myDLPcfg.a = acc_d473bw_n499bw; // (ICM_20948_ACCEL_CONFIG_DLPCFG_e) + // acc_d246bw_n265bw - means 3db bandwidth is 246 hz and nyquist bandwidth is 265 hz + // acc_d111bw4_n136bw + // acc_d50bw4_n68bw8 + // acc_d23bw9_n34bw4 + // acc_d11bw5_n17bw + // acc_d5bw7_n8bw3 - means 3 db bandwidth is 5.7 hz and nyquist bandwidth is 8.3 hz + // acc_d473bw_n499bw + + myDLPcfg.g = gyr_d361bw4_n376bw5; // (ICM_20948_GYRO_CONFIG_1_DLPCFG_e) + // gyr_d196bw6_n229bw8 + // gyr_d151bw8_n187bw6 + // gyr_d119bw5_n154bw3 + // gyr_d51bw2_n73bw3 + // gyr_d23bw9_n35bw9 + // gyr_d11bw6_n17bw8 + // gyr_d5bw7_n8bw9 + // gyr_d361bw4_n376bw5 + + myICM.setDLPFcfg( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myDLPcfg ); + if( myICM.status != ICM_20948_Stat_Ok){ + SERIAL_PORT.print(F("setDLPcfg returned: ")); + SERIAL_PORT.println(myICM.statusString()); + } + + // Choose whether or not to use DLPF + // Here we're also showing another way to access the status values, and that it is OK to supply individual sensor masks to these functions + ICM_20948_Status_e accDLPEnableStat = myICM.enableDLPF( ICM_20948_Internal_Acc, true ); + ICM_20948_Status_e gyrDLPEnableStat = myICM.enableDLPF( ICM_20948_Internal_Gyr, true ); + SERIAL_PORT.print(F("Enable DLPF for Accelerometer returned: ")); SERIAL_PORT.println(myICM.statusString(accDLPEnableStat)); + SERIAL_PORT.print(F("Enable DLPF for Gyroscope returned: ")); SERIAL_PORT.println(myICM.statusString(gyrDLPEnableStat)); + + // Now we're going to set up interrupts. There are a lot of options, but for this test we're just configuring the interrupt pin and enabling interrupts to tell us when new data is ready +/* + ICM_20948_Status_e cfgIntActiveLow ( bool active_low ); + ICM_20948_Status_e cfgIntOpenDrain ( bool open_drain ); + ICM_20948_Status_e cfgIntLatch ( bool latching ); // If not latching then the interrupt is a 50 us pulse + + 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 + + ICM_20948_Status_e cfgFsyncActiveLow ( bool active_low ); + 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 + + ICM_20948_Status_e intEnableI2C ( bool enable ); + ICM_20948_Status_e intEnableDMP ( bool enable ); + ICM_20948_Status_e intEnablePLL ( bool enable ); + ICM_20948_Status_e intEnableWOM ( bool enable ); + ICM_20948_Status_e intEnableWOF ( bool enable ); + ICM_20948_Status_e intEnableRawDataReady ( bool enable ); + ICM_20948_Status_e intEnableOverflowFIFO ( uint8_t bm_enable ); + ICM_20948_Status_e intEnableWatermarkFIFO ( uint8_t bm_enable ); + */ + myICM.cfgIntActiveLow(true); // Active low to be compatible with the breakout board's pullup resistor + myICM.cfgIntOpenDrain(false); // Push-pull, though open-drain would also work thanks to the pull-up resistors on the breakout + myICM.cfgIntLatch(true); // Latch the interrupt until cleared + SERIAL_PORT.print(F("cfgIntLatch returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + myICM.WOMThreshold(WOM_threshold); // set WoM threshold + SERIAL_PORT.print(F("Set threshold returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + myICM.intEnableWOM(true); // enable interrupts on WakeOnMotion + SERIAL_PORT.print(F("intEnableWOM returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + myICM.WOMThreshold(WOM_threshold); // set WoM threshold - just in case... + SERIAL_PORT.print(F("Set threshold returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + SERIAL_PORT.println(); + SERIAL_PORT.println(F("Configuration complete!")); +} + +void loop() { + if( isrFired ){ // If our isr flag is set then clear the interrupts on the ICM + isrFired = false; + myICM.getAGMT(); // get the A, G, M, and T readings +// printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units + SERIAL_PORT.println(F("Shock detected")); + delay(30); + myICM.clearInterrupts(); // This would be efficient... but not compatible with Uno + } + + myICM.clearInterrupts(); // clear interrupts for next time - + // usually you'd do this only if an interrupt has occurred, however + // on the 328p I2C usage can block interrupts. This means that sometimes + // an interrupt is missed. When missed, if using an edge-based interrupt + // and only clearing interrupts when one was detected there will be no more + // edges to respond to, so no more interrupts will be detected. Here are + // some possible solutions: + // 1. use a level based interrupt + // 2. use the pulse-based interrupt in ICM settings (set cfgIntLatch to false) + // 3. use a microcontroller with nestable interrupts + // 4. clear the interrupts often + +} + +void icmISR( void ){ + isrFired = true; // Can't use I2C within ISR on 328p, so just set a flag to know that data is available +} + + +// Below here are some helper functions to print the data nicely! +void printPaddedInt16b( int16_t val ){ + if(val > 0){ + SERIAL_PORT.print(" "); + if(val < 10000){ SERIAL_PORT.print("0"); } + if(val < 1000 ){ SERIAL_PORT.print("0"); } + if(val < 100 ){ SERIAL_PORT.print("0"); } + if(val < 10 ){ SERIAL_PORT.print("0"); } + }else{ + SERIAL_PORT.print("-"); + if(abs(val) < 10000){ SERIAL_PORT.print("0"); } + if(abs(val) < 1000 ){ SERIAL_PORT.print("0"); } + if(abs(val) < 100 ){ SERIAL_PORT.print("0"); } + if(abs(val) < 10 ){ SERIAL_PORT.print("0"); } + } + SERIAL_PORT.print(abs(val)); +} + +void printRawAGMT( ICM_20948_AGMT_t agmt){ + SERIAL_PORT.print("RAW. Acc [ "); + printPaddedInt16b( agmt.acc.axes.x ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.acc.axes.y ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.acc.axes.z ); + SERIAL_PORT.print(" ], Gyr [ "); + printPaddedInt16b( agmt.gyr.axes.x ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.gyr.axes.y ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.gyr.axes.z ); + SERIAL_PORT.print(" ], Mag [ "); + printPaddedInt16b( agmt.mag.axes.x ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.mag.axes.y ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.mag.axes.z ); + SERIAL_PORT.print(" ], Tmp [ "); + printPaddedInt16b( agmt.tmp.val ); + SERIAL_PORT.print(" ]"); + SERIAL_PORT.println(); +} + + +void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){ + float aval = abs(val); + if(val < 0){ + SERIAL_PORT.print("-"); + }else{ + SERIAL_PORT.print(" "); + } + for( uint8_t indi = 0; indi < leading; indi++ ){ + uint32_t tenpow = 0; + if( indi < (leading-1) ){ + tenpow = 1; + } + for(uint8_t c = 0; c < (leading-1-indi); c++){ + tenpow *= 10; + } + if( aval < tenpow){ + SERIAL_PORT.print("0"); + }else{ + break; + } + } + if(val < 0){ + SERIAL_PORT.print(-val, decimals); + }else{ + SERIAL_PORT.print(val, decimals); + } +} + +void printScaledAGMT( ICM_20948_AGMT_t agmt){ + SERIAL_PORT.print("Scaled. Acc (mg) [ "); + printFormattedFloat( myICM.accX(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.accY(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.accZ(), 5, 2 ); + SERIAL_PORT.print(" ], Gyr (DPS) [ "); + printFormattedFloat( myICM.gyrX(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.gyrY(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.gyrZ(), 5, 2 ); + SERIAL_PORT.print(" ], Mag (uT) [ "); + printFormattedFloat( myICM.magX(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.magY(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.magZ(), 5, 2 ); + SERIAL_PORT.print(" ], Tmp (C) [ "); + printFormattedFloat( myICM.temp(), 5, 2 ); + SERIAL_PORT.print(" ]"); + SERIAL_PORT.println(); +} From cb2bcb6ca62735791668f0e8cd9fe3f0f9a65a3e Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Sat, 5 Dec 2020 14:08:52 +0100 Subject: [PATCH 07/10] Add files via upload --- .../Example4_WakeOnMotion.ino.ino | 337 ++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino.ino diff --git a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino.ino b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino.ino new file mode 100644 index 0000000..f2233b9 --- /dev/null +++ b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino.ino @@ -0,0 +1,337 @@ +/**************************************************************** + * Example4_WakeOnMotion.ino + * ICM 20948 Arduino Library Demo + * Based on Example3_Interrupts.ino by Owen Lyke @ SparkFun Electronics + * Original Creation Date: Dec 5 2020 + * Created by mkrawcz1 (***** ***) + * + * For this example you must connect the interrupt pin "INT" on the breakout + * board to the pin specified by "INT_PIN" on your microcontroller. + * + * This code is freeware; + * + * Distributed as-is; no warranty is given. + ***************************************************************/ +#include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU + +//#define USE_SPI // Uncomment this to use SPI + +#define SERIAL_PORT Serial + +#define INT_PIN 2 // Make sure to connect this pin on your uC to the "INT" pin on the ICM-20948 breakout +//#define LED_PIN 13 +#define LED_PIN LED_BUILTIN +#define BUFFER_SAMPLE_NUM 32 + +#define SPI_PORT SPI // Your desired SPI port. Used only when "USE_SPI" is defined +#define SPI_FREQ 5000000// You can override the default SPI frequency +#define CS_PIN 2 // Which pin you connect CS to. Used only when "USE_SPI" is defined + +#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined +#define AD0_VAL 1 // The value of the last bit of the I2C address. + // On the SparkFun 9DoF IMU breakout the default is 1, and when + // the ADR jumper is closed the value becomes 0 + +#ifdef USE_SPI + ICM_20948_SPI myICM; // If using SPI create an ICM_20948_SPI object +#else + ICM_20948_I2C myICM; // Otherwise create an ICM_20948_I2C object +#endif + +// Some vars to control or respond to interrupts +volatile bool isrFired = false; +volatile bool sensorSleep = false; +volatile bool canToggle = false; +unsigned int WOM_threshold=200; + +void setup() { + + pinMode(INT_PIN, INPUT_PULLUP); // Using a pullup b/c ICM-20948 Breakout board has an onboard pullup as well and we don't want them to compete + attachInterrupt(digitalPinToInterrupt(INT_PIN), icmISR, FALLING); // Set up a falling interrupt + + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, !sensorSleep); + + SERIAL_PORT.begin(115200); + while(!SERIAL_PORT){}; + +#ifdef USE_SPI + SPI_PORT.begin(); +#else + WIRE_PORT.begin(); + WIRE_PORT.setClock(400000); +#endif + + bool initialized = false; + while( !initialized ){ + +#ifdef USE_SPI + myICM.begin( CS_PIN, SPI_PORT, SPI_FREQ ); // Here we are using the user-defined SPI_FREQ as the clock speed of the SPI bus +#else + myICM.begin( WIRE_PORT, AD0_VAL ); +#endif + + SERIAL_PORT.print( F("Initialization of the sensor returned: ") ); + SERIAL_PORT.println( myICM.statusString() ); + if( myICM.status != ICM_20948_Stat_Ok ){ + SERIAL_PORT.println( "Trying again..." ); + delay(500); + }else{ + initialized = true; + } + } + + // In this advanced example we'll cover how to do WakeOn Motion action using interrupts + SERIAL_PORT.println("Device connected!"); + + // Here we are doing a SW reset to make sure the device starts in a known state + myICM.swReset( ); + if( myICM.status != ICM_20948_Stat_Ok){ + SERIAL_PORT.print(F("Software Reset returned: ")); + SERIAL_PORT.println(myICM.statusString()); + } + delay(250); + + // Now wake the sensor up + myICM.sleep( sensorSleep ); + myICM.lowPower( false ); + + // The next few configuration functions accept a bit-mask of sensors for which the settings should be applied. + + // Set Gyro and Accelerometer to a particular sample mode + // options: ICM_20948_Sample_Mode_Continuous + // ICM_20948_Sample_Mode_Cycled + myICM.setSampleMode( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), ICM_20948_Sample_Mode_Cycled ); + SERIAL_PORT.print(F("setSampleMode returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + + ICM_20948_smplrt_t mySmplrt; + mySmplrt.g = 54; + myICM.setSampleRate( ICM_20948_Internal_Gyr, mySmplrt ); + SERIAL_PORT.print(F("setSampleRate returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + // Set full scale ranges for both acc and gyr + ICM_20948_fss_t myFSS; // This uses a "Full Scale Settings" structure that can contain values for all configurable sensors + + myFSS.a = gpm2; // (ICM_20948_ACCEL_CONFIG_FS_SEL_e) + // gpm2 + // gpm4 + // gpm8 + // gpm16 + + myFSS.g = dps250; // (ICM_20948_GYRO_CONFIG_1_FS_SEL_e) + // dps250 + // dps500 + // dps1000 + // dps2000 + + myICM.setFullScale( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myFSS ); + if( myICM.status != ICM_20948_Stat_Ok){ + SERIAL_PORT.print(F("setFullScale returned: ")); + SERIAL_PORT.println(myICM.statusString()); + } + + + // Set up Digital Low-Pass Filter configuration + ICM_20948_dlpcfg_t myDLPcfg; // Similar to FSS, this uses a configuration structure for the desired sensors + myDLPcfg.a = acc_d473bw_n499bw; // (ICM_20948_ACCEL_CONFIG_DLPCFG_e) + // acc_d246bw_n265bw - means 3db bandwidth is 246 hz and nyquist bandwidth is 265 hz + // acc_d111bw4_n136bw + // acc_d50bw4_n68bw8 + // acc_d23bw9_n34bw4 + // acc_d11bw5_n17bw + // acc_d5bw7_n8bw3 - means 3 db bandwidth is 5.7 hz and nyquist bandwidth is 8.3 hz + // acc_d473bw_n499bw + + myDLPcfg.g = gyr_d361bw4_n376bw5; // (ICM_20948_GYRO_CONFIG_1_DLPCFG_e) + // gyr_d196bw6_n229bw8 + // gyr_d151bw8_n187bw6 + // gyr_d119bw5_n154bw3 + // gyr_d51bw2_n73bw3 + // gyr_d23bw9_n35bw9 + // gyr_d11bw6_n17bw8 + // gyr_d5bw7_n8bw9 + // gyr_d361bw4_n376bw5 + + myICM.setDLPFcfg( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myDLPcfg ); + if( myICM.status != ICM_20948_Stat_Ok){ + SERIAL_PORT.print(F("setDLPcfg returned: ")); + SERIAL_PORT.println(myICM.statusString()); + } + + // Choose whether or not to use DLPF + // Here we're also showing another way to access the status values, and that it is OK to supply individual sensor masks to these functions + ICM_20948_Status_e accDLPEnableStat = myICM.enableDLPF( ICM_20948_Internal_Acc, true ); + ICM_20948_Status_e gyrDLPEnableStat = myICM.enableDLPF( ICM_20948_Internal_Gyr, true ); + SERIAL_PORT.print(F("Enable DLPF for Accelerometer returned: ")); SERIAL_PORT.println(myICM.statusString(accDLPEnableStat)); + SERIAL_PORT.print(F("Enable DLPF for Gyroscope returned: ")); SERIAL_PORT.println(myICM.statusString(gyrDLPEnableStat)); + + // Now we're going to set up interrupts. There are a lot of options, but for this test we're just configuring the interrupt pin and enabling interrupts to tell us when new data is ready +/* + ICM_20948_Status_e cfgIntActiveLow ( bool active_low ); + ICM_20948_Status_e cfgIntOpenDrain ( bool open_drain ); + ICM_20948_Status_e cfgIntLatch ( bool latching ); // If not latching then the interrupt is a 50 us pulse + + 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 + + ICM_20948_Status_e cfgFsyncActiveLow ( bool active_low ); + 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 + + ICM_20948_Status_e intEnableI2C ( bool enable ); + ICM_20948_Status_e intEnableDMP ( bool enable ); + ICM_20948_Status_e intEnablePLL ( bool enable ); + ICM_20948_Status_e intEnableWOM ( bool enable ); + ICM_20948_Status_e intEnableWOF ( bool enable ); + ICM_20948_Status_e intEnableRawDataReady ( bool enable ); + ICM_20948_Status_e intEnableOverflowFIFO ( uint8_t bm_enable ); + ICM_20948_Status_e intEnableWatermarkFIFO ( uint8_t bm_enable ); + */ + myICM.cfgIntActiveLow(true); // Active low to be compatible with the breakout board's pullup resistor + myICM.cfgIntOpenDrain(false); // Push-pull, though open-drain would also work thanks to the pull-up resistors on the breakout + myICM.cfgIntLatch(true); // Latch the interrupt until cleared + SERIAL_PORT.print(F("cfgIntLatch returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + myICM.WOMThreshold(WOM_threshold); // set WoM threshold + SERIAL_PORT.print(F("Set threshold returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + myICM.intEnableWOM(true); // enable interrupts on WakeOnMotion + SERIAL_PORT.print(F("intEnableWOM returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + myICM.WOMThreshold(WOM_threshold); // set WoM threshold - just in case... + SERIAL_PORT.print(F("Set threshold returned: ")); + SERIAL_PORT.println(myICM.statusString()); + + SERIAL_PORT.println(); + SERIAL_PORT.println(F("Configuration complete!")); +} + +void loop() { + if( isrFired ){ // If our isr flag is set then clear the interrupts on the ICM + isrFired = false; + myICM.getAGMT(); // get the A, G, M, and T readings +// printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units + SERIAL_PORT.println(F("Shock detected")); + delay(30); + myICM.clearInterrupts(); // This would be efficient... but not compatible with Uno + } + + myICM.clearInterrupts(); // clear interrupts for next time - + // usually you'd do this only if an interrupt has occurred, however + // on the 328p I2C usage can block interrupts. This means that sometimes + // an interrupt is missed. When missed, if using an edge-based interrupt + // and only clearing interrupts when one was detected there will be no more + // edges to respond to, so no more interrupts will be detected. Here are + // some possible solutions: + // 1. use a level based interrupt + // 2. use the pulse-based interrupt in ICM settings (set cfgIntLatch to false) + // 3. use a microcontroller with nestable interrupts + // 4. clear the interrupts often + +} + +void icmISR( void ){ + isrFired = true; // Can't use I2C within ISR on 328p, so just set a flag to know that data is available +} + + +// Below here are some helper functions to print the data nicely! +void printPaddedInt16b( int16_t val ){ + if(val > 0){ + SERIAL_PORT.print(" "); + if(val < 10000){ SERIAL_PORT.print("0"); } + if(val < 1000 ){ SERIAL_PORT.print("0"); } + if(val < 100 ){ SERIAL_PORT.print("0"); } + if(val < 10 ){ SERIAL_PORT.print("0"); } + }else{ + SERIAL_PORT.print("-"); + if(abs(val) < 10000){ SERIAL_PORT.print("0"); } + if(abs(val) < 1000 ){ SERIAL_PORT.print("0"); } + if(abs(val) < 100 ){ SERIAL_PORT.print("0"); } + if(abs(val) < 10 ){ SERIAL_PORT.print("0"); } + } + SERIAL_PORT.print(abs(val)); +} + +void printRawAGMT( ICM_20948_AGMT_t agmt){ + SERIAL_PORT.print("RAW. Acc [ "); + printPaddedInt16b( agmt.acc.axes.x ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.acc.axes.y ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.acc.axes.z ); + SERIAL_PORT.print(" ], Gyr [ "); + printPaddedInt16b( agmt.gyr.axes.x ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.gyr.axes.y ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.gyr.axes.z ); + SERIAL_PORT.print(" ], Mag [ "); + printPaddedInt16b( agmt.mag.axes.x ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.mag.axes.y ); + SERIAL_PORT.print(", "); + printPaddedInt16b( agmt.mag.axes.z ); + SERIAL_PORT.print(" ], Tmp [ "); + printPaddedInt16b( agmt.tmp.val ); + SERIAL_PORT.print(" ]"); + SERIAL_PORT.println(); +} + + +void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){ + float aval = abs(val); + if(val < 0){ + SERIAL_PORT.print("-"); + }else{ + SERIAL_PORT.print(" "); + } + for( uint8_t indi = 0; indi < leading; indi++ ){ + uint32_t tenpow = 0; + if( indi < (leading-1) ){ + tenpow = 1; + } + for(uint8_t c = 0; c < (leading-1-indi); c++){ + tenpow *= 10; + } + if( aval < tenpow){ + SERIAL_PORT.print("0"); + }else{ + break; + } + } + if(val < 0){ + SERIAL_PORT.print(-val, decimals); + }else{ + SERIAL_PORT.print(val, decimals); + } +} + +void printScaledAGMT( ICM_20948_AGMT_t agmt){ + SERIAL_PORT.print("Scaled. Acc (mg) [ "); + printFormattedFloat( myICM.accX(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.accY(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.accZ(), 5, 2 ); + SERIAL_PORT.print(" ], Gyr (DPS) [ "); + printFormattedFloat( myICM.gyrX(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.gyrY(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.gyrZ(), 5, 2 ); + SERIAL_PORT.print(" ], Mag (uT) [ "); + printFormattedFloat( myICM.magX(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.magY(), 5, 2 ); + SERIAL_PORT.print(", "); + printFormattedFloat( myICM.magZ(), 5, 2 ); + SERIAL_PORT.print(" ], Tmp (C) [ "); + printFormattedFloat( myICM.temp(), 5, 2 ); + SERIAL_PORT.print(" ]"); + SERIAL_PORT.println(); +} From fd3469aa5a3605a5b4d8fb50465437447426cc3f Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Sat, 5 Dec 2020 14:24:33 +0100 Subject: [PATCH 08/10] Delete Example4_WakeOnMotion.ino --- .../Example4_WakeOnMotion.ino | 339 ------------------ 1 file changed, 339 deletions(-) delete mode 100644 examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino diff --git a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino deleted file mode 100644 index 8df97c4..0000000 --- a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino +++ /dev/null @@ -1,339 +0,0 @@ -/**************************************************************** - * Example3_Interrupts.ino - * ICM 20948 Arduino Library Demo - * Builds on Example2_Advanced.ino to set up interrupts when data is ready - * Owen Lyke @ SparkFun Electronics - * Original Creation Date: June 5 2019 - * - * For this example you must connect the interrupt pin "INT" on the breakout - * board to the pin specified by "INT_PIN" on your microcontroller. - * - * This code is beerware; if you see me (or any other SparkFun employee) at the - * local, and you've found our code helpful, please buy us a round! - * - * Distributed as-is; no warranty is given. - ***************************************************************/ -#include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU - -//#define USE_SPI // Uncomment this to use SPI - -#define SERIAL_PORT Serial - -#define INT_PIN 2 // Make sure to connect this pin on your uC to the "INT" pin on the ICM-20948 breakout -//#define LED_PIN 13 -#define LED_PIN LED_BUILTIN -#define BUFFER_SAMPLE_NUM 32 - -#define SPI_PORT SPI // Your desired SPI port. Used only when "USE_SPI" is defined -#define SPI_FREQ 5000000// You can override the default SPI frequency -#define CS_PIN 2 // Which pin you connect CS to. Used only when "USE_SPI" is defined - -#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined -#define AD0_VAL 1 // The value of the last bit of the I2C address. - // On the SparkFun 9DoF IMU breakout the default is 1, and when - // the ADR jumper is closed the value becomes 0 - -#ifdef USE_SPI - ICM_20948_SPI myICM; // If using SPI create an ICM_20948_SPI object -#else - ICM_20948_I2C myICM; // Otherwise create an ICM_20948_I2C object -#endif - -// Some vars to control or respond to interrupts -volatile bool isrFired = false; -volatile bool sensorSleep = false; -volatile bool canToggle = false; -unsigned int WOM_threshold=255; -ICM_20948_ACCEL_WOM_THR_t pdata; - -void setup() { - - pinMode(INT_PIN, INPUT_PULLUP); // Using a pullup b/c ICM-20948 Breakout board has an onboard pullup as well and we don't want them to compete - attachInterrupt(digitalPinToInterrupt(INT_PIN), icmISR, FALLING); // Set up a falling interrupt - - pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, !sensorSleep); - - SERIAL_PORT.begin(115200); - while(!SERIAL_PORT){}; - -#ifdef USE_SPI - SPI_PORT.begin(); -#else - WIRE_PORT.begin(); - WIRE_PORT.setClock(400000); -#endif - - bool initialized = false; - while( !initialized ){ - -#ifdef USE_SPI - myICM.begin( CS_PIN, SPI_PORT, SPI_FREQ ); // Here we are using the user-defined SPI_FREQ as the clock speed of the SPI bus -#else - myICM.begin( WIRE_PORT, AD0_VAL ); -#endif - - SERIAL_PORT.print( F("Initialization of the sensor returned: ") ); - SERIAL_PORT.println( myICM.statusString() ); - if( myICM.status != ICM_20948_Stat_Ok ){ - SERIAL_PORT.println( "Trying again..." ); - delay(500); - }else{ - initialized = true; - } - } - - // In this advanced example we'll cover how to do WakeOn Motion action using interrupts - SERIAL_PORT.println("Device connected!"); - - // Here we are doing a SW reset to make sure the device starts in a known state - myICM.swReset( ); - if( myICM.status != ICM_20948_Stat_Ok){ - SERIAL_PORT.print(F("Software Reset returned: ")); - SERIAL_PORT.println(myICM.statusString()); - } - delay(250); - - // Now wake the sensor up - myICM.sleep( sensorSleep ); - myICM.lowPower( false ); - - // The next few configuration functions accept a bit-mask of sensors for which the settings should be applied. - - // Set Gyro and Accelerometer to a particular sample mode - // options: ICM_20948_Sample_Mode_Continuous - // ICM_20948_Sample_Mode_Cycled - myICM.setSampleMode( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), ICM_20948_Sample_Mode_Cycled ); - SERIAL_PORT.print(F("setSampleMode returned: ")); - SERIAL_PORT.println(myICM.statusString()); - - - ICM_20948_smplrt_t mySmplrt; - mySmplrt.g = 54; - myICM.setSampleRate( ICM_20948_Internal_Gyr, mySmplrt ); - SERIAL_PORT.print(F("setSampleRate returned: ")); - SERIAL_PORT.println(myICM.statusString()); - - // Set full scale ranges for both acc and gyr - ICM_20948_fss_t myFSS; // This uses a "Full Scale Settings" structure that can contain values for all configurable sensors - - myFSS.a = gpm2; // (ICM_20948_ACCEL_CONFIG_FS_SEL_e) - // gpm2 - // gpm4 - // gpm8 - // gpm16 - - myFSS.g = dps250; // (ICM_20948_GYRO_CONFIG_1_FS_SEL_e) - // dps250 - // dps500 - // dps1000 - // dps2000 - - myICM.setFullScale( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myFSS ); - if( myICM.status != ICM_20948_Stat_Ok){ - SERIAL_PORT.print(F("setFullScale returned: ")); - SERIAL_PORT.println(myICM.statusString()); - } - - - // Set up Digital Low-Pass Filter configuration - ICM_20948_dlpcfg_t myDLPcfg; // Similar to FSS, this uses a configuration structure for the desired sensors - myDLPcfg.a = acc_d473bw_n499bw; // (ICM_20948_ACCEL_CONFIG_DLPCFG_e) - // acc_d246bw_n265bw - means 3db bandwidth is 246 hz and nyquist bandwidth is 265 hz - // acc_d111bw4_n136bw - // acc_d50bw4_n68bw8 - // acc_d23bw9_n34bw4 - // acc_d11bw5_n17bw - // acc_d5bw7_n8bw3 - means 3 db bandwidth is 5.7 hz and nyquist bandwidth is 8.3 hz - // acc_d473bw_n499bw - - myDLPcfg.g = gyr_d361bw4_n376bw5; // (ICM_20948_GYRO_CONFIG_1_DLPCFG_e) - // gyr_d196bw6_n229bw8 - // gyr_d151bw8_n187bw6 - // gyr_d119bw5_n154bw3 - // gyr_d51bw2_n73bw3 - // gyr_d23bw9_n35bw9 - // gyr_d11bw6_n17bw8 - // gyr_d5bw7_n8bw9 - // gyr_d361bw4_n376bw5 - - myICM.setDLPFcfg( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myDLPcfg ); - if( myICM.status != ICM_20948_Stat_Ok){ - SERIAL_PORT.print(F("setDLPcfg returned: ")); - SERIAL_PORT.println(myICM.statusString()); - } - - // Choose whether or not to use DLPF - // Here we're also showing another way to access the status values, and that it is OK to supply individual sensor masks to these functions - ICM_20948_Status_e accDLPEnableStat = myICM.enableDLPF( ICM_20948_Internal_Acc, true ); - ICM_20948_Status_e gyrDLPEnableStat = myICM.enableDLPF( ICM_20948_Internal_Gyr, true ); - SERIAL_PORT.print(F("Enable DLPF for Accelerometer returned: ")); SERIAL_PORT.println(myICM.statusString(accDLPEnableStat)); - SERIAL_PORT.print(F("Enable DLPF for Gyroscope returned: ")); SERIAL_PORT.println(myICM.statusString(gyrDLPEnableStat)); - - // Now we're going to set up interrupts. There are a lot of options, but for this test we're just configuring the interrupt pin and enabling interrupts to tell us when new data is ready -/* - ICM_20948_Status_e cfgIntActiveLow ( bool active_low ); - ICM_20948_Status_e cfgIntOpenDrain ( bool open_drain ); - ICM_20948_Status_e cfgIntLatch ( bool latching ); // If not latching then the interrupt is a 50 us pulse - - 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 - - ICM_20948_Status_e cfgFsyncActiveLow ( bool active_low ); - 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 - - ICM_20948_Status_e intEnableI2C ( bool enable ); - ICM_20948_Status_e intEnableDMP ( bool enable ); - ICM_20948_Status_e intEnablePLL ( bool enable ); - ICM_20948_Status_e intEnableWOM ( bool enable ); - ICM_20948_Status_e intEnableWOF ( bool enable ); - ICM_20948_Status_e intEnableRawDataReady ( bool enable ); - ICM_20948_Status_e intEnableOverflowFIFO ( uint8_t bm_enable ); - ICM_20948_Status_e intEnableWatermarkFIFO ( uint8_t bm_enable ); - */ - myICM.cfgIntActiveLow(true); // Active low to be compatible with the breakout board's pullup resistor - myICM.cfgIntOpenDrain(false); // Push-pull, though open-drain would also work thanks to the pull-up resistors on the breakout - myICM.cfgIntLatch(true); // Latch the interrupt until cleared - SERIAL_PORT.print(F("cfgIntLatch returned: ")); - SERIAL_PORT.println(myICM.statusString()); - - myICM.WOMThreshold(WOM_threshold); // set WoM threshold - SERIAL_PORT.print(F("Set threshold returned: ")); - SERIAL_PORT.println(myICM.statusString()); - - myICM.intEnableWOM(true); // enable interrupts on WakeOnMotion - SERIAL_PORT.print(F("intEnableWOM returned: ")); - SERIAL_PORT.println(myICM.statusString()); - - myICM.WOMThreshold(WOM_threshold); // set WoM threshold - just in case... - SERIAL_PORT.print(F("Set threshold returned: ")); - SERIAL_PORT.println(myICM.statusString()); - - SERIAL_PORT.println(); - SERIAL_PORT.println(F("Configuration complete!")); -} - -void loop() { - if( isrFired ){ // If our isr flag is set then clear the interrupts on the ICM - isrFired = false; - myICM.getAGMT(); // get the A, G, M, and T readings -// printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units - SERIAL_PORT.println(F("Shock detected")); - delay(30); - myICM.clearInterrupts(); // This would be efficient... but not compatible with Uno - } - - myICM.clearInterrupts(); // clear interrupts for next time - - // usually you'd do this only if an interrupt has occurred, however - // on the 328p I2C usage can block interrupts. This means that sometimes - // an interrupt is missed. When missed, if using an edge-based interrupt - // and only clearing interrupts when one was detected there will be no more - // edges to respond to, so no more interrupts will be detected. Here are - // some possible solutions: - // 1. use a level based interrupt - // 2. use the pulse-based interrupt in ICM settings (set cfgIntLatch to false) - // 3. use a microcontroller with nestable interrupts - // 4. clear the interrupts often - -} - -void icmISR( void ){ - isrFired = true; // Can't use I2C within ISR on 328p, so just set a flag to know that data is available -} - - -// Below here are some helper functions to print the data nicely! -void printPaddedInt16b( int16_t val ){ - if(val > 0){ - SERIAL_PORT.print(" "); - if(val < 10000){ SERIAL_PORT.print("0"); } - if(val < 1000 ){ SERIAL_PORT.print("0"); } - if(val < 100 ){ SERIAL_PORT.print("0"); } - if(val < 10 ){ SERIAL_PORT.print("0"); } - }else{ - SERIAL_PORT.print("-"); - if(abs(val) < 10000){ SERIAL_PORT.print("0"); } - if(abs(val) < 1000 ){ SERIAL_PORT.print("0"); } - if(abs(val) < 100 ){ SERIAL_PORT.print("0"); } - if(abs(val) < 10 ){ SERIAL_PORT.print("0"); } - } - SERIAL_PORT.print(abs(val)); -} - -void printRawAGMT( ICM_20948_AGMT_t agmt){ - SERIAL_PORT.print("RAW. Acc [ "); - printPaddedInt16b( agmt.acc.axes.x ); - SERIAL_PORT.print(", "); - printPaddedInt16b( agmt.acc.axes.y ); - SERIAL_PORT.print(", "); - printPaddedInt16b( agmt.acc.axes.z ); - SERIAL_PORT.print(" ], Gyr [ "); - printPaddedInt16b( agmt.gyr.axes.x ); - SERIAL_PORT.print(", "); - printPaddedInt16b( agmt.gyr.axes.y ); - SERIAL_PORT.print(", "); - printPaddedInt16b( agmt.gyr.axes.z ); - SERIAL_PORT.print(" ], Mag [ "); - printPaddedInt16b( agmt.mag.axes.x ); - SERIAL_PORT.print(", "); - printPaddedInt16b( agmt.mag.axes.y ); - SERIAL_PORT.print(", "); - printPaddedInt16b( agmt.mag.axes.z ); - SERIAL_PORT.print(" ], Tmp [ "); - printPaddedInt16b( agmt.tmp.val ); - SERIAL_PORT.print(" ]"); - SERIAL_PORT.println(); -} - - -void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){ - float aval = abs(val); - if(val < 0){ - SERIAL_PORT.print("-"); - }else{ - SERIAL_PORT.print(" "); - } - for( uint8_t indi = 0; indi < leading; indi++ ){ - uint32_t tenpow = 0; - if( indi < (leading-1) ){ - tenpow = 1; - } - for(uint8_t c = 0; c < (leading-1-indi); c++){ - tenpow *= 10; - } - if( aval < tenpow){ - SERIAL_PORT.print("0"); - }else{ - break; - } - } - if(val < 0){ - SERIAL_PORT.print(-val, decimals); - }else{ - SERIAL_PORT.print(val, decimals); - } -} - -void printScaledAGMT( ICM_20948_AGMT_t agmt){ - SERIAL_PORT.print("Scaled. Acc (mg) [ "); - printFormattedFloat( myICM.accX(), 5, 2 ); - SERIAL_PORT.print(", "); - printFormattedFloat( myICM.accY(), 5, 2 ); - SERIAL_PORT.print(", "); - printFormattedFloat( myICM.accZ(), 5, 2 ); - SERIAL_PORT.print(" ], Gyr (DPS) [ "); - printFormattedFloat( myICM.gyrX(), 5, 2 ); - SERIAL_PORT.print(", "); - printFormattedFloat( myICM.gyrY(), 5, 2 ); - SERIAL_PORT.print(", "); - printFormattedFloat( myICM.gyrZ(), 5, 2 ); - SERIAL_PORT.print(" ], Mag (uT) [ "); - printFormattedFloat( myICM.magX(), 5, 2 ); - SERIAL_PORT.print(", "); - printFormattedFloat( myICM.magY(), 5, 2 ); - SERIAL_PORT.print(", "); - printFormattedFloat( myICM.magZ(), 5, 2 ); - SERIAL_PORT.print(" ], Tmp (C) [ "); - printFormattedFloat( myICM.temp(), 5, 2 ); - SERIAL_PORT.print(" ]"); - SERIAL_PORT.println(); -} From 0b1560090a97b80203e4cd71b92e73df1d8531ff Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Sat, 5 Dec 2020 14:29:52 +0100 Subject: [PATCH 09/10] Rename Example4_WakeOnMotion.ino.ino to Example4_WakeOnMotion.ino --- .../{Example4_WakeOnMotion.ino.ino => Example4_WakeOnMotion.ino} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/Arduino/Example4_WakeOnMotion/{Example4_WakeOnMotion.ino.ino => Example4_WakeOnMotion.ino} (100%) diff --git a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino.ino b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino similarity index 100% rename from examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino.ino rename to examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino From fe5c711958938b02ce91dbc2d34c7d08879168c1 Mon Sep 17 00:00:00 2001 From: Marcin Krawczyk Date: Mon, 7 Dec 2020 20:51:57 +0100 Subject: [PATCH 10/10] Add files via upload --- .../Example4_WakeOnMotion/Example4_WakeOnMotion.ino | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino index f2233b9..2481857 100644 --- a/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino +++ b/examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino @@ -42,7 +42,8 @@ volatile bool isrFired = false; volatile bool sensorSleep = false; volatile bool canToggle = false; -unsigned int WOM_threshold=200; +unsigned int WOM_threshold=255; +double lastTrigerred; void setup() { @@ -50,7 +51,7 @@ void setup() { attachInterrupt(digitalPinToInterrupt(INT_PIN), icmISR, FALLING); // Set up a falling interrupt pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, !sensorSleep); + digitalWrite(LED_PIN, LOW); SERIAL_PORT.begin(115200); while(!SERIAL_PORT){}; @@ -215,7 +216,9 @@ void loop() { isrFired = false; myICM.getAGMT(); // get the A, G, M, and T readings // printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units - SERIAL_PORT.println(F("Shock detected")); + SERIAL_PORT.println(F("Shock detected")); + digitalWrite(LED_PIN, HIGH); + lastTrigerred=millis(); delay(30); myICM.clearInterrupts(); // This would be efficient... but not compatible with Uno } @@ -231,7 +234,8 @@ void loop() { // 2. use the pulse-based interrupt in ICM settings (set cfgIntLatch to false) // 3. use a microcontroller with nestable interrupts // 4. clear the interrupts often - + if(millis()-lastTrigerred>1000) + digitalWrite(LED_PIN, LOW);; } void icmISR( void ){