Skip to content

Commit

Permalink
Update quaternion calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrica-equinor committed Aug 10, 2023
1 parent f7806ca commit b0512e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
56 changes: 53 additions & 3 deletions backend/api.test/Database/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Api.Test.Services
public class TestPose
{
[Fact]
public void TestRotation()
public void TestRotationNorth()
{
var mockAngleAxisParameters = new EchoVector(0, 0, 1);
float mockAngle = MathF.PI / 2;
float mockAngle = 0;

var expected = new Orientation()
{
Expand All @@ -22,7 +22,51 @@ public void TestRotation()
};

Assert.Equal(
expected.X,
expected.Z,
new Pose(mockAngleAxisParameters, mockAngle).Orientation.Z,
3.0
);
}

[Fact]
public void TestRotationSouth()
{
var mockAngleAxisParameters = new EchoVector(0, 0, 1);
float mockAngle = MathF.PI;

var expected = new Orientation()
{
X = 0,
Y = 0,
Z = -0.7071F,
W = 0.7071F
};

var pose = new Pose(mockAngleAxisParameters, mockAngle);

Assert.Equal(
expected.Z,
pose.Orientation.Z,
3.0
);
}

[Fact]
public void TestNegativaRotation()
{
var mockAngleAxisParameters = new EchoVector(0, 0, 1);
float mockAngle = -180F * MathF.PI / 180F;

var expected = new Orientation()
{
X = 0,
Y = 0,
Z = 1F,
W = 0
};

Assert.Equal(
expected.Z,
new Pose(mockAngleAxisParameters, mockAngle).Orientation.Z,
3.0
);
Expand Down Expand Up @@ -64,6 +108,12 @@ private static AxisAngle ConvertOrientation(Orientation orientation)
if (orientation.Z >= 0)
angle = 2 * MathF.Acos(qw);

angle = (450 * MathF.PI / 180) - angle;

angle %= 2F * MathF.PI;

if (angle < 0) angle += 2F * MathF.PI;

return new AxisAngle(new EchoVector(0, 0, 1), angle);
}

Expand Down
16 changes: 13 additions & 3 deletions backend/api/Database/Models/Pose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,25 @@ public class Pose

// Since this is a ground robot the only quaternion vector
// that makes sense is up (0, 0, 1)
public Orientation AxisAngleToQuaternion(float angle)
// Echo representes North at 0deg and increases this value clockwise
// Our representation has East at 0deg with rotations anti-clockwise
public Orientation AxisAngleToQuaternion(float echoAngle)
{
float angle;
echoAngle %= 2F * MathF.PI;

if (echoAngle < 0) echoAngle += 2F * MathF.PI;

angle = (450 * MathF.PI / 180) - echoAngle;

var quaternion = new Orientation()
{
X = 0,
Y = 0,
Z = MathF.Sin(angle / 2),
W = MathF.Cos(angle / 2)
};

return quaternion;
}

Expand All @@ -125,10 +135,10 @@ float w
Orientation = new Orientation(x_ori, y_ori, z_ori, w);
}

public Pose(EchoVector enuPosition, float angle)
public Pose(EchoVector enuPosition, float echoAngle)
{
Position = new Position(enuPosition.East, enuPosition.North, enuPosition.Up);
Orientation = AxisAngleToQuaternion(angle);
Orientation = AxisAngleToQuaternion(echoAngle);
}

public Pose(Position position, Orientation orientation)
Expand Down

0 comments on commit b0512e8

Please sign in to comment.