Sensor data entered this runtime and disappeared into the costmap. At 0.05 m with inflation the costmap has destroyed ray order and object shape, so a dock detector fitting lines to find a V plate cannot reconstruct what the laser saw - two 0.2 m wings on that grid are one blob. A reinforcement-learning local planner wants the raw scan for the same reason. Neither had a route: ActionContext held only TF, and LocalPlanner::initialize takes only a costmap. NavigationServer now owns a SensorStore and feeds it from all five doors, handing a read-only port to the action registry and to the controller runner. The store gets the reading BEFORE the filter. Anything that wants filtering filters upstream; the store does not know a filter exists. With laser SOR on, the store and the costmap deliberately see different data - one wants outliers gone, the other wants the true shape - and the comment at addLaserScan now says so, because the one it replaces claimed the opposite. remove* mirrors into the store, dropping readings while keeping the topic declared. A host that has disowned data while the store still serves it means the two disagree about what the robot can currently see, and max_age only covers the tail of that. Reaching a local planner without breaking every other one: widening LocalPlanner::initialize would break hybrid, mppi, priest, stanley and deep_mpc at once for the sake of one planner. A planner that wants sensor data inherits SensorConsumer instead, and ControllerRunner finds out with a dynamic_cast after loading it, at all three sites that build an instance and always before initialize(). Planners that do not inherit it cast to null and nothing happens, so nothing needs rebuilding. That cast crosses a .so boundary and depends on matching RTTI, which a green build does not prove - hence the log line, and hence TestControllerSensorProbe, dlopen'd like a real plugin, to check it actually matches. sensor_store/report_period prints what the store holds every N seconds, off by default. Until a consumer exists it is the only way to see whether data arrives at all, and it is what separates a mistyped topic from a dead sensor - without it the two look identical. It reads age from the control loop's clock, not robot::Time::now(), which is the wall clock while a simulated scan is not. Config is refused at boot, not at first use: an unknown data_type, a short name two packages share, a duplicate topic, a capacity below 1, a max_age that is not positive. Declaring no sensor at all is valid and leaves the store empty and silent - a runtime with no consumer of raw data holds nothing, which is an answer rather than a reason to refuse to start. robot_nav_msgs/OccupancyGrid is registered here rather than in robot_sensor_ports, which ships only its own package's types; pulling robot_nav_msgs into every translation unit that touches the store would be the cost of doing otherwise.
392 lines
14 KiB
C++
392 lines
14 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* move_base2 — dựng toàn bộ thành phần runtime thật từ config và TF.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#ifndef MOVE_BASE2_NAVIGATION_RUNTIME_H_
|
|
#define MOVE_BASE2_NAVIGATION_RUNTIME_H_
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <robot_costmap_2d/costmap_2d_robot.h>
|
|
|
|
#include <move_base2/bridges/mission_adapter_bridge.h>
|
|
#include <move_base2/bridges/mission_layer.h>
|
|
#include <move_base2/config/move_base2_config.h>
|
|
#include <move_base2/io/runtime_stats.h>
|
|
#include <move_base2/control_loop.h>
|
|
#include <move_base2/io/costmap_exporter.h>
|
|
#include <move_base2/ports/clock_port.h>
|
|
#include <move_base2/ports/path_check_port.h>
|
|
#include <move_base2/ports/pose_port.h>
|
|
#include <move_base2/runners/action_runner.h>
|
|
#include <move_base2/runners/controller_runner.h>
|
|
#include <move_base2/runners/planner_runner.h>
|
|
#include <move_base2/runners/recovery_runner.h>
|
|
|
|
namespace tf3
|
|
{
|
|
class BufferCore;
|
|
}
|
|
|
|
namespace robot_sensor_ports
|
|
{
|
|
class SensorReader;
|
|
}
|
|
|
|
namespace move_base2
|
|
{
|
|
|
|
/**
|
|
* @class CostmapPosePort
|
|
* @brief Cổng pose lấy từ costmap.
|
|
*
|
|
* `Costmap2DROBOT::getRobotPose` đã gói sẵn việc tra TF kèm `transform_tolerance` và trả false khi
|
|
* transform quá cũ — đúng ngữ nghĩa mà @ref PosePort cần, nên không có gì phải viết lại.
|
|
*
|
|
* @note Con trỏ costmap là **non-owning**. Lấy lại mỗi lần dùng chứ không cache `Costmap2D*` bên
|
|
* trong: con trỏ costmap có thể bị thay.
|
|
*/
|
|
class CostmapPosePort final : public PosePort
|
|
{
|
|
public:
|
|
explicit CostmapPosePort(robot_costmap_2d::Costmap2DROBOT* costmap = nullptr) : costmap_(costmap)
|
|
{
|
|
}
|
|
|
|
void setCostmap(robot_costmap_2d::Costmap2DROBOT* costmap)
|
|
{
|
|
costmap_ = costmap;
|
|
}
|
|
|
|
/// @brief Buffer TF để tra frame lạ. Non-owning; null = @ref lookupPose luôn trả false.
|
|
void setTf(const std::shared_ptr<tf3::BufferCore>& tf)
|
|
{
|
|
tf_ = tf;
|
|
}
|
|
|
|
bool getRobotPose(robot_geometry_msgs::PoseStamped& pose) const override
|
|
{
|
|
if (costmap_ == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
return costmap_->getRobotPose(pose);
|
|
}
|
|
|
|
bool lookupPose(const std::string& frame,
|
|
robot_geometry_msgs::PoseStamped& pose) const override;
|
|
|
|
private:
|
|
robot_costmap_2d::Costmap2DROBOT* costmap_;
|
|
std::shared_ptr<tf3::BufferCore> tf_;
|
|
};
|
|
|
|
/**
|
|
* @class CostmapPathCheck
|
|
* @brief Hiện thực @ref PathCheckPort trên costmap ĐIỀU KHIỂN (local).
|
|
*
|
|
* Local chứ không global, hai lý do và cả hai đều quan trọng:
|
|
* - **Độ tươi**: local chạy ở `update_frequency: 6.0` Hz, global chỉ `1.0` Hz. Vật cản vừa xuất
|
|
* hiện có mặt ở local trước.
|
|
* - **Đồng thuận với detour**: `DetourPathRecovery` lập đường vòng trên chính local costmap.
|
|
* Monitor báo "bẩn" trên một lưới mà planner detour lại thấy "sạch" là vòng lặp vô nghĩa.
|
|
*
|
|
* Plan ở frame global (`map`) còn lưới ở `odom`, nên phải quy đổi — tra TF **một lần** cho cả lượt
|
|
* quét (xem @ref PlanarTransform), không phải mỗi pose.
|
|
*
|
|
* @note Con trỏ costmap **non-owning và có thể bị thay** — lấy lại mỗi lần dùng, không cache
|
|
* `Costmap2D*` bên trong.
|
|
*/
|
|
class CostmapPathCheck final : public PathCheckPort
|
|
{
|
|
public:
|
|
void setCostmap(robot_costmap_2d::Costmap2DROBOT* costmap)
|
|
{
|
|
costmap_ = costmap;
|
|
}
|
|
|
|
void setTf(const std::shared_ptr<tf3::BufferCore>& tf)
|
|
{
|
|
tf_ = tf;
|
|
}
|
|
|
|
bool checkPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan,
|
|
std::size_t from_index, double horizon, PlanBlockResult& out) const override;
|
|
|
|
private:
|
|
robot_costmap_2d::Costmap2DROBOT* costmap_ = nullptr;
|
|
std::shared_ptr<tf3::BufferCore> tf_;
|
|
};
|
|
|
|
/**
|
|
* @class CostmapStatusAdapter
|
|
* @brief Cổng "costmap còn hạn không" lấy từ costmap thật.
|
|
*
|
|
* Trỏ vào costmap **điều khiển** (local), giống hệt bản cũ (`move_base.cpp:2720` hỏi
|
|
* `controller_costmap_robot_`): đó là costmap mà local planner tránh vật cản trên đó, nên nó mới là
|
|
* cái quyết định robot có được lái hay không. Costmap global cũ đi thì chỉ ảnh hưởng chất lượng
|
|
* plan, và state machine đã có `planner_patience` lo phần đó.
|
|
*
|
|
* @note Costmap null trả **false** — không biết thì không cho chạy.
|
|
*/
|
|
class CostmapStatusAdapter final : public CostmapStatusPort
|
|
{
|
|
public:
|
|
explicit CostmapStatusAdapter(robot_costmap_2d::Costmap2DROBOT* costmap = nullptr)
|
|
: costmap_(costmap)
|
|
{
|
|
}
|
|
|
|
void setCostmap(robot_costmap_2d::Costmap2DROBOT* costmap)
|
|
{
|
|
costmap_ = costmap;
|
|
}
|
|
|
|
bool isCurrent() const override
|
|
{
|
|
return costmap_ != nullptr && costmap_->isCurrent();
|
|
}
|
|
|
|
private:
|
|
robot_costmap_2d::Costmap2DROBOT* costmap_; ///< non-owning
|
|
};
|
|
|
|
/**
|
|
* @class NavigationRuntime
|
|
* @brief Sở hữu và dựng mọi thành phần thật của runtime: costmap, runner, bridge.
|
|
*
|
|
* Tách khỏi @ref NavigationServer để chỗ **lắp ráp** không lẫn với chỗ **hiện thực contract host**.
|
|
* `NavigationServer` vẫn nhận cổng qua `configureLoop`, nên nó kiểm được bằng cổng giả y như trước;
|
|
* lớp này chỉ là một cách dựng bộ cổng đó từ config thật.
|
|
*
|
|
* ## Thứ tự dựng là bắt buộc, không phải tuỳ ý
|
|
*
|
|
* `state_machine.recovery_behavior_count` **không đến từ YAML** — nó là số behavior
|
|
* `RecoveryRunner` nạp được **thật**. Đọc từ config thì một behavior hỏng vẫn khiến state machine
|
|
* tin là còn đường phục hồi, và lỗi đầu tiên sẽ dẫn thẳng tới `ABORTED` mà không ai hiểu vì sao.
|
|
* Trình tự bắt buộc, được @ref build tuân thủ:
|
|
*
|
|
* `MoveBase2Config::load()` → `RecoveryRunner::configure()` → gán `recovery_behavior_count`
|
|
* → `MoveBase2Config::validate()`
|
|
*
|
|
* ## Costmap
|
|
*
|
|
* Hai costmap được dựng ở đây và **chỉ `start()` sau khi mọi thứ khác đã sẵn sàng**: `start()` cho
|
|
* chạy thread cập nhật của costmap, và một thread cập nhật chạy trong khi planner chưa nạp xong là
|
|
* cửa sổ để mọi thứ chạm vào nhau ở trạng thái nửa vời.
|
|
*/
|
|
class NavigationRuntime
|
|
{
|
|
public:
|
|
NavigationRuntime();
|
|
~NavigationRuntime();
|
|
|
|
NavigationRuntime(const NavigationRuntime&) = delete;
|
|
NavigationRuntime& operator=(const NavigationRuntime&) = delete;
|
|
|
|
/**
|
|
* @brief Pha 1: đọc config và dựng hai costmap. **Chưa nạp plugin nào.**
|
|
*
|
|
* Tách khỏi @ref buildRunners là bắt buộc, không phải để cho gọn. `SBPLLatticePlanner::initialize`
|
|
* **chặn** cho tới khi costmap có kích thước khác 0 (`sbpl_lattice_planner.cpp:132`), mà kích
|
|
* thước đó chỉ được đặt khi static map tới `StaticLayer`. Nếu nạp planner ngay trong một pha thì
|
|
* planner chờ map, còn map thì chờ pha đó xong để được đẩy vào — khoá chết, và SBPL `exit(1)` sau
|
|
* 2 giây, giết cả tiến trình.
|
|
*
|
|
* Trình tự đúng, giống hệt bản cũ (`move_base.cpp:260` dựng costmap → `:287` đẩy map → `:306` nạp
|
|
* planner): dựng costmap → gắn vào SensorGateway và phát lại static map → rồi mới nạp plugin.
|
|
*
|
|
* @param tf Buffer TF do host cấp. Bắt buộc khác null; phải sống lâu hơn object này.
|
|
* @param[out] error Lý do thất bại; chỉ ghi khi hàm trả false.
|
|
*/
|
|
bool buildCostmaps(const std::shared_ptr<tf3::BufferCore>& tf, std::string& error);
|
|
|
|
/**
|
|
* @brief Pha 2: nạp planner, controller, recovery, action rồi kiểm cấu hình.
|
|
*
|
|
* Chỉ gọi sau khi static map đã được đẩy vào costmap — xem @ref buildCostmaps.
|
|
*
|
|
* Thất bại ở bất kỳ bước nào cũng để runtime ở trạng thái **chưa dựng** (@ref built false), chứ
|
|
* không phải nửa dựng nửa không.
|
|
*/
|
|
bool buildRunners(std::string& error);
|
|
|
|
/**
|
|
* @brief Cấp kho cảm biến thô cho action handler và local planner.
|
|
*
|
|
* Phải gọi **trước** @ref buildRunners: cả hai bên nhận cổng này lúc nạp plugin, và một planner
|
|
* có quyền dùng nó ngay trong `initialize()` của nó.
|
|
*
|
|
* @param reader Non-owning, được phép null (không có kho). Phải sống lâu hơn runtime — chủ sở hữu
|
|
* kho là @ref NavigationServer, và nó thả runtime trước khi thả kho.
|
|
*/
|
|
void setSensorReader(const robot_sensor_ports::SensorReader* reader)
|
|
{
|
|
sensors_ = reader;
|
|
}
|
|
|
|
/// @brief Hai costmap đã dựng xong chưa (pha 1).
|
|
bool costmapsReady() const
|
|
{
|
|
return global_costmap_ != nullptr && local_costmap_ != nullptr;
|
|
}
|
|
|
|
bool built() const
|
|
{
|
|
return built_;
|
|
}
|
|
|
|
/// @brief Cho costmap bắt đầu cập nhật. Gọi sau khi control loop đã được cấu hình xong.
|
|
void start();
|
|
|
|
/// @brief Dừng cập nhật costmap.
|
|
void stop();
|
|
|
|
/**
|
|
* @brief Cập nhật footprint cho cả hai costmap và làm mới cache collision của local planner.
|
|
*
|
|
* Có thể gọi trong pha khởi tạo, sau @ref buildCostmaps nhưng trước @ref buildRunners: khi đó chỉ
|
|
* cập nhật hai costmap, để local planner đầu tiên đọc đúng footprint trong `initialize()`. Khi
|
|
* runtime đã dựng xong, hàm phải được gọi từ control thread và sẽ refresh cache local planner.
|
|
* Hai costmap có footprint riêng; chỉ đổi một bên sẽ làm planner global và local controller dùng
|
|
* hai hình robot khác nhau.
|
|
*/
|
|
bool setRobotFootprint(const std::vector<robot_geometry_msgs::Point>& footprint);
|
|
|
|
/// @brief Bộ cổng để bơm vào @ref ControlLoop. Rỗng nếu chưa @ref build.
|
|
ControlLoopDeps deps();
|
|
|
|
const MoveBase2Config& config() const
|
|
{
|
|
return config_;
|
|
}
|
|
|
|
/**
|
|
* @brief Bộ telemetry dùng chung. Null cho tới khi @ref buildCostmaps chạy xong.
|
|
*
|
|
* Non-owning theo hướng người dùng: runtime sở hữu, bên gọi chỉ mượn. Trả về null vẫn hợp lệ —
|
|
* mọi hàm của @ref RuntimeStats an toàn với con trỏ null ở phía người gọi (@ref ScopedSection).
|
|
*/
|
|
RuntimeStats* stats()
|
|
{
|
|
return stats_.get();
|
|
}
|
|
|
|
robot_costmap_2d::Costmap2DROBOT* globalCostmap()
|
|
{
|
|
return global_costmap_.get();
|
|
}
|
|
|
|
robot_costmap_2d::Costmap2DROBOT* localCostmap()
|
|
{
|
|
return local_costmap_.get();
|
|
}
|
|
|
|
MissionAdapterBridge& mission()
|
|
{
|
|
return mission_;
|
|
}
|
|
|
|
/**
|
|
* @brief Framework mission đứng sau bridge.
|
|
*
|
|
* `missionLayer().started()` là câu hỏi "order có được cắt thành chặng không". False nghĩa là
|
|
* layer bị tắt bằng config hoặc không nạp được nguồn nào — bên gọi phải tự đưa order xuống
|
|
* navigation theo đường trực tiếp.
|
|
*/
|
|
MissionLayer& missionLayer()
|
|
{
|
|
return mission_layer_;
|
|
}
|
|
|
|
PlannerRunner& planner()
|
|
{
|
|
return planner_;
|
|
}
|
|
|
|
ControllerRunner& controller()
|
|
{
|
|
return controller_;
|
|
}
|
|
|
|
/// @brief Runner recovery — server nối `setPlanSource` cho behavior họ path qua đây.
|
|
RecoveryRunner& recoveryRunner()
|
|
{
|
|
return recovery_;
|
|
}
|
|
|
|
/// @brief Bộ kết xuất costmap cho host publish lên rviz.
|
|
CostmapExporter& globalExporter()
|
|
{
|
|
return global_exporter_;
|
|
}
|
|
|
|
CostmapExporter& localExporter()
|
|
{
|
|
return local_exporter_;
|
|
}
|
|
|
|
private:
|
|
MoveBase2Config config_;
|
|
bool built_ = false;
|
|
|
|
std::shared_ptr<tf3::BufferCore> tf_;
|
|
|
|
// Costmap phải được khai TRƯỚC các runner: runner giữ con trỏ tới chúng, nên chúng phải bị huỷ
|
|
// SAU. Thứ tự khai báo thành viên chính là thứ tự huỷ ngược.
|
|
/// Telemetry của cả runtime. Luôn tồn tại; tắt hay bật do `runtime_stats_period` quyết định.
|
|
/// Dựng trong buildCostmaps() ngay sau khi đọc config, vì nó phải chụp được thread mà costmap tạo.
|
|
std::unique_ptr<RuntimeStats> stats_;
|
|
|
|
/// Guard "không đi mù": hỏi costmap điều khiển xem observation buffer còn hạn không.
|
|
CostmapStatusAdapter costmap_status_;
|
|
|
|
std::unique_ptr<robot_costmap_2d::Costmap2DROBOT> global_costmap_;
|
|
std::unique_ptr<robot_costmap_2d::Costmap2DROBOT> local_costmap_;
|
|
|
|
/// Footprint đang thực sự có hiệu lực ở từng costmap; dùng để rollback khi local planner không
|
|
/// dựng lại được cache collision của footprint mới.
|
|
std::vector<robot_geometry_msgs::Point> global_footprint_;
|
|
std::vector<robot_geometry_msgs::Point> local_footprint_;
|
|
|
|
SystemClock clock_;
|
|
|
|
/// Kho cảm biến thô dùng chung. Non-owning; null = host không cấp.
|
|
const robot_sensor_ports::SensorReader* sensors_ = nullptr;
|
|
|
|
/**
|
|
* HAI nguồn pose, khác FRAME — không gộp được.
|
|
*
|
|
* `Costmap2DROBOT::getRobotPose` trả pose trong **global frame của chính costmap đó**: costmap
|
|
* global dùng `map`, costmap local dùng `odom`. Global planner lập plan trên lưới `map` nên start
|
|
* phải ở `map`; local planner và recovery làm việc trên lưới `odom` nên pose phải ở `odom`.
|
|
*
|
|
* Dùng nhầm nguồn không gây lỗi nào nhìn thấy được — chỉ là start của planner nằm ở gốc odom thay
|
|
* vì vị trí thật, và planner báo "failed to set start state". Đã xảy ra một lần.
|
|
*/
|
|
CostmapPosePort global_pose_; ///< frame `map` — planner, chống quẩn, pose báo về host.
|
|
CostmapPosePort local_pose_; ///< frame `odom` — controller và recovery.
|
|
CostmapPathCheck path_check_; ///< quét tuyến trên costmap local (PathMonitor + lọc plan).
|
|
CostmapExporter global_exporter_;
|
|
CostmapExporter local_exporter_;
|
|
|
|
PlannerRunner planner_;
|
|
ControllerRunner controller_;
|
|
RecoveryRunner recovery_;
|
|
ActionRunner action_;
|
|
MissionAdapterBridge mission_;
|
|
|
|
/// Khai báo SAU bridge: hai thread của layer gọi vào bridge, nên chúng phải chết trước nó.
|
|
MissionLayer mission_layer_;
|
|
};
|
|
|
|
} // namespace move_base2
|
|
|
|
#endif // MOVE_BASE2_NAVIGATION_RUNTIME_H_
|