|
| 1 | +/**************************************************************** |
| 2 | + * Testing basic functionality of the ICM 20948 on the OpenLog Artemis. |
| 3 | + * Select "SparkFun Apollo3" \ SparkFun RedBoard Artemis ATP" as the board. |
| 4 | + * |
| 5 | + * Based on: |
| 6 | + * |
| 7 | + * Example1_Basics.ino |
| 8 | + * ICM 20948 Arduino Library Demo |
| 9 | + * Use the default configuration to stream 9-axis IMU data |
| 10 | + * Owen Lyke @ SparkFun Electronics |
| 11 | + * Original Creation Date: April 17 2019 |
| 12 | + * |
| 13 | + * This code is beerware; if you see me (or any other SparkFun employee) at the |
| 14 | + * local, and you've found our code helpful, please buy us a round! |
| 15 | + * |
| 16 | + * Distributed as-is; no warranty is given. |
| 17 | + ***************************************************************/ |
| 18 | + |
| 19 | +// OLA Specifics: |
| 20 | +const byte PIN_IMU_POWER = 27; // The Red SparkFun version of the OLA (V10) uses pin 27 |
| 21 | +//const byte PIN_IMU_POWER = 22; // The Black SparkX version of the OLA (X04) uses pin 22 |
| 22 | +const byte PIN_IMU_INT = 37; |
| 23 | +const byte PIN_IMU_CHIP_SELECT = 44; |
| 24 | + |
| 25 | + |
| 26 | +#include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU |
| 27 | + |
| 28 | +#define USE_SPI // Uncomment this to use SPI |
| 29 | + |
| 30 | +#define SERIAL_PORT Serial |
| 31 | + |
| 32 | +#define SPI_PORT SPI // Your desired SPI port. Used only when "USE_SPI" is defined |
| 33 | +#define CS_PIN PIN_IMU_CHIP_SELECT // Which pin you connect CS to. Used only when "USE_SPI" is defined. OLA uses pin 44. |
| 34 | + |
| 35 | +#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined |
| 36 | +#define AD0_VAL 1 // The value of the last bit of the I2C address. |
| 37 | + // On the SparkFun 9DoF IMU breakout the default is 1, and when |
| 38 | + // the ADR jumper is closed the value becomes 0 |
| 39 | + |
| 40 | +#ifdef USE_SPI |
| 41 | + ICM_20948_SPI myICM; // If using SPI create an ICM_20948_SPI object |
| 42 | +#else |
| 43 | + ICM_20948_I2C myICM; // Otherwise create an ICM_20948_I2C object |
| 44 | +#endif |
| 45 | + |
| 46 | + |
| 47 | +void setup() { |
| 48 | + |
| 49 | +#ifdef USE_SPI |
| 50 | + SPI_PORT.begin(); |
| 51 | +#else |
| 52 | + WIRE_PORT.begin(); |
| 53 | + WIRE_PORT.setClock(400000); |
| 54 | +#endif |
| 55 | + |
| 56 | + enableCIPOpullUp(); // Enable CIPO pull-up on the OLA |
| 57 | + |
| 58 | + pinMode(PIN_IMU_CHIP_SELECT, OUTPUT); |
| 59 | + digitalWrite(PIN_IMU_CHIP_SELECT, HIGH); //Be sure IMU is deselected |
| 60 | + |
| 61 | + //Reset ICM by power cycling it |
| 62 | + imuPowerOff(); |
| 63 | + |
| 64 | + delay(10); |
| 65 | + |
| 66 | + imuPowerOn(); // Enable power for the OLA IMU |
| 67 | + |
| 68 | + delay(100); // Wait for the IMU to power up |
| 69 | + |
| 70 | + SERIAL_PORT.begin(115200); |
| 71 | + while(!SERIAL_PORT){}; |
| 72 | + |
| 73 | + bool initialized = false; |
| 74 | + while( !initialized ){ |
| 75 | + |
| 76 | +#ifdef USE_SPI |
| 77 | + myICM.begin( CS_PIN, SPI_PORT ); |
| 78 | +#else |
| 79 | + myICM.begin( WIRE_PORT, AD0_VAL ); |
| 80 | +#endif |
| 81 | + |
| 82 | + SERIAL_PORT.print( F("Initialization of the sensor returned: ") ); |
| 83 | + SERIAL_PORT.println( myICM.statusString() ); |
| 84 | + if( myICM.status != ICM_20948_Stat_Ok ){ |
| 85 | + SERIAL_PORT.println( "Trying again..." ); |
| 86 | + delay(500); |
| 87 | + }else{ |
| 88 | + initialized = true; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void loop() { |
| 94 | + |
| 95 | + if( myICM.dataReady() ){ |
| 96 | + myICM.getAGMT(); // The values are only updated when you call 'getAGMT' |
| 97 | +// printRawAGMT( myICM.agmt ); // Uncomment this to see the raw values, taken directly from the agmt structure |
| 98 | + printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units |
| 99 | + delay(30); |
| 100 | + }else{ |
| 101 | + Serial.println("Waiting for data"); |
| 102 | + delay(500); |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +// Below here are some helper functions to print the data nicely! |
| 109 | + |
| 110 | +void printPaddedInt16b( int16_t val ){ |
| 111 | + if(val > 0){ |
| 112 | + SERIAL_PORT.print(" "); |
| 113 | + if(val < 10000){ SERIAL_PORT.print("0"); } |
| 114 | + if(val < 1000 ){ SERIAL_PORT.print("0"); } |
| 115 | + if(val < 100 ){ SERIAL_PORT.print("0"); } |
| 116 | + if(val < 10 ){ SERIAL_PORT.print("0"); } |
| 117 | + }else{ |
| 118 | + SERIAL_PORT.print("-"); |
| 119 | + if(abs(val) < 10000){ SERIAL_PORT.print("0"); } |
| 120 | + if(abs(val) < 1000 ){ SERIAL_PORT.print("0"); } |
| 121 | + if(abs(val) < 100 ){ SERIAL_PORT.print("0"); } |
| 122 | + if(abs(val) < 10 ){ SERIAL_PORT.print("0"); } |
| 123 | + } |
| 124 | + SERIAL_PORT.print(abs(val)); |
| 125 | +} |
| 126 | + |
| 127 | +void printRawAGMT( ICM_20948_AGMT_t agmt){ |
| 128 | + SERIAL_PORT.print("RAW. Acc [ "); |
| 129 | + printPaddedInt16b( agmt.acc.axes.x ); |
| 130 | + SERIAL_PORT.print(", "); |
| 131 | + printPaddedInt16b( agmt.acc.axes.y ); |
| 132 | + SERIAL_PORT.print(", "); |
| 133 | + printPaddedInt16b( agmt.acc.axes.z ); |
| 134 | + SERIAL_PORT.print(" ], Gyr [ "); |
| 135 | + printPaddedInt16b( agmt.gyr.axes.x ); |
| 136 | + SERIAL_PORT.print(", "); |
| 137 | + printPaddedInt16b( agmt.gyr.axes.y ); |
| 138 | + SERIAL_PORT.print(", "); |
| 139 | + printPaddedInt16b( agmt.gyr.axes.z ); |
| 140 | + SERIAL_PORT.print(" ], Mag [ "); |
| 141 | + printPaddedInt16b( agmt.mag.axes.x ); |
| 142 | + SERIAL_PORT.print(", "); |
| 143 | + printPaddedInt16b( agmt.mag.axes.y ); |
| 144 | + SERIAL_PORT.print(", "); |
| 145 | + printPaddedInt16b( agmt.mag.axes.z ); |
| 146 | + SERIAL_PORT.print(" ], Tmp [ "); |
| 147 | + printPaddedInt16b( agmt.tmp.val ); |
| 148 | + SERIAL_PORT.print(" ]"); |
| 149 | + SERIAL_PORT.println(); |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){ |
| 154 | + float aval = abs(val); |
| 155 | + if(val < 0){ |
| 156 | + SERIAL_PORT.print("-"); |
| 157 | + }else{ |
| 158 | + SERIAL_PORT.print(" "); |
| 159 | + } |
| 160 | + for( uint8_t indi = 0; indi < leading; indi++ ){ |
| 161 | + uint32_t tenpow = 0; |
| 162 | + if( indi < (leading-1) ){ |
| 163 | + tenpow = 1; |
| 164 | + } |
| 165 | + for(uint8_t c = 0; c < (leading-1-indi); c++){ |
| 166 | + tenpow *= 10; |
| 167 | + } |
| 168 | + if( aval < tenpow){ |
| 169 | + SERIAL_PORT.print("0"); |
| 170 | + }else{ |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + if(val < 0){ |
| 175 | + SERIAL_PORT.print(-val, decimals); |
| 176 | + }else{ |
| 177 | + SERIAL_PORT.print(val, decimals); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +void printScaledAGMT( ICM_20948_AGMT_t agmt){ |
| 182 | + SERIAL_PORT.print("Scaled. Acc (mg) [ "); |
| 183 | + printFormattedFloat( myICM.accX(), 5, 2 ); |
| 184 | + SERIAL_PORT.print(", "); |
| 185 | + printFormattedFloat( myICM.accY(), 5, 2 ); |
| 186 | + SERIAL_PORT.print(", "); |
| 187 | + printFormattedFloat( myICM.accZ(), 5, 2 ); |
| 188 | + SERIAL_PORT.print(" ], Gyr (DPS) [ "); |
| 189 | + printFormattedFloat( myICM.gyrX(), 5, 2 ); |
| 190 | + SERIAL_PORT.print(", "); |
| 191 | + printFormattedFloat( myICM.gyrY(), 5, 2 ); |
| 192 | + SERIAL_PORT.print(", "); |
| 193 | + printFormattedFloat( myICM.gyrZ(), 5, 2 ); |
| 194 | + SERIAL_PORT.print(" ], Mag (uT) [ "); |
| 195 | + printFormattedFloat( myICM.magX(), 5, 2 ); |
| 196 | + SERIAL_PORT.print(", "); |
| 197 | + printFormattedFloat( myICM.magY(), 5, 2 ); |
| 198 | + SERIAL_PORT.print(", "); |
| 199 | + printFormattedFloat( myICM.magZ(), 5, 2 ); |
| 200 | + SERIAL_PORT.print(" ], Tmp (C) [ "); |
| 201 | + printFormattedFloat( myICM.temp(), 5, 2 ); |
| 202 | + SERIAL_PORT.print(" ]"); |
| 203 | + SERIAL_PORT.println(); |
| 204 | +} |
| 205 | + |
| 206 | +void imuPowerOn() |
| 207 | +{ |
| 208 | + pinMode(PIN_IMU_POWER, OUTPUT); |
| 209 | + digitalWrite(PIN_IMU_POWER, HIGH); |
| 210 | +} |
| 211 | +void imuPowerOff() |
| 212 | +{ |
| 213 | + pinMode(PIN_IMU_POWER, OUTPUT); |
| 214 | + digitalWrite(PIN_IMU_POWER, LOW); |
| 215 | +} |
| 216 | + |
| 217 | +bool enableCIPOpullUp() |
| 218 | +{ |
| 219 | + //Add CIPO pull-up |
| 220 | + ap3_err_t retval = AP3_OK; |
| 221 | + am_hal_gpio_pincfg_t cipoPinCfg = AP3_GPIO_DEFAULT_PINCFG; |
| 222 | + cipoPinCfg.uFuncSel = AM_HAL_PIN_6_M0MISO; |
| 223 | + cipoPinCfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA; |
| 224 | + cipoPinCfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_PUSHPULL; |
| 225 | + cipoPinCfg.uIOMnum = AP3_SPI_IOM; |
| 226 | + cipoPinCfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K; |
| 227 | + padMode(MISO, cipoPinCfg, &retval); |
| 228 | + return (retval == AP3_OK); |
| 229 | +} |
0 commit comments