feat: add entry_direction and derive an upside-down lidar from TF

The charger goal heading always pointed out of the station, so only robots
that reverse onto the charger were supported. Front-charging robots now set
`entry_direction: forward` (goal yaw + pi, position unchanged) together with
a front `scan_topic`. The default stays `backward`, so existing configs keep
their behaviour.

Once per run the handler looks up base_link <- scan frame and:
- fails the action if the scan plane is tilted more than 5 deg;
- fails the action if the lidar faces the side opposite to entry_direction,
  instead of writing a reversed goal;
- mirrors the bearings when the lidar is mounted upside down (roll pi),
  as the trolley handler does. No config key is needed for that.

Tests: forward vs backward, direction mismatch, upside-down V / taped V /
arc, tilted scan plane, invalid entry_direction.
This commit is contained in:
2026-11-10 15:06:00 +07:00
parent 5bbcf37f1d
commit c125bd069c
5 changed files with 287 additions and 9 deletions

View File

@@ -14,6 +14,7 @@
*********************************************************************/
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
@@ -191,7 +192,8 @@ robot_protocol_msgs::Action makeAction(const std::string& profile = std::string(
class Rig
{
public:
explicit Rig(const std::string& ns = "actions_dock")
explicit Rig(const std::string& ns = "actions_dock",
const std::string& direction = "backward", double base_yaw = M_PI)
{
robot_sensor_ports::SensorStoreConfig store_config;
robot_sensor_ports::SensorEntryConfig entry;
@@ -208,15 +210,33 @@ public:
// kết quả cũng là lệch của phép DÒ, không phải của phép quy đổi.
setSensorPose(0.0, 0.0, 0.0, 0.0);
// Keep the historic map <- scan test geometry and place the robot base so that the
// same lidar is rear-facing by default. This also leaves scan-time TF independent.
tf3::TransformStampedMsg base;
base.header.frame_id = kGlobalFrame;
base.child_frame_id = "base_link";
base.transform.rotation.z = std::sin(base_yaw / 2.0);
base.transform.rotation.w = std::cos(base_yaw / 2.0);
tf.setTransform(base, "test", /*is_static=*/true);
ctx.tf = &tf;
ctx.sensors = &store;
ctx.global_frame = kGlobalFrame;
registry.setContext(ctx);
if (ns == "actions_dock")
{
nh.setParam("actions_dock/detect/entry_direction", direction);
}
loaded = registry.loadFromConfig(nh, ns);
if (ns == "actions_dock")
{
nh.setParam("actions_dock/detect/entry_direction", std::string("backward"));
}
handler = registry.find("DetectCharger");
}
void setSensorPose(double x, double y, double yaw, double stamp_s)
void setSensorPose(double x, double y, double yaw, double stamp_s,
double roll = 0.0, double pitch = 0.0)
{
tf3::TransformStampedMsg t;
t.header.stamp = toTf3Time(stamp_s);
@@ -225,10 +245,13 @@ public:
t.transform.translation.x = x;
t.transform.translation.y = y;
t.transform.translation.z = 0.0;
t.transform.rotation.x = 0.0;
t.transform.rotation.y = 0.0;
t.transform.rotation.z = std::sin(yaw * 0.5);
t.transform.rotation.w = std::cos(yaw * 0.5);
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);
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;
t.transform.rotation.w = cr * cp * cy + sr * sp * sy;
tf.setTransform(t, "test", /*is_static=*/false);
}
@@ -295,6 +318,48 @@ public:
ActionHandler* handler = nullptr;
};
double yawOf(const tf3::QuaternionMsg& q)
{
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 angleDistance(double a, double b)
{
return std::fabs(std::atan2(std::sin(a - b), std::cos(a - b)));
}
// A mirrored physical scan keeps the original ray indices and mirrors their bearings;
// reverse both arrays to synthesize the same asymmetric scene with a roll-pi sensor.
ActionStatus feedMounted(Rig& rig, const std::vector<Segment>& scene,
const std::vector<Segment>& tapes, double roll, double pitch = 0.0,
double yaw = 0.0)
{
ActionStatus status = ActionStatus::kRunning;
for (int i = 0; i < 8 && status == ActionStatus::kRunning; ++i)
{
const double t = 10.0 + 0.1 * i;
rig.setSensorPose(0.0, 0.0, yaw, t, roll, pitch);
auto scan = makeScan(scene, t, 600, tapes);
if (std::fabs(roll) > 1.0)
{
std::reverse(scan.ranges.begin(), scan.ranges.end());
std::reverse(scan.intensities.begin(), scan.intensities.end());
}
robot_sensor_ports::put(rig.store, kScanTopic, scan, robot::Time(t));
const auto tick = rig.handler->update(robot::Time(t));
status = tick.status;
rig.last_message = tick.message;
}
return status;
}
void expectSameGoal(const tf3::TransformStampedMsg& a, const tf3::TransformStampedMsg& b)
{
EXPECT_NEAR(a.transform.translation.x, b.transform.translation.x, 0.001);
EXPECT_NEAR(a.transform.translation.y, b.transform.translation.y, 0.001);
EXPECT_LT(angleDistance(yawOf(a.transform.rotation), yawOf(b.transform.rotation)), 0.001);
}
// ================================================================================================
// 1. Đường chạy đủ
// ================================================================================================
@@ -620,6 +685,80 @@ TEST(DockPatternAction, RefusesToLoadWithoutASensorStore)
EXPECT_EQ(registry.find("DetectCharger"), nullptr);
}
TEST(DockPatternAction, ForwardAndBackwardUseSamePositionAndOppositeHeadings)
{
Rig rear;
Rig front("actions_dock", "forward", 0.0);
ASSERT_TRUE(rear.loaded);
ASSERT_TRUE(front.loaded);
ASSERT_TRUE(rear.handler->start(makeAction(), robot::Time(10.0)));
ASSERT_TRUE(front.handler->start(makeAction(), robot::Time(10.0)));
const auto scene = vPlate(1.20, M_PI / 4.0, 0.30);
ASSERT_EQ(feedMounted(rear, scene, {}, 0.0), ActionStatus::kSucceeded) << rear.last_message;
ASSERT_EQ(feedMounted(front, scene, {}, 0.0), ActionStatus::kSucceeded) << front.last_message;
const auto a = rear.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time());
const auto b = front.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time());
EXPECT_NEAR(a.transform.translation.x, b.transform.translation.x, 0.001);
EXPECT_NEAR(a.transform.translation.y, b.transform.translation.y, 0.001);
EXPECT_NEAR(angleDistance(yawOf(a.transform.rotation), yawOf(b.transform.rotation)), M_PI, 0.001);
}
TEST(DockPatternAction, DirectionMismatchFailsWithoutGoal)
{
Rig rig("actions_dock", "backward", 0.0);
ASSERT_TRUE(rig.loaded);
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(10.0)));
EXPECT_EQ(feedMounted(rig, vPlate(1.2, M_PI / 4.0, 0.30), {}, 0.0), ActionStatus::kFailed);
EXPECT_NE(rig.last_message.find("entry_direction backward"), std::string::npos);
EXPECT_NE(rig.last_message.find("faces forward"), std::string::npos);
EXPECT_ANY_THROW(rig.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time()));
}
TEST(DockPatternAction, UpsideDownScanMatchesUprightForVArcAndTape)
{
for (const std::string profile : {"v_std", "v_tape", "cradle"})
{
SCOPED_TRACE(profile);
Rig upright;
Rig flipped;
ASSERT_TRUE(upright.loaded);
ASSERT_TRUE(flipped.loaded);
ASSERT_TRUE(upright.handler->start(makeAction(profile), robot::Time(10.0)));
ASSERT_TRUE(flipped.handler->start(makeAction(profile), robot::Time(10.0)));
auto scene = profile == "cradle" ? concaveCradle(1.20, 0.40, 2.0)
: vPlate(1.20, M_PI / 4.0, 0.30);
auto tapes = profile == "v_tape" ? vPlateTapes(1.20, M_PI / 4.0, 0.30, 0.12)
: std::vector<Segment>{};
// Shift the scene sideways so a missing reflection cannot pass by symmetry.
for (auto& s : scene) { s.ay += 0.08; s.by += 0.08; }
for (auto& s : tapes) { s.ay += 0.08; s.by += 0.08; }
ASSERT_EQ(feedMounted(upright, scene, tapes, 0.0, 0.0, 0.4), ActionStatus::kSucceeded)
<< upright.last_message;
ASSERT_EQ(feedMounted(flipped, scene, tapes, M_PI, 0.0, 0.4), ActionStatus::kSucceeded)
<< flipped.last_message;
expectSameGoal(upright.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time()),
flipped.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time()));
}
}
TEST(DockPatternAction, TiltedScanPlaneFailsWithoutGoal)
{
Rig rig;
ASSERT_TRUE(rig.loaded);
ASSERT_TRUE(rig.handler->start(makeAction(), robot::Time(10.0)));
EXPECT_EQ(feedMounted(rig, vPlate(1.2, M_PI / 4.0, 0.30), {}, 0.0, 0.11),
ActionStatus::kFailed);
EXPECT_NE(rig.last_message.find("tilted"), std::string::npos);
EXPECT_ANY_THROW(rig.tf.lookupTransform(kGlobalFrame, kOutFrame, tf3::Time()));
}
TEST(DockPatternAction, RefusesInvalidEntryDirection)
{
Rig rig("actions_dock", "sideways");
EXPECT_FALSE(rig.loaded);
EXPECT_EQ(rig.handler, nullptr);
}
} // namespace
int main(int argc, char** argv)