From 6f0246baf3fde64e2a8f3ff81ac2afa3ba5fad19 Mon Sep 17 00:00:00 2001 From: Adrian Collister Date: Tue, 9 Jun 2026 04:28:55 -0700 Subject: [PATCH] Add test for Flex contact and touch sensors. The test verifies that contact sensors on subtrees and touch sensors on sites correctly register contacts involving Flex components. PiperOrigin-RevId: 929109418 Change-Id: I1865311a6da838d75ef33989ac7f3c2d975cdbff --- test/engine/engine_sensor_test.cc | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/engine/engine_sensor_test.cc b/test/engine/engine_sensor_test.cc index 1ab1dc77..1332ba99 100644 --- a/test/engine/engine_sensor_test.cc +++ b/test/engine/engine_sensor_test.cc @@ -1813,5 +1813,59 @@ TEST_F(SensorTest, TactileMeshIdMismatchedValidator) { mj_deleteModel(m); } +// Test that contact and touch sensors work with Flex contacts. +TEST_F(SensorTest, FlexContactSensors) { + static constexpr char xml[] = R"( + + + )"; + + char error[1024] = {0}; + mjModel* m = LoadModelFromString(xml, error, sizeof(error)); + ASSERT_THAT(m, NotNull()) << error; + mjData* d = mj_makeData(m); + + mj_forward(m, d); + + // We expect at least one contact between the flex and the floor + ASSERT_GT(d->ncon, 0) << "No contacts generated"; + + // Check the contact sensor "flex_contact_subtree" + vector flex_contact_subtree = GetSensor(m, d, "flex_contact_subtree"); + EXPECT_GT(flex_contact_subtree[0], 0) + << "Flex contact sensor (subtree) did not detect any contacts"; + + // Check the contact sensor "flex_contact_body" + vector flex_contact_body = GetSensor(m, d, "flex_contact_body"); + EXPECT_EQ(flex_contact_body[0], 0) + << "Flex contact sensor (body) should not match contacts on child bodies"; + + // Check the touch sensor "floor_touch" + vector floor_touch = GetSensor(m, d, "floor_touch"); + EXPECT_GT(floor_touch[0], 0.0) + << "Floor touch sensor did not detect any force"; + + mj_deleteData(d); + mj_deleteModel(m); +} + } // namespace } // namespace mujoco