TrolleyDetectionActionHandler finds a 4-leg trolley in a raw LaserScan with dock_pattern_detector's LegPatternMatcher, filters the detections and writes a static dock_target frame for the docking leg that drives under the trolley. - entry_direction forward/backward sets the goal heading; a lidar that faces the wrong way fails the action instead of writing a reversed goal - an upside-down lidar (roll pi in TF) is detected from TF and its bearings are mirrored; a scan plane tilted more than 5 deg fails the action - profiles validated at boot; optional prior, reflective tape (all/entry) - gtest (11 tests) incl. a recorded Gazebo scan from a roll-pi lidar - sim models trolley_4legs (+ taped variants) the robot can drive under, spawn launch and generator script - doc/MEASURE_AND_CONFIGURE.md: how to measure a trolley and fill the profile
540 lines
20 KiB
C++
540 lines
20 KiB
C++
/*********************************************************************
|
||
*
|
||
* Software License Agreement (BSD License)
|
||
*
|
||
* Kiểm handler dò xe hàng — chạy ĐÚNG chuỗi mà runtime chạy: kho cảm biến thật + buffer tf3 thật +
|
||
* plugin nạp qua Boost.DLL. Chỉ scan là dựng sẵn (chiếu tia lên 4 chân hộp), nên đáp án biết trước.
|
||
*
|
||
* Trọng tâm là những chỗ chỉ HỎNG KHI GHÉP, mà test lõi không thấy: heading theo `entry_direction`,
|
||
* lidar nhìn ngược hướng chui, lidar gắn lộn ngược (roll π), prior tính trong base_link chứ không
|
||
* trong frame lidar, quy TF tại đúng stamp, và các đường từ chối lúc boot. Một test chạy trên scan
|
||
* Gazebo thật (test/data) để chốt cách đọc lidar lộn ngược bằng dữ liệu, không chỉ bằng mô hình.
|
||
*
|
||
* Author: DuongTD
|
||
*********************************************************************/
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <cstdlib>
|
||
#include <limits>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include <yaml-cpp/yaml.h>
|
||
|
||
#include <robot/node_handle.h>
|
||
#include <tf3/buffer_core.h>
|
||
|
||
#include <action_core/action_registry.h>
|
||
#include <robot_sensor_ports/sensor_store.h>
|
||
#include <robot_sensor_ports/sensor_types.h>
|
||
|
||
namespace
|
||
{
|
||
using action_core::ActionContext;
|
||
using action_core::ActionHandler;
|
||
using action_core::ActionRegistry;
|
||
using action_core::ActionStatus;
|
||
|
||
constexpr const char* kNamespace = "actions_trolley";
|
||
constexpr const char* kGlobalFrame = "map";
|
||
constexpr const char* kBaseFrame = "base_link";
|
||
constexpr const char* kFrontFrame = "front_laser";
|
||
constexpr const char* kBackFrame = "back_laser";
|
||
/// Lidar trước gắn lộn ngược (roll π) như hook_150 trong sim.
|
||
constexpr const char* kFlippedFrame = "front_laser_flipped";
|
||
/// Lidar trước gắn chúi xuống quá giới hạn của handler.
|
||
constexpr const char* kTiltedFrame = "front_laser_tilted";
|
||
constexpr const char* kFrontTopic = "/f_scan";
|
||
constexpr const char* kBackTopic = "/b_scan";
|
||
constexpr const char* kOutFrame = "dock_target";
|
||
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||
|
||
/// Lidar của hook_150 cách tâm robot 0.454 m dọc trục x.
|
||
constexpr double kLaserX = 0.454; // [m]
|
||
|
||
/// Xe `tci_trolley` (mô hình sim).
|
||
constexpr double kOpening = 0.517; // [m]
|
||
constexpr double kDepth = 0.720; // [m]
|
||
constexpr double kLeg = 0.030; // [m]
|
||
|
||
/// Tâm xe trong frame lidar khi cạnh vào cách lidar 0.9 m.
|
||
constexpr double kCartInLaser = 0.9 + kDepth / 2.0; // [m]
|
||
|
||
/// @brief Quy giây thành tf3::Time QUA robot::Time — cùng một hàm đổi thì stamp khớp từng bit.
|
||
tf3::Time toTf3Time(double t)
|
||
{
|
||
const robot::Time rt(t);
|
||
return tf3::Time(rt.sec, rt.nsec);
|
||
}
|
||
|
||
struct Segment
|
||
{
|
||
double ax, ay, bx, by; // [m]
|
||
};
|
||
|
||
/// @brief 4 chân hộp của xe: tâm (cx, cy) trong frame lidar, trục x của xe theo @p axis.
|
||
std::vector<Segment> cartLegs(double cx, double cy, double axis)
|
||
{
|
||
std::vector<Segment> out;
|
||
const double lx[4] = { kDepth / 2.0, kDepth / 2.0, -kDepth / 2.0, -kDepth / 2.0 };
|
||
const double ly[4] = { kOpening / 2.0, -kOpening / 2.0, kOpening / 2.0, -kOpening / 2.0 };
|
||
const double c = std::cos(axis);
|
||
const double s = std::sin(axis);
|
||
for (int k = 0; k < 4; ++k)
|
||
{
|
||
const double x = cx + c * lx[k] - s * ly[k];
|
||
const double y = cy + s * lx[k] + c * ly[k];
|
||
const double h = kLeg / 2.0;
|
||
const double qx[4] = { h, -h, -h, h };
|
||
const double qy[4] = { h, h, -h, -h };
|
||
double px[4], py[4];
|
||
for (int i = 0; i < 4; ++i)
|
||
{
|
||
px[i] = x + c * qx[i] - s * qy[i];
|
||
py[i] = y + s * qx[i] + c * qy[i];
|
||
}
|
||
for (int i = 0; i < 4; ++i)
|
||
{
|
||
out.push_back({ px[i], py[i], px[(i + 1) % 4], py[(i + 1) % 4] });
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/// @brief Cảnh nhìn từ lidar lộn ngược (roll π): mọi điểm đối dấu y so với frame đứng thẳng.
|
||
std::vector<Segment> mirrorY(std::vector<Segment> world)
|
||
{
|
||
for (Segment& s : world)
|
||
{
|
||
s.ay = -s.ay;
|
||
s.by = -s.by;
|
||
}
|
||
return world;
|
||
}
|
||
|
||
/// @brief Scan Gazebo đã chụp sẵn trong test/data. Đọc range qua strtof vì rostopic echo in `inf`,
|
||
/// thứ yaml-cpp không đổi thẳng sang số.
|
||
bool loadRecordedScan(const std::string& path, robot_sensor_msgs::LaserScan& scan)
|
||
{
|
||
YAML::Node node;
|
||
try
|
||
{
|
||
node = YAML::LoadFile(path);
|
||
}
|
||
catch (const YAML::Exception&)
|
||
{
|
||
return false;
|
||
}
|
||
scan.header.frame_id = node["frame_id"].as<std::string>();
|
||
scan.angle_min = node["angle_min"].as<float>();
|
||
scan.angle_max = node["angle_max"].as<float>();
|
||
scan.angle_increment = node["angle_increment"].as<float>();
|
||
scan.range_min = node["range_min"].as<float>();
|
||
scan.range_max = node["range_max"].as<float>();
|
||
scan.ranges.clear();
|
||
for (const YAML::Node& r : node["ranges"])
|
||
{
|
||
scan.ranges.push_back(std::strtof(r.Scalar().c_str(), nullptr));
|
||
}
|
||
return !scan.ranges.empty();
|
||
}
|
||
|
||
/// @brief Scan kiểu SICK S300 sim (541 tia, ±1.588 rad) chiếu lên @p world.
|
||
robot_sensor_msgs::LaserScan makeScan(const std::vector<Segment>& world, const char* frame,
|
||
double stamp_s)
|
||
{
|
||
constexpr std::size_t kRays = 541;
|
||
robot_sensor_msgs::LaserScan scan;
|
||
scan.header.frame_id = frame;
|
||
scan.header.stamp = robot::Time(stamp_s);
|
||
scan.angle_min = -1.588249619f;
|
||
scan.angle_max = 1.588249619f;
|
||
scan.angle_increment = (scan.angle_max - scan.angle_min) / static_cast<float>(kRays - 1);
|
||
scan.range_min = 0.05f;
|
||
scan.range_max = 29.0f;
|
||
scan.ranges.assign(kRays, static_cast<float>(kInf));
|
||
|
||
for (std::size_t i = 0; i < kRays; ++i)
|
||
{
|
||
const double bearing = scan.angle_min + static_cast<double>(i) * scan.angle_increment;
|
||
const double dx = std::cos(bearing);
|
||
const double dy = std::sin(bearing);
|
||
for (const Segment& s : world)
|
||
{
|
||
const double ex = s.bx - s.ax;
|
||
const double ey = s.by - s.ay;
|
||
const double det = dx * (-ey) - dy * (-ex);
|
||
if (std::fabs(det) < 1e-12)
|
||
{
|
||
continue;
|
||
}
|
||
const double t = (s.ax * (-ey) - s.ay * (-ex)) / det;
|
||
const double u = (dx * s.ay - dy * s.ax) / det;
|
||
if (t > 0.0 && u >= 0.0 && u <= 1.0 && t < scan.ranges[i])
|
||
{
|
||
scan.ranges[i] = static_cast<float>(t);
|
||
}
|
||
}
|
||
}
|
||
return scan;
|
||
}
|
||
|
||
robot_protocol_msgs::Action makeAction(const std::string& type, const std::string& profile = "")
|
||
{
|
||
robot_protocol_msgs::Action action;
|
||
action.actionType = type;
|
||
action.actionId = "trolley-1";
|
||
if (!profile.empty())
|
||
{
|
||
robot_protocol_msgs::ActionParameter p;
|
||
p.key = "trolley_profile";
|
||
p.value = profile;
|
||
action.actionParameters.push_back(p);
|
||
}
|
||
return action;
|
||
}
|
||
|
||
double yawOf(const tf3::TransformStampedMsg& t)
|
||
{
|
||
const auto& q = t.transform.rotation;
|
||
return std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
||
}
|
||
|
||
double yawError(double a, double b)
|
||
{
|
||
return std::fabs(std::atan2(std::sin(a - b), std::cos(a - b)));
|
||
}
|
||
|
||
/// @brief Rig: kho thật + tf3 thật + plugin nạp qua đúng đường Boost.DLL.
|
||
class Rig
|
||
{
|
||
public:
|
||
Rig()
|
||
{
|
||
robot_sensor_ports::SensorStoreConfig store_config;
|
||
for (const char* topic : { kFrontTopic, kBackTopic })
|
||
{
|
||
robot_sensor_ports::SensorEntryConfig entry;
|
||
entry.topic = topic;
|
||
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;
|
||
store_ok = store.configure(store_config, error);
|
||
|
||
// Lidar gắn cứng: trước ở +0.454 m nhìn về trước, sau ở −0.454 m nhìn về sau — như hook_150.
|
||
setStatic(kFrontFrame, kLaserX, 0.0);
|
||
setStatic(kBackFrame, -kLaserX, M_PI);
|
||
|
||
ctx.tf = &tf;
|
||
ctx.sensors = &store;
|
||
ctx.global_frame = kGlobalFrame;
|
||
registry.setContext(ctx);
|
||
loaded = registry.loadFromConfig(nh, kNamespace);
|
||
}
|
||
|
||
/// @brief Lidar gắn cứng trong base_link. Góc theo quy ước URDF: R = Rz(yaw)·Ry(pitch)·Rx(roll).
|
||
void setStatic(const char* child, double x, double yaw, double roll = 0.0, double pitch = 0.0,
|
||
double z = 0.0)
|
||
{
|
||
const double cr = std::cos(roll * 0.5), sr = std::sin(roll * 0.5);
|
||
const double cp = std::cos(pitch * 0.5), sp = std::sin(pitch * 0.5);
|
||
const double cy = std::cos(yaw * 0.5), sy = std::sin(yaw * 0.5);
|
||
tf3::TransformStampedMsg t;
|
||
t.header.stamp = tf3::Time();
|
||
t.header.frame_id = kBaseFrame;
|
||
t.child_frame_id = child;
|
||
t.transform.translation.x = x;
|
||
t.transform.translation.z = z;
|
||
t.transform.rotation.w = cr * cp * cy + sr * sp * sy;
|
||
t.transform.rotation.x = sr * cp * cy - cr * sp * sy;
|
||
t.transform.rotation.y = cr * sp * cy + sr * cp * sy;
|
||
t.transform.rotation.z = cr * cp * sy - sr * sp * cy;
|
||
tf.setTransform(t, "test", /*is_static=*/true);
|
||
}
|
||
|
||
void setRobotPose(double x, double y, double yaw, double stamp_s)
|
||
{
|
||
tf3::TransformStampedMsg t;
|
||
t.header.stamp = toTf3Time(stamp_s);
|
||
t.header.frame_id = kGlobalFrame;
|
||
t.child_frame_id = kBaseFrame;
|
||
t.transform.translation.x = x;
|
||
t.transform.translation.y = y;
|
||
t.transform.rotation.z = std::sin(yaw * 0.5);
|
||
t.transform.rotation.w = std::cos(yaw * 0.5);
|
||
tf.setTransform(t, "test", /*is_static=*/false);
|
||
}
|
||
|
||
/// @brief Bơm @p count scan rồi tick, đúng nhịp control loop. Robot đứng yên ở (rx, ry, ryaw).
|
||
ActionStatus feed(ActionHandler* handler, const std::vector<Segment>& world, const char* topic,
|
||
const char* frame, int count, double rx = 0.0, double ry = 0.0,
|
||
double ryaw = 0.0)
|
||
{
|
||
return feedScan(handler, makeScan(world, frame, 0.0), topic, count, rx, ry, ryaw);
|
||
}
|
||
|
||
/// @brief Như feed() nhưng với một scan dựng sẵn; mỗi lần bơm chỉ đổi stamp.
|
||
ActionStatus feedScan(ActionHandler* handler, robot_sensor_msgs::LaserScan scan,
|
||
const char* topic, int count, double rx = 0.0, double ry = 0.0,
|
||
double ryaw = 0.0)
|
||
{
|
||
ActionStatus status = ActionStatus::kRunning;
|
||
for (int i = 0; i < count; ++i)
|
||
{
|
||
const double t = 10.0 + 0.1 * i;
|
||
setRobotPose(rx, ry, ryaw, t);
|
||
scan.header.stamp = robot::Time(t);
|
||
robot_sensor_ports::put(store, topic, scan, robot::Time(t));
|
||
const action_core::ActionTick tick = handler->update(robot::Time(t));
|
||
status = tick.status;
|
||
last_message = tick.message;
|
||
if (status != ActionStatus::kRunning)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
return status;
|
||
}
|
||
|
||
tf3::TransformStampedMsg goal()
|
||
{
|
||
return tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time());
|
||
}
|
||
|
||
std::string last_message;
|
||
bool store_ok = false;
|
||
bool loaded = false;
|
||
robot::NodeHandle nh;
|
||
tf3::BufferCore tf;
|
||
robot_sensor_ports::SensorStore store;
|
||
ActionContext ctx;
|
||
ActionRegistry registry;
|
||
};
|
||
|
||
// ================================================================================================
|
||
// 1. Đường chạy đủ + heading theo entry_direction
|
||
// ================================================================================================
|
||
|
||
TEST(TrolleyDetectionAction, ForwardEntryGoalPointsIntoTheCart)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.store_ok);
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kFrontTopic, kFrontFrame, 10),
|
||
ActionStatus::kSucceeded)
|
||
<< rig.last_message;
|
||
|
||
// Tâm xe trong base_link = 0.454 + 1.26; robot ở gốc map. Tiến vào: heading trỏ VÀO xe (+x).
|
||
const tf3::TransformStampedMsg out = rig.goal();
|
||
EXPECT_NEAR(out.transform.translation.x, kLaserX + kCartInLaser, 0.01);
|
||
EXPECT_NEAR(out.transform.translation.y, 0.0, 0.01);
|
||
EXPECT_LT(yawError(yawOf(out), 0.0), 0.02);
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, BackwardEntryGoalPointsOutOfTheEntry)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolleyBackward");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolleyBackward"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kBackTopic, kBackFrame, 10),
|
||
ActionStatus::kSucceeded)
|
||
<< rig.last_message;
|
||
|
||
// Xe ở SAU robot: tâm tại −(0.454 + 1.26). Lùi vào: heading trỏ RA cạnh vào, về phía robot
|
||
// đứng lúc dò (+x) — robot lùi thẳng vào gầm, đầu robot vẫn hướng +x.
|
||
const tf3::TransformStampedMsg out = rig.goal();
|
||
EXPECT_NEAR(out.transform.translation.x, -(kLaserX + kCartInLaser), 0.01);
|
||
EXPECT_NEAR(out.transform.translation.y, 0.0, 0.01);
|
||
EXPECT_LT(yawError(yawOf(out), 0.0), 0.02);
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, TheGoalFollowsTheRobotPose)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
const double rx = 2.0, ry = 1.0, ryaw = 0.5;
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kFrontTopic, kFrontFrame, 10, rx, ry,
|
||
ryaw),
|
||
ActionStatus::kSucceeded)
|
||
<< rig.last_message;
|
||
|
||
const double d = kLaserX + kCartInLaser;
|
||
const tf3::TransformStampedMsg out = rig.goal();
|
||
EXPECT_NEAR(out.transform.translation.x, rx + d * std::cos(ryaw), 0.01);
|
||
EXPECT_NEAR(out.transform.translation.y, ry + d * std::sin(ryaw), 0.01);
|
||
EXPECT_LT(yawError(yawOf(out), ryaw), 0.02);
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, PriorIsDeclaredInBaseLinkNotInTheLidarFrame)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
// Profile `tci_trolley_prior`: prior_x = 1.714 trong base_link, search_radius 0.30. Trong frame
|
||
// lidar tâm xe ở 1.26 — lệch 0.454 > 0.30. Chỉ qua khi prior được tính trong base_link.
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley", "tci_trolley_prior"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kFrontTopic, kFrontFrame, 10),
|
||
ActionStatus::kSucceeded)
|
||
<< rig.last_message;
|
||
}
|
||
|
||
// ================================================================================================
|
||
// 2. Lidar lộn ngược
|
||
// ================================================================================================
|
||
|
||
TEST(TrolleyDetectionAction, UpsideDownLidarIsReadThroughItsTf)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
// Lidar trước gắn roll π như hook_150. Nhìn từ trên xuống xe lệch TRÁI 0.15 m và xoay 0.1 rad;
|
||
// trong frame của chính lidar mọi thứ đối dấu y. Đọc TF chỉ lấy yaw thì đích lệch 0.30 m sang
|
||
// phải và yaw đổi dấu — robot chui vào chân xe.
|
||
rig.setStatic(kFlippedFrame, kLaserX, 0.0, M_PI);
|
||
const double cy = 0.15, axis = M_PI + 0.1;
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, mirrorY(cartLegs(kCartInLaser, cy, axis)), kFrontTopic, kFlippedFrame, 10),
|
||
ActionStatus::kSucceeded)
|
||
<< rig.last_message;
|
||
|
||
const tf3::TransformStampedMsg out = rig.goal();
|
||
EXPECT_NEAR(out.transform.translation.x, kLaserX + kCartInLaser, 0.01);
|
||
EXPECT_NEAR(out.transform.translation.y, cy, 0.01);
|
||
EXPECT_LT(yawError(yawOf(out), axis + M_PI), 0.02);
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, FindsTheTrolleyInARecordedGazeboScan)
|
||
{
|
||
// Scan /f_scan thật của Gazebo, lidar roll π, robot KHÔNG đứng ở pose chuẩn bị: tâm xe cách
|
||
// base_link 2.30 m, lệch ngang 0.22 m, xoay 17.7°. Đáp án lấy từ pose model trong Gazebo — xem
|
||
// đầu file data. Profile `tci_trolley` không có prior, đúng như config sim.
|
||
robot_sensor_msgs::LaserScan scan;
|
||
ASSERT_TRUE(loadRecordedScan(std::string(TROLLEY_DETECTION_ACTION_TEST_DATA_DIR) +
|
||
"/f_scan_tci_trolley_roll_pi.yaml",
|
||
scan));
|
||
ASSERT_EQ(scan.ranges.size(), 541u);
|
||
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
// base_link -> front_laser_link của hook_150 trong URDF sim.
|
||
rig.setStatic(scan.header.frame_id.c_str(), kLaserX, 0.0, M_PI, 0.0, 0.184);
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feedScan(h, scan, kFrontTopic, 10), ActionStatus::kSucceeded) << rig.last_message;
|
||
|
||
// Robot ở gốc map nên map = base_link. Tiến vào: heading = trục +x của xe.
|
||
const tf3::TransformStampedMsg out = rig.goal();
|
||
EXPECT_NEAR(out.transform.translation.x, 2.301, 0.02);
|
||
EXPECT_NEAR(out.transform.translation.y, 0.216, 0.02);
|
||
EXPECT_LT(yawError(yawOf(out), 0.309), 0.02);
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, FailsWhenTheScanPlaneIsTilted)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
// Chúi xuống 0.2 rad (11.5°): mô hình chân 2D không còn đúng. Phải fail, không được ghi đích.
|
||
rig.setStatic(kTiltedFrame, kLaserX, 0.0, 0.0, 0.2);
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kFrontTopic, kTiltedFrame, 3),
|
||
ActionStatus::kFailed);
|
||
EXPECT_NE(rig.last_message.find("tilted"), std::string::npos) << rig.last_message;
|
||
}
|
||
|
||
// ================================================================================================
|
||
// 3. Đường từ chối
|
||
// ================================================================================================
|
||
|
||
TEST(TrolleyDetectionAction, FailsWhenTheLidarFacesAgainstTheEntryDirection)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolleyWrong");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
// `forward` khai với lidar sau: goal sẽ ngược 180°. Phải fail ngay, không được ghi đích.
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolleyWrong"), robot::Time(10.0)));
|
||
EXPECT_EQ(rig.feed(h, cartLegs(kCartInLaser, 0.0, M_PI), kBackTopic, kBackFrame, 3),
|
||
ActionStatus::kFailed);
|
||
EXPECT_NE(rig.last_message.find("entry_direction"), std::string::npos) << rig.last_message;
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, TimesOutWithTheMatcherReasonWhenNoCartIsThere)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
|
||
ASSERT_TRUE(h->start(makeAction("DetectTrolley"), robot::Time(10.0)));
|
||
// Chỉ một bức tường: không có chân nào. timeout = 5 s → cần hơn 50 tick 0.1 s.
|
||
const std::vector<Segment> wall = { { 2.0, -3.0, 2.0, 3.0 } };
|
||
EXPECT_EQ(rig.feed(h, wall, kFrontTopic, kFrontFrame, 60), ActionStatus::kFailed);
|
||
EXPECT_NE(rig.last_message.find("no 'tci_trolley' trolley"), std::string::npos)
|
||
<< rig.last_message;
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, RefusesAnUnknownProfile)
|
||
{
|
||
Rig rig;
|
||
ASSERT_TRUE(rig.loaded);
|
||
ActionHandler* h = rig.registry.find("DetectTrolley");
|
||
ASSERT_NE(h, nullptr);
|
||
// KHÔNG âm thầm rơi về default: dùng nhầm hình học là chui vào sai chỗ.
|
||
EXPECT_FALSE(h->start(makeAction("DetectTrolley", "khong_co_profile_nay"), robot::Time(10.0)));
|
||
}
|
||
|
||
TEST(TrolleyDetectionAction, RefusesToLoadWithoutASensorStore)
|
||
{
|
||
robot::NodeHandle nh;
|
||
tf3::BufferCore tf;
|
||
ActionContext ctx;
|
||
ctx.tf = &tf;
|
||
ctx.sensors = nullptr;
|
||
ctx.global_frame = kGlobalFrame;
|
||
|
||
ActionRegistry registry;
|
||
registry.setContext(ctx);
|
||
registry.loadFromConfig(nh, kNamespace);
|
||
EXPECT_EQ(registry.find("DetectTrolley"), nullptr);
|
||
}
|
||
|
||
} // namespace
|
||
|
||
int main(int argc, char** argv)
|
||
{
|
||
#ifdef TROLLEY_DETECTION_ACTION_TEST_CONFIG_DIR
|
||
setenv("PNKX_NAV_CORE_CONFIG_DIR", TROLLEY_DETECTION_ACTION_TEST_CONFIG_DIR, 0);
|
||
#endif
|
||
#ifdef TROLLEY_DETECTION_ACTION_TEST_LIBRARY_DIR
|
||
setenv("PNKX_NAV_CORE_LIBRARY_PATH", TROLLEY_DETECTION_ACTION_TEST_LIBRARY_DIR, 0);
|
||
#endif
|
||
|
||
testing::InitGoogleTest(&argc, argv);
|
||
return RUN_ALL_TESTS();
|
||
}
|