/* Interfacing with an IMU, and sending its values wirelessly via the nRF24L01 chip to another Xiao device, which will read and printout its values. */ #include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU #include #include #include #include #define AD0_VAL 1 // Using I2C communication for IMU ICM_20948_I2C myICM; RF24 radio(D6, D7); // CE, CSN const byte address[6] = "00001"; void setup() { Serial.begin(115200); Wire.begin(); Wire.setClock(400000); myICM.begin(Wire, AD0_VAL); Serial.print(F("Initialization of the sensor returned: ")); Serial.println(myICM.statusString()); if (myICM.status != ICM_20948_Stat_Ok) { Serial.println("Trying again..."); delay(500); } else { Serial.println("Success! Connected to IMU."); } radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_MIN); radio.stopListening(); } void loop() { if (myICM.dataReady()) { myICM.getAGMT(); // The values are only updated when you call 'getAGMT' // printRawAGMT( myICM.agmt ); // Uncomment this to see the raw values, taken directly from the agmt structure // printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units delay(30); } else { Serial.println("Waiting for data"); delay(500); } // const char text[] = "Hello World"; // radio.write(&text, sizeof(text)); float acc_IMU[3]; acc_IMU[0] = ((float)myICM.accX())/1000.; acc_IMU[1] = ((float)myICM.accY())/1000.; acc_IMU[2] = ((float)myICM.accZ())/1000.; radio.write(&acc_IMU, sizeof(acc_IMU)); delay(10); } void printPaddedInt16b(int16_t val) { if (val > 0) { Serial.print(" "); if (val < 10000) { Serial.print("0"); } if (val < 1000) { Serial.print("0"); } if (val < 100) { Serial.print("0"); } if (val < 10) { Serial.print("0"); } } else { Serial.print("-"); if (abs(val) < 10000) { Serial.print("0"); } if (abs(val) < 1000) { Serial.print("0"); } if (abs(val) < 100) { Serial.print("0"); } if (abs(val) < 10) { Serial.print("0"); } } Serial.print(abs(val)); } void printRawAGMT(ICM_20948_AGMT_t agmt) { Serial.print("RAW. Acc [ "); printPaddedInt16b(agmt.acc.axes.x); Serial.print(", "); printPaddedInt16b(agmt.acc.axes.y); Serial.print(", "); printPaddedInt16b(agmt.acc.axes.z); Serial.print(" ], Gyr [ "); printPaddedInt16b(agmt.gyr.axes.x); Serial.print(", "); printPaddedInt16b(agmt.gyr.axes.y); Serial.print(", "); printPaddedInt16b(agmt.gyr.axes.z); Serial.print(" ], Mag [ "); printPaddedInt16b(agmt.mag.axes.x); Serial.print(", "); printPaddedInt16b(agmt.mag.axes.y); Serial.print(", "); printPaddedInt16b(agmt.mag.axes.z); Serial.print(" ], Tmp [ "); printPaddedInt16b(agmt.tmp.val); Serial.print(" ]"); Serial.println(); } void printFormattedFloat(float val, uint8_t leading, uint8_t decimals) { float aval = abs(val); if (val < 0) { Serial.print("-"); } else { Serial.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.print("0"); } else { break; } } if (val < 0) { Serial.print(-val, decimals); } else { Serial.print(val, decimals); } } void printScaledAGMT(ICM_20948_I2C *sensor) { Serial.print("Scaled. Acc (mg) [ "); printFormattedFloat(sensor->accX(), 5, 2); Serial.print(", "); printFormattedFloat(sensor->accY(), 5, 2); Serial.print(", "); printFormattedFloat(sensor->accZ(), 5, 2); Serial.print(" ], Gyr (DPS) [ "); printFormattedFloat(sensor->gyrX(), 5, 2); Serial.print(", "); printFormattedFloat(sensor->gyrY(), 5, 2); Serial.print(", "); printFormattedFloat(sensor->gyrZ(), 5, 2); Serial.print(" ], Mag (uT) [ "); printFormattedFloat(sensor->magX(), 5, 2); Serial.print(", "); printFormattedFloat(sensor->magY(), 5, 2); Serial.print(", "); printFormattedFloat(sensor->magZ(), 5, 2); Serial.print(" ], Tmp (C) [ "); printFormattedFloat(sensor->temp(), 5, 2); Serial.print(" ]"); Serial.println(); }