Skip to content

Commit 1ce49be

Browse files
committed
integrated sflp with mqekf
1 parent 677039d commit 1ce49be

File tree

8 files changed

+45
-24
lines changed

8 files changed

+45
-24
lines changed

MIDAS/src/data_logging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void log_begin(LogSink& sink) {
7878
*/
7979
void log_data(LogSink& sink, RocketData& data) {
8080
log_from_sensor_data(sink, data.imu);
81-
log_from_sensor_data(sink, data.hw_filtered);
81+
log_from_sensor_data(sink, data.sflp);
8282
log_from_sensor_data(sink, data.barometer);
8383
log_from_sensor_data(sink, data.voltage);
8484
log_from_sensor_data(sink, data.gps);

MIDAS/src/gnc/mqekf.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,20 @@ QuaternionMEKF::QuaternionMEKF()
7575
state = AngularKalmanData();
7676
}
7777

78-
void QuaternionMEKF::tick(float dt, Magnetometer &magnetometer, Velocity &angular_velocity, Acceleration &acceleration, FSMState FSM_state)
78+
void QuaternionMEKF::tick(float dt, Magnetometer &magnetometer, Velocity &angular_velocity, Acceleration &acceleration, FSMState FSM_state, Velocity &gyro_bias_sflp)
7979
{
8080
if (FSM_state >= FSMState::STATE_IDLE) //
8181
{
8282

8383
// setQ(dt, sd);
8484
// priori(dt, orientation, FSM_state, acceleration);
8585
// update(barometer, acceleration, orientation, FSM_state, gps);
86-
86+
x(3) = gyro_bias_sflp.vx * pi/180;
87+
x(4) = gyro_bias_sflp.vy* pi/180;
88+
x(5) = gyro_bias_sflp.vz* pi/180;
8789
time_update(angular_velocity, dt);
8890
measurement_update(acceleration, magnetometer);
91+
8992
calculate_tilt();
9093
Eigen::Matrix<float, 4, 1> curr_quat = quaternion(); // w,x,y,z
9194

@@ -355,11 +358,6 @@ AngularKalmanData QuaternionMEKF::getState()
355358
void QuaternionMEKF::calculate_tilt()
356359
{
357360

358-
const float alpha = 0.98; // Higher values dampen out current measurements --> reduce peaks
359-
360-
// The guess & check method!
361-
// Quat --> euler --> rotation matrix --> reference&cur vector --> dot product for angle!
362-
363361
Eigen::Quaternion<float>
364362
ref = Eigen::Quaternionf(0, 0, 0, 1);
365363

@@ -379,12 +377,33 @@ void QuaternionMEKF::calculate_tilt()
379377
}
380378

381379
// const float gain = 0.2;
382-
// // Arthur's Comp Filter
380+
// Arthur's Comp Filter
383381
// float filtered_tilt = gain * tilt + (1 - gain) * prev_tilt;
384382
// prev_tilt = filtered_tilt;
385383
state.mq_tilt = tilt;
384+
}
385+
386+
387+
void QuaternionMEKF::calculate_tilt(IMU_SFLP imu_sflp)
388+
{
389+
390+
Eigen::Vector3f ref(1,0,0);
391+
392+
Eigen::Quaternion<float> sflp_quat = Eigen::Quaternionf(imu_sflp.quaternion.w,imu_sflp.quaternion.x,imu_sflp.quaternion.y,imu_sflp.quaternion.z);
393+
sflp_quat.normalize();
394+
395+
Eigen::Vector3f rotated_vector = sflp_quat * ref;
396+
397+
Eigen::Vector3f world_vertical(0, 0, 1);
398+
float dot = rotated_vector.dot(world_vertical);
399+
float cur_mag = rotated_vector.norm();
400+
float tilt = 0;
401+
if (cur_mag != 0)
402+
{
403+
tilt = acos(dot / (cur_mag));
404+
}
386405

387-
// Serial.print("TILT: ");
406+
state.sflp_tilt = tilt;
388407
}
389408

390409
QuaternionMEKF mqekf;

MIDAS/src/gnc/mqekf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class QuaternionMEKF
2323
Eigen::Ref<Eigen::Matrix<float, 3, 1> const> const &vhat,
2424
Eigen::Ref<Eigen::Matrix<float, 3, 3> const> const &Rm);
2525

26-
void tick(float dt, Magnetometer &magnetometer, Velocity &angular_velocity, Acceleration &acceleration, FSMState FSM_state);
26+
void tick(float dt, Magnetometer &magnetometer, Velocity &angular_velocity, Acceleration &acceleration, FSMState FSM_state, Velocity &gyro_bias_sflp);
2727
Eigen::Matrix<float, 4, 1> quaternion();
2828
Eigen::Matrix<float, 6, 6> covariance();
2929
Eigen::Matrix<float, 3, 1> gyroscope_bias();
@@ -32,7 +32,8 @@ class QuaternionMEKF
3232
AngularKalmanData getState();
3333
Eigen::Matrix<float, 3, 1> quatToEuler(const Eigen::Matrix<float, 4, 1> &q);
3434
// void tick(float dt, float sd, );
35-
void calculate_tilt();
35+
void calculate_tilt(); //using internal quaternion
36+
void calculate_tilt(IMU_SFLP imu_sflp); //using SFLP quaternion
3637
void set_transition_matrix(const Eigen::Ref<const Eigen::Matrix<float, 3, 1>> &gyr, float Ts);
3738
Eigen::Matrix<float, 3, 3> skew_symmetric_matrix(const Eigen::Ref<const Eigen::Matrix<float, 3, 1>> &vec) const;
3839
Eigen::Matrix<float, 3, 1> magnetometer_measurement_func() const;

MIDAS/src/hardware/Pyro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool error_is_failure(GpioError error_code) {
2626
*/
2727
bool can_fire_igniter(AngularKalmanData angular_kalman_data) {
2828
// With new GNC orientation code we can add a simple check.
29-
return angular_kalman_data.comp_tilt < MAXIMUM_TILT_ANGLE; // comp_tilt or mq_tilt?
29+
return angular_kalman_data.sflp_tilt < MAXIMUM_TILT_ANGLE;
3030
}
3131

3232

MIDAS/src/rocket_state.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ struct RocketData {
189189
SensorData<KalmanData> kalman;
190190
SensorData<AngularKalmanData> angular_kalman_data;
191191
BufferedSensorData<IMU, 16> imu;
192-
SensorData<IMU_SFLP> hw_filtered;
192+
SensorData<IMU_SFLP> sflp;
193193
BufferedSensorData<Barometer, 16> barometer;
194194
SensorData<PyroState> pyro;
195195
SensorData<FSMState> fsm_state;

MIDAS/src/sensor_data.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct Magnetometer {
122122
double mz;
123123
};
124124

125-
struct Quaternion {
125+
struct Quaternion { //long term, remove or rename this struct, it will conflict with libraries where Quaternion is well defined.
126126
float w, x, y, z;
127127

128128
static float dot(const Quaternion& q1, const Quaternion& q2) {
@@ -273,7 +273,8 @@ struct KalmanData {
273273
struct AngularKalmanData {
274274
Quaternion quaternion;
275275
float gyrobias[3];
276-
double comp_tilt = 0.0;
276+
double sflp_tilt = 0.0;
277+
// double comp_tilt = 0.0;
277278
double mq_tilt = 0.0;
278279
bool has_data = false;
279280
OrientationReadingType reading_type = OrientationReadingType::FULL_READING;

MIDAS/src/systems.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ DECLARE_THREAD(imuthread, RocketSystems *arg)
8080
{
8181
xSemaphoreTake(spi_mutex, portMAX_DELAY);
8282
IMU imudata = arg->sensors.imu.read();
83-
IMU_SFLP hw_filter = arg->sensors.imu.read_sflp();
83+
IMU_SFLP sflp = arg->sensors.imu.read_sflp();
8484
xSemaphoreGive(spi_mutex);
8585

8686
// Sensor calibration, if it is triggered.
@@ -101,7 +101,7 @@ DECLARE_THREAD(imuthread, RocketSystems *arg)
101101
imudata.highg_acceleration.az = imudata.highg_acceleration.az + bias.az;
102102

103103
arg->rocket_data.imu.update(imudata);
104-
arg->rocket_data.hw_filtered.update(hw_filter);
104+
arg->rocket_data.sflp.update(sflp);
105105

106106
THREAD_SLEEP(5);
107107
}
@@ -276,13 +276,13 @@ DECLARE_THREAD(angularkalman, RocketSystems *arg)
276276
TickType_t last = xTaskGetTickCount();
277277
arg->rocket_data.command_flags.should_reset_kf = false;
278278
}
279-
279+
IMU_SFLP current_imu_sflp = arg->rocket_data.sflp.getRecent();
280280
IMU current_imu = arg->rocket_data.imu.getRecent();
281281
Acceleration current_high_g = current_imu.highg_acceleration;
282282
Acceleration current_low_g = current_imu.lowg_acceleration;
283283
Magnetometer current_mag = arg->rocket_data.magnetometer.getRecent();
284284
Velocity current_angular_velocity = current_imu.angular_velocity; // degrees
285-
285+
Velocity current_gyro_bias = current_imu_sflp.gyro_bias;
286286
AngularKalmanData current_angular_kalman = arg->rocket_data.angular_kalman_data.getRecent();
287287

288288
Acceleration current_accelerations = {
@@ -294,8 +294,8 @@ DECLARE_THREAD(angularkalman, RocketSystems *arg)
294294
float timestamp = pdTICKS_TO_MS(xTaskGetTickCount()) / 1000.0f;
295295

296296
// Check with Divij
297-
mqekf.tick(dt, current_mag, current_angular_velocity,current_accelerations, FSM_state);
298-
297+
mqekf.tick(dt, current_mag, current_angular_velocity, current_accelerations, FSM_state, current_gyro_bias);
298+
mqekf.calculate_tilt(current_imu_sflp);
299299
AngularKalmanData current_state = mqekf.getState();
300300

301301
arg->rocket_data.angular_kalman_data.update(current_state);

MIDAS/src/telemetry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ TelemetryPacket Telemetry::makePacket(RocketData& data) {
8383

8484
TelemetryPacket packet { };
8585
IMU imu = data.imu.getRecentUnsync();
86-
IMU_SFLP hw_filtered_data = data.hw_filtered.getRecentUnsync();
86+
IMU_SFLP sflp_data = data.sflp.getRecentUnsync();
8787
GPS gps = data.gps.getRecentUnsync();
8888
Voltage voltage = data.voltage.getRecentUnsync();
8989
Barometer barometer = data.barometer.getRecentUnsync();
@@ -111,7 +111,7 @@ TelemetryPacket Telemetry::makePacket(RocketData& data) {
111111

112112
// Tilt & FSM State --> comp_tilt vs mq_tilt
113113
static_assert(FSMState::FSM_STATE_COUNT < 16);
114-
uint16_t tilt_norm = (angular_kalman.mq_tilt / M_PI) * 0x0fff; // Encodes tilt value 0-1 into range 0x0000 - 0x0fff
114+
uint16_t tilt_norm = (angular_kalman.sflp_tilt / M_PI) * 0x0fff; // Encodes tilt value 0-1 into range 0x0000 - 0x0fff
115115
packet.tilt_fsm |= ((tilt_norm << 4) & 0xfff0);
116116
packet.tilt_fsm |= ((uint16_t)fsm & 0x000f);
117117

0 commit comments

Comments
 (0)