vTask in a class?
Posted: Wed Apr 24, 2019 8:10 pm
I'm stuck in a loop. I wrote this class:
I want to make a task in the class but I keep on getting round robin errors, Round robin is that I solve one error and another error pops up, solve that error and another error pops up. The errors start with a message about a thing do about a static function. As you can see, I want to make this last function into a task that can be triggered by the int myMPU9250::fGetAll( ) function.
Any suggestions, ideas, concepts, solutions?
Code: Select all
#include "ESP32_MPU9250.h"
////
////
myMPU9250::myMPU9250( int _queueSize )
{
ESP32_SPI_API _spi;
_spi.setQSize( _queueSize );
// _hEG = xEventGroupCreate();
////
// xTaskCreate( fDoTempratureConversion, "fDoTempratureConversion", 10000, NULL, 3, NULL );
}
////
int myMPU9250::getByte( int _Byte)
{
return _spi.get_rxData8bit( _Byte );
}
////
int myMPU9250::fInit_MPU9250( bool resetMPU9250, bool resetAK8962 )
{
SPI_Err = _spi.fSPIbusConfig( );
// if ( SPI_Err == 0 )
// {
SPI_Err = _spi.fSPIdeviceConfig( );
// select clock source to gyro
SPI_Err = _spi.fWriteSPIdata8bits( PWR_MGMNT_1, CLOCK_SEL_PLL );
vTaskDelay(1);
// enable I2C master mode
SPI_Err = _spi.fWriteSPIdata8bits( USER_CTRL, I2C_MST_EN );
vTaskDelay(1);
// set the I2C bus speed to 400 kHz
SPI_Err = _spi.fWriteSPIdata8bits( I2C_MST_CTRL, I2C_MST_CLK );
vTaskDelay(1);
// set AK8963 to Power Down
fWrite_AK8963 ( AK8963_CNTL1, AK8963_PWR_DOWN );
vTaskDelay(1);
// reset the MPU9250
SPI_Err = _spi.fWriteSPIdata8bits( PWR_MGMNT_1, PWR_RESET );
// wait for MPU-9250 to come back up
vTaskDelay(1);
// reset the AK8963 AK8963_CNTL2,AK8963_RESET
fWrite_AK8963 ( AK8963_CNTL2, AK8963_RESET );
vTaskDelay(1);
// select clock source to gyro
SPI_Err = _spi.fWriteSPIdata8bits( PWR_MGMNT_1, CLOCK_SEL_PLL );
vTaskDelay(1);
// enable accelerometer and gyro PWR_MGMNT_2,SEN_ENABLE
SPI_Err = _spi.fWriteSPIdata8bits( PWR_MGMNT_2, SEN_ENABLE );
vTaskDelay(1);
// setting accel range / scale to 16G as default
setAccelRange( AccellRangeToUse );
// setting the gyro range / scale to 250DPS as default for calibration
setGyroRange( GyroRangeToUse );
// setting bandwidth to 184Hz as default for calibration
setDlpfBandwidthAccelerometer( DLPF_BANDWIDTH_184HZ );
// setting bandwidth to 20Hz as default for calibration
setDlpfBandwidthGyro( DLPF_BANDWIDTH_20HZ );
// setting the sample rate divider to 19 (0x13) as default
SPI_Err = _spi.fWriteSPIdata8bits( SMPDIV, 0x13); // Set sample rate to default of 19
vTaskDelay(1);
// enable I2C master mode
SPI_Err = _spi.fWriteSPIdata8bits( USER_CTRL, I2C_MST_EN );
vTaskDelay(1);
// set the I2C bus speed to 400 kHz
SPI_Err = _spi.fWriteSPIdata8bits( I2C_MST_CTRL, I2C_MST_CLK );
vTaskDelay(1);
/* get the magnetometer calibration */
// set AK8963 to Power Down
fWrite_AK8963 ( AK8963_CNTL1, AK8963_PWR_DOWN );
vTaskDelay(100); // long wait between AK8963 mode changes
// set AK8963 to FUSE ROM access
fWrite_AK8963 ( AK8963_CNTL1, AK8963_FUSE_ROM );
vTaskDelay ( 100 ); // delay for mode change
setMagSensitivityValue( );
vTaskDelay(1);
// AK8963_CNTL1,AK8963_CNT_MEAS2
fWrite_AK8963( AK8963_CNTL1, AK8963_CNT_MEAS2 );
// delay for mode change
vTaskDelay ( 100 );
// AK8963_HXL,7 ;
fReadAK8963(AK8963_HXL, 7 | SPI_READ );
vTaskDelay(1);
fCalculate_GandAbias();
} // int fInit_MPU9250( )
////////////////////////////////////////////////////////////////////////////////////////////
void myMPU9250::fWrite_AK8963( int subAddress, int dataAK8963 )
{
SPI_Err = _spi.fWriteSPIdata8bits( I2C_SLV0_ADDR, AK8963_I2C_ADDR );
SPI_Err = _spi.fWriteSPIdata8bits( I2C_SLV0_REG, subAddress );
SPI_Err = _spi.fWriteSPIdata8bits( I2C_SLV0_DO, dataAK8963 );
SPI_Err = _spi.fWriteSPIdata8bits( I2C_SLV0_CTRL, I2C_SLV0_EN | (uint8_t)1 );
} // void fWrite_AK8963 ( uint8_t subAddress, uint8_t dataAK8963 )
////////////////////////////////////////////////////////////////////////////////////////////
///* sets the DLPF bandwidth to values other than default */
int myMPU9250::setDlpfBandwidthAccelerometer( int bandwidth )
{
SPI_Err = 0;
if ( bandwidth == DLPF_BANDWIDTH_184HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)ACCEL_CONFIG2, (int)ACCEL_DLPF_184);
vTaskDelay(1);
a_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_92HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)ACCEL_CONFIG2, (int)ACCEL_DLPF_92 );
vTaskDelay(1);
a_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_41HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)ACCEL_CONFIG2, (int)ACCEL_DLPF_41 );
vTaskDelay(1);
a_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_20HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)ACCEL_CONFIG2, (int)ACCEL_DLPF_20 );
vTaskDelay(1);
a_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_10HZ )
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)ACCEL_CONFIG2, (int)ACCEL_DLPF_10 );
vTaskDelay(1);
a_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_5HZ )
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)ACCEL_CONFIG2, (int)ACCEL_DLPF_5 );
vTaskDelay(1);
a_bandwidth = bandwidth;
return SPI_Err;
}
} // int setDlpfBandwidth(DlpfBandwidth bandwidth
int myMPU9250::setDlpfBandwidthGyro( int bandwidth )
{
SPI_Err = 0;
if ( bandwidth == DLPF_BANDWIDTH_184HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)CONFIG, (int)GYRO_DLPF_184);
vTaskDelay(1);
g_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_92HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)CONFIG, (int)GYRO_DLPF_92 );
vTaskDelay(1);
g_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_41HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)CONFIG, (int)GYRO_DLPF_41 );
vTaskDelay(1);
g_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_20HZ)
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)CONFIG, (int)GYRO_DLPF_20 );
vTaskDelay(1);
g_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_10HZ )
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)CONFIG, (int)GYRO_DLPF_10 );
vTaskDelay(1);
g_bandwidth = bandwidth;
return SPI_Err;
}
if ( bandwidth == DLPF_BANDWIDTH_5HZ )
{
SPI_Err = _spi.fWriteSPIdata8bits( (int)CONFIG, (int)GYRO_DLPF_5 );
vTaskDelay(1);
g_bandwidth = bandwidth;
return SPI_Err;
}
} // int setDlpfBandwidth(DlpfBandwidth bandwidth
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::useDLPFbandwidth184Hz()
{
return DLPF_BANDWIDTH_184HZ;
}
int myMPU9250::useDLPFbandwidth92Hz()
{
return DLPF_BANDWIDTH_92HZ;
}
int myMPU9250::useDLPFbandwidth41Hz()
{
return DLPF_BANDWIDTH_41HZ;
}
int myMPU9250::useDLPFbandwidth20Hz()
{
return DLPF_BANDWIDTH_20HZ;
}
int myMPU9250::useDLPFbandwidth10Hz()
{
return DLPF_BANDWIDTH_10HZ;
}
int myMPU9250::useDLPFbandwidth5Hz()
{
return DLPF_BANDWIDTH_5HZ;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::SPI_Error()
{
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
bool myMPU9250::isMPU9250_OK()
{
return MPU9250_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::fReadAK8963( int subAddress, int count )
{
// set slave 0 to the AK8963 and set for read
SPI_Err = _spi.fWriteSPIdata8bits ( I2C_SLV0_ADDR, AK8963_I2C_ADDR | I2C_READ_FLAG );
// set the register to the desired AK8963 sub address
// I2C_SLV0_REG, subAddress
SPI_Err = _spi.fWriteSPIdata8bits ( I2C_SLV0_REG, subAddress );
// enable I2C and request the bytes
// I2C_SLV0_CTRL, I2C_SLV0_EN | count
SPI_Err = _spi.fWriteSPIdata8bits ( I2C_SLV0_CTRL, I2C_SLV0_EN | count );
vTaskDelay ( 1 );
return SPI_Err;
} // fReadAK8963(uint8_t subAddress, uint8_t count, uint8_t* dest)
////////////////////////////////////////////////////////////////////////////////////////////
bool myMPU9250::isAK8963_OK()
{
return AK8963_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////
float myMPU9250::getAccelScaleFactor()
{
return _accelScaleFactor;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::setAccelRange( int range )
{
SPI_Err = 0;
if ( ACCEL_RANGE_2G == range )
{
// setting the accel range to 2G
SPI_Err = _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_2G );
vTaskDelay(1);
_accelScaleFactor = (G * 2.0f) / AtoDscaleFactor; // setting the accel scale to 2G
return SPI_Err;
}
if ( ACCEL_RANGE_4G == range )
{
// setting the accel range to 4G
SPI_Err = _spi.fWriteSPIdata8bits(ACCEL_CONFIG, ACCEL_FS_SEL_4G);
vTaskDelay(1);
_accelScaleFactor = (G * 4.0f) / AtoDscaleFactor; // setting the accel scale to 4G
return SPI_Err;
}
if ( ACCEL_RANGE_8G == range )
{
// setting the accel range to 8G
SPI_Err = _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_8G );
vTaskDelay(1);
_accelScaleFactor = (G * 8.0f) / AtoDscaleFactor; // setting the accel scale to 8G
return SPI_Err;
}
if ( ACCEL_RANGE_16G == range )
{
// setting the accel range to 16G
SPI_Err = _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_16G );
vTaskDelay(1);
_accelScaleFactor = (G * 16.0f) / AtoDscaleFactor; // setting the accel scale to 16G
return SPI_Err;
}
} //int myMPU9250::setAccelRange( int range )
////////////////////////////////////////////////////////////////////////////////////////////
void myMPU9250::setACCEL_RANGE_2G()
{
AccellRangeToUse = ACCEL_RANGE_2G;
}
void myMPU9250::setACCEL_RANGE_4G()
{
AccellRangeToUse = ACCEL_RANGE_4G;
}
void myMPU9250::setACCEL_RANGE_8G()
{
AccellRangeToUse = ACCEL_RANGE_8G;
}
void myMPU9250::setACCEL_RANGE_16G()
{
AccellRangeToUse = ACCEL_RANGE_16G;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::setGyroRange( int range )
{
SPI_Err = 0;
if ( GYRO_RANGE_250DPS == range )
{
// setting the gyro range to 250DPS
SPI_Err = _spi.fWriteSPIdata8bits(GYRO_CONFIG, GYRO_FS_SEL_250DPS);
vTaskDelay(1);
_gyroScaleFactor = 250.0f / AtoDscaleFactor; // setting the gyro scale to 250DPS
return SPI_Err;
}
if ( GYRO_RANGE_500DPS == range )
{
// setting the gyro range to 500DPS
SPI_Err = _spi.fWriteSPIdata8bits( GYRO_CONFIG, GYRO_FS_SEL_500DPS );
vTaskDelay(1);
_gyroScaleFactor = (500.0f / AtoDscaleFactor) * _d2r; // setting the gyro scale to 500DPS
return SPI_Err;
}
if ( GYRO_RANGE_1000DPS == range )
{
// setting the gyro range to 1000DPS
SPI_Err = _spi.fWriteSPIdata8bits( GYRO_CONFIG, GYRO_FS_SEL_1000DPS );
vTaskDelay(1);
_gyroScaleFactor = (1000.0f / AtoDscaleFactor) * _d2r; // setting the gyro scale to 1000DPS
return SPI_Err;
}
if ( GYRO_RANGE_2000DPS == range )
{
// setting the gyro range to 2000DPS
SPI_Err = _spi.fWriteSPIdata8bits( GYRO_CONFIG, GYRO_FS_SEL_2000DPS );
vTaskDelay(1);
_gyroScaleFactor = (2000.0f / AtoDscaleFactor) * _d2r; // setting the gyro scale to 2000DPS
return SPI_Err;
}
} // int myMPU9250::setGyroRange( int range )
////////////////////////////////////////////////////////////////////////////////////////////
float myMPU9250::getGyroScaleFactor()
{
return _gyroScaleFactor;
}
////////////////////////////////////////////////////////////////////////////////////////////
float myMPU9250::getGyroScale()
{
return _gyroScaleFactor;
}
int myMPU9250::useGYRO_RANGE_250DPS()
{
GyroRangeToUse = GYRO_RANGE_250DPS;
return GyroRangeToUse;
}
int myMPU9250::useGYRO_RANGE_500DPS()
{
GyroRangeToUse = GYRO_RANGE_500DPS;
return GyroRangeToUse;
}
int myMPU9250::useGYRO_RANGE_1000DPS()
{
GyroRangeToUse = GYRO_RANGE_1000DPS;
return GyroRangeToUse;
}
int myMPU9250::useGYRO_RANGE_2000DPS()
{
GyroRangeToUse = GYRO_RANGE_2000DPS;
return GyroRangeToUse;
}
////////////////////////////////////////////////////////////////////////////////////////////
float myMPU9250::getMagScaleXFactor()
{
return x_MSF.x;
}
float myMPU9250::getMagScaleYFactor()
{
return x_MSF.y;
}
float myMPU9250::getMagScaleZFactor()
{
return x_MSF.z;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::setMagSensitivityValue( )
{
SPI_Err = 0;
// read the AK8963 ASA registers and compute magnetometer scale factors
SPI_Err = fReadAK8963( AK8963_ASA, 3 );
vTaskDelay( 1 );
SPI_Err = _spi.fReadSPI ( 3, EXT_SENS_DATA_00 | SPI_READ );
if (SPI_Err != 0 )
{
x_MSF.x = (float)( _spi.get_rxData8bit(0) - 128 ) / 256.0f + 1.0f; // Return x-axis sensitivity adjustment values, etc.
x_MSF.y = (float)( _spi.get_rxData8bit(1) - 128 ) / 256.0f + 1.0f; // Return y-axis sensitivity adjustment values, etc.
x_MSF.z = (float)( _spi.get_rxData8bit(2) - 128 ) / 256.0f + 1.0f; // Return z-axis sensitivity adjustment values, etc.
}
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::getRegisteredError()
{
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::getAdata()
{
SPI_Err = 0;
SPI_Err = _spi.fReadSPI( 8 , 0X3A | SPI_READ );
if ( SPI_Err == 0 )
{
x_Araw.x = 0.0;
x_Araw.y = 0.0;
x_Araw.z = 0.0;
// transform
x_Araw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(2) << 8) | _spi.get_rxData8bit(3)));
// x_Araw.y /= 1000;
x_Araw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(4) << 8) | _spi.get_rxData8bit(5)));
// x_Araw.x /= 1000;
x_Araw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(6) << 8) | _spi.get_rxData8bit(7)));
// x_Araw.z /= 1000;
x_Araw.z *= -1;
// scale and bias
x_Ascaled.x = ( (x_Araw.x * _accelScaleFactor) - x_Abias.x ) * x_ASF.x;
x_Ascaled.y = ( (x_Araw.y * _accelScaleFactor) - x_Abias.y ) * x_ASF.y;
x_Ascaled.z = ( (x_Araw.z * _accelScaleFactor) - x_Abias.z ) * x_ASF.z;
}
// reads one address previous to the accelerometer data
// found that from time to time only positive values returned if
// was read directly
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
float myMPU9250::getAxRaw()
{
return x_Araw.x;
}
float myMPU9250::getAyRaw()
{
return x_Araw.y;
}
float myMPU9250::getAzRaw()
{
return x_Araw.z;
}
float myMPU9250::getAxScaled()
{
return x_Ascaled.x;
}
float myMPU9250::getAyScaled()
{
return x_Ascaled.y;
}
float myMPU9250::getAzScaled()
{
return x_Ascaled.z;
}
void myMPU9250::setXbiasScale( float _bs )
{
x_ASF.x = _bs;
}
void myMPU9250::setYbiasScale( float _bs )
{
x_ASF.y = _bs;
}
void myMPU9250::setZbiasScale( float _bs )
{
x_ASF.z = _bs;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::getGdata()
{
SPI_Err = 0;
// SPI_Err = _spi.fReadSPI( 6 , GYRO_OUTX | SPI_READ );
SPI_Err = _spi.fReadSPI( 8 , 0x41 | SPI_READ );
if ( SPI_Err == 0 )
{
x_Graw.x = 0.0f;
x_Graw.y = 0.0f;
x_Graw.z = 0.0f;
// transform
x_Graw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(2) << 8) | _spi.get_rxData8bit(3)));
// x_Graw.y = x_Graw.y / 10; // decrease sensitivity
x_Graw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(4) << 8) | _spi.get_rxData8bit(5)));
// x_Graw.x = x_Graw.x / 10;
x_Graw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(6) << 8) | _spi.get_rxData8bit(7)));
x_Graw.z = (-1 * x_Graw.z);
// scale
x_Gscaled.x = ( ( x_Graw.x * _gyroScaleFactor ) - x_Gbias.x) ;
x_Gscaled.y = ( x_Graw.y * _gyroScaleFactor ) - x_Gbias.y;
x_Gscaled.z = ( x_Graw.z * _gyroScaleFactor ) - x_Gbias.z;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
float myMPU9250::getGxScaled()
{
return x_Gscaled.x;
}
float myMPU9250::getGyScaled()
{
return x_Gscaled.y;
}
float myMPU9250::getGzScaled()
{
return x_Gscaled.z;
}
float myMPU9250::getGxRaw()
{
return x_Graw.x;
}
float myMPU9250::getGyRaw()
{
return x_Graw.y;
}
float myMPU9250::getGzRaw()
{
return x_Graw.z;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::getMdata()
{
SPI_Err = 0;
SPI_Err = _spi.fReadSPI( 8 , 0x47 | SPI_READ );
/*ST2 register has a role as data reading end register, also.
When any of measurement data register is read in continuous measurement mode or external trigger measurement mode,
it means data reading start and taken as data reading until ST2 register is read.
Therefore, when any of measurement data is read, be sure to read ST2 register at the end.
that's why reading the extra register
*/
if ( SPI_Err == 0 )
{
magDataOverFlow = false;
x_Mraw.x = 0.0;
x_Mraw.y = 0.0;
x_Mraw.z = 0.0;
x_Mraw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(3) << 8) | _spi.get_rxData8bit(2)));
x_Mraw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(5) << 8) | _spi.get_rxData8bit(4)));
x_Mraw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(7) << 8) | _spi.get_rxData8bit(6)));
x_Mscaled.x = (x_Mraw.x * mRes * x_MSF.x) - x_Mbias.x;
x_Mscaled.y = (x_Mraw.y * mRes * x_MSF.y) - x_Mbias.y;
x_Mscaled.z = (x_Mraw.z * mRes * x_MSF.z) - x_Mbias.z;
x_Mscaled.x *= x_Mscale.x; // poor man's soft iron calibration
x_Mscaled.y *= x_Mscale.y;
x_Mscaled.z *= x_Mscale.z;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
bool myMPU9250::getMagDataOverflow()
{
return magDataOverFlow;
}
float myMPU9250::getMxRaw()
{
return x_Mraw.x;
}
float myMPU9250::getMyRaw()
{
return x_Mraw.y;
}
float myMPU9250::getMzRaw()
{
return x_Mraw.z;
}
float myMPU9250::getMxScaled()
{
return x_Mscaled.x;
}
float myMPU9250::getMyScaled()
{
return x_Mscaled.y;
}
float myMPU9250::getMzScaled()
{
return x_Mscaled.z;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::fSetInterrupt()
{
SPI_Err = 0;
/* setting the interrupt */
// INT_PIN_CFG,INT_PULSE_50US setup interrupt, 50 us pulse
SPI_Err = _spi.fWriteSPIdata8bits( INT_PIN_CFG, INT_PULSE_50US );
// INT_ENABLE,INT_RAW_RDY_EN set to data ready
SPI_Err = _spi.fWriteSPIdata8bits( INT_ENABLE, INT_RAW_RDY_EN );
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::fQueueupReadAGMdata( )
{
SPI_Err = _spi.fSPIqueueupTransactions_read( 8 , 0X3A | SPI_READ );
SPI_Err = _spi.fReadSPI( 6 , GYRO_OUTX | SPI_READ );
SPI_Err = _spi.fReadSPI( 6 , EXT_SENS_DATA_00 | SPI_READ );
}
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::fReadQueuedAdata( )
{
// SPI_Err = _spi.fSPIreadQueuedTransaction( 3 );
// x_Araw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(2) << 8) | _spi.get_rxData8bit(3)));
// x_Araw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(4) << 8) | _spi.get_rxData8bit(5)));
// x_Araw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(6) << 8) | _spi.get_rxData8bit(7)));
// x_Ascaled.x = x_Araw.x * _accelScaleFactor;
// x_Ascaled.y = x_Araw.y * _accelScaleFactor;
// x_Ascaled.z = x_Araw.z * _accelScaleFactor;
//SPI_Err = _spi.fSPIreadQueuedTransaction( 6 );
// x_Graw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(0) << 8) | _spi.get_rxData8bit(1)));
// x_Graw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(2) << 8) | _spi.get_rxData8bit(3)));
// x_Graw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(4) << 8) | _spi.get_rxData8bit(5)));
// x_Gscaled.x = x_Graw.x * _gyroScaleFactor;
// x_Gscaled.y = x_Graw.y * _gyroScaleFactor;
// x_Gscaled.z = x_Graw.z * _gyroScaleFactor;
// SPI_Err = _spi.fSPIreadQueuedTransaction( 6 );
// x_Mraw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(1) << 8) | _spi.get_rxData8bit(0)));
// x_Mraw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(3) << 8) | _spi.get_rxData8bit(2)));
// x_Mraw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(5) << 8) | _spi.get_rxData8bit(4)));
// x_Mscaled.x = (x_Mraw.x * mRes * x_MSF.x);
// x_Mscaled.y = (x_Mraw.y * mRes * x_MSF.y);
// x_Mscaled.z = (x_Mraw.z * mRes * x_MSF.z);
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
/* sets the sample rate divider to values other than default */
//void myMPU9250::setSrd(uint8_t srd)
//{
//
// // SPI_Err = _spi.fWriteSPIdata8bits( SMPDIV, 0x04 );
// /* setting the sample rate divider to 19 to facilitate setting up magnetometer */
// SPI_Err = _spi.fWriteSPIdata8bits( SMPDIV, 19 );
// vTaskDelay( 1 );
// if (srd > 9)
// {
// // set AK8963 to Power Down
// fWrite_AK8963 ( AK8963_CNTL1, AK8963_PWR_DOWN );
// }
// vTaskDelay( 100 );
// // set AK8963 to 16 bit resolution, 8 Hz update rate
// fWrite_AK8963( AK8963_CNTL1, AK8963_CNT_MEAS2 );
// vTaskDelay(100); // long wait between AK8963 mode changes
// // instruct the MPU9250 to get 7 bytes of data from the AK8963 at the sample rate
//
// // readAK8963Registers(AK8963_HXL,7,_buffer);
// // } else {
// // // set AK8963 to Power Down
// // if(writeAK8963Register(AK8963_CNTL1,AK8963_PWR_DOWN) < 0){
// // return -2;
// // }
// // delay(100); // long wait between AK8963 mode changes
// // // set AK8963 to 16 bit resolution, 100 Hz update rate
// // if(writeAK8963Register(AK8963_CNTL1,AK8963_CNT_MEAS2) < 0){
// // return -3;
// // }
// // delay(100); // long wait between AK8963 mode changes
// // // instruct the MPU9250 to get 7 bytes of data from the AK8963 at the sample rate
// // readAK8963Registers(AK8963_HXL,7,_buffer);
// // }
// // /* setting the sample rate divider */
// // if(writeRegister(SMPDIV,srd) < 0){ // setting the sample rate divider
// // return -4;
// // }
// // _srd = srd;
// // return 1;
//}
////////////////////////////////////////////////////////////////////////////////////////////
void myMPU9250::setAXbias( float _ab )
{
x_Abias.x = _ab;
}
void myMPU9250::setAYbias( float _ab )
{
x_Abias.y = _ab;
}
void myMPU9250::setAZbias( float _ab )
{
x_Abias.z = _ab;
}
float myMPU9250::getAXbias( )
{
return x_Abias.x;
}
float myMPU9250::getAYbias( )
{
return x_Abias.y;
}
float myMPU9250::getAZbias( )
{
return x_Abias.z;
}
////////////////////////////////////////////////////////////////////////////////////////////
void myMPU9250::setGXbias( float _gb )
{
x_Gbias.x = _gb;
}
void myMPU9250::setGYbias( float _gb )
{
x_Gbias.y = _gb;
}
void myMPU9250::setGZbias( float _gb )
{
x_Gbias.z = _gb;
}
float myMPU9250::getGXbias( )
{
return x_Gbias.x;
}
float myMPU9250::getGYbias( )
{
return x_Gbias.y;
}
float myMPU9250::getGZbias( )
{
return x_Gbias.z;
}
////////////////////////////////////////////////////////////////////////////////////////////
void myMPU9250::setMXbias( float _mb )
{
x_Mbias.x = _mb * mRes * x_MSF.x;
}
void myMPU9250::setMYbias( float _mb )
{
x_Mbias.y = _mb * mRes * x_MSF.y;
}
void myMPU9250::setMZbias( float _mb )
{
x_Mbias.z = _mb * mRes * x_MSF.z;
}
float myMPU9250::getMXbias( )
{
return x_Mbias.x;
}
float myMPU9250::getMYbias( )
{
return x_Mbias.y;
}
float myMPU9250::getMZbias( )
{
return x_Mbias.z;
}
void myMPU9250::setMxScale( float _ms )
{
x_Mscale.x = _ms;
}
void myMPU9250::setMyScale( float _ms )
{
x_Mscale.y = _ms;
}
void myMPU9250::setMzScale( float _ms )
{
x_Mscale.z = _ms;
}
////////////////////////////////////////////////////////////////////////////////////////////
void myMPU9250::fCalculate_GandAbias( )
{
float tempGx = 0.0f;
float tempGy = 0.0f;
float tempGz = 0.0f;
float tempAx = 0.0f;
float tempAy = 0.0f;
float tempAz = 0.0f;
float tempMx = 0.0f;
float tempMy = 0.0f;
float tempMz = 0.0f;
int numOfSamples = 100;
x_Abias.x = 0.0f;
x_Abias.y = 0.0f;
x_Abias.z = 0.0f;
setGXbias( 0.0f );
setGYbias( 0.0f );
setGZbias( 0.0f );
//// do X,Y,Z gyro and accelerometer bias
// take reading
for ( int i = 0 ; i < numOfSamples; i++ )
{
// take reading
// getAdata();
fGetAll( ); // using non-transformed get 4/24/2019
////
////
tempAx = ( tempAx + getAxScaled() ) / (float)numOfSamples;
tempAy = ( tempAy + getAyScaled() ) / (float)numOfSamples;
tempAz = ( tempAz + getAzScaled() ) / (float)numOfSamples;
// getGdata();
tempGx = ( tempGx + getGxScaled() ) / (float)numOfSamples;
tempGy = ( tempGy + getGyScaled() ) / (float)numOfSamples;
tempGz = ( tempGz + getGzScaled() ) / (float)numOfSamples;
vTaskDelay( 20 );
}
// set accelrometer scale
float _axmax = 0.0f, _aymax = 0.0f, _azmax = 0.0f;
float _axmin = 0.0f, _aymin = 0.0f, _azmin = 0.0f;
if ( tempAx > 9.0f )
{
_axmax = tempAx;
}
if ( tempAx < -9.0f )
{
_axmin = tempAx;
}
if ( tempAy > 9.0f )
{
_aymax = tempAy;
}
if ( tempAy < -9.0f )
{
_aymin = tempAy;
}
if ( tempAz > 9.0f )
{
_azmax = tempAz;
}
if ( tempAz < -9.0f )
{
_azmin = tempAz;
}
//// find bias and scale factor
if ((abs(_axmin) > 9.0f) && (abs(_axmax) > 9.0f))
{
x_Abias.x = ( _axmin + _axmax ) / 2.0f;
setXbiasScale( G / ((abs(_axmin) + abs(_axmax)) / 2.0f) );
}
else
{
x_Abias.x = tempAx; // transformed during get
}
//
if ((abs(_aymin) > 9.0f) && (abs(_aymax) > 9.0f))
{
x_Abias.y = ( _aymin + _aymax ) / 2.0f;
setYbiasScale( G / ((abs(_aymin) + abs(_aymax)) / 2.0f) );
}
else
{
x_Abias.y = tempAy;
}
if ((abs(_azmin) > 9.0f) && (abs(_azmax) > 9.0f))
{
x_Abias.z = (_azmin + _azmax) / 2.0f;
setZbiasScale( G / ((abs(_azmin) + abs(_azmax)) / 2.0f) );
}
else
{
x_Abias.z = tempAz;
}
x_Gbias.x = tempGx; // transformed during get
x_Gbias.y = tempGy;
x_Gbias.z = tempGz;
} //void fCalculate_GandAbias( )
////////////////////////////////////////////////////////////////////////////////////////////
int myMPU9250::fGetAll( )
{
SPI_Err = 0;
SPI_Err = _spi.fReadSPI( 22 , GetAllDataAddress | SPI_READ );
if ( SPI_Err == 0 )
{
x_Araw.x = 0.0;
x_Araw.y = 0.0;
x_Araw.z = 0.0;
// transform
x_Araw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(2) << 8) | _spi.get_rxData8bit(3)));
x_Araw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(4) << 8) | _spi.get_rxData8bit(5)));
x_Araw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(6) << 8) | _spi.get_rxData8bit(7)));
x_Araw.z *= -1; // a transform operation
// scale and bias
x_Ascaled.x = ( (x_Araw.x * _accelScaleFactor) - x_Abias.x ) * x_ASF.x;
x_Ascaled.y = ( (x_Araw.y * _accelScaleFactor) - x_Abias.y ) * x_ASF.y;
x_Ascaled.z = ( (x_Araw.z * _accelScaleFactor) - x_Abias.z ) * x_ASF.z;
//
tempCount = ((int)(int16_t)(((int16_t)_spi.get_rxData8bit(8) << 8) | _spi.get_rxData8bit(9)));
temperature = ((((float)tempCount) - _tempOffset) / _tempScale) + _tempOffset;
//
x_Graw.x = 0.0f;
x_Graw.y = 0.0f;
x_Graw.z = 0.0f;
// transform, the x input axis is on the y plane
x_Graw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(10) << 8) | _spi.get_rxData8bit(11)));
x_Graw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(12) << 8) | _spi.get_rxData8bit(13)));
x_Graw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(14) << 8) | _spi.get_rxData8bit(15)));
x_Graw.z = (-1 * x_Graw.z); // transformation
// scale
x_Gscaled.x = ( ( x_Graw.x * _gyroScaleFactor ) - x_Gbias.x);
x_Gscaled.y = ( x_Graw.y * _gyroScaleFactor ) - x_Gbias.y;
x_Gscaled.z = ( x_Graw.z * _gyroScaleFactor ) - x_Gbias.z;
x_Mraw.x = 0.0;
x_Mraw.y = 0.0;
x_Mraw.z = 0.0;
x_Mraw.x = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(17) << 8) | _spi.get_rxData8bit(16)));
x_Mraw.y = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(19) << 8) | _spi.get_rxData8bit(18)));
x_Mraw.z = ((float)(int16_t)(((int16_t)_spi.get_rxData8bit(21) << 8) | _spi.get_rxData8bit(20)));
x_Mscaled.x = (x_Mraw.x * mRes * x_MSF.x) - x_Mbias.x;
x_Mscaled.y = (x_Mraw.y * mRes * x_MSF.y) - x_Mbias.y;
x_Mscaled.z = (x_Mraw.z * mRes * x_MSF.z) - x_Mbias.z;
x_Mscaled.x *= x_Mscale.x; // poor man's soft iron calibration
x_Mscaled.y *= x_Mscale.y;
x_Mscaled.z *= x_Mscale.z;
}
return SPI_Err;
}
////////////////////////////////////////////////////////////////////////////////////////////
//void myMPU9250::fDoTempratureConversion( void * parameter )
//{
// while(1)
// {
// xEventGroupWaitBits( _hEG, evtTempratureConversion, pdTRUE, pdTRUE, portMAX_DELAY );
//
// temperature = ((((float)tempCount) - _tempOffset) / _tempScale) + _tempOffset;
// }
//}
////////////////////////////////////////////////////////////////////////////////////////////
Any suggestions, ideas, concepts, solutions?