![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Example
Dependencies: mbed BMI160 max32630fthr
main.cpp@4:3d7fae7f7b75, 2016-12-20 (annotated)
- Committer:
- j3
- Date:
- Tue Dec 20 23:59:35 2016 +0000
- Revision:
- 4:3d7fae7f7b75
- Parent:
- 3:250503cb7cb3
- Child:
- 5:4deb38e46a10
Outputting both acc and gyro data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:0db9a7ed2e63 | 1 | /********************************************************************** |
j3 | 0:0db9a7ed2e63 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:0db9a7ed2e63 | 3 | * |
j3 | 0:0db9a7ed2e63 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:0db9a7ed2e63 | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:0db9a7ed2e63 | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:0db9a7ed2e63 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:0db9a7ed2e63 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:0db9a7ed2e63 | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:0db9a7ed2e63 | 10 | * |
j3 | 0:0db9a7ed2e63 | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:0db9a7ed2e63 | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:0db9a7ed2e63 | 13 | * |
j3 | 0:0db9a7ed2e63 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:0db9a7ed2e63 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:0db9a7ed2e63 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:0db9a7ed2e63 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:0db9a7ed2e63 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:0db9a7ed2e63 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:0db9a7ed2e63 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:0db9a7ed2e63 | 21 | * |
j3 | 0:0db9a7ed2e63 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:0db9a7ed2e63 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:0db9a7ed2e63 | 24 | * Products, Inc. Branding Policy. |
j3 | 0:0db9a7ed2e63 | 25 | * |
j3 | 0:0db9a7ed2e63 | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:0db9a7ed2e63 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:0db9a7ed2e63 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:0db9a7ed2e63 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:0db9a7ed2e63 | 30 | * ownership rights. |
j3 | 0:0db9a7ed2e63 | 31 | **********************************************************************/ |
j3 | 0:0db9a7ed2e63 | 32 | |
j3 | 0:0db9a7ed2e63 | 33 | |
j3 | 0:0db9a7ed2e63 | 34 | #include "mbed.h" |
j3 | 0:0db9a7ed2e63 | 35 | #include "SDFileSystem.h" |
j3 | 0:0db9a7ed2e63 | 36 | #include "max32630fthr.h" |
j3 | 0:0db9a7ed2e63 | 37 | #include "bmi160.h" |
j3 | 0:0db9a7ed2e63 | 38 | |
j3 | 1:a3fa54415b4e | 39 | |
j3 | 0:0db9a7ed2e63 | 40 | void dumpImuRegisters(BMI160 &imu); |
j3 | 0:0db9a7ed2e63 | 41 | void printRegister(BMI160 &imu, BMI160::Registers reg); |
j3 | 1:a3fa54415b4e | 42 | void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg); |
j3 | 1:a3fa54415b4e | 43 | |
j3 | 0:0db9a7ed2e63 | 44 | |
j3 | 0:0db9a7ed2e63 | 45 | int main() |
j3 | 0:0db9a7ed2e63 | 46 | { |
j3 | 1:a3fa54415b4e | 47 | MAX32630FTHR pegasus; |
j3 | 1:a3fa54415b4e | 48 | pegasus.init(MAX32630FTHR::VIO_3V3); |
j3 | 0:0db9a7ed2e63 | 49 | |
j3 | 0:0db9a7ed2e63 | 50 | DigitalOut rLED(LED1, LED_OFF); |
j3 | 0:0db9a7ed2e63 | 51 | DigitalOut gLED(LED2, LED_OFF); |
j3 | 4:3d7fae7f7b75 | 52 | DigitalOut bLED(LED3, LED_OFF); |
j3 | 0:0db9a7ed2e63 | 53 | |
j3 | 1:a3fa54415b4e | 54 | I2C i2cBus(P5_7, P6_0); |
j3 | 3:250503cb7cb3 | 55 | i2cBus.frequency(400000); |
j3 | 1:a3fa54415b4e | 56 | BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO); |
j3 | 0:0db9a7ed2e63 | 57 | |
j3 | 1:a3fa54415b4e | 58 | imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL); |
j3 | 1:a3fa54415b4e | 59 | wait_ms(100); |
j3 | 1:a3fa54415b4e | 60 | imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL); |
j3 | 2:0d7433075663 | 61 | wait_ms(100); |
j3 | 1:a3fa54415b4e | 62 | |
j3 | 4:3d7fae7f7b75 | 63 | uint32_t failures = 0; |
j3 | 4:3d7fae7f7b75 | 64 | |
j3 | 4:3d7fae7f7b75 | 65 | printf("\033[H"); //home |
j3 | 4:3d7fae7f7b75 | 66 | printf("\033[0J"); //erase from cursor to end of screen |
j3 | 4:3d7fae7f7b75 | 67 | |
j3 | 4:3d7fae7f7b75 | 68 | BMI160::AccConfig accConfig; |
j3 | 4:3d7fae7f7b75 | 69 | //example of using getSensorConfig |
j3 | 4:3d7fae7f7b75 | 70 | if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) |
j3 | 4:3d7fae7f7b75 | 71 | { |
j3 | 4:3d7fae7f7b75 | 72 | printf("ACC Range = %d\n", accConfig.range); |
j3 | 4:3d7fae7f7b75 | 73 | printf("ACC UnderSampling = %d\n", accConfig.us); |
j3 | 4:3d7fae7f7b75 | 74 | printf("ACC BandWidthParam = %d\n", accConfig.bwp); |
j3 | 4:3d7fae7f7b75 | 75 | printf("ACC OutputDataRate = %d\n\n", accConfig.odr); |
j3 | 4:3d7fae7f7b75 | 76 | } |
j3 | 4:3d7fae7f7b75 | 77 | else |
j3 | 4:3d7fae7f7b75 | 78 | { |
j3 | 4:3d7fae7f7b75 | 79 | printf("Failed to get accelerometer configuration\n"); |
j3 | 4:3d7fae7f7b75 | 80 | failures++; |
j3 | 4:3d7fae7f7b75 | 81 | } |
j3 | 1:a3fa54415b4e | 82 | |
j3 | 4:3d7fae7f7b75 | 83 | //example of setting user defined configuration |
j3 | 4:3d7fae7f7b75 | 84 | accConfig.range = BMI160::SENS_4G; |
j3 | 4:3d7fae7f7b75 | 85 | accConfig.us = BMI160::ACC_US_OFF; |
j3 | 4:3d7fae7f7b75 | 86 | accConfig.bwp = BMI160::ACC_BWP_2; |
j3 | 4:3d7fae7f7b75 | 87 | accConfig.odr = BMI160::ACC_ODR_8; |
j3 | 4:3d7fae7f7b75 | 88 | if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) |
j3 | 4:3d7fae7f7b75 | 89 | { |
j3 | 4:3d7fae7f7b75 | 90 | printf("ACC Range = %d\n", accConfig.range); |
j3 | 4:3d7fae7f7b75 | 91 | printf("ACC UnderSampling = %d\n", accConfig.us); |
j3 | 4:3d7fae7f7b75 | 92 | printf("ACC BandWidthParam = %d\n", accConfig.bwp); |
j3 | 4:3d7fae7f7b75 | 93 | printf("ACC OutputDataRate = %d\n\n", accConfig.odr); |
j3 | 4:3d7fae7f7b75 | 94 | } |
j3 | 4:3d7fae7f7b75 | 95 | else |
j3 | 4:3d7fae7f7b75 | 96 | { |
j3 | 4:3d7fae7f7b75 | 97 | printf("Failed to set accelerometer configuration\n"); |
j3 | 4:3d7fae7f7b75 | 98 | failures++; |
j3 | 4:3d7fae7f7b75 | 99 | } |
j3 | 4:3d7fae7f7b75 | 100 | |
j3 | 4:3d7fae7f7b75 | 101 | BMI160::GyroConfig gyroConfig; |
j3 | 4:3d7fae7f7b75 | 102 | if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 103 | { |
j3 | 4:3d7fae7f7b75 | 104 | printf("GYRO Range = %d\n", gyroConfig.range); |
j3 | 4:3d7fae7f7b75 | 105 | printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp); |
j3 | 4:3d7fae7f7b75 | 106 | printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr); |
j3 | 4:3d7fae7f7b75 | 107 | } |
j3 | 4:3d7fae7f7b75 | 108 | else |
j3 | 4:3d7fae7f7b75 | 109 | { |
j3 | 4:3d7fae7f7b75 | 110 | printf("Failed to get gyroscope configuration\n"); |
j3 | 4:3d7fae7f7b75 | 111 | failures++; |
j3 | 4:3d7fae7f7b75 | 112 | } |
j3 | 4:3d7fae7f7b75 | 113 | |
j3 | 4:3d7fae7f7b75 | 114 | wait(5.0); |
j3 | 4:3d7fae7f7b75 | 115 | printf("\033[H"); //home |
j3 | 4:3d7fae7f7b75 | 116 | printf("\033[0J"); //erase from cursor to end of screen |
j3 | 4:3d7fae7f7b75 | 117 | |
j3 | 4:3d7fae7f7b75 | 118 | if(failures == 0) |
j3 | 4:3d7fae7f7b75 | 119 | { |
j3 | 4:3d7fae7f7b75 | 120 | float imuTemperature; |
j3 | 4:3d7fae7f7b75 | 121 | BMI160::SensorData accData; |
j3 | 4:3d7fae7f7b75 | 122 | BMI160::SensorData gyroData; |
j3 | 4:3d7fae7f7b75 | 123 | BMI160::SensorTime sensorTime; |
j3 | 3:250503cb7cb3 | 124 | |
j3 | 4:3d7fae7f7b75 | 125 | while(1) |
j3 | 4:3d7fae7f7b75 | 126 | { |
j3 | 4:3d7fae7f7b75 | 127 | imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range); |
j3 | 4:3d7fae7f7b75 | 128 | imu.getTemperature(&imuTemperature); |
j3 | 4:3d7fae7f7b75 | 129 | |
j3 | 4:3d7fae7f7b75 | 130 | printf("ACC xAxis = %s%4.3f\n", "\033[K", accData.xAxis.scaled); |
j3 | 4:3d7fae7f7b75 | 131 | printf("ACC yAxis = %s%4.3f\n", "\033[K", accData.yAxis.scaled); |
j3 | 4:3d7fae7f7b75 | 132 | printf("ACC zAxis = %s%4.3f\n\n", "\033[K", accData.zAxis.scaled); |
j3 | 4:3d7fae7f7b75 | 133 | |
j3 | 4:3d7fae7f7b75 | 134 | printf("GYRO xAxis = %s%5.1f\n", "\033[K", gyroData.xAxis.scaled); |
j3 | 4:3d7fae7f7b75 | 135 | printf("GYRO yAxis = %s%5.1f\n", "\033[K", gyroData.yAxis.scaled); |
j3 | 4:3d7fae7f7b75 | 136 | printf("GYRO zAxis = %s%5.1f\n\n", "\033[K", gyroData.zAxis.scaled); |
j3 | 4:3d7fae7f7b75 | 137 | |
j3 | 4:3d7fae7f7b75 | 138 | printf("Sensor Time = %s%f\n", "\033[K", sensorTime.seconds); |
j3 | 4:3d7fae7f7b75 | 139 | printf("Sensor Temperature = %s%5.3f\n", "\033[K", imuTemperature); |
j3 | 4:3d7fae7f7b75 | 140 | |
j3 | 4:3d7fae7f7b75 | 141 | printf("\033[H"); //home |
j3 | 4:3d7fae7f7b75 | 142 | gLED = !gLED; |
j3 | 4:3d7fae7f7b75 | 143 | } |
j3 | 4:3d7fae7f7b75 | 144 | } |
j3 | 4:3d7fae7f7b75 | 145 | else |
j3 | 4:3d7fae7f7b75 | 146 | { |
j3 | 4:3d7fae7f7b75 | 147 | while(1) |
j3 | 4:3d7fae7f7b75 | 148 | { |
j3 | 4:3d7fae7f7b75 | 149 | rLED = !rLED; |
j3 | 4:3d7fae7f7b75 | 150 | wait(0.25); |
j3 | 4:3d7fae7f7b75 | 151 | } |
j3 | 0:0db9a7ed2e63 | 152 | } |
j3 | 0:0db9a7ed2e63 | 153 | } |
j3 | 0:0db9a7ed2e63 | 154 | |
j3 | 0:0db9a7ed2e63 | 155 | |
j3 | 0:0db9a7ed2e63 | 156 | //***************************************************************************** |
j3 | 0:0db9a7ed2e63 | 157 | void dumpImuRegisters(BMI160 &imu) |
j3 | 0:0db9a7ed2e63 | 158 | { |
j3 | 0:0db9a7ed2e63 | 159 | printRegister(imu, BMI160::CHIP_ID); |
j3 | 1:a3fa54415b4e | 160 | printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA); |
j3 | 1:a3fa54415b4e | 161 | printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1); |
j3 | 1:a3fa54415b4e | 162 | printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST); |
j3 | 1:a3fa54415b4e | 163 | printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1); |
j3 | 0:0db9a7ed2e63 | 164 | printRegister(imu, BMI160::CMD); |
j3 | 0:0db9a7ed2e63 | 165 | printf("\n"); |
j3 | 0:0db9a7ed2e63 | 166 | } |
j3 | 0:0db9a7ed2e63 | 167 | |
j3 | 0:0db9a7ed2e63 | 168 | |
j3 | 0:0db9a7ed2e63 | 169 | //***************************************************************************** |
j3 | 0:0db9a7ed2e63 | 170 | void printRegister(BMI160 &imu, BMI160::Registers reg) |
j3 | 0:0db9a7ed2e63 | 171 | { |
j3 | 0:0db9a7ed2e63 | 172 | uint8_t data; |
j3 | 1:a3fa54415b4e | 173 | if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 174 | { |
j3 | 0:0db9a7ed2e63 | 175 | printf("IMU Register 0x%02x = 0x%02x\n", reg, data); |
j3 | 0:0db9a7ed2e63 | 176 | } |
j3 | 0:0db9a7ed2e63 | 177 | else |
j3 | 0:0db9a7ed2e63 | 178 | { |
j3 | 0:0db9a7ed2e63 | 179 | printf("Failed to read register\n"); |
j3 | 0:0db9a7ed2e63 | 180 | } |
j3 | 0:0db9a7ed2e63 | 181 | } |
j3 | 0:0db9a7ed2e63 | 182 | |
j3 | 0:0db9a7ed2e63 | 183 | |
j3 | 0:0db9a7ed2e63 | 184 | //***************************************************************************** |
j3 | 1:a3fa54415b4e | 185 | void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg) |
j3 | 0:0db9a7ed2e63 | 186 | { |
j3 | 0:0db9a7ed2e63 | 187 | uint8_t numBytes = ((stopReg - startReg) + 1); |
j3 | 1:a3fa54415b4e | 188 | uint8_t buff[numBytes]; |
j3 | 0:0db9a7ed2e63 | 189 | uint8_t offset = static_cast<uint8_t>(startReg); |
j3 | 0:0db9a7ed2e63 | 190 | |
j3 | 1:a3fa54415b4e | 191 | if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 192 | { |
j3 | 0:0db9a7ed2e63 | 193 | for(uint8_t idx = offset; idx < (numBytes + offset); idx++) |
j3 | 0:0db9a7ed2e63 | 194 | { |
j3 | 0:0db9a7ed2e63 | 195 | printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]); |
j3 | 0:0db9a7ed2e63 | 196 | } |
j3 | 0:0db9a7ed2e63 | 197 | } |
j3 | 0:0db9a7ed2e63 | 198 | else |
j3 | 0:0db9a7ed2e63 | 199 | { |
j3 | 0:0db9a7ed2e63 | 200 | printf("Failed to read block\n"); |
j3 | 0:0db9a7ed2e63 | 201 | } |
j3 | 0:0db9a7ed2e63 | 202 | } |