Skip to content

Commit 8ecd714

Browse files
sh1shirkbalaji4SZhOU-crbhog
authored
Kf onboarding (#58)
* Co-authored-by: SZhOU-c <SZhOU-c@users.noreply.github.com> * ] Co-authored-by: SZhOU-c <SZhOU-c@users.noreply.github.com> Co-authored-by: sh1shir <sh1shir@users.noreply.github.com> Co-authored-by: Rithvik Bhogavilli <rbhogavilli@gmail.com> * Co-authored-by: sh1shir <sh1shir@users.noreply.github.com> Co-authored-by: SZhOU-c <SZhOU-c@users.noreply.github.com> * Co-authored-by: Ishaan Kandamuri <ishaank21@users.noreply.github.com> Co-authored-by: williamyeh1 <williamyeh1@users.noreply.github.com> Co-authored-by: sh1shir <sh1shir@users.noreply.github.com> * Added Thread Sleep * Implemented Kalman Filter into yessir.cpp * changed the P_k update equation to match kalmanfilter.net * reverting the P_k update equation * addressed Nick's comments * switched from lowG accel to highG accel * added the KF reset in `system.cpp` * redoing the dumb reset i had * removed `head` function in `Buffer.h` --------- Co-authored-by: keshavbalaji <keshavbalaji2397@gmail.com> Co-authored-by: SZhOU-c <SZhOU-c@users.noreply.github.com> Co-authored-by: sh1shir <sh1shir@users.noreply.github.com> Co-authored-by: Rithvik Bhogavilli <rbhogavilli@gmail.com>
1 parent de66dd4 commit 8ecd714

File tree

8 files changed

+402
-53
lines changed

8 files changed

+402
-53
lines changed

MIDAS/src/Buffer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ struct Buffer {
7777
}
7878
return arr;
7979
}
80+
81+
/**
82+
* @brief Reads a range of items into a passed array, which can be larger than the actual count of items.
83+
*
84+
* @param write_to An array of at least n items to write to
85+
* @param start The index to start reading from (inclusive)
86+
* @param length the number of items to read
87+
* @return How many items were actually read
88+
*/
89+
// TODO make this function return a std::array?
90+
size_t readSlice(T write_to[], size_t start, size_t len) {
91+
size_t i = 0;
92+
size_t idx = oldest_idx() + start;
93+
while (i < len) {
94+
write_to[i++] = buffer[idx++];
95+
if (idx == BUFFER_SIZE) {
96+
idx = 0;
97+
}
98+
}
99+
return i;
100+
}
80101

81102

82103
private:

MIDAS/src/gnc/example_kf.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@
22

33
ExampleKalmanFilter::ExampleKalmanFilter() : KalmanFilter() {}
44

5-
void ExampleKalmanFilter::initialize() {}
5+
void ExampleKalmanFilter::initialize(RocketSystems* args) {}
66

77

88
void ExampleKalmanFilter::priori() {
99
x_priori = (F_mat * x_k);
1010
P_priori = (F_mat * P_k * F_mat.transpose()) + Q;
1111
}
1212

13-
void ExampleKalmanFilter::update() {}
13+
void ExampleKalmanFilter::update(Barometer barometer, Acceleration acceleration, Orientation orientation, FSMState current_stat) {}
1414

1515
void ExampleKalmanFilter::setQ(float dt, float sd) {}
1616

1717
void ExampleKalmanFilter::setF(float dt) {}
1818

1919
KalmanData ExampleKalmanFilter::getState() { return KalmanData(); }
2020

21-
void ExampleKalmanFilter::setState(KalmanData state)
21+
void ExampleKalmanFilter::setState(KalmanState state)
2222
{
23-
this->state = state;
23+
this->state.position.px = state.state_est_pos_x;
24+
this->state.position.py = state.state_est_pos_y;
25+
this->state.position.pz = state.state_est_pos_z;
26+
this->state.acceleration.ax = state.state_est_accel_x;
27+
this->state.acceleration.ay = state.state_est_accel_y;
28+
this->state.acceleration.az = state.state_est_accel_z;
29+
this->state.velocity.vx =state.state_est_vel_x;
30+
this->state.velocity.vy =state.state_est_vel_y;
31+
this->state.velocity.vz =state.state_est_vel_z;
2432
}
2533

2634
ExampleKalmanFilter example_kf;

MIDAS/src/gnc/example_kf.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
#include "kalman_filter.h"
44

55
// makes a kalman filter with 9 state variables and 3 sensor inputs
6-
class ExampleKalmanFilter : public KalmanFilter<9, 3>
6+
class ExampleKalmanFilter : public KalmanFilter<9, 4>
77
{
88
public:
99
ExampleKalmanFilter();
1010

11-
void initialize() override;
11+
void initialize(RocketSystems* args) override;
12+
//virtual void initialize(Orientation &orientation, Barometer &barometer, Acceleration &Acceleration);
1213
void priori() override;
13-
void update() override;
14+
void update(Barometer barometer, Acceleration acceleration, Orientation orientation, FSMState current_state) override;
15+
16+
KalmanData getState() override;
17+
void setState(KalmanState state) override;
1418

1519
void setQ(float dt, float sd);
1620
void setF(float dt);
1721

18-
KalmanData getState() override;
19-
void setState(KalmanData state) override;
20-
2122
Eigen::Matrix<float, 3, 1> bodyToGlobal(euler_t angles, Eigen::Matrix<float, 3, 1> x_k);
2223

2324
private:
2425
KalmanData state;
26+
KalmanState kalman_state;
2527
};
2628

27-
extern ExampleKalmanFilter example_kf;
29+
extern ExampleKalmanFilter example_kf;

MIDAS/src/gnc/kalman_filter.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
#include <Eigen/Eigen>
99
#include "sensor_data.h"
10+
#include "systems.h"
11+
12+
13+
struct KalmanState {
14+
float state_est_pos_x;
15+
float state_est_vel_x;
16+
float state_est_accel_x;
17+
float state_est_pos_y;
18+
float state_est_vel_y;
19+
float state_est_accel_y;
20+
float state_est_pos_z;
21+
float state_est_vel_z;
22+
float state_est_accel_z;
23+
};
1024

1125
template <int _NumStates, int _NumInputs>
1226
class KalmanFilter
@@ -41,11 +55,11 @@ class KalmanFilter
4155

4256
B = Eigen::Matrix<float, _NumStates, _NumInputs>::Zero();
4357
}
44-
45-
virtual void initialize() = 0;
58+
59+
virtual void initialize(RocketSystems* args) = 0;
4660
virtual void priori() = 0;
47-
virtual void update() = 0;
61+
virtual void update(Barometer barometer, Acceleration acceleration, Orientation orientation, FSMState current_state) = 0;
4862

4963
virtual KalmanData getState() = 0;
50-
virtual void setState(KalmanData state) = 0;
64+
virtual void setState(KalmanState state) = 0;
5165
};

0 commit comments

Comments
 (0)