2012-03-09 22:45:05 -04:00
|
|
|
//
|
|
|
|
// Unit tests for the AP_Math rotations code
|
|
|
|
//
|
2015-10-19 16:30:26 -03:00
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-03-09 22:45:05 -04:00
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
void setup();
|
|
|
|
void loop();
|
|
|
|
|
2015-10-16 17:22:11 -03:00
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
2012-03-09 22:45:05 -04:00
|
|
|
|
2013-01-13 02:26:48 -04:00
|
|
|
static void print_vector(Vector3f &v)
|
|
|
|
{
|
2014-11-22 01:10:00 -04:00
|
|
|
hal.console->printf("[%.4f %.4f %.4f]\n",
|
2017-04-13 08:31:52 -03:00
|
|
|
(double)v.x,
|
|
|
|
(double)v.y,
|
|
|
|
(double)v.z);
|
2013-01-13 02:26:48 -04:00
|
|
|
}
|
|
|
|
|
2012-11-04 01:05:37 -03:00
|
|
|
// test rotation method accuracy
|
|
|
|
static void test_rotation_accuracy(void)
|
|
|
|
{
|
|
|
|
Matrix3f attitude;
|
|
|
|
Vector3f small_rotation;
|
|
|
|
float roll, pitch, yaw;
|
|
|
|
float rot_angle;
|
|
|
|
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("\nRotation method accuracy:\n");
|
2012-11-04 01:05:37 -03:00
|
|
|
|
2016-04-21 20:54:59 -03:00
|
|
|
// test roll
|
2017-04-13 08:31:52 -03:00
|
|
|
for(int16_t i = 0; i < 90; i++ ) {
|
2016-04-21 20:54:59 -03:00
|
|
|
|
|
|
|
// reset initial attitude
|
2017-04-13 08:31:52 -03:00
|
|
|
attitude.from_euler(0.0f, 0.0f, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
|
|
|
|
// calculate small rotation vector
|
|
|
|
rot_angle = ToRad(i);
|
2017-04-13 08:31:52 -03:00
|
|
|
small_rotation = Vector3f(rot_angle, 0.0f, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
|
|
|
|
// apply small rotation
|
|
|
|
attitude.rotate(small_rotation);
|
|
|
|
|
|
|
|
// get resulting attitude's euler angles
|
|
|
|
attitude.to_euler(&roll, &pitch, &yaw);
|
|
|
|
|
|
|
|
// now try via from_axis_angle
|
|
|
|
Matrix3f r2;
|
2017-04-13 08:31:52 -03:00
|
|
|
r2.from_axis_angle(Vector3f(1.0f, 0.0f, 0.0f), rot_angle);
|
|
|
|
attitude.from_euler(0.0f, 0.0f, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
attitude = r2 * attitude;
|
|
|
|
|
|
|
|
float roll2, pitch2, yaw2;
|
|
|
|
attitude.to_euler(&roll2, &pitch2, &yaw2);
|
|
|
|
|
|
|
|
// display results
|
|
|
|
hal.console->printf("actual angle: %d angle1:%4.2f angle2:%4.2f\n",
|
2017-04-13 08:31:52 -03:00
|
|
|
(int)i,
|
|
|
|
(double)ToDeg(roll),
|
|
|
|
(double)ToDeg(roll2));
|
2016-04-21 20:54:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test pitch
|
2017-04-13 08:31:52 -03:00
|
|
|
for(int16_t i = 0; i < 90; i++ ) {
|
2016-04-21 20:54:59 -03:00
|
|
|
|
|
|
|
// reset initial attitude
|
2017-04-13 08:31:52 -03:00
|
|
|
attitude.from_euler(0.0f, 0.0f, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
|
|
|
|
// calculate small rotation vector
|
|
|
|
rot_angle = ToRad(i);
|
2017-04-13 08:31:52 -03:00
|
|
|
small_rotation = Vector3f(0.0f ,rot_angle, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
|
|
|
|
// apply small rotation
|
|
|
|
attitude.rotate(small_rotation);
|
|
|
|
|
|
|
|
// get resulting attitude's euler angles
|
|
|
|
attitude.to_euler(&roll, &pitch, &yaw);
|
|
|
|
|
|
|
|
// now try via from_axis_angle
|
|
|
|
Matrix3f r2;
|
2017-04-13 08:31:52 -03:00
|
|
|
r2.from_axis_angle(Vector3f(0.0f ,1.0f, 0.0f), rot_angle);
|
|
|
|
attitude.from_euler(0.0f, 0.0f, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
attitude = r2 * attitude;
|
|
|
|
|
|
|
|
float roll2, pitch2, yaw2;
|
|
|
|
attitude.to_euler(&roll2, &pitch2, &yaw2);
|
|
|
|
|
|
|
|
// display results
|
|
|
|
hal.console->printf("actual angle: %d angle1:%4.2f angle2:%4.2f\n",
|
2017-04-13 08:31:52 -03:00
|
|
|
(int)i,
|
|
|
|
(double)ToDeg(pitch),
|
|
|
|
(double)ToDeg(pitch2));
|
2016-04-21 20:54:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// test yaw
|
2017-04-13 08:31:52 -03:00
|
|
|
for(int16_t i = 0; i < 90; i++ ) {
|
2012-11-04 01:05:37 -03:00
|
|
|
|
|
|
|
// reset initial attitude
|
2017-04-13 08:31:52 -03:00
|
|
|
attitude.from_euler(0.0f, 0.0f, 0.0f);
|
2012-11-04 01:05:37 -03:00
|
|
|
|
|
|
|
// calculate small rotation vector
|
|
|
|
rot_angle = ToRad(i);
|
2017-04-13 08:31:52 -03:00
|
|
|
small_rotation = Vector3f(0.0f, 0.0f, rot_angle);
|
2012-11-04 01:05:37 -03:00
|
|
|
|
|
|
|
// apply small rotation
|
|
|
|
attitude.rotate(small_rotation);
|
|
|
|
|
|
|
|
// get resulting attitude's euler angles
|
|
|
|
attitude.to_euler(&roll, &pitch, &yaw);
|
|
|
|
|
2016-04-21 20:54:59 -03:00
|
|
|
// now try via from_axis_angle
|
|
|
|
Matrix3f r2;
|
2017-04-13 08:31:52 -03:00
|
|
|
r2.from_axis_angle(Vector3f(0.0f, 0.0f, 1.0f), rot_angle);
|
|
|
|
attitude.from_euler(0.0f, 0.0f, 0.0f);
|
2016-04-21 20:54:59 -03:00
|
|
|
attitude = r2 * attitude;
|
|
|
|
|
|
|
|
float roll2, pitch2, yaw2;
|
|
|
|
attitude.to_euler(&roll2, &pitch2, &yaw2);
|
|
|
|
|
2012-11-04 01:05:37 -03:00
|
|
|
// display results
|
2016-04-21 20:54:59 -03:00
|
|
|
hal.console->printf("actual angle: %d angle1:%4.2f angle2:%4.2f\n",
|
2017-04-13 08:31:52 -03:00
|
|
|
(int)i,
|
|
|
|
(double)ToDeg(yaw),
|
|
|
|
(double)ToDeg(yaw2));
|
2012-11-04 01:05:37 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-13 02:26:48 -04:00
|
|
|
static void test_euler(enum Rotation rotation, float roll, float pitch, float yaw)
|
|
|
|
{
|
|
|
|
Vector3f v, v1, v2, diff;
|
|
|
|
Matrix3f rotmat;
|
2015-05-02 06:10:44 -03:00
|
|
|
const float accuracy = 1.0e-6f;
|
2013-01-13 02:26:48 -04:00
|
|
|
|
|
|
|
v.x = 1;
|
|
|
|
v.y = 2;
|
|
|
|
v.z = 3;
|
|
|
|
v1 = v;
|
|
|
|
|
|
|
|
v1.rotate(rotation);
|
|
|
|
|
|
|
|
rotmat.from_euler(radians(roll), radians(pitch), radians(yaw));
|
|
|
|
v2 = v;
|
|
|
|
v2 = rotmat * v2;
|
|
|
|
|
|
|
|
diff = (v2 - v1);
|
|
|
|
if (diff.length() > accuracy) {
|
2014-11-22 01:10:00 -04:00
|
|
|
hal.console->printf("euler test %u failed : yaw:%d roll:%d pitch:%d\n",
|
|
|
|
(unsigned)rotation,
|
|
|
|
(int)yaw,
|
|
|
|
(int)roll,
|
|
|
|
(int)pitch);
|
|
|
|
hal.console->printf("fast rotated: ");
|
2013-01-13 02:26:48 -04:00
|
|
|
print_vector(v1);
|
2014-11-22 01:10:00 -04:00
|
|
|
hal.console->printf("slow rotated: ");
|
2013-01-13 02:26:48 -04:00
|
|
|
print_vector(v2);
|
2014-11-22 01:10:00 -04:00
|
|
|
hal.console->printf("\n");
|
2013-01-13 02:26:48 -04:00
|
|
|
}
|
2020-04-07 02:53:05 -03:00
|
|
|
|
|
|
|
// quaternion rotation test
|
2020-04-07 23:39:12 -03:00
|
|
|
const float q_accuracy = 1.0e-3f;
|
|
|
|
Quaternion q, qe;
|
2020-04-07 02:53:05 -03:00
|
|
|
q.from_rotation(rotation);
|
2020-04-07 23:39:12 -03:00
|
|
|
qe.from_euler(radians(roll), radians(pitch), radians(yaw));
|
|
|
|
float q_roll, q_pitch, q_yaw, qe_roll, qe_pitch, qe_yaw;
|
2020-04-07 02:53:05 -03:00
|
|
|
q.to_euler(q_roll, q_pitch, q_yaw);
|
2020-04-07 23:39:12 -03:00
|
|
|
qe.to_euler(qe_roll, qe_pitch, qe_yaw);
|
|
|
|
const float roll_diff = fabsf(wrap_PI(q_roll - qe_roll));
|
|
|
|
const float pitch_diff = fabsf(wrap_PI(q_pitch - qe_pitch));
|
|
|
|
const float yaw_diff = fabsf(wrap_PI(q_yaw - qe_yaw));
|
|
|
|
if ((roll_diff > q_accuracy) || (pitch_diff > q_accuracy) || (yaw_diff > q_accuracy)) {
|
|
|
|
hal.console->printf("quaternion test %u failed : yaw:%f/%f roll:%f/%f pitch:%f/%f\n",
|
2020-04-07 02:53:05 -03:00
|
|
|
(unsigned)rotation,
|
2020-04-07 23:39:12 -03:00
|
|
|
(double)q_yaw,(double)qe_yaw,
|
|
|
|
(double)q_roll,(double)qe_roll,
|
|
|
|
(double)q_pitch,(double)qe_pitch);
|
2020-04-07 02:53:05 -03:00
|
|
|
}
|
2013-01-13 02:26:48 -04:00
|
|
|
}
|
|
|
|
|
2015-10-26 21:17:00 -03:00
|
|
|
static void test_rotate_inverse(void)
|
|
|
|
{
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("\nrotate inverse test(Vector (1,1,1)):\n");
|
2017-04-13 08:31:52 -03:00
|
|
|
Vector3f vec(1.0f,1.0f,1.0f), cmp_vec(1.0f, 1.0f, 1.0f);
|
|
|
|
for (enum Rotation r = ROTATION_NONE;
|
|
|
|
r < ROTATION_MAX;
|
2015-10-26 21:17:00 -03:00
|
|
|
r = (enum Rotation)((uint8_t)r+1)) {
|
2017-04-13 08:31:52 -03:00
|
|
|
hal.console->printf("\nROTATION(%d) ", r);
|
2015-10-26 21:17:00 -03:00
|
|
|
vec.rotate(r);
|
|
|
|
print_vector(vec);
|
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
hal.console->printf("INV_ROTATION(%d)", r);
|
2015-10-26 21:17:00 -03:00
|
|
|
vec.rotate_inverse(r);
|
|
|
|
print_vector(vec);
|
2017-04-13 08:31:52 -03:00
|
|
|
if ((vec - cmp_vec).length() > 1e-5) {
|
|
|
|
hal.console->printf("Rotation Test Failed!!! %.8f\n", (double)(vec - cmp_vec).length());
|
2015-10-26 21:17:00 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 02:26:48 -04:00
|
|
|
static void test_eulers(void)
|
|
|
|
{
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("euler tests\n");
|
2013-01-13 02:26:48 -04:00
|
|
|
test_euler(ROTATION_NONE, 0, 0, 0);
|
|
|
|
test_euler(ROTATION_YAW_45, 0, 0, 45);
|
|
|
|
test_euler(ROTATION_YAW_90, 0, 0, 90);
|
|
|
|
test_euler(ROTATION_YAW_135, 0, 0, 135);
|
|
|
|
test_euler(ROTATION_YAW_180, 0, 0, 180);
|
|
|
|
test_euler(ROTATION_YAW_225, 0, 0, 225);
|
|
|
|
test_euler(ROTATION_YAW_270, 0, 0, 270);
|
|
|
|
test_euler(ROTATION_YAW_315, 0, 0, 315);
|
|
|
|
test_euler(ROTATION_ROLL_180, 180, 0, 0);
|
|
|
|
test_euler(ROTATION_ROLL_180_YAW_45, 180, 0, 45);
|
|
|
|
test_euler(ROTATION_ROLL_180_YAW_90, 180, 0, 90);
|
|
|
|
test_euler(ROTATION_ROLL_180_YAW_135, 180, 0, 135);
|
|
|
|
test_euler(ROTATION_PITCH_180, 0, 180, 0);
|
|
|
|
test_euler(ROTATION_ROLL_180_YAW_225, 180, 0, 225);
|
|
|
|
test_euler(ROTATION_ROLL_180_YAW_270, 180, 0, 270);
|
|
|
|
test_euler(ROTATION_ROLL_180_YAW_315, 180, 0, 315);
|
|
|
|
test_euler(ROTATION_ROLL_90, 90, 0, 0);
|
|
|
|
test_euler(ROTATION_ROLL_90_YAW_45, 90, 0, 45);
|
|
|
|
test_euler(ROTATION_ROLL_90_YAW_90, 90, 0, 90);
|
|
|
|
test_euler(ROTATION_ROLL_90_YAW_135, 90, 0, 135);
|
|
|
|
test_euler(ROTATION_ROLL_270, 270, 0, 0);
|
|
|
|
test_euler(ROTATION_ROLL_270_YAW_45, 270, 0, 45);
|
|
|
|
test_euler(ROTATION_ROLL_270_YAW_90, 270, 0, 90);
|
|
|
|
test_euler(ROTATION_ROLL_270_YAW_135, 270, 0, 135);
|
|
|
|
test_euler(ROTATION_PITCH_90, 0, 90, 0);
|
2017-04-13 08:31:52 -03:00
|
|
|
test_euler(ROTATION_PITCH_270, 0, 270, 0);
|
|
|
|
test_euler(ROTATION_PITCH_180_YAW_90, 0, 180, 90);
|
|
|
|
test_euler(ROTATION_PITCH_180_YAW_270, 0, 180, 270);
|
|
|
|
test_euler(ROTATION_ROLL_90_PITCH_90, 90, 90, 0);
|
|
|
|
test_euler(ROTATION_ROLL_180_PITCH_90,180, 90, 0);
|
|
|
|
test_euler(ROTATION_ROLL_270_PITCH_90,270, 90, 0);
|
|
|
|
test_euler(ROTATION_ROLL_90_PITCH_180, 90, 180, 0);
|
|
|
|
test_euler(ROTATION_ROLL_270_PITCH_180,270,180, 0);
|
|
|
|
test_euler(ROTATION_ROLL_90_PITCH_270, 90, 270, 0);
|
|
|
|
test_euler(ROTATION_ROLL_180_PITCH_270,180,270, 0);
|
|
|
|
test_euler(ROTATION_ROLL_270_PITCH_270,270,270, 0);
|
|
|
|
test_euler(ROTATION_ROLL_90_PITCH_180_YAW_90, 90, 180, 90);
|
2014-11-22 01:10:00 -04:00
|
|
|
test_euler(ROTATION_ROLL_90_YAW_270, 90, 0, 270);
|
2015-12-28 14:23:18 -04:00
|
|
|
test_euler(ROTATION_ROLL_90_PITCH_68_YAW_293,90,68.8,293.3);
|
2019-04-09 07:03:36 -03:00
|
|
|
test_euler(ROTATION_PITCH_7, 0, 7, 0);
|
2013-08-26 10:02:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool have_rotation(const Matrix3f &m)
|
|
|
|
{
|
|
|
|
Matrix3f mt = m.transposed();
|
2017-04-13 08:31:52 -03:00
|
|
|
for (enum Rotation r = ROTATION_NONE;
|
|
|
|
r < ROTATION_MAX;
|
|
|
|
r = (enum Rotation)((uint8_t)(r + 1))) {
|
|
|
|
Vector3f v(1.0f, 2.0f, 3.0f);
|
2013-08-26 10:02:48 -03:00
|
|
|
Vector3f v2 = v;
|
|
|
|
v2.rotate(r);
|
|
|
|
v2 = mt * v2;
|
|
|
|
if ((v2 - v).length() < 0.01f) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void missing_rotations(void)
|
|
|
|
{
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("testing for missing rotations\n");
|
2017-04-13 08:31:52 -03:00
|
|
|
for (uint16_t yaw = 0; yaw < 360; yaw += 90)
|
|
|
|
for (uint16_t pitch = 0; pitch < 360; pitch += 90)
|
|
|
|
for (uint16_t roll = 0; roll < 360; roll += 90) {
|
2013-08-26 10:02:48 -03:00
|
|
|
Matrix3f m;
|
|
|
|
m.from_euler(ToRad(roll), ToRad(pitch), ToRad(yaw));
|
|
|
|
if (!have_rotation(m)) {
|
|
|
|
hal.console->printf("Missing rotation (%u, %u, %u)\n", roll, pitch, yaw);
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 02:26:48 -04:00
|
|
|
}
|
|
|
|
|
2017-11-22 23:19:29 -04:00
|
|
|
static void test_rotate_matrix(void)
|
|
|
|
{
|
|
|
|
for (enum Rotation r = ROTATION_NONE;
|
|
|
|
r < ROTATION_MAX;
|
|
|
|
r = (enum Rotation)((uint8_t)r+1)) {
|
|
|
|
//hal.console->printf("\nROTATION(%d)\n", r);
|
|
|
|
Vector3f vec(1,2,3);
|
|
|
|
Vector3f vec2 = vec;
|
|
|
|
vec.rotate(r);
|
|
|
|
Matrix3f m;
|
|
|
|
m.from_rotation(r);
|
|
|
|
vec2 = m * vec2;
|
|
|
|
//print_vector(vec);
|
|
|
|
//print_vector(vec2);
|
|
|
|
if ((vec - vec2).length() > 1e-5) {
|
|
|
|
hal.console->printf("Rotation Test Failed!!! %.8f\n", (double)(vec - vec2).length());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hal.console->printf("test_rotate_matrix passed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-09 22:45:05 -04:00
|
|
|
/*
|
2012-08-17 03:20:14 -03:00
|
|
|
* rotation tests
|
2012-03-09 22:45:05 -04:00
|
|
|
*/
|
|
|
|
void setup(void)
|
|
|
|
{
|
2017-11-22 23:19:29 -04:00
|
|
|
hal.console->begin(115200);
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("rotation unit tests\n\n");
|
2012-11-04 01:05:37 -03:00
|
|
|
test_rotation_accuracy();
|
2013-01-13 02:26:48 -04:00
|
|
|
test_eulers();
|
2013-08-26 10:02:48 -03:00
|
|
|
missing_rotations();
|
2015-10-26 21:17:00 -03:00
|
|
|
test_rotate_inverse();
|
2017-11-22 23:19:29 -04:00
|
|
|
test_rotate_matrix();
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("rotation unit tests done\n\n");
|
2012-03-09 22:45:05 -04:00
|
|
|
}
|
|
|
|
|
2012-09-18 15:08:18 -03:00
|
|
|
void loop(void) {}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|