Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* ICM20948 Basic Example Code 00002 by: Kris Winer 00003 modified by Eric Nativel MBEB_OS6 port 00004 date: 29, MArch 2021 00005 license: Beerware - Use this code however you'd like. If you 00006 find it useful you can buy me a beer some time. 00007 Modified by Brent Wilkins July 19, 2016 00008 Demonstrate basic ICM20948 functionality including parameterizing the register 00009 addresses, initializing the sensor, getting properly scaled accelerometer, 00010 gyroscope, and magnetometer data out. Added display functions to allow display 00011 to on breadboard monitor. Addition of 9 DoF sensor fusion using open source 00012 Madgwick and Mahony filter algorithms. Pimoroni icm20948 and stm32L432kc nucleo board 00013 */ 00014 00015 #include "mbed.h" 00016 #include "ahrs.h" 00017 #include "icm20948.h" 00018 #include <cstdio> 00019 #include <stdint.h> 00020 00021 using namespace std::chrono; 00022 Timer t1; 00023 typedef unsigned char byte; 00024 float selft[6]; 00025 static BufferedSerial pc(USBTX, USBRX); 00026 00027 00028 char msg[255]; 00029 00030 void setup() 00031 { 00032 //Set up I2C 00033 00034 pc.set_baud(9600); 00035 pc.set_format( 00036 /* bits */ 8, 00037 /* parity */ BufferedSerial::None, 00038 /* stop bit */ 1 00039 ); 00040 // Reset ICM20948 00041 begin(); 00042 00043 writeByte(ICM20948_ADDRESS, PWR_MGMT_1, READ_FLAGS); 00044 thread_sleep_for(100); 00045 writeByte(ICM20948_ADDRESS, PWR_MGMT_1, 0x01); 00046 thread_sleep_for(100); 00047 00048 // Read the WHO_AM_I register, this is a good test of communication 00049 byte c = readByte(ICM20948_ADDRESS, WHO_AM_I_ICM20948); 00050 sprintf(msg,"ICM20948 I AM 0x %x I should be 0x %x",c,0xEA); 00051 pc.write(msg, strlen(msg)); 00052 if (c == 0xEA) // WHO_AM_I should always be 0x71 00053 { 00054 sprintf(msg,"ICM20948 is online...\n"); 00055 pc.write(msg, strlen(msg)); 00056 // writeByte(ICM20948_ADDRESS, REG_BANK_SEL, 0x10); 00057 // Start by performing self test and reporting values 00058 ICM20948SelfTest(selft); 00059 sprintf(msg,"x-axis self test: acceleration trim within : %f of factory value\n",selft[0]); 00060 pc.write(msg, strlen(msg)); 00061 sprintf(msg,"y-axis self test: acceleration trim within : %f of factory value\n",selft[1]); 00062 pc.write(msg, strlen(msg)); 00063 sprintf(msg,"z-axis self test: acceleration trim within : %f of factory value\n",selft[2]); 00064 pc.write(msg, strlen(msg)); 00065 sprintf(msg,"x-axis self test: gyration trim within : %f of factory value\n",selft[3]); 00066 pc.write(msg, strlen(msg)); 00067 sprintf(msg,"y-axis self test: gyration trim within : %f of factory value\n",selft[4]); 00068 pc.write(msg, strlen(msg)); 00069 sprintf(msg,"z-axis self test: gyration trim within : %f of factory value\n",selft[5]); 00070 pc.write(msg, strlen(msg)); 00071 // Calibrate gyro and accelerometers, load biases in bias registers 00072 calibrateICM20948(gyroBias, accelBias); 00073 00074 initICM20948(); 00075 // Initialize device for active mode read of acclerometer, gyroscope, and 00076 // temperature 00077 sprintf(msg,"ICM20948 initialized for active data mode....\n"); 00078 pc.write(msg, strlen(msg)); 00079 // Read the WHO_AM_I register of the magnetometer, this is a good test of 00080 // communication 00081 tempCount =readTempData(); // Read the adc values 00082 // Temperature in degrees Centigrade 00083 temperature = ((float) tempCount) / 333.87 + 21.0; 00084 // Print temperature in degrees Centigrade 00085 sprintf(msg,"Temperature is %f degrees C\n",temperature); 00086 pc.write(msg, strlen(msg)); 00087 byte d = readByte(AK09916_ADDRESS<<1, WHO_AM_I_AK09916); 00088 sprintf(msg,"AK8963 I AM 0x %x I should be 0x %d\n",d,0x09); 00089 pc.write(msg, strlen(msg)); 00090 00091 if (d != 0x09) 00092 { 00093 // Communication failed, stop here 00094 sprintf(msg,"Communication with magnetometer failed, abort!\n"); 00095 pc.write(msg, strlen(msg)); 00096 exit(0); 00097 } 00098 00099 // Get magnetometer calibration from AK8963 ROM 00100 initAK09916(); 00101 // Initialize device for active mode read of magnetometer 00102 sprintf(msg,"AK09916 initialized for active data mode....\n"); 00103 pc.write(msg, strlen(msg)); 00104 00105 00106 00107 // Get sensor resolutions, only need to do this once 00108 getAres(); 00109 getGres(); 00110 getMres(); 00111 // The next call delays for 4 seconds, and then records about 15 seconds of 00112 // data to calculate bias and scale. 00113 magCalICM20948(magBias, magScale); 00114 sprintf(msg,"AK09916 mag biases (mG)\n %f\n%f\n%f\n",magBias[0],magBias[1],magBias[2]); 00115 pc.write(msg, strlen(msg)); 00116 sprintf(msg,"AK09916 mag scale (mG)\n %f\n%f\n%f\n",magScale[0],magScale[1],magScale[2]); 00117 pc.write(msg, strlen(msg)); 00118 thread_sleep_for(2000); // Add delay to see results before pc spew of data 00119 } // if (c == 0x71) 00120 else 00121 { 00122 sprintf(msg,"Could not connect to ICM20948: 0x%x",c); 00123 pc.write(msg, strlen(msg)); 00124 // Communication failed, stop here 00125 sprintf(msg," Communication failed, abort!\n"); 00126 pc.write(msg, strlen(msg)); 00127 exit(0); 00128 } 00129 } 00130 int main(void) 00131 {int i=0; 00132 setup(); 00133 while(i<100) 00134 { 00135 // If intPin goes high, all data registers have new data 00136 // On interrupt, check if data ready interrupt 00137 if (readByte(ICM20948_ADDRESS, INT_STATUS_1) & 0x01) 00138 { 00139 readAccelData(accelCount); // Read the x/y/z adc values 00140 00141 // Now we'll calculate the accleration value into actual g's 00142 // This depends on scale being set 00143 ax = (float)accelCount[0] * aRes; // - accelBias[0]; 00144 ay = (float)accelCount[1] * aRes; // - accelBias[1]; 00145 az = (float)accelCount[2] * aRes; // - accelBias[2]; 00146 sprintf(msg,"X-acceleration: %f mg\n",1000*ax); 00147 pc.write(msg, strlen(msg)); 00148 sprintf(msg,"Y-acceleration: %f mg\n",1000*ay); 00149 pc.write(msg, strlen(msg)); 00150 sprintf(msg,"Z-acceleration: %f mg\n",1000*az); 00151 pc.write(msg, strlen(msg)); 00152 readGyroData(gyroCount); // Read the x/y/z adc values 00153 00154 // Calculate the gyro value into actual degrees per second 00155 // This depends on scale being set 00156 gx = (float)gyroCount[0] * gRes; 00157 gy = (float)gyroCount[1] * gRes; 00158 gz = (float)gyroCount[2] * gRes; 00159 sprintf(msg,"x -gyroscope: %f and bias %f deg/s\n",gx,gyroBias[0]); 00160 pc.write(msg, strlen(msg)); 00161 readMagData(magCount); // Read the x/y/z adc values 00162 00163 // Calculate the magnetometer values in milliGauss 00164 // Include factory calibration per data sheet and user environmental 00165 // corrections 00166 // Get actual magnetometer value, this depends on scale being set 00167 mx = (float)magCount[0] * mRes - magBias[0]; 00168 my = (float)magCount[1] * mRes - magBias[1]; 00169 mz = (float)magCount[2] * mRes - magBias[2]; 00170 // if (readByte(ICM20948_ADDRESS, INT_STATUS) & 0x01) 00171 00172 // Must be called before updating quaternions! 00173 updateTime(); 00174 00175 // Sensors x (y)-axis of the accelerometer is aligned with the y (x)-axis of 00176 // the magnetometer; the magnetometer z-axis (+ down) is opposite to z-axis 00177 // (+ up) of accelerometer and gyro! We have to make some allowance for this 00178 // orientationmismatch in feeding the output to the quaternion filter. For the 00179 // ICM20948, we have chosen a magnetic rotation that keeps the sensor forward 00180 // along the x-axis just like in the LSM9DS0 sensor. This rotation can be 00181 // modified to allow any convenient orientation convention. This is ok by 00182 // aircraft orientation standards! Pass gyro rate as rad/s 00183 MahonyQuaternionUpdate(ax, ay, az, gx * DEG_TO_RAD, 00184 gy * DEG_TO_RAD, gz * DEG_TO_RAD, my, 00185 mx, mz, deltat); 00186 00187 // Define output variables from updated quaternion---these are Tait-Bryan 00188 // angles, commonly used in aircraft orientation. In this coordinate system, 00189 // the positive z-axis is down toward Earth. Yaw is the angle between Sensor 00190 // x-axis and Earth magnetic North (or true North if corrected for local 00191 // declination, looking down on the sensor positive yaw is counterclockwise. 00192 // Pitch is angle between sensor x-axis and Earth ground plane, toward the 00193 // Earth is positive, up toward the sky is negative. Roll is angle between 00194 // sensor y-axis and Earth ground plane, y-axis up is positive roll. These 00195 // arise from the definition of the homogeneous rotation matrix constructed 00196 // from quaternions. Tait-Bryan angles as well as Euler angles are 00197 // non-commutative; that is, the get the correct orientation the rotations 00198 // must be applied in the correct order which for this configuration is yaw, 00199 // pitch, and then roll. 00200 // For more see 00201 // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles 00202 // which has additional links. 00203 yaw = atan2(2.0f * (*(getQ()+1) * *(getQ()+2) + *getQ() 00204 * *(getQ()+3)), *getQ() * *getQ() + *(getQ()+1) 00205 * *(getQ()+1) - *(getQ()+2) * *(getQ()+2) - *(getQ()+3) 00206 * *(getQ()+3)); 00207 pitch = -asin(2.0f * (*(getQ()+1) * *(getQ()+3) - *getQ() 00208 * *(getQ()+2))); 00209 roll = atan2(2.0f * (*getQ() * *(getQ()+1) + *(getQ()+2) 00210 * *(getQ()+3)), *getQ() * *getQ() - *(getQ()+1) 00211 * *(getQ()+1) - *(getQ()+2) * *(getQ()+2) + *(getQ()+3) 00212 * *(getQ()+3)); 00213 pitch *= RAD_TO_DEG; 00214 yaw *= RAD_TO_DEG; 00215 00216 // Declination of SparkFun Electronics (40°05'26.6"N 105°11'05.9"W) is 00217 // 8° 30' E ± 0° 21' (or 8.5°) on 2016-07-19 00218 // 1° 46' E 2021-03-27 00219 // - http://www.ngdc.noaa.gov/geomag-web/#declination 00220 yaw -= 1.7666; 00221 roll *= RAD_TO_DEG; 00222 00223 00224 sprintf(msg,"Yaw %f, Pitch %f, Roll %f\n ",yaw,pitch,roll); 00225 pc.write(msg, strlen(msg)); 00226 sumCount = 0; 00227 sum = 0; 00228 } 00229 i++; 00230 //thread_sleep_for(200); 00231 } 00232 return 0; 00233 }
Generated on Sun Jul 17 2022 16:50:25 by
1.7.2