|
| 1 | +/**************************************************************** |
| 2 | + * Example5_DMP.ino |
| 3 | + * ICM 20948 Arduino Library Demo |
| 4 | + * Initialize the DMP based on the TDK InvenSense ICM20948_eMD_nucleo_1.0 example-icm20948 |
| 5 | + * Paul Clark, February 9th 2021 |
| 6 | + * Based on original code by: |
| 7 | + * Owen Lyke @ SparkFun Electronics |
| 8 | + * Original Creation Date: April 17 2019 |
| 9 | + * |
| 10 | + * Please see License.md for the license information. |
| 11 | + * |
| 12 | + * Distributed as-is; no warranty is given. |
| 13 | + ***************************************************************/ |
| 14 | +#include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU |
| 15 | + |
| 16 | +//#define USE_SPI // Uncomment this to use SPI |
| 17 | + |
| 18 | +#define SERIAL_PORT Serial |
| 19 | + |
| 20 | +#define SPI_PORT SPI // Your desired SPI port. Used only when "USE_SPI" is defined |
| 21 | +#define CS_PIN 2 // Which pin you connect CS to. Used only when "USE_SPI" is defined |
| 22 | + |
| 23 | +#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined |
| 24 | +#define AD0_VAL 1 // The value of the last bit of the I2C address. |
| 25 | + // On the SparkFun 9DoF IMU breakout the default is 1, and when |
| 26 | + // the ADR jumper is closed the value becomes 0 |
| 27 | + |
| 28 | +#ifdef USE_SPI |
| 29 | + ICM_20948_SPI myICM; // If using SPI create an ICM_20948_SPI object |
| 30 | +#else |
| 31 | + ICM_20948_I2C myICM; // Otherwise create an ICM_20948_I2C object |
| 32 | +#endif |
| 33 | + |
| 34 | + |
| 35 | +void setup() { |
| 36 | + |
| 37 | + SERIAL_PORT.begin(115200); |
| 38 | + while(!SERIAL_PORT){}; |
| 39 | + |
| 40 | +#ifdef USE_SPI |
| 41 | + SPI_PORT.begin(); |
| 42 | +#else |
| 43 | + WIRE_PORT.begin(); |
| 44 | + WIRE_PORT.setClock(400000); |
| 45 | +#endif |
| 46 | + |
| 47 | + myICM.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 48 | + |
| 49 | + bool initialized = false; |
| 50 | + while( !initialized ){ |
| 51 | + |
| 52 | +#ifdef USE_SPI |
| 53 | + myICM.begin( CS_PIN, SPI_PORT ); |
| 54 | +#else |
| 55 | + myICM.begin( WIRE_PORT, AD0_VAL ); |
| 56 | +#endif |
| 57 | + |
| 58 | + SERIAL_PORT.print( F("Initialization of the sensor returned: ") ); |
| 59 | + SERIAL_PORT.println( myICM.statusString() ); |
| 60 | + if( myICM.status != ICM_20948_Stat_Ok ){ |
| 61 | + SERIAL_PORT.println( "Trying again..." ); |
| 62 | + delay(500); |
| 63 | + }else{ |
| 64 | + initialized = true; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +void loop() { |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +// Below here are some helper functions to print the data nicely! |
| 76 | + |
| 77 | +void printPaddedInt16b( int16_t val ){ |
| 78 | + if(val > 0){ |
| 79 | + SERIAL_PORT.print(" "); |
| 80 | + if(val < 10000){ SERIAL_PORT.print("0"); } |
| 81 | + if(val < 1000 ){ SERIAL_PORT.print("0"); } |
| 82 | + if(val < 100 ){ SERIAL_PORT.print("0"); } |
| 83 | + if(val < 10 ){ SERIAL_PORT.print("0"); } |
| 84 | + }else{ |
| 85 | + SERIAL_PORT.print("-"); |
| 86 | + if(abs(val) < 10000){ SERIAL_PORT.print("0"); } |
| 87 | + if(abs(val) < 1000 ){ SERIAL_PORT.print("0"); } |
| 88 | + if(abs(val) < 100 ){ SERIAL_PORT.print("0"); } |
| 89 | + if(abs(val) < 10 ){ SERIAL_PORT.print("0"); } |
| 90 | + } |
| 91 | + SERIAL_PORT.print(abs(val)); |
| 92 | +} |
| 93 | + |
| 94 | +void printRawAGMT( ICM_20948_AGMT_t agmt){ |
| 95 | + SERIAL_PORT.print("RAW. Acc [ "); |
| 96 | + printPaddedInt16b( agmt.acc.axes.x ); |
| 97 | + SERIAL_PORT.print(", "); |
| 98 | + printPaddedInt16b( agmt.acc.axes.y ); |
| 99 | + SERIAL_PORT.print(", "); |
| 100 | + printPaddedInt16b( agmt.acc.axes.z ); |
| 101 | + SERIAL_PORT.print(" ], Gyr [ "); |
| 102 | + printPaddedInt16b( agmt.gyr.axes.x ); |
| 103 | + SERIAL_PORT.print(", "); |
| 104 | + printPaddedInt16b( agmt.gyr.axes.y ); |
| 105 | + SERIAL_PORT.print(", "); |
| 106 | + printPaddedInt16b( agmt.gyr.axes.z ); |
| 107 | + SERIAL_PORT.print(" ], Mag [ "); |
| 108 | + printPaddedInt16b( agmt.mag.axes.x ); |
| 109 | + SERIAL_PORT.print(", "); |
| 110 | + printPaddedInt16b( agmt.mag.axes.y ); |
| 111 | + SERIAL_PORT.print(", "); |
| 112 | + printPaddedInt16b( agmt.mag.axes.z ); |
| 113 | + SERIAL_PORT.print(" ], Tmp [ "); |
| 114 | + printPaddedInt16b( agmt.tmp.val ); |
| 115 | + SERIAL_PORT.print(" ]"); |
| 116 | + SERIAL_PORT.println(); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){ |
| 121 | + float aval = abs(val); |
| 122 | + if(val < 0){ |
| 123 | + SERIAL_PORT.print("-"); |
| 124 | + }else{ |
| 125 | + SERIAL_PORT.print(" "); |
| 126 | + } |
| 127 | + for( uint8_t indi = 0; indi < leading; indi++ ){ |
| 128 | + uint32_t tenpow = 0; |
| 129 | + if( indi < (leading-1) ){ |
| 130 | + tenpow = 1; |
| 131 | + } |
| 132 | + for(uint8_t c = 0; c < (leading-1-indi); c++){ |
| 133 | + tenpow *= 10; |
| 134 | + } |
| 135 | + if( aval < tenpow){ |
| 136 | + SERIAL_PORT.print("0"); |
| 137 | + }else{ |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + if(val < 0){ |
| 142 | + SERIAL_PORT.print(-val, decimals); |
| 143 | + }else{ |
| 144 | + SERIAL_PORT.print(val, decimals); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void printScaledAGMT( ICM_20948_AGMT_t agmt){ |
| 149 | + SERIAL_PORT.print("Scaled. Acc (mg) [ "); |
| 150 | + printFormattedFloat( myICM.accX(), 5, 2 ); |
| 151 | + SERIAL_PORT.print(", "); |
| 152 | + printFormattedFloat( myICM.accY(), 5, 2 ); |
| 153 | + SERIAL_PORT.print(", "); |
| 154 | + printFormattedFloat( myICM.accZ(), 5, 2 ); |
| 155 | + SERIAL_PORT.print(" ], Gyr (DPS) [ "); |
| 156 | + printFormattedFloat( myICM.gyrX(), 5, 2 ); |
| 157 | + SERIAL_PORT.print(", "); |
| 158 | + printFormattedFloat( myICM.gyrY(), 5, 2 ); |
| 159 | + SERIAL_PORT.print(", "); |
| 160 | + printFormattedFloat( myICM.gyrZ(), 5, 2 ); |
| 161 | + SERIAL_PORT.print(" ], Mag (uT) [ "); |
| 162 | + printFormattedFloat( myICM.magX(), 5, 2 ); |
| 163 | + SERIAL_PORT.print(", "); |
| 164 | + printFormattedFloat( myICM.magY(), 5, 2 ); |
| 165 | + SERIAL_PORT.print(", "); |
| 166 | + printFormattedFloat( myICM.magZ(), 5, 2 ); |
| 167 | + SERIAL_PORT.print(" ], Tmp (C) [ "); |
| 168 | + printFormattedFloat( myICM.temp(), 5, 2 ); |
| 169 | + SERIAL_PORT.print(" ]"); |
| 170 | + SERIAL_PORT.println(); |
| 171 | +} |
0 commit comments