Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

31 estimator ekf refactor #68

Merged
merged 26 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fc18b30
Renamed estimator files.
iandareid Jun 26, 2024
ebaf7a4
Created estimator_ekf.cpp in prep for refactor.
iandareid Jun 26, 2024
74fc930
Refactored CMakeLists and made extendable for full refactor.
iandareid Jun 26, 2024
a23e055
Refactored the estimator classes.
iandareid Jun 26, 2024
d857fab
Added the include file for the ekf.
iandareid Jun 27, 2024
146cb5a
Added files for ekf and did the initial port. !UNTESTED!
iandareid Jun 27, 2024
515c87c
Added overlooked update functions. Removed checks.
iandareid Jun 27, 2024
1a933e1
Added the first dynamic model to the estimator_continuous_discrete.
iandareid Jun 27, 2024
092d8fb
Added attitude Jacobian and updated ekf class accordingly.
iandareid Jun 27, 2024
c6007f2
Fully replaced the Attitude model propagation with the ekf
iandareid Jun 27, 2024
d084b2c
Swapped attitude over to the new ekf functions, tested!
iandareid Jun 27, 2024
75b022d
Moved the defintions to a more sensible location.
iandareid Jun 27, 2024
d91c233
Replaced the position dynamic propagation.
iandareid Jun 28, 2024
fead1d4
Added attitude step jacobian.
iandareid Jun 28, 2024
f7a0a8b
Merge remote-tracking branch 'origin/main' into 31-estimator-ekf-refa…
iandareid Jul 1, 2024
5b8e0c7
Fixed the CMakeLists from param_manager changes.
iandareid Jul 1, 2024
ebf8ed3
Working refactor of the entire EKF using single measurement updates.
iandareid Jul 11, 2024
335954e
Fixed pseudo measurment update.
iandareid Jul 11, 2024
8ecc0ed
Added documentation for the measurment prediction and Jacobians.
iandareid Jul 11, 2024
1803b13
Temporarily fixed heading issue.
iandareid Jul 11, 2024
e8fab08
Added one step position estimate.
iandareid Jul 11, 2024
0e7434a
Added more clean binding.
iandareid Jul 11, 2024
aef48bd
Merge branch 'main' into 31-estimator-ekf-refactor
iandareid Jul 12, 2024
93228bc
Wrapped up the merge with the header files that were renamed.
iandareid Jul 12, 2024
03761d1
Fixed compilation errors after merge.
iandareid Jul 12, 2024
a54788a
re-organized include order
bsutherland333 Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rosplane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ install(TARGETS

# Estimator
add_executable(rosplane_estimator_node
src/estimator_base.cpp
src/estimator_example.cpp)
src/estimator_ros.cpp
src/estimator_ekf.cpp
src/estimator_continuous_discrete.cpp)
target_link_libraries(rosplane_estimator_node
${YAML_CPP_LIBRARIES}
)
Expand Down
126 changes: 126 additions & 0 deletions rosplane/include/estimator_continuous_discrete.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#ifndef ESTIMATOR_CONTINUOUS_DISCRETE_H
#define ESTIMATOR_CONTINUOUS_DISCRETE_H

#include <math.h>

#include <Eigen/Geometry>
#include <yaml-cpp/yaml.h>

#include "estimator_ekf.hpp"
#include "estimator_ros.hpp"

namespace rosplane
{

class EstimatorContinuousDiscrete : public EstimatorEKF
{
public:
EstimatorContinuousDiscrete();
EstimatorContinuousDiscrete(bool use_params);

private:
virtual void estimate(const Input & input, Output & output);

double lpf_a_;
float alpha_;
float alpha1_;
int N_;

float lpf_gyro_x_;
float lpf_gyro_y_;
float lpf_gyro_z_;
float lpf_static_;
float lpf_diff_;
float lpf_accel_x_;
float lpf_accel_y_;
float lpf_accel_z_;

float phat_;
float qhat_;
float rhat_;
float Vwhat_;
float phihat_;
float thetahat_;
float psihat_; // TODO: link to an inital condiditons param


Eigen::VectorXf attitude_dynamics(const Eigen::VectorXf& state, const Eigen::VectorXf& measurements);
std::function<Eigen::VectorXf(const Eigen::VectorXf, const Eigen::VectorXf)> attitude_dynamics_model;

Eigen::MatrixXf attitude_jacobian(const Eigen::VectorXf& state, const Eigen::VectorXf& angular_rates);
std::function<Eigen::MatrixXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> attitude_jacobian_model;

Eigen::MatrixXf attitude_input_jacobian(const Eigen::VectorXf& state, const Eigen::VectorXf& angular_rates);
std::function<Eigen::MatrixXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> attitude_input_jacobian_model;

Eigen::VectorXf attitude_measurement_prediction(const Eigen::VectorXf& state, const Eigen::VectorXf& inputs);
std::function<Eigen::VectorXf(const Eigen::VectorXf, const Eigen::VectorXf)> attitude_measurement_model;

Eigen::MatrixXf attitude_measurement_jacobian(const Eigen::VectorXf& state, const Eigen::VectorXf& inputs);
std::function<Eigen::MatrixXf(const Eigen::VectorXf, const Eigen::VectorXf)> attitude_measurement_jacobian_model;

Eigen::VectorXf position_dynamics(const Eigen::VectorXf& state, const Eigen::VectorXf& measurements);
std::function<Eigen::VectorXf(const Eigen::VectorXf, const Eigen::VectorXf)> position_dynamics_model;

Eigen::MatrixXf position_jacobian(const Eigen::VectorXf& state, const Eigen::VectorXf& measurements);
std::function<Eigen::MatrixXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> position_jacobian_model;

Eigen::MatrixXf position_input_jacobian(const Eigen::VectorXf& state, const Eigen::VectorXf& inputs);
std::function<Eigen::MatrixXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> position_input_jacobian_model;

Eigen::VectorXf position_measurement_prediction(const Eigen::VectorXf& state, const Eigen::VectorXf& input);
std::function<Eigen::VectorXf(const Eigen::VectorXf, const Eigen::VectorXf)> position_measurement_model;

Eigen::MatrixXf position_measurement_jacobian(const Eigen::VectorXf& state, const Eigen::VectorXf& input);
std::function<Eigen::MatrixXf(const Eigen::VectorXf, const Eigen::VectorXf)> position_measurement_jacobian_model;

Eigen::Vector2f xhat_a_; // 2
Eigen::Matrix2f P_a_; // 2x2

Eigen::VectorXf xhat_p_; // 7
Eigen::MatrixXf P_p_; // 7x7

Eigen::Matrix2f Q_a_; // 2x2
Eigen::Matrix3f Q_g_;
Eigen::Matrix3f R_accel_;

Eigen::MatrixXf Q_p_; // 7x7
Eigen::MatrixXf R_p_; // 6x6
Eigen::VectorXf f_p_; // 7
Eigen::MatrixXf A_p_; // 7x7
float h_p_;
Eigen::VectorXf C_p_; // 7
Eigen::VectorXf L_p_; // 7

// TODO: not used
float gate_threshold_ = 9.21; // chi2(q = .01, df = 2)

void check_xhat_a();

void bind_functions();

/**
* @brief This declares each parameter as a parameter so that the ROS2 parameter system can recognize each parameter.
* It also sets the default parameter, which will then be overridden by a launch script.
*/
void declare_parameters();

/**
* @brief Initializes some variables that depend on ROS2 parameters
*/
void update_measurement_model_parameters();

/**
* @brief Initializes the covariance matrices and process noise matrices with the ROS2 parameters
*/
void initialize_uncertainties();

/**
* @brief Initializes the state covariance matrix with the ROS2 parameters
*/
void initialize_state_covariances();
};

} // namespace rosplane

#endif // ESTIMATOR_CONTINUOUS_DISCRETE_H
53 changes: 53 additions & 0 deletions rosplane/include/estimator_ekf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef ESTIMATOR_EKF_H
#define ESTIMATOR_EKF_H

#include <cassert>
#include <math.h>
#include <tuple>

#include <Eigen/Geometry>
#include <yaml-cpp/yaml.h>

#include "estimator_ros.hpp"

namespace rosplane
{

class EstimatorEKF : public EstimatorROS
{
public:
EstimatorEKF();

protected:
std::tuple<Eigen::MatrixXf, Eigen::VectorXf> measurement_update(Eigen::VectorXf x,
Eigen::VectorXf inputs,
std::function<Eigen::VectorXf(const Eigen::VectorXf, const Eigen::VectorXf)> measurement_model,
Eigen::VectorXf y,
std::function<Eigen::MatrixXf(const Eigen::VectorXf, const Eigen::VectorXf)> measurement_jacobian,
Eigen::MatrixXf R,
Eigen::MatrixXf P);

std::tuple<Eigen::MatrixXf, Eigen::VectorXf> propagate_model(Eigen::VectorXf x,
std::function<Eigen::VectorXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> dynamic_model,
std::function<Eigen::MatrixXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> jacobian,
Eigen::VectorXf inputs,
std::function<Eigen::MatrixXf(const Eigen::VectorXf&, const Eigen::VectorXf&)> input_jacobian,
Eigen::MatrixXf P,
Eigen::MatrixXf Q,
Eigen::MatrixXf Q_g,
float Ts);

std::tuple<Eigen::MatrixXf, Eigen::VectorXf> single_measurement_update(float measurement,
float mesurement_prediction,
float measurement_uncertainty,
Eigen::VectorXf measurement_jacobian,
Eigen::VectorXf x,
Eigen::MatrixXf P);

private:
virtual void estimate(const Input & input, Output & output) override = 0;
};

} // namespace rosplane

#endif // ESTIMATOR_EKF_H
99 changes: 0 additions & 99 deletions rosplane/include/estimator_example.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
/**
* @file estimator_base.h
* @file estimator_ros.h
*
* Base class definition for autopilot estimator in chapter 8 of UAVbook, see http://uavbook.byu.edu/doku.php
* ROS-interface class definition for autopilot estimator in chapter 8 of UAVbook, see http://uavbook.byu.edu/doku.php
*
* @author Gary Ellingson <gary.ellingson@byu.edu>
* Based on orignal work by Gary Ellingson.
*
* @author Ian Reid <ian.reid@byu.edu>
*/

#ifndef ESTIMATOR_BASE_H
#define ESTIMATOR_BASE_H
#ifndef ESTIMATOR_ROS_H
#define ESTIMATOR_ROS_H

#include <chrono>
#include <yaml-cpp/yaml.h>

#include <ament_index_cpp/get_package_share_directory.hpp>
#include <geometry_msgs/msg/twist_stamped.hpp>
#include <rclcpp/rclcpp.hpp>
#include <rosflight_msgs/msg/airspeed.hpp>
#include <rosflight_msgs/msg/barometer.hpp>
#include <rosflight_msgs/msg/status.hpp>
#include <sensor_msgs/msg/imu.hpp>
#include <sensor_msgs/msg/nav_sat_fix.hpp>
#include <yaml-cpp/yaml.h>

#include "param_manager.hpp"
#include "rosplane_msgs/msg/state.hpp"
Expand All @@ -31,10 +34,10 @@ using namespace std::chrono_literals;
namespace rosplane
{

class EstimatorBase : public rclcpp::Node
class EstimatorROS : public rclcpp::Node
{
public:
EstimatorBase();
EstimatorROS();

protected:
struct Input
Expand Down Expand Up @@ -82,7 +85,6 @@ class EstimatorBase : public rclcpp::Node
virtual void estimate(const Input & input,
Output & output) = 0;

protected:
ParamManager params_;
bool gps_init_;
double init_lat_ = 0.0; /**< Initial latitude in degrees */
Expand Down Expand Up @@ -139,7 +141,7 @@ class EstimatorBase : public rclcpp::Node
void declare_parameters();

/**
* @brief Determines the period of a timer based on the ROS2 parameter and starts it
* @brief Determines the period of a timer rosd on the ROS2 parameter and starts it
*/
void set_timer();

Expand All @@ -162,4 +164,4 @@ class EstimatorBase : public rclcpp::Node

} // namespace rosplane

#endif // ESTIMATOR_BASE_H
#endif // ESTIMATOR_ROS_H
Loading
Loading