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.
Dependencies: mbed BMI160 max32630fthr_pitch USBDevice Math
Orientation.cpp
00001 #include "mbed.h" 00002 #include "bmi160.h" 00003 #include "max32630fthr.h" 00004 #include "USBSerial.h" 00005 #include "stdlib.h" 00006 #include "math.h" 00007 #include "Orientation.h" 00008 00009 I2C i2cBus(I2C2_SDA, I2C2_SCL); 00010 BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO); 00011 00012 BMI160::SensorData accData; 00013 BMI160::SensorData gyroData; 00014 BMI160::SensorTime sensorTime; 00015 BMI160::AccConfig accConfig; 00016 BMI160::GyroConfig gyroConfig; 00017 float imuTemperature; 00018 00019 00020 Serial pc2(P2_1, P2_0); 00021 00022 void Orientation::init(){ 00023 pitch = 0; 00024 k = 0.65; 00025 time1 = sensorTime.seconds; 00026 time2 = sensorTime.seconds; 00027 00028 00029 i2cBus.frequency(400000); 00030 writeReg(imu, BMI160::GYR_RANGE, BMI160::DPS_500); 00031 writeReg(imu, BMI160::GYR_CONF, BMI160::GYRO_ODR_13); 00032 writeReg(imu, BMI160::FOC_CONF, BMI160::FOC_VALUE ); 00033 wait(0.5); 00034 writeReg(imu, BMI160::OFFSET_6, BMI160::FOC_ENABLE_GYR_ACC); 00035 00036 writeReg(imu, BMI160::CMD, BMI160::FOC_START); 00037 00038 00039 pc2.printf("\033[H"); //home 00040 pc2.printf("\033[0J"); //erase from cursor to end of screen 00041 00042 uint32_t failures = 0; 00043 00044 if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) 00045 { 00046 pc2.printf("Failed to set gyroscope power mode\r\n"); 00047 failures++; 00048 } 00049 wait_ms(100); 00050 00051 if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) 00052 { 00053 pc2.printf("Failed to set accelerometer power mode\r\n"); 00054 failures++; 00055 } 00056 wait_ms(100); 00057 00058 00059 00060 //example of using getSensorConfig 00061 if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) 00062 { 00063 pc2.printf("ACC Range = %d\r\n", accConfig.range); 00064 pc2.printf("ACC UnderSampling = %d\r\n", accConfig.us); 00065 pc2.printf("ACC BandWidthParam = %d\r\n", accConfig.bwp); 00066 pc2.printf("ACC OutputDataRate = %d\r\n\r\n", accConfig.odr); 00067 } 00068 else 00069 { 00070 pc2.printf("Failed to get accelerometer configuration\r\n"); 00071 failures++; 00072 } 00073 00074 //example of setting user defined configuration 00075 accConfig.range = BMI160::SENS_2G; //rage is 2g 00076 accConfig.us = BMI160::ACC_US_OFF; //undersampling is off 00077 accConfig.bwp = BMI160::ACC_BWP_0; //average 4 cycles 00078 accConfig.odr = BMI160::ACC_ODR_9; //output data rate 00079 if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) 00080 { 00081 pc2.printf("ACC Range = %d\r\n", accConfig.range); 00082 pc2.printf("ACC UnderSampling = %d\r\n", accConfig.us); 00083 pc2.printf("ACC BandWidthParam = %d\r\n", accConfig.bwp); 00084 pc2.printf("ACC OutputDataRate = %d\r\n\r\n", accConfig.odr); 00085 } 00086 else 00087 { 00088 pc2.printf("Failed to set accelerometer configuration\r\n"); 00089 failures++; 00090 } 00091 00092 if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR) 00093 { 00094 pc2.printf("GYRO Range = %d\r\n", gyroConfig.range); 00095 pc2.printf("GYRO BandWidthParam = %d\r\n", gyroConfig.bwp); 00096 pc2.printf("GYRO OutputDataRate = %d\r\n\r\n", gyroConfig.odr); 00097 00098 00099 } 00100 else 00101 { 00102 pc2.printf("Failed to get gyroscope configuration\r\n"); 00103 failures++; 00104 } 00105 00106 wait(1.0); 00107 00108 } 00109 00110 00111 float Orientation::getPitch() 00112 { 00113 return pitch; 00114 } 00115 00116 void Orientation::updatePitch() 00117 { 00118 imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range); 00119 imu.getTemperature(&imuTemperature); 00120 00121 time2 = sensorTime.seconds; 00122 pitch = compFilter(k, pitch, gyroData.xAxis.scaled, accData.yAxis.scaled, accData.zAxis.scaled, time2 - time1); 00123 //daplink.printf("Forward: %s%4.3f\r\n", "\033[K", apitch); 00124 time1 = time2; 00125 // return pitch; 00126 printRegister(imu, BMI160::GYR_CONF); 00127 } 00128 00129 //***************************************************************************** 00130 void dumpImuRegisters(BMI160 &imu) 00131 { 00132 printRegister(imu, BMI160::CHIP_ID); 00133 printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA); 00134 printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1); 00135 printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST); 00136 printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1); 00137 printRegister(imu, BMI160::CMD); 00138 // pc.printf("\r\n"); 00139 } 00140 00141 00142 //***************************************************************************** 00143 void printRegister(BMI160 &imu, BMI160::Registers reg) 00144 { 00145 uint8_t data; 00146 if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR) 00147 { 00148 // pc.printf("IMU Register 0x%02x = 0x%02x\r\n", reg, data); 00149 // daplink.printf("IMU Register 0x%02x = 0x%02x\r\n", reg, data); 00150 } 00151 else 00152 { 00153 // pc.printf("Failed to read register\r\n"); 00154 } 00155 } 00156 00157 //***************************************************************************** 00158 void writeReg(BMI160 &imu, BMI160::Registers reg, uint8_t data) 00159 { 00160 imu.writeRegister(reg, data); 00161 00162 00163 } 00164 00165 00166 //***************************************************************************** 00167 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg) 00168 { 00169 uint8_t numBytes = ((stopReg - startReg) + 1); 00170 uint8_t buff[32]; 00171 uint8_t offset = static_cast<uint8_t>(startReg); 00172 00173 if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR) 00174 { 00175 for(uint8_t idx = offset; idx < (numBytes + offset); idx++) 00176 { 00177 // pc.printf("IMU Register 0x%02x = 0x%02x\r\n", idx, buff[idx - offset]); 00178 } 00179 } 00180 else 00181 { 00182 // pc.printf("Failed to read block\r\n"); 00183 } 00184 } 00185 00186 00187 00188 float compFilter(float K, float pitch, float gyroX, float accY, float accZ,float DT) 00189 { 00190 return ((K * (pitch + (gyroX * DT))) + ((1.0F - K) * ((180.0F / 3.1459F) * atan(accY/accZ)))); 00191 }
Generated on Fri Jul 15 2022 04:03:02 by
1.7.2