Team Walter / Mbed 2 deprecated Example

Dependencies:   mbed BMI160 max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 **********************************************************************/
00032 
00033 
00034 #include "mbed.h"
00035 #include "max32630fthr.h"
00036 #include "bmi160.h"
00037 
00038 
00039 void dumpImuRegisters(BMI160 &imu);
00040 void printRegister(BMI160 &imu, BMI160::Registers reg);
00041 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg);
00042 
00043 
00044 int main()
00045 {
00046     //MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00047     
00048     DigitalOut rLED(LED1, LED_ON);
00049     DigitalOut gLED(LED2, LED_ON);
00050     DigitalOut bLED(LED3, LED_ON);
00051     
00052     //for(int i = 0; i < 10; i++) 
00053 //    {
00054 //        
00055 //        rLED = 1;
00056 //        wait(1.0);
00057 //        rLED = 0;
00058 //        wait(1.0);
00059 //        
00060 //        printf("Blinking time %d \n", i);
00061 //        
00062 //        }
00063 //    
00064 //    rLED = 0;
00065 //    bLED = 1;
00066 //    
00067 //    printf("The end");
00068 //    
00069     I2C i2cBus(P5_7, P6_0);
00070     i2cBus.frequency(400000);
00071     BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
00072     
00073     printf("\033[H");  //home
00074     printf("\033[0J");  //erase from cursor to end of screen
00075     
00076     uint32_t failures = 0;
00077     
00078     if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
00079     {
00080         printf("Failed to set gyroscope power mode\n");
00081         failures++;
00082     }
00083     wait_ms(100);
00084     
00085     if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
00086     {
00087         printf("Failed to set accelerometer power mode\n");
00088         failures++;
00089     }
00090     wait_ms(100);
00091     
00092     
00093     BMI160::AccConfig accConfig;
00094 //    example of using getSensorConfig
00095     if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
00096     {
00097         printf("ACC Range = %d\n", accConfig.range);
00098         printf("ACC UnderSampling = %d\n", accConfig.us);
00099         printf("ACC BandWidthParam = %d\n", accConfig.bwp);
00100         printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
00101     }
00102     else
00103     {
00104         printf("Failed to get accelerometer configuration\n");
00105         failures++;
00106     }
00107     
00108 //    example of setting user defined configuration
00109     accConfig.range = BMI160::SENS_4G;
00110     accConfig.us = BMI160::ACC_US_OFF;
00111     accConfig.bwp = BMI160::ACC_BWP_2;
00112     accConfig.odr = BMI160::ACC_ODR_8;
00113     if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
00114     {
00115         printf("ACC Range = %d\n", accConfig.range);
00116         printf("ACC UnderSampling = %d\n", accConfig.us);
00117         printf("ACC BandWidthParam = %d\n", accConfig.bwp);
00118         printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
00119     }
00120     else
00121     {
00122         printf("Failed to set accelerometer configuration\n");
00123         failures++;
00124     }
00125     
00126     BMI160::GyroConfig gyroConfig;
00127     if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR)
00128     {
00129         printf("GYRO Range = %d\n", gyroConfig.range);
00130         printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp);
00131         printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr);
00132     }
00133     else
00134     {
00135         printf("Failed to get gyroscope configuration\n");
00136         failures++;
00137     }
00138     
00139     wait(1.0);
00140     printf("\033[H");  //home
00141     printf("\033[0J");  //erase from cursor to end of screen
00142     
00143     if(failures == 0)
00144     {
00145 //        flo//at imuTemperature; 
00146         BMI160::SensorData accData;
00147         BMI160::SensorData gyroData;
00148         BMI160::SensorTime sensorTime;
00149         
00150         while(1)
00151         {
00152             imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
00153             imu.getTemperature(&imuTemperature);
00154             
00155             printf("ACC xAxis = %s%4.3f\n", "\033[K", accData.xAxis.scaled);
00156             printf("ACC yAxis = %s%4.3f\n", "\033[K", accData.yAxis.scaled);
00157             printf("ACC zAxis = %s%4.3f\n\n", "\033[K", accData.zAxis.scaled);
00158             
00159             printf("GYRO xAxis = %s%5.1f\n", "\033[K", gyroData.xAxis.scaled);
00160             printf("GYRO yAxis = %s%5.1f\n", "\033[K", gyroData.yAxis.scaled);
00161             printf("GYRO zAxis = %s%5.1f\n\n", "\033[K", gyroData.zAxis.scaled);
00162             
00163             printf("Sensor Time = %s%f\n", "\033[K", sensorTime.seconds);
00164             printf("Sensor Temperature = %s%5.3f\n", "\033[K", imuTemperature);
00165             
00166             printf("\033[H");  //home
00167             gLED = !gLED;
00168         }
00169     }
00170     else
00171     {
00172         while(1)
00173         {
00174             rLED = !rLED;
00175             wait(0.25);
00176         }
00177     }
00178 }
00179 
00180 
00181 //*****************************************************************************
00182 void dumpImuRegisters(BMI160 &imu)
00183 {
00184     printRegister(imu, BMI160::CHIP_ID);
00185     printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA);
00186     printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1);
00187     printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST);
00188     printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1);
00189     printRegister(imu, BMI160::CMD);
00190     printf("\n");
00191 }
00192 
00193 
00194 //*****************************************************************************
00195 void printRegister(BMI160 &imu, BMI160::Registers reg)
00196 {
00197     uint8_t data;
00198     if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR)
00199     {
00200         printf("IMU Register 0x%02x = 0x%02x\n", reg, data);
00201     }
00202     else
00203     {
00204         printf("Failed to read register\n");
00205     }
00206 }
00207 
00208 
00209 //*****************************************************************************
00210 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg)
00211 {
00212     uint8_t numBytes = ((stopReg - startReg) + 1);
00213     uint8_t buff[numBytes];
00214     uint8_t offset = static_cast<uint8_t>(startReg);
00215     
00216     if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR)
00217     {
00218         for(uint8_t idx = offset; idx < (numBytes + offset); idx++)
00219         {
00220             printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]);
00221         }
00222     }
00223     else
00224     {
00225         printf("Failed to read block\n");
00226     }
00227 }