|
| 1 | +#include "miruLowPassFilterConverter.h" |
| 2 | +#include "architecture/utilities/avsEigenSupport.h" |
| 3 | + |
| 4 | +/*! This method checks the input message to ensure it is linked. |
| 5 | + @return void |
| 6 | + @param callTime [ns] Time the method is called |
| 7 | +*/ |
| 8 | +void MiruLowPassFilterConverter::Reset(uint64_t callTime) { |
| 9 | + if (!this->imuAccelDataInMsg.isLinked()) { |
| 10 | + this->bskLogger->bskLog(BSK_ERROR, "miruLowPassFilterConverter.imuAccelDataInMsg wasn't connected."); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +/*! This method writes the module output message using the given input message. |
| 15 | + @return void |
| 16 | + @param callTime [ns] Time the method is called |
| 17 | +*/ |
| 18 | +void MiruLowPassFilterConverter::UpdateState(uint64_t callTime) { |
| 19 | + auto lowPass = LowPassFilter(); |
| 20 | + int smallestFutureIndex = 0; |
| 21 | + int numberOfValidGyroMeasurements = 0; |
| 22 | + double firstFutureTime = -1; |
| 23 | + double meanMeasurementTime = 0; |
| 24 | + AccDataMsgPayload gyrBuffer = this->imuAccelDataInMsg(); |
| 25 | + for (int index = 0; index < MAX_ACC_BUF_PKT; index++) { |
| 26 | + double gyroMeasuredTime = gyrBuffer.accPkts[index].measTime*NANO2SEC; |
| 27 | + if (gyroMeasuredTime < firstFutureTime || firstFutureTime<0){ |
| 28 | + smallestFutureIndex = index; |
| 29 | + firstFutureTime = gyroMeasuredTime; |
| 30 | + } |
| 31 | + meanMeasurementTime += gyroMeasuredTime; |
| 32 | + numberOfValidGyroMeasurements += 1; |
| 33 | + } |
| 34 | + lowPass.setFilterCutoff(this->cutOffFrequency); |
| 35 | + lowPass.setFilterStep(this->hStep); |
| 36 | + if (numberOfValidGyroMeasurements > 0){ |
| 37 | + meanMeasurementTime /= numberOfValidGyroMeasurements; |
| 38 | + /*! - Loop through buffer for all future measurements since the previous time to filter omega_BN_B*/ |
| 39 | + for (int index = 0; index < MAX_ACC_BUF_PKT; index++) { |
| 40 | + int shiftedIndex = (index + smallestFutureIndex) % MAX_ACC_BUF_PKT; |
| 41 | + auto omega_BN_B = Eigen::Map<Eigen::Vector3d>(gyrBuffer.accPkts[shiftedIndex].gyro_B); |
| 42 | + /*! - Apply low-pass filter to gyro measurements to get smoothed body rate*/ |
| 43 | + lowPass.processMeasurement(omega_BN_B); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Write the output message |
| 48 | + IMUSensorMsgPayload imuSensorOut = IMUSensorMsgPayload(); |
| 49 | + imuSensorOut.timeTag = meanMeasurementTime; |
| 50 | + imuSensorOut.numberOfValidGyroMeasurements = numberOfValidGyroMeasurements; |
| 51 | + Eigen::Vector3d omega_BN_B = lowPass.getCurrentState(); |
| 52 | + eigenVector3d2CArray(omega_BN_B, imuSensorOut.AngVelPlatform); |
| 53 | + this->imuSensorOutMsg.write(&imuSensorOut, moduleID, callTime); |
| 54 | +} |
| 55 | + |
| 56 | +/*! Set the low pass filter parameters |
| 57 | + @param double step |
| 58 | + @param double frequencyCutOff |
| 59 | +*/ |
| 60 | +void MiruLowPassFilterConverter::setLowPassFilter(double step, double frequencyCutOff) { |
| 61 | + this->hStep = step; |
| 62 | + this->cutOffFrequency = frequencyCutOff; |
| 63 | +} |
0 commit comments