mirror of
https://github.com/iNavFlight/inav.git
synced 2025-07-25 09:16:01 +03:00
implemented magnethometer simulation
This commit is contained in:
parent
3b381093ff
commit
57e14001e8
4 changed files with 58 additions and 7 deletions
|
@ -3418,7 +3418,7 @@ bool mspFCProcessInOutCommand(uint16_t cmdMSP, sbuf_t *dst, sbuf_t *src, mspResu
|
||||||
break;
|
break;
|
||||||
case MSP_SIMULATOR:
|
case MSP_SIMULATOR:
|
||||||
tmp_u8 = sbufReadU8(src); //MSP_SIMULATOR version
|
tmp_u8 = sbufReadU8(src); //MSP_SIMULATOR version
|
||||||
if (tmp_u8 != 1) break;
|
if (tmp_u8 != 2) break;
|
||||||
|
|
||||||
simulatorData.flags = sbufReadU8(src);
|
simulatorData.flags = sbufReadU8(src);
|
||||||
if ((simulatorData.flags & SIMU_ENABLE) == 0) {
|
if ((simulatorData.flags & SIMU_ENABLE) == 0) {
|
||||||
|
@ -3428,6 +3428,9 @@ bool mspFCProcessInOutCommand(uint16_t cmdMSP, sbuf_t *dst, sbuf_t *src, mspResu
|
||||||
|
|
||||||
baroStartCalibration();
|
baroStartCalibration();
|
||||||
|
|
||||||
|
DISABLE_STATE(COMPASS_CALIBRATED);
|
||||||
|
compassInit();
|
||||||
|
|
||||||
simulatorData.flags = 0;
|
simulatorData.flags = 0;
|
||||||
//review: many states were affected. reboot?
|
//review: many states were affected. reboot?
|
||||||
|
|
||||||
|
@ -3437,12 +3440,15 @@ bool mspFCProcessInOutCommand(uint16_t cmdMSP, sbuf_t *dst, sbuf_t *src, mspResu
|
||||||
else if (!areSensorsCalibrating()) {
|
else if (!areSensorsCalibrating()) {
|
||||||
if (!ARMING_FLAG(SIMULATOR_MODE)) { // just once
|
if (!ARMING_FLAG(SIMULATOR_MODE)) { // just once
|
||||||
baroStartCalibration();
|
baroStartCalibration();
|
||||||
|
|
||||||
//todo: magnetometer emulation support
|
if (compassConfig()->mag_hardware != 0){
|
||||||
sensorsClear(SENSOR_MAG);
|
sensorsSet(SENSOR_MAG);
|
||||||
mag.magADC[X] = 0;
|
ENABLE_STATE(COMPASS_CALIBRATED);
|
||||||
mag.magADC[Y] = 0;
|
DISABLE_ARMING_FLAG(ARMING_DISABLED_HARDWARE_FAILURE);
|
||||||
mag.magADC[Z] = 0;
|
mag.magADC[X] = 800;
|
||||||
|
mag.magADC[Y] = 0;
|
||||||
|
mag.magADC[Z] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
ENABLE_ARMING_FLAG(SIMULATOR_MODE);
|
ENABLE_ARMING_FLAG(SIMULATOR_MODE);
|
||||||
LOG_D(SYSTEM, "Simulator enabled");
|
LOG_D(SYSTEM, "Simulator enabled");
|
||||||
|
@ -3550,6 +3556,16 @@ bool mspFCProcessInOutCommand(uint16_t cmdMSP, sbuf_t *dst, sbuf_t *src, mspResu
|
||||||
else {
|
else {
|
||||||
sbufAdvance(src,4);
|
sbufAdvance(src,4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sensors(SENSOR_MAG))
|
||||||
|
{
|
||||||
|
mag.magADC[X] = ((int16_t)sbufReadU16(src)) / 20; //16000/20 = 800uT
|
||||||
|
mag.magADC[Y] = ((int16_t)sbufReadU16(src)) / 20;
|
||||||
|
mag.magADC[Z] = ((int16_t)sbufReadU16(src)) / 20;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sbufAdvance(src, 2*3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
DISABLE_STATE(GPS_FIX);
|
DISABLE_STATE(GPS_FIX);
|
||||||
|
@ -3561,6 +3577,27 @@ bool mspFCProcessInOutCommand(uint16_t cmdMSP, sbuf_t *dst, sbuf_t *src, mspResu
|
||||||
sbufWriteU16(dst, (uint16_t)input[INPUT_STABILIZED_YAW]);
|
sbufWriteU16(dst, (uint16_t)input[INPUT_STABILIZED_YAW]);
|
||||||
sbufWriteU16(dst, (uint16_t)(ARMING_FLAG(ARMED) ? input[INPUT_STABILIZED_THROTTLE] : -500));
|
sbufWriteU16(dst, (uint16_t)(ARMING_FLAG(ARMED) ? input[INPUT_STABILIZED_THROTTLE] : -500));
|
||||||
|
|
||||||
|
/*
|
||||||
|
debug[0] = mag.magADC[X];
|
||||||
|
debug[1] = mag.magADC[Y];
|
||||||
|
debug[2] = mag.magADC[Z];
|
||||||
|
//debug[3] = sensors(SENSOR_MAG) ? 1 : 0;
|
||||||
|
//debug[4] = STATE(COMPASS_CALIBRATED) ? 1 : 0;
|
||||||
|
fpVector3_t vMag;
|
||||||
|
fpVector3_t magBF;
|
||||||
|
magBF.x = mag.magADC[X];
|
||||||
|
magBF.y = mag.magADC[Y];
|
||||||
|
magBF.z = mag.magADC[Z];
|
||||||
|
quaternionRotateVectorInv(&vMag, &magBF, &orientation); // BF -> EF
|
||||||
|
debug[3] = vMag.x;
|
||||||
|
debug[4] = vMag.y;
|
||||||
|
debug[5] = vMag.z;
|
||||||
|
|
||||||
|
debug[6] = getvCorrectedMagNorth().x*1000;
|
||||||
|
debug[7] = getvCorrectedMagNorth().y*1000;
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
simulatorData.debugIndex++;
|
simulatorData.debugIndex++;
|
||||||
if (simulatorData.debugIndex == 8){
|
if (simulatorData.debugIndex == 8){
|
||||||
simulatorData.debugIndex = 0;
|
simulatorData.debugIndex = 0;
|
||||||
|
|
|
@ -327,6 +327,10 @@ static void imuMahonyAHRSupdate(float dt, const fpVector3_t * gyroBF, const fpVe
|
||||||
// Normalize to unit vector
|
// Normalize to unit vector
|
||||||
vectorNormalize(&vMag, &vMag);
|
vectorNormalize(&vMag, &vMag);
|
||||||
|
|
||||||
|
if (ARMING_FLAG(SIMULATOR_MODE)) {
|
||||||
|
imuSetMagneticDeclination(0);
|
||||||
|
}
|
||||||
|
|
||||||
// Reference mag field vector heading is Magnetic North in EF. We compute that by rotating True North vector by declination and assuming Z-component is zero
|
// Reference mag field vector heading is Magnetic North in EF. We compute that by rotating True North vector by declination and assuming Z-component is zero
|
||||||
// magnetometer error is cross product between estimated magnetic north and measured magnetic north (calculated in EF)
|
// magnetometer error is cross product between estimated magnetic north and measured magnetic north (calculated in EF)
|
||||||
vectorCrossProduct(&vErr, &vMag, &vCorrectedMagNorth);
|
vectorCrossProduct(&vErr, &vMag, &vCorrectedMagNorth);
|
||||||
|
|
|
@ -369,6 +369,11 @@ bool compassIsCalibrationComplete(void)
|
||||||
|
|
||||||
void compassUpdate(timeUs_t currentTimeUs)
|
void compassUpdate(timeUs_t currentTimeUs)
|
||||||
{
|
{
|
||||||
|
if (ARMING_FLAG(SIMULATOR_MODE)) {
|
||||||
|
magUpdatedAtLeastOnce = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
static sensorCalibrationState_t calState;
|
static sensorCalibrationState_t calState;
|
||||||
static timeUs_t calStartedAt = 0;
|
static timeUs_t calStartedAt = 0;
|
||||||
static int16_t magPrev[XYZ_AXIS_COUNT];
|
static int16_t magPrev[XYZ_AXIS_COUNT];
|
||||||
|
|
|
@ -63,6 +63,11 @@ hardwareSensorStatus_e getHwAccelerometerStatus(void)
|
||||||
|
|
||||||
hardwareSensorStatus_e getHwCompassStatus(void)
|
hardwareSensorStatus_e getHwCompassStatus(void)
|
||||||
{
|
{
|
||||||
|
if (ARMING_FLAG(SIMULATOR_MODE)) {
|
||||||
|
return HW_SENSOR_OK;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(USE_MAG)
|
#if defined(USE_MAG)
|
||||||
if (detectedSensors[SENSOR_INDEX_MAG] != MAG_NONE) {
|
if (detectedSensors[SENSOR_INDEX_MAG] != MAG_NONE) {
|
||||||
if (compassIsHealthy()) {
|
if (compassIsHealthy()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue