Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 5 additions & 0 deletions include/gyakuenki_cpp/node/gyakuenki_cpp_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "gyakuenki_interfaces/srv/update_camera_offset.hpp"
#include "ninshiki_interfaces/msg/detected_object.hpp"
#include "ninshiki_interfaces/msg/detected_objects.hpp"
#include "keisan/ekf/ekf_ball.hpp"

namespace gyakuenki_cpp
{
Expand Down Expand Up @@ -71,6 +72,10 @@ class GyakuenkiCppNode

rclcpp::Service<GetCameraOffset>::SharedPtr get_camera_offset_service;
rclcpp::Service<UpdateCameraOffset>::SharedPtr update_camera_offset_service;

keisan::ekf_ball ball_ekf_;
bool ball_initialized_ = false;
rclcpp::Time last_ball_time_;
};

} // namespace gyakuenki_cpp
Expand Down
30 changes: 30 additions & 0 deletions src/gyakuenki_cpp/node/gyakuenki_cpp_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ GyakuenkiCppNode::GyakuenkiCppNode(
keisan::Matrix<4, 1> Pc;
ProjectedObject projected_ball =
this->ipm->map_object(*message, "base_footprint", Pc);

rclcpp::Time now = this->node->now();

if (!ball_initialized_) {
ball_ekf_.init(Pc[0][0], Pc[1][0]);
last_ball_time_ = now;
ball_initialized_ = true;
return;
}

double dt = (now - last_ball_time_).seconds();
last_ball_time_ = now;

if (dt <= 0.0) {
return;
}

ball_ekf_.predict(dt);

keisan::Matrix<2,1> z;
z[0][0] = Pc[0][0];
z[1][0] = Pc[1][0];

ball_ekf_.update(z);

auto pos = ball_ekf_.getPosition();

projected_ball.position.x = pos(0,0);
projected_ball.position.y = pos(1,0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these logic codes to separate cpp files. Cpp files inside node directory should only contain the node configuration itself (publisher-subscriber). Can refer to akushon, ninshiki_cpp, or other packages as well.

projected_ball_publisher->publish(projected_ball);
} catch (std::exception & e) {
RCLCPP_ERROR(this->node->get_logger(), e.what());
Expand Down
Loading