This commit is contained in:
2026-01-12 15:49:52 +07:00
parent bff56e85f1
commit 78b11b60fe
11 changed files with 594 additions and 27 deletions

View File

@@ -5,6 +5,8 @@
#include <robot/node_handle.h>
#include <tf3/buffer_core.h>
#include <geometry_msgs/Vector3.h>
#include <xmlrpcpp/XmlRpcValue.h>
#include <robot_geometry_msgs/Point.h>
namespace amr_control
{
@@ -76,6 +78,13 @@ namespace amr_control
{
vda_5050_client_api_ptr_.reset();
}
// Stop costmap publishing
if (costmap_publisher_ptr_)
{
costmap_publisher_ptr_->stopPublishing();
costmap_publisher_ptr_.reset();
}
}
void AmrController::init(ros::NodeHandle &nh, std::shared_ptr<tf2_ros::Buffer> buffer)
@@ -241,10 +250,10 @@ namespace amr_control
move_base_ptr_ = move_base_loader_();
sensor_converter_ptr_ = std::make_shared<amr_control::SensorConverter>(nh, move_base_ptr_);
move_base_ptr_->initialize(tf_core_ptr_);
// Request map từ service hoặc đợi message (vì map server chỉ publish một lần)
robot::log_info("[%s:%d]\n Requesting map from server...", __FILE__, __LINE__);
sensor_converter_ptr_->requestMapFromServer(nh);
// Initialize costmap publisher for RViz visualization
robot::log_info("[%s:%d]\n Initializing costmap publisher...", __FILE__, __LINE__);
costmap_publisher_ptr_ = std::make_shared<amr_control::AmrPublisher>(nh, move_base_ptr_);
ros::Rate r(3);
do
@@ -257,11 +266,75 @@ namespace amr_control
move_base_ptr_->getFeedback() != nullptr &&
move_base_ptr_->getFeedback()->is_ready)
{
// Read footprint from parameter server and set it
std::vector<robot_geometry_msgs::Point> footprint;
if (nh.hasParam("footprint"))
{
XmlRpc::XmlRpcValue footprint_xmlrpc;
nh.getParam("footprint", footprint_xmlrpc);
if (footprint_xmlrpc.getType() == XmlRpc::XmlRpcValue::TypeArray && footprint_xmlrpc.size() >= 3)
{
footprint.reserve(footprint_xmlrpc.size());
for (int i = 0; i < footprint_xmlrpc.size(); ++i)
{
if (footprint_xmlrpc[i].getType() == XmlRpc::XmlRpcValue::TypeArray && footprint_xmlrpc[i].size() == 2)
{
robot_geometry_msgs::Point pt;
pt.x = static_cast<double>(footprint_xmlrpc[i][0]);
pt.y = static_cast<double>(footprint_xmlrpc[i][1]);
pt.z = 0.0;
footprint.push_back(pt);
}
}
if (footprint.size() >= 3)
{
move_base_ptr_->setRobotFootprint(footprint);
robot::log_info("[%s:%d] Set robot footprint with %zu points", __FILE__, __LINE__, footprint.size());
for (size_t i = 0; i < footprint.size(); ++i)
{
robot::log_info("[%s:%d] Footprint point[%zu]: (%.3f, %.3f)",
__FILE__, __LINE__, i, footprint[i].x, footprint[i].y);
}
}
else
{
robot::log_warning("[%s:%d] Footprint must have at least 3 points, got %zu. Using default footprint.",
__FILE__, __LINE__, footprint.size());
}
}
else
{
robot::log_warning("[%s:%d] Invalid footprint parameter format. Expected array of arrays with at least 3 points.",
__FILE__, __LINE__);
}
}
else
{
robot::log_info("[%s:%d] No footprint parameter found, using default footprint from move_base config.",
__FILE__, __LINE__);
}
robot_geometry_msgs::Vector3 linear;
linear.x = 0.3;
move_base_ptr_->setTwistLinear(linear);
linear.x = -0.3;
move_base_ptr_->setTwistLinear(linear);
// Request map từ service hoặc đợi message (vì map server chỉ publish một lần)
robot::log_info("[%s:%d]\n Requesting map from server...", __FILE__, __LINE__);
sensor_converter_ptr_->requestMapFromServer(nh);
// Start publishing costmaps to RViz
// Global costmap: 1 Hz, Local costmap: 5 Hz
if (costmap_publisher_ptr_)
{
robot::log_info("[%s:%d]\n Starting costmap publishing to RViz...", __FILE__, __LINE__);
costmap_publisher_ptr_->startPublishing(1.0, 5.0);
}
}
}
catch (const std::exception &e)