first commit
This commit is contained in:
653
src/navigation_server.cpp
Normal file
653
src/navigation_server.cpp
Normal file
@@ -0,0 +1,653 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* move_base2 — cài đặt facade BaseNavigation.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <move_base2/navigation_server.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <robot_nav_2d_utils/conversions.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
namespace
|
||||
{
|
||||
|
||||
/// @brief Lấy phần tử theo khoá, trả về giá trị mặc định nếu không có.
|
||||
template <typename MapT>
|
||||
typename MapT::mapped_type lookupOrDefault(const MapT& container,
|
||||
const typename MapT::key_type& key)
|
||||
{
|
||||
const auto it = container.find(key);
|
||||
return it == container.end() ? typename MapT::mapped_type() : it->second;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NavigationServer::NavigationServer()
|
||||
{
|
||||
// nav_feedback_ là thành viên của contract host và được host đọc qua con trỏ, nên phải tồn tại
|
||||
// ngay từ lúc dựng — trước cả initialize().
|
||||
nav_feedback_ = std::make_shared<robot::move_base_core::NavFeedback>();
|
||||
nav_feedback_->navigation_state = robot::move_base_core::State::PENDING;
|
||||
nav_feedback_->feed_back_str = "chưa khởi tạo";
|
||||
nav_feedback_->goal_checked = false;
|
||||
nav_feedback_->is_ready = false;
|
||||
}
|
||||
|
||||
NavigationServer::~NavigationServer() = default;
|
||||
|
||||
// ================================================================================================
|
||||
// Cấu hình lõi
|
||||
// ================================================================================================
|
||||
|
||||
bool NavigationServer::configureLoop(const ControlLoopConfig& config, const ControlLoopDeps& deps,
|
||||
std::string& error)
|
||||
{
|
||||
if (!loop_.configure(config, deps, error))
|
||||
{
|
||||
nav_feedback_->is_ready = false;
|
||||
nav_feedback_->feed_back_str = "cấu hình lỗi: " + error;
|
||||
return false;
|
||||
}
|
||||
|
||||
robot_base_frame_ = config.robot_base_frame;
|
||||
|
||||
nav_feedback_->is_ready = true;
|
||||
nav_feedback_->feed_back_str = "sẵn sàng";
|
||||
refreshFeedback();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NavigationServer::configureSensors(const SensorGatewayConfig& config, std::string& error)
|
||||
{
|
||||
return sensors_.configure(config, error);
|
||||
}
|
||||
|
||||
void NavigationServer::attachCostmaps(robot_costmap_2d::LayeredCostmap* global,
|
||||
robot_costmap_2d::LayeredCostmap* local)
|
||||
{
|
||||
sensors_.attach(global, local);
|
||||
|
||||
// Phát lại static map đã nhận trước khi costmap tồn tại. Chỉ static map, cố ý: một laser scan cũ
|
||||
// phát lại vào costmap là dựng vật cản ở chỗ robot có thể đã rời khỏi từ lâu — im lặng và nguy
|
||||
// hiểm hơn hẳn việc chờ mẫu kế tiếp, vốn chỉ cách vài chục ms.
|
||||
std::map<std::string, robot_nav_msgs::OccupancyGrid> maps;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
maps = static_maps_;
|
||||
|
||||
// `map_save_`/`map_name_save_` là cặp biến PUBLIC của BaseNavigation mà host tự gán (đường bù
|
||||
// của bản cũ cho đúng vấn đề này). Tôn trọng nó để host không phải sửa gì, nhưng không để nó
|
||||
// ghi đè bản đã đi qua addStaticMap.
|
||||
if (!map_name_save_.empty() && maps.find(map_name_save_) == maps.end())
|
||||
{
|
||||
maps[map_name_save_] = map_save_;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& entry : maps)
|
||||
{
|
||||
sensors_.pushStaticMap(entry.first, entry.second);
|
||||
}
|
||||
}
|
||||
|
||||
bool NavigationServer::spinOnce()
|
||||
{
|
||||
// Trước khi tính lệnh: đẩy xuống controller những gì host đã đặt từ thread của nó. Đặt ở đây chứ
|
||||
// không ở cuối cycle để trần vận tốc có hiệu lực ngay trong chính cycle này — chậm một cycle
|
||||
// nghĩa là một chu kỳ nữa chạy quá tốc độ mà tầng an toàn vừa yêu cầu hạ.
|
||||
pushHostInputsToController();
|
||||
|
||||
const bool running = loop_.step();
|
||||
publishCommand();
|
||||
refreshFeedback();
|
||||
return running;
|
||||
}
|
||||
|
||||
void NavigationServer::publishCommand()
|
||||
{
|
||||
// getTwist() của contract host là LỆNH vận tốc đang phát, không phải vận tốc đo được: host lấy nó
|
||||
// rồi publish thẳng ra cmd_vel (amr_publiser.cpp:360-370). Đổ odometry vào đây tạo vòng lặp dương
|
||||
// — robot giữ nguyên tốc độ hiện tại vô hạn và VelocityArbiter bị vô hiệu hoàn toàn.
|
||||
//
|
||||
// Nguồn duy nhất đúng là lệnh vừa qua bộ trọng tài. Dấu thời gian lấy theo cycle của control loop
|
||||
// chứ không phải giờ hệ thống lúc gọi: host loại lệnh quá hạn, nên control loop treo phải làm dấu
|
||||
// thời gian đứng yên để host thấy được và ngừng phát.
|
||||
const robot_geometry_msgs::Twist& command = loop_.lastCommand();
|
||||
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
twist_.velocity = robot_nav_2d_utils::twist3Dto2D(command);
|
||||
twist_.header.stamp = loop_.lastCycleTime();
|
||||
twist_.header.frame_id = robot_base_frame_;
|
||||
}
|
||||
|
||||
robot::move_base_core::State NavigationServer::toHostState(NavigationState state)
|
||||
{
|
||||
using HostState = robot::move_base_core::State;
|
||||
switch (state)
|
||||
{
|
||||
case NavigationState::kIdle:
|
||||
return HostState::PENDING;
|
||||
case NavigationState::kPlanning:
|
||||
return HostState::PLANNING;
|
||||
case NavigationState::kControlling:
|
||||
return HostState::CONTROLLING;
|
||||
case NavigationState::kRecovering:
|
||||
// Contract host chỉ có CLEARING cho giai đoạn phục hồi. Ánh xạ về đó để host cũ không phải
|
||||
// đổi gì; ngữ nghĩa mới (recovery có thời lượng, có thể phát vận tốc) nằm ở phía lõi.
|
||||
return HostState::CLEARING;
|
||||
case NavigationState::kExecutingActions:
|
||||
// Contract host không có khái niệm action (D8 mới thêm). ACTIVE (actionlib: goal đang được
|
||||
// xử lý) là ánh xạ đúng: giữ nghĩa "chặng chưa xong" nhưng KHÔNG phải CONTROLLING — host
|
||||
// VDA5050 đang suy `driving = true` từ CONTROLLING (amr_vda_5050_client_api.cpp:1233), mà
|
||||
// robot lúc này đứng yên làm action; báo "đang chạy" cho fleet master là báo sai.
|
||||
return HostState::ACTIVE;
|
||||
case NavigationState::kPaused:
|
||||
return HostState::PAUSED;
|
||||
case NavigationState::kCancelling:
|
||||
return HostState::PREEMPTING;
|
||||
case NavigationState::kSucceeded:
|
||||
return HostState::SUCCEEDED;
|
||||
case NavigationState::kAborted:
|
||||
return HostState::ABORTED;
|
||||
case NavigationState::kCancelled:
|
||||
return HostState::PREEMPTED;
|
||||
}
|
||||
return HostState::LOST;
|
||||
}
|
||||
|
||||
void NavigationServer::refreshFeedback()
|
||||
{
|
||||
nav_feedback_->navigation_state = toHostState(loop_.state());
|
||||
|
||||
const char* reason = loop_.lastReason();
|
||||
if (reason != nullptr && reason[0] != '\0')
|
||||
{
|
||||
nav_feedback_->feed_back_str = reason;
|
||||
}
|
||||
|
||||
robot_geometry_msgs::Pose2D pose2d;
|
||||
nav_feedback_->goal_checked = getRobotPose(pose2d);
|
||||
if (nav_feedback_->goal_checked)
|
||||
{
|
||||
nav_feedback_->current_pose = pose2d;
|
||||
}
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Khởi tạo
|
||||
// ================================================================================================
|
||||
|
||||
void NavigationServer::initialize(robot::TFListenerPtr tf)
|
||||
{
|
||||
tf_ = tf;
|
||||
|
||||
// Phần dựng costmap, planner runner, controller runner và recovery runner từ tf này thuộc bước
|
||||
// nối dây runtime. Cho tới lúc đó, các cổng phải được bơm vào qua configureLoop() — cố ý KHÔNG
|
||||
// tự dựng cổng giả ở đây, vì một runtime chạy được với cổng giả là thứ nguy hiểm nhất có thể có.
|
||||
if (!loop_.initialized())
|
||||
{
|
||||
nav_feedback_->is_ready = false;
|
||||
nav_feedback_->feed_back_str = "đã nhận tf, chờ configureLoop() nạp các cổng runtime";
|
||||
}
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Footprint
|
||||
// ================================================================================================
|
||||
|
||||
void NavigationServer::setRobotFootprint(const std::vector<robot_geometry_msgs::Point>& fprt)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
footprint_ = fprt;
|
||||
}
|
||||
|
||||
std::vector<robot_geometry_msgs::Point> NavigationServer::getRobotFootprint()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return footprint_;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Nhận dữ liệu sensor
|
||||
// ================================================================================================
|
||||
|
||||
// Khuôn chung của cả năm hàm dưới đây: cất giữ dưới `data_mutex_`, ĐÓNG lock, rồi mới đẩy vào
|
||||
// costmap. Thứ tự đó là bắt buộc chứ không phải phong cách — `StaticLayer::incomingMap` có thể gọi
|
||||
// `LayeredCostmap::resizeMap`, hàm này chờ mutex master costmap và có thể đứng trọn một chu kỳ
|
||||
// `updateMap`. Đẩy trong lúc còn giữ `data_mutex_` sẽ kéo theo `getTwist`/`getRobotFootprint`/
|
||||
// `getStaticMap` của host chết chờ cùng, trong khi host chỉ có MỘT thread phục vụ mọi callback.
|
||||
|
||||
void NavigationServer::addStaticMap(const std::string& map_name, robot_nav_msgs::OccupancyGrid map)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
static_maps_[map_name] = map;
|
||||
}
|
||||
sensors_.pushStaticMap(map_name, map);
|
||||
}
|
||||
|
||||
void NavigationServer::addLaserScan(const std::string& laser_scan_name,
|
||||
robot_sensor_msgs::LaserScan laser_scan)
|
||||
{
|
||||
// Lọc TRƯỚC khi cất: bản được cất và bản costmap nhìn thấy phải là một. Bản cũ cũng cất bản đã
|
||||
// lọc; nếu getter trả bản thô còn costmap thấy bản lọc thì hai nguồn sự thật sẽ lệch nhau.
|
||||
const robot_sensor_msgs::LaserScan prepared = sensors_.prepareLaserScan(laser_scan);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
laser_scans_[laser_scan_name] = prepared;
|
||||
}
|
||||
sensors_.pushLaserScan(laser_scan_name, prepared);
|
||||
}
|
||||
|
||||
void NavigationServer::addPointCloud(const std::string& point_cloud_name,
|
||||
robot_sensor_msgs::PointCloud point_cloud)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
point_clouds_[point_cloud_name] = point_cloud;
|
||||
}
|
||||
sensors_.pushPointCloud(point_cloud_name, point_cloud);
|
||||
}
|
||||
|
||||
void NavigationServer::addPointCloud2(const std::string& point_cloud2_name,
|
||||
robot_sensor_msgs::PointCloud2 point_cloud2)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
point_cloud2s_[point_cloud2_name] = point_cloud2;
|
||||
}
|
||||
sensors_.pushPointCloud2(point_cloud2_name, point_cloud2);
|
||||
}
|
||||
|
||||
void NavigationServer::addDepthCameraData(const std::string& topic,
|
||||
robot_sensor_msgs::DepthCameraData::ConstPtr data)
|
||||
{
|
||||
if (data == nullptr)
|
||||
{
|
||||
return; // Bỏ mẫu null thay vì cất một con trỏ rỗng để tầng sau vấp phải.
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
depth_camera_data_[topic] = data;
|
||||
}
|
||||
sensors_.pushDepthCameraData(topic, data);
|
||||
}
|
||||
|
||||
void NavigationServer::addOdometry(const std::string& /*odometry_name*/,
|
||||
robot_nav_msgs::Odometry odometry)
|
||||
{
|
||||
// CHỈ cất odometry. Không đụng twist_: xem publishCommand() — twist_ là lệnh phát ra, còn đây là
|
||||
// vận tốc đo được. Trộn hai thứ đó là đưa cảm biến vào thẳng đường lệnh.
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
odometry_ = std::move(odometry);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Đọc dữ liệu sensor
|
||||
// ================================================================================================
|
||||
|
||||
robot_nav_msgs::OccupancyGrid NavigationServer::getStaticMap(const std::string& map_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return lookupOrDefault(static_maps_, map_name);
|
||||
}
|
||||
|
||||
robot_sensor_msgs::LaserScan NavigationServer::getLaserScan(const std::string& laser_scan_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return lookupOrDefault(laser_scans_, laser_scan_name);
|
||||
}
|
||||
|
||||
robot_sensor_msgs::PointCloud NavigationServer::getPointCloud(const std::string& point_cloud_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return lookupOrDefault(point_clouds_, point_cloud_name);
|
||||
}
|
||||
|
||||
robot_sensor_msgs::PointCloud2 NavigationServer::getPointCloud2(
|
||||
const std::string& point_cloud2_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return lookupOrDefault(point_cloud2s_, point_cloud2_name);
|
||||
}
|
||||
|
||||
std::map<std::string, robot_nav_msgs::OccupancyGrid> NavigationServer::getAllStaticMaps()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return static_maps_;
|
||||
}
|
||||
|
||||
std::map<std::string, robot_sensor_msgs::LaserScan> NavigationServer::getAllLaserScans()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return laser_scans_;
|
||||
}
|
||||
|
||||
std::map<std::string, robot_sensor_msgs::PointCloud> NavigationServer::getAllPointClouds()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return point_clouds_;
|
||||
}
|
||||
|
||||
std::map<std::string, robot_sensor_msgs::PointCloud2> NavigationServer::getAllPointCloud2s()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return point_cloud2s_;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeStaticMap(const std::string& map_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return static_maps_.erase(map_name) > 0;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeLaserScan(const std::string& laser_scan_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return laser_scans_.erase(laser_scan_name) > 0;
|
||||
}
|
||||
|
||||
bool NavigationServer::removePointCloud(const std::string& point_cloud_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return point_clouds_.erase(point_cloud_name) > 0;
|
||||
}
|
||||
|
||||
bool NavigationServer::removePointCloud2(const std::string& point_cloud2_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return point_cloud2s_.erase(point_cloud2_name) > 0;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeAllStaticMaps()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
static_maps_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeAllLaserScans()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
laser_scans_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeAllPointClouds()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
point_clouds_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeAllPointCloud2s()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
point_cloud2s_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NavigationServer::removeAllData()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
static_maps_.clear();
|
||||
laser_scans_.clear();
|
||||
point_clouds_.clear();
|
||||
point_cloud2s_.clear();
|
||||
depth_camera_data_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Sáu entry point di chuyển -> một NavigationRequest
|
||||
// ================================================================================================
|
||||
|
||||
bool NavigationServer::submit(const NavigationRequest& request)
|
||||
{
|
||||
std::string reason;
|
||||
if (loop_.submit(request, reason))
|
||||
{
|
||||
last_reject_reason_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
last_reject_reason_ = reason;
|
||||
nav_feedback_->feed_back_str = "từ chối yêu cầu: " + reason;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NavigationServer::moveTo(const robot_geometry_msgs::PoseStamped& goal,
|
||||
double xy_goal_tolerance, double yaw_goal_tolerance)
|
||||
{
|
||||
NavigationRequest request;
|
||||
request.profile = MotionProfile::kPosition;
|
||||
request.goal = goal;
|
||||
request.tolerance.xy = xy_goal_tolerance;
|
||||
request.tolerance.yaw = yaw_goal_tolerance;
|
||||
return submit(request);
|
||||
}
|
||||
|
||||
bool NavigationServer::moveTo(const robot_protocol_msgs::Order& msg,
|
||||
const robot_geometry_msgs::PoseStamped& goal,
|
||||
double xy_goal_tolerance, double yaw_goal_tolerance)
|
||||
{
|
||||
NavigationRequest request;
|
||||
request.profile = MotionProfile::kPosition;
|
||||
request.goal = goal;
|
||||
request.tolerance.xy = xy_goal_tolerance;
|
||||
request.tolerance.yaw = yaw_goal_tolerance;
|
||||
request.order = std::make_shared<robot_protocol_msgs::Order>(msg);
|
||||
return submit(request);
|
||||
}
|
||||
|
||||
bool NavigationServer::dockTo(const std::string& maker,
|
||||
const robot_geometry_msgs::PoseStamped& goal,
|
||||
double xy_goal_tolerance, double yaw_goal_tolerance)
|
||||
{
|
||||
NavigationRequest request;
|
||||
request.profile = MotionProfile::kDocking;
|
||||
request.goal = goal;
|
||||
request.tolerance.xy = xy_goal_tolerance;
|
||||
request.tolerance.yaw = yaw_goal_tolerance;
|
||||
request.marker = maker;
|
||||
return submit(request);
|
||||
}
|
||||
|
||||
bool NavigationServer::dockTo(const robot_protocol_msgs::Order& msg, const std::string& marker,
|
||||
const robot_geometry_msgs::PoseStamped& goal,
|
||||
double xy_goal_tolerance, double yaw_goal_tolerance)
|
||||
{
|
||||
NavigationRequest request;
|
||||
request.profile = MotionProfile::kDocking;
|
||||
request.goal = goal;
|
||||
request.tolerance.xy = xy_goal_tolerance;
|
||||
request.tolerance.yaw = yaw_goal_tolerance;
|
||||
request.marker = marker;
|
||||
request.order = std::make_shared<robot_protocol_msgs::Order>(msg);
|
||||
return submit(request);
|
||||
}
|
||||
|
||||
bool NavigationServer::moveStraightTo(const robot_geometry_msgs::PoseStamped& goal,
|
||||
double xy_goal_tolerance)
|
||||
{
|
||||
NavigationRequest request;
|
||||
request.profile = MotionProfile::kGoStraight;
|
||||
request.goal = goal;
|
||||
request.tolerance.xy = xy_goal_tolerance;
|
||||
return submit(request);
|
||||
}
|
||||
|
||||
bool NavigationServer::rotateTo(const robot_geometry_msgs::PoseStamped& goal,
|
||||
double yaw_goal_tolerance)
|
||||
{
|
||||
NavigationRequest request;
|
||||
request.profile = MotionProfile::kRotate;
|
||||
request.goal = goal;
|
||||
request.tolerance.yaw = yaw_goal_tolerance;
|
||||
return submit(request);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Điều khiển vòng đời
|
||||
// ================================================================================================
|
||||
|
||||
void NavigationServer::pause()
|
||||
{
|
||||
loop_.requestPause();
|
||||
}
|
||||
|
||||
void NavigationServer::resume()
|
||||
{
|
||||
loop_.requestResume();
|
||||
}
|
||||
|
||||
void NavigationServer::cancel()
|
||||
{
|
||||
loop_.requestCancel();
|
||||
}
|
||||
|
||||
bool NavigationServer::setTwistLinear(const robot_geometry_msgs::Vector3& linear)
|
||||
{
|
||||
// Không phải lệnh jog dù tên nghe như vậy: đây là TRẦN vận tốc, dấu chọn chiều, và host truyền
|
||||
// xuống đây tốc độ đã bị tầng an toàn hạ (amr_control.cpp:561, 671-680).
|
||||
if (!std::isfinite(linear.x) || !std::isfinite(linear.y) || !std::isfinite(linear.z))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Chỉ cất lại. Host gọi từ thread của nó (OPC-UA/VDA5050/ROS) còn ControllerPort không
|
||||
// thread-safe, nên việc đẩy xuống controller thuộc về spinOnce() — xem pushHostInputsToController.
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
if (linear.x < 0.0)
|
||||
{
|
||||
pending_linear_backward_ = linear;
|
||||
has_pending_linear_backward_ = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
pending_linear_forward_ = linear;
|
||||
has_pending_linear_forward_ = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NavigationServer::setTwistAngular(const robot_geometry_msgs::Vector3& angular)
|
||||
{
|
||||
if (!std::isfinite(angular.x) || !std::isfinite(angular.y) || !std::isfinite(angular.z))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
pending_angular_ = angular;
|
||||
has_pending_angular_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void NavigationServer::pushHostInputsToController()
|
||||
{
|
||||
ControllerPort* controller = loop_.controllerPort();
|
||||
if (controller == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
robot_geometry_msgs::Vector3 linear_forward;
|
||||
robot_geometry_msgs::Vector3 linear_backward;
|
||||
robot_geometry_msgs::Vector3 angular;
|
||||
bool push_forward = false;
|
||||
bool push_backward = false;
|
||||
bool push_angular = false;
|
||||
robot_geometry_msgs::Twist velocity;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
|
||||
push_forward = has_pending_linear_forward_;
|
||||
push_backward = has_pending_linear_backward_;
|
||||
push_angular = has_pending_angular_;
|
||||
linear_forward = pending_linear_forward_;
|
||||
linear_backward = pending_linear_backward_;
|
||||
angular = pending_angular_;
|
||||
has_pending_linear_forward_ = false;
|
||||
has_pending_linear_backward_ = false;
|
||||
has_pending_angular_ = false;
|
||||
|
||||
velocity = odometry_.twist.twist;
|
||||
}
|
||||
|
||||
// Đẩy xuống NGOÀI lock: controller là plugin bên thứ ba, thời gian chạy của nó không được phép
|
||||
// chặn các getter mà host đang gọi.
|
||||
if (push_forward)
|
||||
{
|
||||
controller->setTwistLinear(linear_forward);
|
||||
}
|
||||
if (push_backward)
|
||||
{
|
||||
controller->setTwistLinear(linear_backward);
|
||||
}
|
||||
if (push_angular)
|
||||
{
|
||||
controller->setTwistAngular(angular);
|
||||
}
|
||||
|
||||
controller->setMeasuredVelocity(velocity);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Đọc trạng thái
|
||||
// ================================================================================================
|
||||
|
||||
bool NavigationServer::getRobotPose(robot_geometry_msgs::PoseStamped& pose)
|
||||
{
|
||||
PosePort* port = loop_.posePort();
|
||||
if (port == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Hỏi thẳng cổng mỗi lần, không cache: mất TF phải nhìn thấy được ngay tại lời gọi này chứ không
|
||||
// phải nhận về một pose cũ đã hết hạn.
|
||||
return port->getRobotPose(pose);
|
||||
}
|
||||
|
||||
bool NavigationServer::getRobotPose(robot_geometry_msgs::Pose2D& pose)
|
||||
{
|
||||
robot_geometry_msgs::PoseStamped stamped;
|
||||
if (!getRobotPose(stamped))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const robot_nav_2d_msgs::Pose2DStamped converted =
|
||||
robot_nav_2d_utils::poseStampedToPose2D(stamped);
|
||||
pose = converted.pose;
|
||||
return true;
|
||||
}
|
||||
|
||||
robot_nav_2d_msgs::Twist2DStamped NavigationServer::getTwist()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||
return twist_;
|
||||
}
|
||||
|
||||
robot::move_base_core::NavFeedback* NavigationServer::getFeedback()
|
||||
{
|
||||
return nav_feedback_.get();
|
||||
}
|
||||
|
||||
robot::move_base_core::PlannerDataOutput NavigationServer::getGlobalData()
|
||||
{
|
||||
return global_data_;
|
||||
}
|
||||
|
||||
robot::move_base_core::PlannerDataOutput NavigationServer::getLocalData()
|
||||
{
|
||||
return local_data_;
|
||||
}
|
||||
|
||||
} // namespace move_base2
|
||||
Reference in New Issue
Block a user