-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add KalmanFilter for rmoss_util #44
Open
baiyeweiguang
wants to merge
10
commits into
robomaster-oss:humble
Choose a base branch
from
baiyeweiguang:humble
base: humble
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d30622d
Add Kalman Filter
baiyeweiguang 61f5dbe
add build_depend in rmoss_util/package.xml
baiyeweiguang 1f4480a
fix ament_target_dependencies() failed in Eigen3
baiyeweiguang e376f10
export dependencies of eigen3_cmake_module
baiyeweiguang 74fc552
link eigen3 in rmoss_util/test/CmakeLists.txt
baiyeweiguang 68db866
add getter of KalmanFilter
baiyeweiguang 6f0b8a5
link Eigen3::Eigen
baiyeweiguang 52a9a86
update workflow
baiyeweiguang 29e716e
rename KalmanFilter to ExtendedKalmanFilter
baiyeweiguang 1b0d23e
fix a bug
baiyeweiguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ jobs: | |
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- name: Pre install | ||
run: | | ||
/usr/bin/bash -c "apt-get update && apt-get install -y libunwind-dev libceres-dev" | ||
- name: Build rmoss_core | ||
uses: ros-tooling/[email protected] | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# find package | ||
find_package(ament_index_cpp REQUIRED) | ||
find_package(rcpputils REQUIRED) | ||
find_package(rclcpp REQUIRED) | ||
find_package(sensor_msgs REQUIRED) | ||
find_package(rmoss_interfaces REQUIRED) | ||
find_package(cv_bridge REQUIRED) | ||
find_package(OpenCV REQUIRED) | ||
|
||
# create rmoss_util::common lib | ||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src COMMON_SRCS) | ||
add_library(common SHARED ${COMMON_SRCS}) | ||
ament_target_dependencies(common | ||
rclcpp | ||
ament_index_cpp | ||
rcpputils | ||
sensor_msgs | ||
rmoss_interfaces | ||
cv_bridge | ||
OpenCV | ||
) | ||
target_include_directories(common PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include> | ||
) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
rmoss_util/test/CMakeLists.txt → rmoss_util/common/test/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
find_package(ament_cmake_gtest REQUIRED) | ||
|
||
ament_add_gtest(test_url_resolve test_url_resolve.cpp) | ||
target_link_libraries(test_url_resolve ${PROJECT_NAME}) | ||
target_link_libraries(test_url_resolve ${PROJECT_NAME}::common) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# find package | ||
find_package(Eigen3 REQUIRED) | ||
find_package(eigen3_cmake_module REQUIRED) | ||
find_package(Ceres REQUIRED) | ||
find_package(OpenCV REQUIRED) | ||
|
||
# create rmoss_util::math lib | ||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src MATH_SRCS) | ||
add_library(math SHARED ${MATH_SRCS}) | ||
ament_target_dependencies(math | ||
eigen3_cmake_module | ||
Eigen3 | ||
Ceres | ||
OpenCV | ||
) | ||
target_include_directories(math PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include> | ||
) |
177 changes: 177 additions & 0 deletions
177
rmoss_util/math/include/rmoss_util/extended_kalman_filter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright 2024 RoboMaster-OSS | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RMOSS_UTIL__EXTENDED_KALMAN_FILTER_HPP_ | ||
#define RMOSS_UTIL__EXTENDED_KALMAN_FILTER_HPP_ | ||
|
||
#include <Eigen/Dense> | ||
#include <ceres/ceres.h> | ||
|
||
// 一些Eigen的工具函数,参考https://github.com/TomLKoller/ADEKF | ||
namespace Eigen | ||
{ | ||
// 获取一个Eigen矩阵的行数 | ||
template<typename Derived> | ||
static constexpr int dof = | ||
internal::traits<typename Derived::PlainObject>::RowsAtCompileTime; | ||
|
||
// 用于绕过Eigen表达式模板,获取表达式结果。如果不使用eval(),auto将无法正常获取表达式结果类型。 | ||
template<typename T> auto eval(const T & result) {return result;} | ||
|
||
template<typename BinaryOp, typename LhsType, typename RhsType> | ||
auto eval(const CwiseBinaryOp<BinaryOp, LhsType, RhsType> & result) | ||
{ | ||
// The eval() function returns the result of the operation | ||
// If this is not used auto will not work correctly | ||
return result.eval(); | ||
} | ||
|
||
template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> | ||
auto eval(const Block<XprType, BlockRows, BlockCols, InnerPanel> & result) | ||
{ | ||
return result.eval(); | ||
} | ||
|
||
} // namespace Eigen | ||
|
||
namespace rmoss_util | ||
{ | ||
|
||
/** | ||
* @class ExtendedKalmanFilter | ||
* | ||
* @brief 可自动微分的扩展卡尔曼滤波器 | ||
* | ||
* @param XN 状态量维度 | ||
*/ | ||
template<int XN> | ||
class ExtendedKalmanFilter | ||
{ | ||
using VectorX = Eigen::Matrix<double, XN, 1>; | ||
using MatrixXX = Eigen::Matrix<double, XN, XN>; | ||
|
||
public: | ||
ExtendedKalmanFilter(const VectorX & x, const MatrixXX & P) | ||
: x_(x), P_(P) {} | ||
|
||
/** | ||
* @brief 重置状态 | ||
*/ | ||
void reset(const VectorX & x, const MatrixXX & P) | ||
{ | ||
x_ = x; | ||
P_ = P; | ||
} | ||
|
||
/** | ||
* 预测步 | ||
* @brief 使用自动微分的雅可比矩阵,预测状态估计 | ||
* @tparam 状态转移方程f(x,u),Functor | ||
* @tparam 控制量类型 | ||
* @param transition_model 状态转移方程 | ||
* @param Q 过程噪声协方差 | ||
* @param u 控制量 | ||
*/ | ||
template<typename StateTransitionModel, typename ... Controls> | ||
VectorX predict( | ||
StateTransitionModel transition_model, const MatrixXX & Q, | ||
const Controls &... u) | ||
{ | ||
// 绑定控制参数到状态转移方程 | ||
auto f = std::bind(transition_model, std::placeholders::_1, u ...); | ||
|
||
// x_jet(i).v[i]保存状态量x_(i)的导数 | ||
Eigen::Matrix<ceres::Jet<double, XN>, XN, 1> x_jet; | ||
x_jet.setZero(); | ||
for (int i = 0; i < XN; i++) { | ||
x_jet(i).a = x_(i); | ||
// 对自身求导为1 | ||
x_jet(i).v[i] = 1.0; | ||
} | ||
|
||
// 状态转移 | ||
auto x_p_jet = f(x_jet); | ||
|
||
// 获取雅可比矩阵同时更新状态量 | ||
for (int i = 0; i < XN; ++i) { | ||
x_(i) = x_p_jet[i].a; | ||
F_.row(i) = x_p_jet[i].v.transpose(); | ||
} | ||
|
||
P_ = F_ * P_ * F_.transpose() + Q; | ||
|
||
return x_; | ||
} | ||
|
||
/** | ||
* 更新步 | ||
* @tparam Measurement 测量量类型 | ||
* @tparam MeasurementModel 观测方程 h(x,v),Functor | ||
* @param measurementModel 观测方程 | ||
* @param R 测量噪声协方差 | ||
* @param z 测量值 | ||
*/ | ||
template<typename Measurement, typename MeasurementModel, | ||
int ZN = Eigen::dof<Measurement>> | ||
VectorX update( | ||
MeasurementModel h, | ||
const Eigen::Matrix<double, Eigen::dof<Measurement>, | ||
Eigen::dof<Measurement>> & R, | ||
const Measurement & z) | ||
{ | ||
// 观测方程的雅可比矩阵 | ||
Eigen::Matrix<double, ZN, XN> H; | ||
Eigen::Matrix<double, ZN, 1> z_hat; | ||
H.setZero(); | ||
z_hat.setZero(); | ||
|
||
// x_jet(i).v[i]保存状态量x_(i)的导数 | ||
Eigen::Matrix<ceres::Jet<double, XN>, XN, 1> x_jet; | ||
for (int i = 0; i < XN; i++) { | ||
x_jet(i).a = x_(i); | ||
// 对自身求导为1 | ||
x_jet(i).v[i] = 1.0; | ||
} | ||
|
||
// 观测方程 | ||
auto z_jet = Eigen::eval(h(x_jet)); | ||
|
||
for (int i = 0; i < ZN; i++) { | ||
z_hat(i) = z_jet[i].a; | ||
H.row(i) = z_jet[i].v.transpose(); | ||
} | ||
auto K = Eigen::eval(P_ * H.transpose() * (H * P_ * H.transpose() + R).inverse()); | ||
|
||
x_ = x_ + K * (z - z_hat); | ||
P_ = (MatrixXX::Identity() - K * H) * P_; | ||
|
||
return x_; | ||
} | ||
|
||
VectorX get_state() const {return x_;} | ||
|
||
MatrixXX get_covariance() const {return P_;} | ||
|
||
private: | ||
// 当前状态分布的均值 | ||
VectorX x_; | ||
|
||
// 当前状态分布的协方差 | ||
MatrixXX P_; | ||
|
||
// 状态转移方程的雅可比矩阵 | ||
MatrixXX F_; | ||
}; | ||
} // namespace rmoss_util | ||
#endif // RMOSS_UTIL__EXTENDED_KALMAN_FILTER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI这里可以改成使用rosdep install吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ros-ci会运行rosdep install命令,但似乎不能正确安装libceres-dev