You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
957 lines
36 KiB
957 lines
36 KiB
#include "BNO055.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "BNO055", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "BNO055", this, _LOG_ERROR, X )
|
|
|
|
const char * BNO_ERROR_MESSAGES[11] = {
|
|
"No error",
|
|
"Peripheral initialization error",
|
|
"System initialization error",
|
|
"Self test result failed",
|
|
"Register map value out of range",
|
|
"Register map address out of range",
|
|
"Register map write error",
|
|
"BNO low power mode not available for selected operation mode",
|
|
"Accelerometer power mode not available",
|
|
"Fusion algorithm configuration error",
|
|
"Sensor configuration error"
|
|
};
|
|
const char * BNO_STATUS_MESSAGES[7] = {
|
|
"System idle",
|
|
"System Error",
|
|
"Initializing peripherals",
|
|
"System Initialization",
|
|
"Executing selftest",
|
|
"Sensor fusion algorithm running",
|
|
"System running without fusion algorithm"
|
|
};
|
|
|
|
const uint32_t BNO_OUTPUT_DATA_DELAY[12] = {
|
|
1000,
|
|
10,
|
|
50,
|
|
10,
|
|
20,
|
|
100,
|
|
20,
|
|
10,
|
|
50,
|
|
20,
|
|
10,
|
|
10
|
|
};
|
|
|
|
sensorBNO::sensorBNO( config_setting_t *Settings, PATHFINDER_DBC *DBC ): basicThread(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "BNO055" );
|
|
m_pBLOG = objectPathfinderBinaryLog::getInstance();
|
|
m_pBus = NULL;
|
|
m_pDBC = DBC;
|
|
m_pDevicePath = "/dev/i2c-1";
|
|
m_bDeviceAddress = 0x29;
|
|
m_nSampleDelay = 100;
|
|
m_bTargetWM = BNO_WM_CONFIG;
|
|
m_bActualWM = 0;
|
|
memset( &m_sSettings, 0 , sizeof(BNO_SETTINGS) );
|
|
m_sSettings.wmode = BNO_WM_IMU;
|
|
m_sSettings.map_x = 1;
|
|
m_sSettings.map_y = 2;
|
|
m_sSettings.map_z = 3;
|
|
|
|
memset( &m_sID, 0, sizeof(BNO_ID_MAP) );
|
|
memset( &m_sDATA, 0, sizeof(BNO_DATA_MAP) );
|
|
memset( &m_sSTATUS, 0, sizeof(BNO_STATUS_MAP) );
|
|
memset( &m_sCONFIG0, 0, sizeof(BNO_CONFIG0_MAP) );
|
|
memset( &m_sCONFIG1, 0, sizeof(BNO_CONFIG1_MAP) );
|
|
};
|
|
|
|
sensorBNO::~sensorBNO(){
|
|
waitThreadEnd( true );
|
|
if(m_pBus != NULL ) delete( m_pBus );
|
|
};
|
|
|
|
void sensorBNO::logConfig(){
|
|
char tBuff[256];
|
|
sprintf( tBuff, "OPR_MODE: 0x%02X", m_sCONFIG0.OP_MODE );
|
|
LOG( tBuff );
|
|
switch( m_sCONFIG0.OP_MODE & BNO_WMODE_MASK ) {
|
|
case BNO_WM_CONFIG: LOG("CHIP WM is CONFIG"); break;
|
|
case BNO_WM_ACC_ONLY: LOG("CHIP WM is Acc Only"); break;
|
|
case BNO_WM_MAG_ONLY: LOG("CHIP WM is Magnetometer Only"); break;
|
|
case BNO_WM_GYR_ONLY: LOG("CHIP WM is Gyroscope Only"); break;
|
|
case BNO_WM_ACC_MAG: LOG("CHIP WM is Acc and Magnetometer"); break;
|
|
case BNO_WM_ACC_GYR: LOG("CHIP WM is Acc and Gyroscope"); break;
|
|
case BNO_WM_MAG_GYR: LOG("CHIP WM is Magnetometer and Gyroscope"); break;
|
|
case BNO_WM_AMG_NO_FUSION: LOG("CHIP WM is All sensors - No Fusion!"); break;
|
|
case BNO_WM_IMU: LOG("CHIP WM is Fusion IMU"); break;
|
|
case BNO_WM_COMPAS: LOG("CHIP WM is Fusion Compas"); break;
|
|
case BNO_WM_M4G: LOG("CHIP WM is Fusion Magnetometer and Gyroscope"); break;
|
|
case BNO_WM_NDOF_FMC_OFF: LOG("CHIP WM is Fusion NDOF no fast Mag calibartion"); break;
|
|
case BNO_WM_NDOF: LOG("CHIP WM is Fusion NDOF"); break;
|
|
default: LOG_ERROR("CHIP WM is Unknown!");
|
|
};
|
|
switch( m_eAccWorkMode ) {
|
|
case BNO_ACC_NORMAL: LOG("ACC WM is Normal"); break;
|
|
case BNO_ACC_SUSPEND: LOG("ACC WM is Suspended"); break;
|
|
case BNO_ACC_LP1: LOG("ACC WM is Low Power 1"); break;
|
|
case BNO_ACC_STANDBY: LOG("ACC WM is Standby"); break;
|
|
case BNO_ACC_LP2: LOG("ACC WM is Low Power 2"); break;
|
|
case BNO_ACC_DEEP_SUSPEND: LOG("ACC WM is Deep Suspend"); break;
|
|
};
|
|
switch( m_eAccRange ) {
|
|
case BNO_ACC_2G: LOG("ACC Range is 2G"); break;
|
|
case BNO_ACC_4G: LOG("ACC Range is 4G"); break;
|
|
case BNO_ACC_8G: LOG("ACC Range is 8G"); break;
|
|
case BNO_ACC_16G: LOG("ACC Range is 16G"); break;
|
|
};
|
|
switch( m_eAccBandwidth ) {
|
|
case BNO_ACC_7_81HZ: LOG("ACC Bandwidth is 7.81Hz"); break;
|
|
case BNO_ACC_15_36HZ: LOG("ACC Bandwidth is 15.36Hz"); break;
|
|
case BNO_ACC_31_25HZ: LOG("ACC Bandwidth is 31.25Hz"); break;
|
|
case BNO_ACC_62_50HZ: LOG("ACC Bandwidth is 62.5Hz"); break;
|
|
case BNO_ACC_125HZ: LOG("ACC Bandwidth is 125Hz"); break;
|
|
case BNO_ACC_250HZ: LOG("ACC Bandwidth is 250Hz"); break;
|
|
case BNO_ACC_500HZ: LOG("ACC Bandwidth is 500Hz"); break;
|
|
case BNO_ACC_1000HZ: LOG("ACC Bandwidth is 1KHz"); break;
|
|
};
|
|
};
|
|
|
|
bool sensorBNO::updateSettings(){
|
|
if( m_pDevicePath == NULL ) return false;
|
|
if( m_pBus == NULL ) return false;
|
|
if( m_pDBC!= NULL ) {
|
|
if( !m_pDBC->getSettingsIMU( &m_sSettings ) ) return false;
|
|
};
|
|
m_bTargetWM = BNO_WM_CONFIG;
|
|
setMode( BNO_WM_CONFIG );
|
|
LOG("Mode Set to Config");
|
|
/* Reset */
|
|
LOG("Trigger Reset");
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x20 );
|
|
/* Delay incrased to 30ms due to power issues https://tinyurl.com/y375z699 */
|
|
SLEEP(1000);
|
|
LOG("Read Chip ID");
|
|
while( m_pBus->read8( BNO055_CHIP_ID_ADDR) != BNO055_ID ){
|
|
SLEEP(10);
|
|
};
|
|
LOG("Done!" );
|
|
SLEEP(50);
|
|
LOG("Set PowerMode");
|
|
/* Set to normal power mode */
|
|
m_pBus->write8( BNO055_PWR_MODE_ADDR, BNO_PM_NORMAL );
|
|
SLEEP(10);
|
|
LOG("Set Page 0");
|
|
m_pBus->write8( BNO055_PAGE_ID_ADDR, 0 );
|
|
/* Set the output units */
|
|
|
|
uint8_t unitsel = (0 << 7) | // Orientation = Android
|
|
(0 << 4) | // Temperature = Celsius
|
|
(0 << 2) | // Euler = Degrees
|
|
(1 << 1) | // Gyro = Rads
|
|
(0 << 0); // Accelerometer = m/s^2
|
|
m_pBus->write8(BNO055_UNIT_SEL_ADDR, unitsel);
|
|
|
|
|
|
/* Configure axis mapping (see section 3.4) */
|
|
uint8_t axis_map_cfg = 0;
|
|
axis_map_cfg = ((m_sSettings.map_z & 3) << 4) | ((m_sSettings.map_y & 3) << 2) | (m_sSettings.map_x & 3);
|
|
m_pBus->write8(BNO055_AXIS_MAP_CONFIG_ADDR, axis_map_cfg );//REMAP_CONFIG_P2); // P0-P7, Default is P1
|
|
SLEEP(10);
|
|
uint8_t axis_sign_cfg = 0;
|
|
axis_sign_cfg = (m_sSettings.map_x & 4) | ((m_sSettings.map_y & 4) >> 1) || ((m_sSettings.map_x & 4) >> 2);
|
|
m_pBus->write8(BNO055_AXIS_MAP_SIGN_ADDR, axis_sign_cfg );//REMAP_SIGN_P2); // P0-P7, Default is P1
|
|
SLEEP(10);
|
|
LOG("Clear Triggers");
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x0 );
|
|
SLEEP( 10 );
|
|
LOG("Start WM");
|
|
setMode( m_sSettings.wmode );
|
|
SLEEP(20);
|
|
m_bTargetWM = m_sSettings.wmode;
|
|
return true;
|
|
};
|
|
|
|
bool sensorBNO::create(){
|
|
if( m_pDevicePath == NULL ) return false;
|
|
if( m_pBus != NULL ) delete( m_pBus );
|
|
m_pBus = new i2cPort( m_pDevicePath, m_bDeviceAddress );
|
|
if( m_pBus != NULL ) {
|
|
if( m_pDBC!= NULL ) {
|
|
if( m_pDBC->getSettingsIMU( &m_sSettings ) ){
|
|
m_bTargetWM = m_sSettings.wmode;
|
|
};
|
|
};
|
|
LOG("Read Chip ID");
|
|
uint8_t id = m_pBus->read8( BNO055_CHIP_ID_ADDR );
|
|
if( id != BNO055_ID ){
|
|
SLEEP( 1000 ); // hold on for boot
|
|
id = m_pBus->read8( BNO055_CHIP_ID_ADDR );
|
|
if( id != BNO055_ID ){
|
|
return false; // still not? ok -> fail
|
|
};
|
|
};
|
|
LOG("Chip ID - OK");
|
|
//setExtCrystalUse( true );
|
|
//SLEEP(1000);
|
|
LOG("Mode Seting");
|
|
/* Switch to config mode (just in case since this is the default) */
|
|
setMode( BNO_WM_CONFIG );
|
|
LOG("Mode Set to Config");
|
|
/* Reset */
|
|
LOG("Trigger Reset");
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x20 );
|
|
/* Delay incrased to 30ms due to power issues https://tinyurl.com/y375z699 */
|
|
SLEEP(1000);
|
|
LOG("Read Chip ID");
|
|
while( m_pBus->read8( BNO055_CHIP_ID_ADDR) != BNO055_ID ){
|
|
SLEEP(10);
|
|
};
|
|
LOG("Done!" );
|
|
SLEEP(50);
|
|
LOG("Set PowerMode");
|
|
/* Set to normal power mode */
|
|
m_pBus->write8( BNO055_PWR_MODE_ADDR, BNO_PM_NORMAL );
|
|
SLEEP(10);
|
|
LOG("Set Page 0");
|
|
m_pBus->write8( BNO055_PAGE_ID_ADDR, 0 );
|
|
/* Set the output units */
|
|
|
|
uint8_t unitsel = (0 << 7) | // Orientation = Android
|
|
(0 << 4) | // Temperature = Celsius
|
|
(0 << 2) | // Euler = Degrees
|
|
(1 << 1) | // Gyro = Rads
|
|
(0 << 0); // Accelerometer = m/s^2
|
|
m_pBus->write8(BNO055_UNIT_SEL_ADDR, unitsel);
|
|
|
|
|
|
/* Configure axis mapping (see section 3.4) */
|
|
uint8_t axis_map_cfg = 0;
|
|
axis_map_cfg = ((m_sSettings.map_z & 3) << 4) | ((m_sSettings.map_y & 3) << 2) | (m_sSettings.map_x & 3);
|
|
m_pBus->write8(BNO055_AXIS_MAP_CONFIG_ADDR, axis_map_cfg );//REMAP_CONFIG_P2); // P0-P7, Default is P1
|
|
SLEEP(10);
|
|
uint8_t axis_sign_cfg = 0;
|
|
axis_sign_cfg = (m_sSettings.map_x & 4) | ((m_sSettings.map_y & 4) >> 1) || ((m_sSettings.map_x & 4) >> 2);
|
|
m_pBus->write8(BNO055_AXIS_MAP_SIGN_ADDR, axis_sign_cfg );//REMAP_SIGN_P2); // P0-P7, Default is P1
|
|
SLEEP(10);
|
|
|
|
LOG("Clear Triggers");
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x0 );
|
|
SLEEP( 10 );
|
|
/*
|
|
LOG("Trigger Self Test");
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x01 );
|
|
SLEEP( 5000 );
|
|
LOG("Read Test Results");
|
|
uint8_t tr = m_pBus->read8( BNO055_SELFTEST_RESULT_ADDR );
|
|
if( ( tr & 0x0F ) == 0x0F ) {
|
|
LOG("Self Test Passed");
|
|
char tBuff[64];
|
|
sprintf( tBuff, "Code: 0x%02X", tr );
|
|
LOG( tBuff );
|
|
} else {
|
|
LOG_ERROR("Self Test Failure!");
|
|
char tBuff[64];
|
|
sprintf( tBuff, "Code: 0x%02X", tr );
|
|
LOG( tBuff );
|
|
return false;
|
|
};
|
|
LOG("Clear Triggers");
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x0 );
|
|
SLEEP(10);
|
|
*/
|
|
/* Set the requested operating mode (see section 3.3) */
|
|
LOG("Start WM");
|
|
if( setMode( m_bTargetWM ) ) {
|
|
SLEEP(20);
|
|
return init();
|
|
};
|
|
} else LOG_ERROR("No I2C Bus Created!");
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::validate(){
|
|
if( getStatus( &m_sSTATUS ) ){
|
|
#ifdef DEBUG
|
|
char tBuff[256];
|
|
sprintf( tBuff, "Temperature: %02X", m_sSTATUS.temper );
|
|
LOG( tBuff );
|
|
sprintf( tBuff, "Calibration: %02X", m_sSTATUS.cal_status );
|
|
LOG( tBuff );
|
|
sprintf( tBuff, "ST Result: %02X", m_sSTATUS.st_result );
|
|
LOG( tBuff );
|
|
sprintf( tBuff, "INT Status: %02X", m_sSTATUS.int_status );
|
|
LOG( tBuff );
|
|
sprintf( tBuff, "CLC Status: %02X", m_sSTATUS.sys_clc_status );
|
|
LOG( tBuff );
|
|
sprintf( tBuff, "SYS Status: %02X", m_sSTATUS.system_status );
|
|
LOG( tBuff );
|
|
sprintf( tBuff, "SYS Error: %02X [%s]", m_sSTATUS.system_error, BNO_ERROR_MESSAGES[m_sSTATUS.system_error] );
|
|
LOG( tBuff );
|
|
#endif
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
void sensorBNO::m_fThreadProc( void ){
|
|
m_bThreadStatus = TH_WORKING;
|
|
while( m_bThreadStatus < TH_ENDED ){
|
|
if( m_bTargetWM != 0 ){
|
|
uint8_t S_S, STS, S_E;
|
|
if( m_bActualWM == 0 ) {
|
|
m_bActualWM = getMode();
|
|
if( m_bActualWM != 0 ) continue;
|
|
// Config Procedures
|
|
} else {
|
|
BNO_EVTREC * tDATA;
|
|
tDATA = getEVT_REC();
|
|
if( tDATA != NULL ) {
|
|
if( m_pBLOG != NULL ) {
|
|
m_pBLOG->registerBNO( tDATA );
|
|
};
|
|
if( m_pDBC != NULL ) {
|
|
m_pDBC->updateBNO_DATA( tDATA );
|
|
};
|
|
free( tDATA );
|
|
};
|
|
};
|
|
/*
|
|
if( getSystemStatus( &S_S, &STS, &S_E ) ){
|
|
switch( S_S ) {
|
|
case 0: {
|
|
char tBuffer[64];
|
|
uint8_t tMode =
|
|
sprintf( tBuffer, "Idle Mode %d:%d", tMode, m_eWorkMode );
|
|
LOG(tBuffer);
|
|
sprintf( tBuffer, "Temperature: %d", getTemp() );
|
|
LOG(tBuffer);
|
|
if( isFullyCalibrated() ) {
|
|
LOG("Fully Calibrated!");
|
|
} else {
|
|
LOG("Not Calibrated!");
|
|
|
|
};
|
|
if( tMode != m_eWorkMode ) {
|
|
if( setMode( m_eWorkMode ) ){
|
|
LOG("Mode Changed");
|
|
SLEEP( 200 );
|
|
} else {
|
|
LOG("Mode Changed Failure");
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case 1: LOG("System Error"); break;
|
|
case 2: LOG("Initializing Peripherals"); break;
|
|
case 3: LOG("System Iniitalization"); break;
|
|
case 4: LOG("Executing Self-Test"); break;
|
|
case 5: LOG("Sensor fusion algorithm running"); break;
|
|
case 6: LOG("System running without fusion algorithms"); break;
|
|
};
|
|
}; */
|
|
SLEEP( BNO_OUTPUT_DATA_DELAY[ m_bActualWM ] );
|
|
} else SLEEP( 1000 );
|
|
};
|
|
};
|
|
|
|
/*!
|
|
* @brief Puts the chip in the specified operating mode
|
|
* @param mode
|
|
* mode values
|
|
* [OPERATION_MODE_CONFIG,
|
|
* OPERATION_MODE_ACCONLY,
|
|
* OPERATION_MODE_MAGONLY,
|
|
* OPERATION_MODE_GYRONLY,
|
|
* OPERATION_MODE_ACCMAG,
|
|
* OPERATION_MODE_ACCGYRO,
|
|
* OPERATION_MODE_MAGGYRO,
|
|
* OPERATION_MODE_AMG,
|
|
* OPERATION_MODE_IMUPLUS,
|
|
* OPERATION_MODE_COMPASS,
|
|
* OPERATION_MODE_M4G,
|
|
* OPERATION_MODE_NDOF_FMC_OFF,
|
|
* OPERATION_MODE_NDOF]
|
|
*/
|
|
bool sensorBNO::setMode( uint8_t mode ){
|
|
if( m_pBus != NULL ){
|
|
if( m_pBus->write8( BNO055_OPR_MODE_ADDR, mode ) ) {
|
|
SLEEP( 30 );
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
uint8_t sensorBNO::getMode(){
|
|
uint8_t retVal = 255;
|
|
if( m_pBus != NULL ){
|
|
retVal = m_pBus->read8( BNO055_OPR_MODE_ADDR ) & BNO_WMODE_MASK;
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
/*!
|
|
* @brief Use the external 32.768KHz crystal
|
|
* @param usextal
|
|
* use external crystal boolean
|
|
*/
|
|
bool sensorBNO::setExtCrystalUse( bool usextal) {
|
|
if( m_pBus != NULL ){
|
|
uint8_t tMode = m_bActualWM;
|
|
|
|
/* Switch to config mode (just in case since this is the default) */
|
|
setMode( BNO_WM_CONFIG );
|
|
SLEEP( 25 );
|
|
m_pBus->write8( BNO055_PAGE_ID_ADDR, 0 );
|
|
if( usextal ){
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x80 );
|
|
} else {
|
|
m_pBus->write8( BNO055_SYS_TRIGGER_ADDR, 0x00 );
|
|
};
|
|
SLEEP( 10 );
|
|
/* Set the requested operating mode (see section 3.3) */
|
|
setMode( tMode );
|
|
SLEEP( 20 );
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/*!
|
|
* @brief Gets the latest system status info
|
|
* @param system_status
|
|
* system status info
|
|
* @param self_test_result
|
|
* self test result
|
|
* @param system_error
|
|
* system error info
|
|
*/
|
|
bool sensorBNO::getSystemStatus( uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error ){
|
|
if( m_pBus != NULL ){
|
|
m_pBus->write8( BNO055_PAGE_ID_ADDR, 0 );
|
|
|
|
/* System Status (see section 4.3.58)
|
|
0 = Idle
|
|
1 = System Error
|
|
2 = Initializing Peripherals
|
|
3 = System Iniitalization
|
|
4 = Executing Self-Test
|
|
5 = Sensor fusion algorithm running
|
|
6 = System running without fusion algorithms
|
|
*/
|
|
|
|
if( system_status != NULL ) *system_status = m_pBus->read8(BNO055_SYS_STAT_ADDR);
|
|
|
|
/* Self Test Results
|
|
1 = test passed, 0 = test failed
|
|
Bit 0 = Accelerometer self test
|
|
Bit 1 = Magnetometer self test
|
|
Bit 2 = Gyroscope self test
|
|
Bit 3 = MCU self test
|
|
0x0F = all good!
|
|
*/
|
|
|
|
if( self_test_result != NULL ) *self_test_result = m_pBus->read8(BNO055_SELFTEST_RESULT_ADDR);
|
|
|
|
/* System Error (see section 4.3.59)
|
|
0 = No error
|
|
1 = Peripheral initialization error
|
|
2 = System initialization error
|
|
3 = Self test result failed
|
|
4 = Register map value out of range
|
|
5 = Register map address out of range
|
|
6 = Register map write error
|
|
7 = BNO low power mode not available for selected operat ion mode
|
|
8 = Accelerometer power mode not available
|
|
9 = Fusion algorithm configuration error
|
|
A = Sensor configuration error
|
|
*/
|
|
|
|
if( system_error != NULL ) *system_error = m_pBus->read8(BNO055_SYS_ERR_ADDR);
|
|
SLEEP( 200 );
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
|
|
/*!
|
|
* @brief Gets the chip revision numbers
|
|
* @param info
|
|
* revision info
|
|
*/
|
|
bool sensorBNO::getRevInfo( bno055_rev_info_t *info ){
|
|
if( m_pBus != NULL ){
|
|
uint8_t a, b;
|
|
memset( info, 0, sizeof( bno055_rev_info_t ) );
|
|
/* Check the accelerometer revision */
|
|
info->accel_rev = m_pBus->read8(BNO055_ACCEL_REV_ID_ADDR);
|
|
/* Check the magnetometer revision */
|
|
info->mag_rev = m_pBus->read8(BNO055_MAG_REV_ID_ADDR);
|
|
/* Check the gyroscope revision */
|
|
info->gyro_rev = m_pBus->read8(BNO055_GYRO_REV_ID_ADDR);
|
|
/* Check the SW revision */
|
|
info->bl_rev = m_pBus->read8(BNO055_BL_REV_ID_ADDR);
|
|
a = m_pBus->read8( BNO055_SW_REV_ID_LSB_ADDR );
|
|
b = m_pBus->read8( BNO055_SW_REV_ID_MSB_ADDR );
|
|
info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/*!
|
|
* @brief Gets current calibration state. Each value should be a uint8_t
|
|
* pointer and it will be set to 0 if not calibrated and 3 if
|
|
* fully calibrated.
|
|
* See section 34.3.54
|
|
* @param sys
|
|
* Current system calibration status, depends on status of all sensors,
|
|
* read-only
|
|
* @param gyro
|
|
* Current calibration status of Gyroscope, read-only
|
|
* @param accel
|
|
* Current calibration status of Accelerometer, read-only
|
|
* @param mag
|
|
* Current calibration status of Magnetometer, read-only
|
|
*/
|
|
bool sensorBNO::getCalibration( uint8_t *sys, uint8_t *gyro, uint8_t *accel, uint8_t *mag ){
|
|
if( m_pBus != NULL ){
|
|
uint8_t calData = m_pBus->read8( BNO055_CALIB_STAT_ADDR );
|
|
if( sys != NULL ) *sys = (calData >> 6) & 0x03;
|
|
if( gyro != NULL ) *gyro = (calData >> 4) & 0x03;
|
|
if( accel != NULL ) *accel = (calData >> 2) & 0x03;
|
|
if( mag != NULL ) *mag = calData & 0x03;
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/*!
|
|
* @brief Gets the temperature in degrees celsius
|
|
* @return temperature in degrees celsius
|
|
*/
|
|
int8_t sensorBNO::getTemp(){
|
|
if( m_pBus != NULL ){
|
|
int8_t temp = (int8_t)(m_pBus->read8(BNO055_TEMP_ADDR));
|
|
return temp;
|
|
};
|
|
return -128;
|
|
};
|
|
|
|
/*!
|
|
* @brief Checks of all cal status values are set to 3 (fully calibrated)
|
|
* @return status of calibration
|
|
*/
|
|
bool sensorBNO::isFullyCalibrated(){
|
|
uint8_t system, gyro, accel, mag;
|
|
if( getCalibration( &system, &gyro, &accel, &mag ) ){
|
|
char tBuff[128];
|
|
sprintf( tBuff, "Calibration Sys: %d Gyr: %d Acc: %d Mag: %d", system, gyro, accel, mag );
|
|
LOG( tBuff );
|
|
switch( m_bActualWM ){
|
|
case BNO_WM_ACC_ONLY: return (accel == 3);
|
|
case BNO_WM_MAG_ONLY: return (mag == 3);
|
|
case BNO_WM_GYR_ONLY:
|
|
case BNO_WM_M4G: /* No magnetometer calibration required. */
|
|
return (gyro == 3);
|
|
case BNO_WM_ACC_MAG:
|
|
case BNO_WM_COMPAS:
|
|
return (accel == 3 && mag == 3);
|
|
case BNO_WM_ACC_GYR:
|
|
case BNO_WM_IMU:
|
|
return (accel == 3 && gyro == 3);
|
|
case BNO_WM_MAG_GYR:
|
|
return (mag == 3 && gyro == 3);
|
|
default:
|
|
return (system == 3 && gyro == 3 && accel == 3 && mag == 3);
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/*!
|
|
* @brief Enter Suspend mode (i.e., sleep)
|
|
*/
|
|
bool sensorBNO::enterSuspendMode(){
|
|
if( m_pBus != NULL ){
|
|
uint8_t tMode = m_bActualWM;
|
|
/* Switch to config mode (just in case since this is the default) */
|
|
setMode( BNO_WM_CONFIG );
|
|
SLEEP( 25 );
|
|
m_pBus->write8( BNO055_PWR_MODE_ADDR, 0x02 );
|
|
/* Set the requested operating mode (see section 3.3) */
|
|
setMode( tMode );
|
|
SLEEP( 20 );
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/*!
|
|
* @brief Enter Normal mode (i.e., wake)
|
|
*/
|
|
bool sensorBNO::enterNormalMode(){
|
|
if( m_pBus != NULL ){
|
|
uint8_t tMode = m_bActualWM;
|
|
/* Switch to config mode (just in case since this is the default) */
|
|
setMode( BNO_WM_CONFIG );
|
|
SLEEP( 25 );
|
|
m_pBus->write8( BNO055_PWR_MODE_ADDR, 0x00 );
|
|
/* Set the requested operating mode (see section 3.3) */
|
|
setMode( tMode );
|
|
SLEEP( 20 );
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
|
|
/*!
|
|
* @brief Gets a vector reading from the specified source
|
|
* @param vector_type
|
|
* possible vector type values
|
|
* [VECTOR_ACCELEROMETER
|
|
* VECTOR_MAGNETOMETER
|
|
* VECTOR_GYROSCOPE
|
|
* VECTOR_EULER
|
|
* VECTOR_LINEARACCEL
|
|
* VECTOR_GRAVITY]
|
|
* @return vector from specified source
|
|
*/
|
|
bool sensorBNO::getVector( uint16_t vector_type, BNO_3D_VECTOR *xyz ){
|
|
if( xyz != NULL ) {
|
|
if( m_pBus != NULL ){
|
|
uint8_t buffer[6];
|
|
memset(buffer, 0, 6);
|
|
int16_t x, y, z;
|
|
x = y = z = 0;
|
|
/* Read vector data (6 bytes) */
|
|
if( m_pBus->readMZ( vector_type, buffer, 6 ) ){
|
|
x = ((int16_t)buffer[0]) | (((int16_t)buffer[1]) << 8);
|
|
y = ((int16_t)buffer[2]) | (((int16_t)buffer[3]) << 8);
|
|
z = ((int16_t)buffer[4]) | (((int16_t)buffer[5]) << 8);
|
|
/*!
|
|
* Convert the value to an appropriate range (section 3.6.4)
|
|
* and assign the value to the Vector type
|
|
*/
|
|
switch( vector_type ){
|
|
case VECTOR_MAGNETOMETER:
|
|
/* 1uT = 16 LSB */
|
|
xyz->x = ((double)x) / 16.0;
|
|
xyz->y = ((double)y) / 16.0;
|
|
xyz->z = ((double)z) / 16.0;
|
|
break;
|
|
case VECTOR_GYROSCOPE:
|
|
/* 1dps = 16 LSB */
|
|
xyz->x = ((double)x) / 16.0;
|
|
xyz->y = ((double)y) / 16.0;
|
|
xyz->z = ((double)z) / 16.0;
|
|
break;
|
|
case VECTOR_EULER:
|
|
/* 1 degree = 16 LSB */
|
|
xyz->x = ((double)x) / 16.0;
|
|
xyz->y = ((double)y) / 16.0;
|
|
xyz->z = ((double)z) / 16.0;
|
|
break;
|
|
case VECTOR_ACCELEROMETER:
|
|
/* 1m/s^2 = 100 LSB */
|
|
xyz->x = ((double)x) / 100.0;
|
|
xyz->y = ((double)y) / 100.0;
|
|
xyz->z = ((double)z) / 100.0;
|
|
break;
|
|
case VECTOR_LINEARACCEL:
|
|
/* 1m/s^2 = 100 LSB */
|
|
xyz->x = ((double)x) / 100.0;
|
|
xyz->y = ((double)y) / 100.0;
|
|
xyz->z = ((double)z) / 100.0;
|
|
break;
|
|
case VECTOR_GRAVITY:
|
|
/* 1m/s^2 = 100 LSB */
|
|
xyz->x = ((double)x) / 100.0;
|
|
xyz->y = ((double)y) / 100.0;
|
|
xyz->z = ((double)z) / 100.0;
|
|
break;
|
|
};
|
|
return true;
|
|
};
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
|
|
/*!
|
|
* @brief Gets a quaternion reading from the specified source
|
|
* @return quaternion reading
|
|
*/
|
|
bool sensorBNO::getQuat( BNO_Quat *quat ){
|
|
if( quat != NULL ){
|
|
if( m_pBus != NULL ){
|
|
uint8_t buffer[8];
|
|
memset(buffer, 0, 8);
|
|
int16_t x, y, z, w;
|
|
x = y = z = w = 0;
|
|
/* Read quat data (8 bytes) */
|
|
if( m_pBus->readMZ( BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8 ) ){
|
|
w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
|
|
x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
|
|
y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
|
|
z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
|
|
|
|
/*!
|
|
* Assign to Quaternion
|
|
* See
|
|
* http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_12~1.pdf
|
|
* 3.6.5.5 Orientation (Quaternion)
|
|
*/
|
|
const double scale = ( 1.0 / (1 << 14) );
|
|
quat->w = scale * w;
|
|
quat->x = scale * x;
|
|
quat->y = scale * y;
|
|
quat->z = scale * z;
|
|
return true;
|
|
};
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
|
|
BNO_EVTREC *sensorBNO::getEVT_REC(){
|
|
const double scale = ( 1.0 / (1 << 14) );
|
|
BNO_EVTREC * retVal = NULL;
|
|
PF_TIMESTAMP * tTS1 = NULL;
|
|
PF_TIMESTAMP * tTS2 = NULL;
|
|
tTS1 = getPFTS();
|
|
if( tTS1 != NULL ) {
|
|
BNO_DATA_MAP * tDATA;
|
|
tDATA = getData();
|
|
if( tDATA != NULL ) {
|
|
tTS2 = getPFTS();
|
|
if( tTS2 != NULL ) {
|
|
retVal = (BNO_EVTREC *) malloc(sizeof(BNO_EVTREC));
|
|
if( retVal != NULL ) {
|
|
uint32_t tVal;
|
|
memset(retVal, 0 , sizeof(BNO_EVTREC));
|
|
retVal->wm_mode = m_bTargetWM;//m_bActualWM;
|
|
if( tTS2->seconds > tTS1->seconds ) {
|
|
tVal = ( tTS1->milliseconds + 1000 + tTS2->milliseconds ) >> 1;
|
|
} else {
|
|
tVal = ( tTS1->milliseconds + tTS2->milliseconds ) >> 1;
|
|
};
|
|
if( tVal > 999 ) {
|
|
retVal->milliseconds = tVal - 1000;
|
|
retVal->seconds = tTS1->seconds + 1;
|
|
} else {
|
|
retVal->milliseconds = tVal;
|
|
retVal->seconds = tTS1->seconds;
|
|
};
|
|
switch( m_bActualWM ) {
|
|
case BNO_WM_ACC_ONLY:
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->acc.x = ((double)tDATA->ACC_DATA_X) / 100.0;
|
|
retVal->acc.y = ((double)tDATA->ACC_DATA_Y) / 100.0;
|
|
retVal->acc.z = ((double)tDATA->ACC_DATA_Z) / 100.0;
|
|
case BNO_WM_MAG_ONLY:
|
|
/* 1uT = 16 LSB */
|
|
retVal->mag.x = ((double)tDATA->MAG_DATA_X) / 16.0;
|
|
retVal->mag.y = ((double)tDATA->MAG_DATA_Y) / 16.0;
|
|
retVal->mag.z = ((double)tDATA->MAG_DATA_Z) / 16.0;
|
|
case BNO_WM_GYR_ONLY:
|
|
/* 1uT = 16 LSB */
|
|
retVal->gyr.x = ((double)tDATA->GYR_DATA_X) / 16.0;
|
|
retVal->gyr.y = ((double)tDATA->GYR_DATA_Y) / 16.0;
|
|
retVal->gyr.z = ((double)tDATA->GYR_DATA_Z) / 16.0;
|
|
case BNO_WM_ACC_MAG:
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->acc.x = ((double)tDATA->ACC_DATA_X) / 100.0;
|
|
retVal->acc.y = ((double)tDATA->ACC_DATA_Y) / 100.0;
|
|
retVal->acc.z = ((double)tDATA->ACC_DATA_Z) / 100.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->mag.x = ((double)tDATA->MAG_DATA_X) / 16.0;
|
|
retVal->mag.y = ((double)tDATA->MAG_DATA_Y) / 16.0;
|
|
retVal->mag.z = ((double)tDATA->MAG_DATA_Z) / 16.0;
|
|
case BNO_WM_ACC_GYR:
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->acc.x = ((double)tDATA->ACC_DATA_X) / 100.0;
|
|
retVal->acc.y = ((double)tDATA->ACC_DATA_Y) / 100.0;
|
|
retVal->acc.z = ((double)tDATA->ACC_DATA_Z) / 100.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->gyr.x = ((double)tDATA->GYR_DATA_X) / 16.0;
|
|
retVal->gyr.y = ((double)tDATA->GYR_DATA_Y) / 16.0;
|
|
retVal->gyr.z = ((double)tDATA->GYR_DATA_Z) / 16.0;
|
|
case BNO_WM_MAG_GYR:
|
|
/* 1uT = 16 LSB */
|
|
retVal->mag.x = ((double)tDATA->MAG_DATA_X) / 16.0;
|
|
retVal->mag.y = ((double)tDATA->MAG_DATA_Y) / 16.0;
|
|
retVal->mag.z = ((double)tDATA->MAG_DATA_Z) / 16.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->gyr.x = ((double)tDATA->GYR_DATA_X) / 16.0;
|
|
retVal->gyr.y = ((double)tDATA->GYR_DATA_Y) / 16.0;
|
|
retVal->gyr.z = ((double)tDATA->GYR_DATA_Z) / 16.0;
|
|
case BNO_WM_AMG_NO_FUSION:
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->acc.x = ((double)tDATA->ACC_DATA_X) / 100.0;
|
|
retVal->acc.y = ((double)tDATA->ACC_DATA_Y) / 100.0;
|
|
retVal->acc.z = ((double)tDATA->ACC_DATA_Z) / 100.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->mag.x = ((double)tDATA->MAG_DATA_X) / 16.0;
|
|
retVal->mag.y = ((double)tDATA->MAG_DATA_Y) / 16.0;
|
|
retVal->mag.z = ((double)tDATA->MAG_DATA_Z) / 16.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->gyr.x = ((double)tDATA->GYR_DATA_X) / 16.0;
|
|
retVal->gyr.y = ((double)tDATA->GYR_DATA_Y) / 16.0;
|
|
retVal->gyr.z = ((double)tDATA->GYR_DATA_Z) / 16.0;
|
|
case BNO_WM_IMU: // Fusion Modes -> All datas Update!
|
|
case BNO_WM_COMPAS:
|
|
case BNO_WM_M4G:
|
|
case BNO_WM_NDOF_FMC_OFF:
|
|
case BNO_WM_NDOF:
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->acc.x = ((double)tDATA->ACC_DATA_X) / 100.0;
|
|
retVal->acc.y = ((double)tDATA->ACC_DATA_Y) / 100.0;
|
|
retVal->acc.z = ((double)tDATA->ACC_DATA_Z) / 100.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->mag.x = ((double)tDATA->MAG_DATA_X) / 16.0;
|
|
retVal->mag.y = ((double)tDATA->MAG_DATA_Y) / 16.0;
|
|
retVal->mag.z = ((double)tDATA->MAG_DATA_Z) / 16.0;
|
|
/* 1uT = 16 LSB */
|
|
retVal->gyr.x = ((double)tDATA->GYR_DATA_X) / 16.0;
|
|
retVal->gyr.y = ((double)tDATA->GYR_DATA_Y) / 16.0;
|
|
retVal->gyr.z = ((double)tDATA->GYR_DATA_Z) / 16.0;
|
|
/* 1 degree = 16 LSB */
|
|
retVal->eul.x = ((double)tDATA->EUL_PITCH) / 16.0;
|
|
retVal->eul.y = ((double)tDATA->EUL_ROLL) / 16.0;
|
|
retVal->eul.z = ((double)tDATA->EUL_YAW) / 16.0;
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->mov.x = ((double)tDATA->LIA_X) / 100.0;
|
|
retVal->mov.y = ((double)tDATA->LIA_Y) / 100.0;
|
|
retVal->mov.z = ((double)tDATA->LIA_Z) / 100.0;
|
|
/* 1m/s^2 = 100 LSB */
|
|
retVal->grav.x = ((double)tDATA->GRV_X ) / 100.0;
|
|
retVal->grav.y = ((double)tDATA->GRV_Y ) / 100.0;
|
|
retVal->grav.z = ((double)tDATA->GRV_Z ) / 100.0;
|
|
/* Quat */
|
|
retVal->quat.w = scale * tDATA->QUA_W;//(((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
|
|
retVal->quat.x = scale * tDATA->QUA_X;//(((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
|
|
retVal->quat.y = scale * tDATA->QUA_Y;//(((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
|
|
retVal->quat.z = scale * tDATA->QUA_Z;//(((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
|
|
};
|
|
};
|
|
free( tTS2 );
|
|
};
|
|
//free( tDATA );
|
|
// No free - getdata uses Class mem
|
|
};
|
|
free( tTS1 );
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
BNO_DATA_MAP *sensorBNO::getData(){
|
|
if( switchPage(0) ) {
|
|
m_pBus->readMZ( BNO_DATA_OFFSET, (uint8_t *)&m_sDATA, sizeof(BNO_DATA_MAP));
|
|
};
|
|
return &m_sDATA;
|
|
};
|
|
|
|
bool sensorBNO::getStatus( BNO_STATUS_MAP *tPtr ){
|
|
if( switchPage(0) ) {
|
|
if( m_pBus->readMZ( BNO_STATUS_OFFSET, (uint8_t *)tPtr, sizeof(BNO_STATUS_MAP))) {
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::resetInterupts(){
|
|
if( m_pBus != NULL ) {
|
|
uint8_t tVal;
|
|
tVal = 0x40;
|
|
if( m_pBus->writeMZ( BNO_SYS_TRIG_OFFSET, (uint8_t *)&tVal, 1 ) ){
|
|
SLEEP( 30 );
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::resetSystem(){
|
|
if( m_pBus != NULL ) {
|
|
uint8_t tVal;
|
|
tVal = 0x20;
|
|
if( m_pBus->writeMZ( BNO_SYS_TRIG_OFFSET, (uint8_t *)&tVal, 1 ) ){
|
|
SLEEP( 30 );
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::startBIST(){
|
|
if( m_pBus != NULL ) {
|
|
uint8_t tVal;
|
|
tVal = 0x01;
|
|
if( m_pBus->writeMZ( BNO_SYS_TRIG_OFFSET, (uint8_t *)&tVal, 1 ) ){
|
|
SLEEP( 30 );
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::setConfigPage0( BNO_CONFIG0_MAP *tPtr ){
|
|
if( switchPage(0) ) {
|
|
if( m_pBus->writeMZ( BNO_CONFIG0_OFFSET, (uint8_t *)tPtr, sizeof(BNO_CONFIG0_MAP))) {
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::getConfigPage0( BNO_CONFIG0_MAP *tPtr ){
|
|
if( switchPage(0) ) {
|
|
if( m_pBus->readMZ( BNO_CONFIG0_OFFSET, (uint8_t *)tPtr, sizeof(BNO_CONFIG0_MAP))) {
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::getConfigPage1( BNO_CONFIG1_MAP *tPtr ){
|
|
if( switchPage(1) ) {
|
|
if( m_pBus->readMZ( BNO_CONFIG1_OFFSET, (uint8_t *)tPtr, sizeof(BNO_CONFIG1_MAP))) {
|
|
return true;
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::switchPage( uint8_t PageID ){
|
|
if( m_pBus!= NULL ){
|
|
if( m_sID.PAGE_ID != PageID ) {
|
|
if( m_pBus->readMZ( BNO_ID_OFFSET, (uint8_t *)&m_sID, sizeof(BNO_ID_MAP))) {
|
|
if( m_sID.PAGE_ID == PageID ) return true;
|
|
m_sID.PAGE_ID = PageID;
|
|
if( m_pBus->writeMZ( BNO_ID_OFFSET, (uint8_t *)&m_sID, sizeof(BNO_ID_MAP)) ) {
|
|
return true;
|
|
};
|
|
};
|
|
} else return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool sensorBNO::isDead(){
|
|
if( m_pBus!= NULL ){
|
|
uint8_t id;
|
|
id = m_pBus->read8( BNO055_CHIP_ID_ADDR );
|
|
if( id == BNO055_ID ){
|
|
return false;
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|