mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpimath] Add CoordinateSystem conversion for Transform3d (#4443)
I also refactored Pose3d's conversion implementation to use the Translation3d and Rotation3d conversions, thereby giving Translation3d and Rotation3d test coverage. No changes were made to the expected values of the Pose3d conversion tests. The expected values of the Transform3d conversion tests were copied from the Pose3d conversion tests without modification.
This commit is contained in:
@@ -10,7 +10,7 @@ import edu.wpi.first.math.util.Units;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CoordinateSystemTest {
|
||||
private void checkConvert(
|
||||
private void checkPose3dConvert(
|
||||
Pose3d poseFrom, Pose3d poseTo, CoordinateSystem coordFrom, CoordinateSystem coordTo) {
|
||||
// "from" to "to"
|
||||
assertEquals(
|
||||
@@ -29,10 +29,34 @@ class CoordinateSystemTest {
|
||||
assertEquals(poseFrom, CoordinateSystem.convert(poseTo, coordTo, coordFrom));
|
||||
}
|
||||
|
||||
private void checkTransform3dConvert(
|
||||
Transform3d transformFrom,
|
||||
Transform3d transformTo,
|
||||
CoordinateSystem coordFrom,
|
||||
CoordinateSystem coordTo) {
|
||||
// "from" to "to"
|
||||
assertEquals(
|
||||
transformTo.getTranslation(),
|
||||
CoordinateSystem.convert(transformFrom.getTranslation(), coordFrom, coordTo));
|
||||
assertEquals(
|
||||
transformTo.getRotation(),
|
||||
CoordinateSystem.convert(transformFrom.getRotation(), coordFrom, coordTo));
|
||||
assertEquals(transformTo, CoordinateSystem.convert(transformFrom, coordFrom, coordTo));
|
||||
|
||||
// "to" to "from"
|
||||
assertEquals(
|
||||
transformFrom.getTranslation(),
|
||||
CoordinateSystem.convert(transformTo.getTranslation(), coordTo, coordFrom));
|
||||
assertEquals(
|
||||
transformFrom.getRotation(),
|
||||
CoordinateSystem.convert(transformTo.getRotation(), coordTo, coordFrom));
|
||||
assertEquals(transformFrom, CoordinateSystem.convert(transformTo, coordTo, coordFrom));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEDNtoNWU() {
|
||||
void testPose3dEDNtoNWU() {
|
||||
// No rotation from EDN to NWU
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d()),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -43,7 +67,7 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.NWU());
|
||||
|
||||
// 45° roll from EDN to NWU
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d(Units.degreesToRadians(45.0), 0.0, 0.0)),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -54,7 +78,7 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.NWU());
|
||||
|
||||
// 45° pitch from EDN to NWU
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d(0.0, Units.degreesToRadians(45.0), 0.0)),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -65,7 +89,7 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.NWU());
|
||||
|
||||
// 45° yaw from EDN to NWU
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d(0.0, 0.0, Units.degreesToRadians(45.0))),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -80,9 +104,9 @@ class CoordinateSystemTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEDNtoNED() {
|
||||
void testPose3dEDNtoNED() {
|
||||
// No rotation from EDN to NED
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d()),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -93,7 +117,7 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.NED());
|
||||
|
||||
// 45° roll from EDN to NED
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d(Units.degreesToRadians(45.0), 0.0, 0.0)),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -104,7 +128,7 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.NED());
|
||||
|
||||
// 45° pitch from EDN to NED
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d(0.0, Units.degreesToRadians(45.0), 0.0)),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -115,7 +139,7 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.NED());
|
||||
|
||||
// 45° yaw from EDN to NED
|
||||
checkConvert(
|
||||
checkPose3dConvert(
|
||||
new Pose3d(1.0, 2.0, 3.0, new Rotation3d(0.0, 0.0, Units.degreesToRadians(45.0))),
|
||||
new Pose3d(
|
||||
3.0,
|
||||
@@ -128,4 +152,100 @@ class CoordinateSystemTest {
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NED());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransform3dEDNtoNWU() {
|
||||
// No rotation from EDN to NWU
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(new Translation3d(1.0, 2.0, 3.0), new Rotation3d()),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, -1.0, -2.0),
|
||||
new Rotation3d(Units.degreesToRadians(-90.0), 0.0, Units.degreesToRadians(-90.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NWU());
|
||||
|
||||
// 45° roll from EDN to NWU
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(
|
||||
new Translation3d(1.0, 2.0, 3.0),
|
||||
new Rotation3d(Units.degreesToRadians(45.0), 0.0, 0.0)),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, -1.0, -2.0),
|
||||
new Rotation3d(Units.degreesToRadians(-45.0), 0.0, Units.degreesToRadians(-90.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NWU());
|
||||
|
||||
// 45° pitch from EDN to NWU
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(
|
||||
new Translation3d(1.0, 2.0, 3.0),
|
||||
new Rotation3d(0.0, Units.degreesToRadians(45.0), 0.0)),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, -1.0, -2.0),
|
||||
new Rotation3d(Units.degreesToRadians(-90.0), 0.0, Units.degreesToRadians(-135.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NWU());
|
||||
|
||||
// 45° yaw from EDN to NWU
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(
|
||||
new Translation3d(1.0, 2.0, 3.0),
|
||||
new Rotation3d(0.0, 0.0, Units.degreesToRadians(45.0))),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, -1.0, -2.0),
|
||||
new Rotation3d(
|
||||
Units.degreesToRadians(-90.0),
|
||||
Units.degreesToRadians(45.0),
|
||||
Units.degreesToRadians(-90.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NWU());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransform3dEDNtoNED() {
|
||||
// No rotation from EDN to NED
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(new Translation3d(1.0, 2.0, 3.0), new Rotation3d()),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, 1.0, 2.0),
|
||||
new Rotation3d(Units.degreesToRadians(90.0), 0.0, Units.degreesToRadians(90.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NED());
|
||||
|
||||
// 45° roll from EDN to NED
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(
|
||||
new Translation3d(1.0, 2.0, 3.0),
|
||||
new Rotation3d(Units.degreesToRadians(45.0), 0.0, 0.0)),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, 1.0, 2.0),
|
||||
new Rotation3d(Units.degreesToRadians(135.0), 0.0, Units.degreesToRadians(90.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NED());
|
||||
|
||||
// 45° pitch from EDN to NED
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(
|
||||
new Translation3d(1.0, 2.0, 3.0),
|
||||
new Rotation3d(0.0, Units.degreesToRadians(45.0), 0.0)),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, 1.0, 2.0),
|
||||
new Rotation3d(Units.degreesToRadians(90.0), 0.0, Units.degreesToRadians(135.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NED());
|
||||
|
||||
// 45° yaw from EDN to NED
|
||||
checkTransform3dConvert(
|
||||
new Transform3d(
|
||||
new Translation3d(1.0, 2.0, 3.0),
|
||||
new Rotation3d(0.0, 0.0, Units.degreesToRadians(45.0))),
|
||||
new Transform3d(
|
||||
new Translation3d(3.0, 1.0, 2.0),
|
||||
new Rotation3d(
|
||||
Units.degreesToRadians(90.0),
|
||||
Units.degreesToRadians(-45.0),
|
||||
Units.degreesToRadians(90.0))),
|
||||
CoordinateSystem.EDN(),
|
||||
CoordinateSystem.NED());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user