Skip to content

Commit 9fba743

Browse files
authored
normalize quaternion sign for planar joint (#3628)
1 parent 7fd2e46 commit 9fba743

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

moveit_core/robot_model/src/planar_joint_model.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ void PlanarJointModel::computeVariablePositions(const Eigen::Isometry3d& transf,
355355

356356
ASSERT_ISOMETRY(transf) // unsanitized input, could contain a non-isometry
357357
Eigen::Quaterniond q(transf.linear());
358+
if (q.w() < 0.0)
359+
{
360+
q.coeffs() *= -1.0;
361+
}
358362
// taken from Bullet
359363
double s_squared = 1.0 - (q.w() * q.w());
360364
if (s_squared < 10.0 * std::numeric_limits<double>::epsilon())

moveit_core/robot_model/test/test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,38 @@ TEST(FloatingJointTest, interpolation_test)
172172
}
173173
}
174174

175+
TEST(PlanarJointTest, ComputeVariablePositionsNormalizeYaw)
176+
{
177+
// Create a simple planar joint model with some dummy parameters
178+
moveit::core::PlanarJointModel pjm("joint", 0, 0);
179+
180+
// Test various angles for normalization
181+
std::vector<double> test_angles = { 0.0, M_PI / 2, -M_PI / 2, M_PI, -M_PI, 3.0, -3.0, 4.1, -4.1 };
182+
for (double angle : test_angles)
183+
{
184+
// Create a transform with the given yaw angle
185+
Eigen::Isometry3d transform = Eigen::Isometry3d::Identity();
186+
Eigen::AngleAxisd q = Eigen::AngleAxisd(angle, Eigen::Vector3d::UnitZ());
187+
transform.linear() = q.toRotationMatrix();
188+
189+
// Compute the variable positions from the transform
190+
double joint_values[3];
191+
pjm.computeVariablePositions(transform, joint_values);
192+
193+
// Check that the x and y values are zero (since we didn't set any translation)
194+
EXPECT_NEAR(joint_values[0], 0.0, 1e-6);
195+
EXPECT_NEAR(joint_values[1], 0.0, 1e-6);
196+
197+
// The yaw value should be normalized to the range [-pi, pi]
198+
double normalized_angle = angle;
199+
while (normalized_angle > M_PI)
200+
normalized_angle -= 2.0 * M_PI;
201+
while (normalized_angle < -M_PI)
202+
normalized_angle += 2.0 * M_PI;
203+
EXPECT_NEAR(joint_values[2], normalized_angle, 1e-6);
204+
}
205+
}
206+
175207
int main(int argc, char** argv)
176208
{
177209
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)