Files
move_base2/test/controller_runner_test.cpp
duongtd b102391fb8 Keep raw sensor readings so plugins can reach what the costmap threw away
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.
2026-08-26 11:35:17 +07:00

577 lines
22 KiB
C++

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — test ControllerRunner: nạp plugin thật qua Boost.DLL, trần vận tốc phải tới được
* plugin, và mọi đường lỗi phải trả về "không có lệnh" chứ không để dữ liệu hỏng đi tiếp.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <string>
#include <vector>
#include <robot/node_handle.h>
#include <move_base2/runners/controller_runner.h>
#include <robot_sensor_ports/sensor_store.h>
#include <robot_sensor_ports/sensor_types.h>
#include "fake_ports.h"
namespace
{
using move_base2::ControllerRunner;
using move_base2::testing::FakePosePort;
/// Pose cố định — gen-2 nhận pose làm tham số của computeVelocityCommands và isGoalReached.
FakePosePort& fixedPose()
{
static FakePosePort pose;
static bool ready = false;
if (!ready)
{
pose.setPosition(0.0, 0.0);
ready = true;
}
return pose;
}
/// [m/s] Lệnh nền của plugin test khi chưa đặt trần và vận tốc đo được bằng 0.
constexpr double kBaseSpeed = 0.25;
/// [rad/s]
constexpr double kBaseYawRate = 0.40;
/**
* @brief Con trỏ costmap giả.
*
* `Costmap2DROBOT` không dựng được trong unit test (cần `tf3::BufferCore` thật và cây config đầy
* đủ). An toàn ở đây vì `test_local_planner.cpp` không alias nào chạm vào con trỏ này — nó chỉ đi
* qua `initialize()` rồi bị bỏ. Đường có costmap thật thuộc test tích hợp (Phase 5).
*/
robot_costmap_2d::Costmap2DROBOT* dummyCostmap()
{
static std::uintptr_t placeholder = 0;
return reinterpret_cast<robot_costmap_2d::Costmap2DROBOT*>(&placeholder);
}
std::vector<robot_geometry_msgs::PoseStamped> makePlan(std::size_t poses = 3)
{
std::vector<robot_geometry_msgs::PoseStamped> plan;
for (std::size_t i = 0; i < poses; ++i)
{
robot_geometry_msgs::PoseStamped pose;
pose.header.frame_id = "map";
pose.pose.position.x = static_cast<double>(i); // [m]
pose.pose.orientation.w = 1.0;
plan.push_back(pose);
}
return plan;
}
robot_geometry_msgs::Vector3 vec(double x, double y = 0.0, double z = 0.0)
{
robot_geometry_msgs::Vector3 v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
robot_geometry_msgs::Twist twist(double vx, double wz = 0.0)
{
robot_geometry_msgs::Twist t;
t.linear.x = vx; // [m/s]
t.angular.z = wz; // [rad/s]
return t;
}
class Fixture
{
public:
explicit Fixture(const std::string& name = "TestControllerOk")
{
robot::NodeHandle nh;
std::string error;
runner_.disableCostmapLockForTesting(); // costmap placeholder
ok_ = runner_.configure(nh, nullptr, dummyCostmap(), &fixedPose(), name, error);
error_ = error;
}
bool ok() const
{
return ok_;
}
const std::string& error() const
{
return error_;
}
ControllerRunner runner_;
private:
bool ok_ = false;
std::string error_;
};
} // namespace
// ================================================================================================
// Cấu hình và nạp plugin
// ================================================================================================
TEST(ControllerRunner, RefusesNullCostmap)
{
robot::NodeHandle nh;
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
std::string error;
EXPECT_FALSE(runner.configure(nh, nullptr, nullptr, &fixedPose(), "TestControllerOk", error));
EXPECT_FALSE(error.empty());
EXPECT_FALSE(runner.configured());
}
TEST(ControllerRunner, RefusesNullPosePort)
{
// Gen-2 nhận pose làm THAM SỐ của computeVelocityCommands và isGoalReached; không có nguồn pose
// thì không gọi được hàm nào trong hai hàm đó.
robot::NodeHandle nh;
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
std::string error;
EXPECT_FALSE(runner.configure(nh, nullptr, dummyCostmap(), nullptr, "TestControllerOk", error));
EXPECT_FALSE(runner.configured());
}
TEST(ControllerRunner, ConfigureFailsWhenTheInitialControllerCannotBeLoaded)
{
robot::NodeHandle nh;
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
std::string error;
EXPECT_FALSE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "TestControllerMissing", error));
EXPECT_FALSE(runner.configured()) << "configure failed but the object still reports itself as "
"configured";
}
TEST(ControllerRunner, LoadsTheInitialControllerAndReportsItAsActive)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
EXPECT_EQ(fixture.runner_.activeController(), "TestControllerOk");
EXPECT_EQ(fixture.runner_.loadedCount(), 1U);
}
TEST(ControllerRunner, SwapsBetweenControllersAndReusesLoadedLibraries)
{
// swapPlanner chạy ở cửa vào mỗi yêu cầu (profile position/docking/...). Đổi qua lại không được
// dlopen lại.
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.swapPlanner("TestControllerSecondary"));
EXPECT_EQ(fixture.runner_.loadedCount(), 2U);
ASSERT_TRUE(fixture.runner_.swapPlanner("TestControllerOk"));
EXPECT_EQ(fixture.runner_.activeController(), "TestControllerOk");
EXPECT_EQ(fixture.runner_.loadedCount(), 2U) << "switched back to the previous controller yet "
"reloaded the library";
}
TEST(ControllerRunner, FailedSwapKeepsThePreviousControllerActive)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
EXPECT_FALSE(fixture.runner_.swapPlanner("TestControllerMissing"));
EXPECT_EQ(fixture.runner_.activeController(), "TestControllerOk");
}
TEST(ControllerRunner, SwapBeforeConfigureIsRefused)
{
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
EXPECT_FALSE(runner.swapPlanner("TestControllerOk"));
}
// ================================================================================================
// Trần vận tốc — đường tầng an toàn hạ tốc độ robot (bước 12)
// ================================================================================================
TEST(ControllerRunner, ForwardVelocityLimitReachesThePlugin)
{
// Nếu lời gọi này không tới được plugin thì tầng an toàn yêu cầu giảm tốc mà robot vẫn chạy
// nguyên tốc độ planner — và không có dấu hiệu nào cả.
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
ASSERT_NEAR(cmd.linear.x, kBaseSpeed, 1e-9);
ASSERT_TRUE(fixture.runner_.setTwistLinear(vec(0.10))); // [m/s]
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 0.10, 1e-9) << "velocity limit never reached the plugin";
}
TEST(ControllerRunner, AngularVelocityLimitReachesThePlugin)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
ASSERT_NEAR(cmd.angular.z, kBaseYawRate, 1e-9);
ASSERT_TRUE(fixture.runner_.setTwistAngular(vec(0.0, 0.0, 0.15))); // [rad/s]
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.angular.z, 0.15, 1e-9);
}
TEST(ControllerRunner, LimitSetBeforeAControllerExistsIsAppliedOnceItIsLoaded)
{
// Thứ tự khởi tạo không do move_base2 quyết: host có thể đặt trần trước khi controller được nạp.
robot::NodeHandle nh;
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "", error)) << error;
EXPECT_TRUE(runner.setTwistLinear(vec(0.08))); // [m/s], chưa có controller nào
ASSERT_TRUE(runner.swapPlanner("TestControllerOk"));
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 0.08, 1e-9) << "a limit set before the controller was loaded got lost";
}
TEST(ControllerRunner, LimitIsReappliedAfterSwappingController)
{
// Trần thuộc về YÊU CẦU chứ không thuộc về instance planner. Instance mới không biết gì về trần
// đã đặt — không áp lại là robot lặng lẽ chạy nhanh hơn mức tầng an toàn cho phép.
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setTwistLinear(vec(0.07))); // [m/s]
ASSERT_TRUE(fixture.runner_.swapPlanner("TestControllerSecondary"));
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 0.07, 1e-9) << "swapping the controller dropped the active velocity "
"limit";
}
TEST(ControllerRunner, ControllerRefusingLimitsReportsFalse)
{
// Host phải biết trần của nó không có hiệu lực, thay vì tưởng đã đặt được.
Fixture fixture("TestControllerRefusesLimits");
ASSERT_TRUE(fixture.ok()) << fixture.error();
EXPECT_FALSE(fixture.runner_.setTwistLinear(vec(0.10)));
EXPECT_FALSE(fixture.runner_.setTwistAngular(vec(0.0, 0.0, 0.10)));
}
TEST(ControllerRunner, RefreshActivePlannerReinitializesItAndRestoresThePlan)
{
// Local planners như HybridController copy footprint vào cache trong initialize(). Refresh phải
// tạo instance mới, nhưng không được làm mất goal/plan giữa mission đang chạy.
Fixture fixture("TestControllerFootprintProbe");
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist before;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(before));
ASSERT_GT(before.linear.x, 0.0);
ASSERT_TRUE(fixture.runner_.refreshActivePlanner());
robot_geometry_msgs::Twist after;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(after));
EXPECT_GT(after.linear.x, before.linear.x)
<< "planner was not recreated, or its active goal/plan was not restored";
}
TEST(ControllerRunner, NonFiniteLimitIsRejected)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
const double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_FALSE(fixture.runner_.setTwistLinear(vec(nan)));
EXPECT_FALSE(fixture.runner_.setTwistAngular(vec(0.0, 0.0, nan)));
}
// ================================================================================================
// Vận tốc đo được
// ================================================================================================
TEST(ControllerRunner, MeasuredVelocityReachesThePlugin)
{
// Interface gen-1 nhận vận tốc hiện tại làm tham số của computeVelocityCommands. Bản cũ đưa nó
// vào bằng con trỏ tới bộ nhớ host ghi (`setOdom(&odometry_)`) — một data race không có gì bảo
// vệ. Ở đây truyền theo giá trị.
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
fixture.runner_.setMeasuredVelocity(twist(0.30)); // [m/s]
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, kBaseSpeed + 0.30, 1e-9) << "measured velocity never reached the "
"plugin";
}
TEST(ControllerRunner, NonFiniteMeasuredVelocityIsDroppedAndTheOldValueKept)
{
// Nhiều local planner dùng vận tốc hiện tại làm mốc giới hạn gia tốc; NaN ở đó lan ra toàn bộ
// cost function.
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
fixture.runner_.setMeasuredVelocity(twist(0.20));
fixture.runner_.setMeasuredVelocity(twist(std::numeric_limits<double>::quiet_NaN()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, kBaseSpeed + 0.20, 1e-9);
}
// ================================================================================================
// Đường lỗi
// ================================================================================================
TEST(ControllerRunner, EmptyPlanIsRefused)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
EXPECT_FALSE(fixture.runner_.setPlan({}));
}
TEST(ControllerRunner, SetPlanWithoutAControllerFails)
{
robot::NodeHandle nh;
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "", error)) << error;
EXPECT_FALSE(runner.setPlan(makePlan()));
}
TEST(ControllerRunner, ComputeWithoutAControllerYieldsNoCommand)
{
robot::NodeHandle nh;
ControllerRunner runner;
runner.disableCostmapLockForTesting(); // costmap của test là placeholder, không dereference được
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "", error)) << error;
robot_geometry_msgs::Twist cmd;
EXPECT_FALSE(runner.computeVelocityCommands(cmd));
}
TEST(ControllerRunner, PluginReturningNoCommandIsPassedThroughAsFalse)
{
Fixture fixture("TestControllerNoCommand");
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
EXPECT_FALSE(fixture.runner_.computeVelocityCommands(cmd));
}
TEST(ControllerRunner, NaNCommandIsBlockedAtTheBoundary)
{
// VelocityArbiter cũng chặn NaN, nhưng chặn tại nguồn cho biết ĐÚNG plugin nào đang trả dữ liệu
// hỏng — arbiter chỉ thấy một con số vô nghĩa không rõ từ đâu.
Fixture fixture("TestControllerNaN");
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
EXPECT_FALSE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_TRUE(std::isfinite(cmd.linear.x)) << "a command containing NaN was still written out";
}
TEST(ControllerRunner, ExceptionFromThePluginIsContained)
{
Fixture fixture("TestControllerThrowing");
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
EXPECT_NO_THROW({ EXPECT_FALSE(fixture.runner_.computeVelocityCommands(cmd)); });
}
TEST(ControllerRunner, CommandIsClearedBeforeEveryAttempt)
{
// Bên gọi dùng lại cùng một biến qua nhiều cycle. Trả false mà để nguyên lệnh cũ trong đó là mời
// tầng trên phát lại một lệnh đã hết hạn.
Fixture fixture("TestControllerNoCommand");
ASSERT_TRUE(fixture.ok()) << fixture.error();
robot_geometry_msgs::Twist cmd = twist(9.0, 9.0);
EXPECT_FALSE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 0.0, 1e-9);
EXPECT_NEAR(cmd.angular.z, 0.0, 1e-9);
}
// ================================================================================================
// setDockingMarker — kênh marker của chặng docking
//
// Docking planner đọc `maker_name` đúng MỘT lần trong initialize() (getMaker). Bản cũ dlopen lại
// planner mỗi lần dock nên luôn thấy giá trị mới; ControllerRunner cache instance nên phải tự dựng
// lại khi marker đổi — không làm là robot dock vào marker của chặng TRƯỚC, lặng lẽ.
// ================================================================================================
TEST(ControllerRunnerDockingMarker, RejectsMarkerNotInMakerSources)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
EXPECT_FALSE(fixture.runner_.setDockingMarker("khong_ton_tai"));
// Marker hợp lệ (test/config: `maker_sources: dock_a dock_b`) phải qua.
EXPECT_TRUE(fixture.runner_.setDockingMarker("dock_a"));
}
TEST(ControllerRunnerDockingMarker, CachedPlannerIsReinitializedWhenMarkerChanges)
{
Fixture fixture;
ASSERT_TRUE(fixture.ok()) << fixture.error();
ASSERT_TRUE(fixture.runner_.setDockingMarker("dock_a"));
ASSERT_TRUE(fixture.runner_.swapPlanner("TestControllerMarkerProbe"));
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 0.11, 1e-9) << "probe could not read maker_name='dock_a' at init";
// Đổi marker rồi swap lại CÙNG planner: instance cache phải được dựng lại để initialize() đọc
// giá trị mới. Nếu không, lệnh vẫn mang mã của dock_a — chính là "dock vào nhầm trạm".
ASSERT_TRUE(fixture.runner_.setDockingMarker("dock_b"));
ASSERT_TRUE(fixture.runner_.swapPlanner("TestControllerMarkerProbe"));
ASSERT_TRUE(fixture.runner_.setPlan(makePlan()));
ASSERT_TRUE(fixture.runner_.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 0.22, 1e-9)
<< "instance cache kept the old maker_name — it must re-init when the marker changes";
}
// ================================================================================================
// Kho cảm biến thô: dynamic_cast qua ranh giới .so
// ================================================================================================
TEST(ControllerRunnerSensors, TheSharedStoreReachesAPlannerThatAsksForIt)
{
// Đây là test DUY NHẤT chứng minh được `dynamic_cast<SensorConsumer*>` khớp RTTI qua `dlopen`.
// Build xanh không nói gì về chuyện đó: cast lệch trả null, planner im lặng chạy không cảm biến.
robot_sensor_ports::SensorStore store;
robot_sensor_ports::SensorStoreConfig store_config;
robot_sensor_ports::SensorEntryConfig entry;
entry.topic = "/probe_scan";
entry.data_type = "robot_sensor_msgs/LaserScan";
entry.capacity = 1;
entry.max_age = 0.5; // [s]
store_config.sensors.push_back(entry);
std::string error;
ASSERT_TRUE(store.configure(store_config, error)) << error;
ControllerRunner runner;
runner.disableCostmapLockForTesting();
runner.setSensorReader(&store); // phải TRƯỚC configure()
robot::NodeHandle nh;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "TestControllerSensorProbe",
error))
<< error;
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
// theta = 1.0 nghĩa là plugin đã cầm cổng đọc TỪ LÚC initialize, không phải sau đó.
EXPECT_NEAR(cmd.angular.z, 1.0, 1e-9) << "dynamic_cast qua .so không khớp — RTTI lệch";
EXPECT_NEAR(cmd.linear.x, 0.0, 1e-9) << "chưa có scan nào thì chưa đọc được gì";
robot_sensor_msgs::LaserScan scan;
scan.ranges.assign(10, 1.0F);
ASSERT_TRUE(robot_sensor_ports::put(store, "/probe_scan", scan, robot::Time(10.0)));
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 1.0, 1e-9) << "plugin không đọc được scan từ kho";
// Không có scan mới: cursor phải giữ, không đếm lại đúng bản cũ.
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 1.0, 1e-9) << "cursor không giữ — cùng một scan bị đếm hai lần";
ASSERT_TRUE(robot_sensor_ports::put(store, "/probe_scan", scan, robot::Time(10.1)));
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 2.0, 1e-9);
}
TEST(ControllerRunnerSensors, APlannerThatDoesNotAskForSensorsIsUnaffected)
{
// Phần lớn planner trong workspace không kế thừa SensorConsumer. Cast ra null và không có gì xảy
// ra — đó là lý do thêm kho không bắt gói nào build lại.
robot_sensor_ports::SensorStore store;
ControllerRunner runner;
runner.disableCostmapLockForTesting();
runner.setSensorReader(&store);
robot::NodeHandle nh;
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "TestControllerOk", error))
<< error;
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, kBaseSpeed, 1e-9);
}
TEST(ControllerRunnerSensors, APlannerStillLoadsWhenNoStoreIsConfigured)
{
// Runtime không khai `sensor_store` thì cổng là null. Planner khai nhận cảm biến vẫn phải nạp
// được và chạy được, chỉ là không đọc được gì.
ControllerRunner runner;
runner.disableCostmapLockForTesting();
robot::NodeHandle nh;
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(),
"TestControllerSensorProbe", error))
<< error;
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.angular.z, 0.0, 1e-9) << "không có kho thì cổng phải là null";
EXPECT_NEAR(cmd.linear.x, 0.0, 1e-9);
}
int main(int argc, char** argv)
{
setenv("PNKX_NAV_CORE_CONFIG_DIR", MOVE_BASE2_TEST_CONFIG_DIR, 0);
setenv("PNKX_NAV_CORE_LIBRARY_PATH", MOVE_BASE2_TEST_LIBRARY_DIR, 0);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}