AHRS
Dependencies: Eigen
AHRS.cpp
- Committer:
- altb2
- Date:
- 2019-10-02
- Revision:
- 19:42ea6dd68185
- Parent:
- 17:f9eed26536d9
- Child:
- 20:1182bc29c195
File content as of revision 19:42ea6dd68185:
#include "AHRS.h" #include "Mahony.h" #include "MadgwickAHRS.h" #include "ekf.h" #define PI 3.141592653589793 using namespace std; //OLD: AHRS::AHRS(uint8_t filtertype, float TS) : RPY_filter(TS), csAG(PA_15),csM(PD_2), spi(PC_12, PC_11, PC_10), imu(&spi, &csM, &csAG), thread(osPriorityBelowNormal, 4096){ AHRS::AHRS(uint8_t filtertype, float TS, bool calib) : imu(PC_9, PA_8, 0xD6, 0x3C), ekf(TS), Mahony_filter(TS), thread(osPriorityBelowNormal, 4096) { /* setup storage */ float gyro[3], accel[3], magnet[3]; for(uint8_t i = 0; i < 3; i++) { gyro[i] = 0.0f; accel[i] = 0.0f; magnet[i] = 0.0f; } /* inform user if imu is not responsive */ while (!imu.begin()) { wait(1); printf("Failed to communicate with LSM9DS1.\r\n"); } /* take mean value of N samples */ uint8_t N = 200; if(calib) { wait_ms(500); /* read and cumsum data */ for(uint16_t i = 0; i < N; i++) { imu.readGyro(); imu.readAccel(); imu.readMag(); gyro[0] += imu.gyroX; gyro[1] += imu.gyroY; gyro[2] += imu.gyroZ; accel[0] += imu.accX; accel[1] += imu.accY; accel[2] += imu.accZ; magnet[0] += imu.magX; magnet[1] += imu.magY; magnet[2] += imu.magZ; wait_ms(10); } /* calculate mean value */ for(uint16_t i = 0; i < 3; i++) { gyro[i] /= (float)N; accel[i] /= (float)N; magnet[i] /= (float)N; } printf("Correct gyro: %1.5f %1.5f %1.5f accel: %1.5f %1.5f %1.5f magnet: %1.5f %1.5f %1.5f\r\n", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2], magnet[0], magnet[1], magnet[2]); } /* gyro */ raw_gx2gx.setup( 1.0, gyro[0]); raw_gy2gy.setup(-1.0, gyro[1]); // y-axis reversed (lefthanded system) raw_gz2gz.setup( 1.0, gyro[2]); // pmic, 23.09.2019 // calibration for acc and mag needs to be redone on real copter pes board with power on and spinning motors (no propellers) /* accel */ raw_ax2ax.setup( 1.0f, accel[0]); // use gain and offset here raw_ay2ay.setup(-1.0f, accel[1]); // y-axis reversed // was -1,0.0 raw_az2az.setup( 1.0, 0.0f); // do not remove gravity! /* magnet */ /* Caibration on 25.9.2019 QK2 delivers: A=[ 0.9904 0 0; 0.0605 1.0158 0;0.0397 -0.0199 0.9938] b0= [0.4160 0.1587 -0.0998], see T:\T-IMS-IndNav\01_Technisches\09_PES_Board_IMU\meas_mag_aFMA\calib_mag_QK2.m */ raw_mx2mx.setup( 0.9904f,0.4160f); raw_my2my.setup( 1.0158f, 0.1587f); raw_mz2mz.setup(-0.9938f, -0.0998f); this->magnet_cal_0[0] = raw_mx2mx(magnet[0]); this->magnet_cal_0[1] = raw_my2my(magnet[1]); this->magnet_cal_0[2] = raw_mz2mz(magnet[2]); local_time = 0.0; this->filtertype = filtertype; // the thread starts thread.start(callback(this, &AHRS::update)); ticker.attach(callback(this, &AHRS::sendSignal), TS); } AHRS::~AHRS() {} void AHRS::update(void) { if(filtertype !=1) Mahony_filter.update(data.sens_gyr[0],data.sens_gyr[1],data.sens_gyr[2],data.sens_acc[0],data.sens_acc[1],data.sens_acc[2]); else ekf.update(data.sens_gyr[0],data.sens_gyr[1],data.sens_gyr[2],data.sens_acc[0],data.sens_acc[1],data.sens_acc[2]); } // while(1) (the thread) }