2015-10-26 21:51:43 -03:00
|
|
|
//
|
|
|
|
// Unit tests for the AP_Math rotations code
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <stdio.h>
|
2021-01-17 20:39:26 -04:00
|
|
|
|
|
|
|
void setup();
|
|
|
|
void loop();
|
|
|
|
|
2015-10-26 21:51:43 -03:00
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
|
|
|
|
2015-12-28 19:19:05 -04:00
|
|
|
#define MAT_ALG_ACCURACY 1e-4f
|
|
|
|
|
2021-01-17 20:39:26 -04:00
|
|
|
typedef float ftype;
|
|
|
|
|
2015-10-26 21:51:43 -03:00
|
|
|
static uint16_t get_random(void)
|
|
|
|
{
|
|
|
|
static uint32_t m_z = 1234;
|
|
|
|
static uint32_t m_w = 76542;
|
|
|
|
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
|
|
|
|
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
|
|
|
|
return ((m_z << 16) + m_w) & 0xF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-17 20:39:26 -04:00
|
|
|
static void show_matrix(ftype *A, int n) {
|
2015-10-26 21:51:43 -03:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < n; j++)
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("%.10f ", A[i * n + j]);
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 20:39:26 -04:00
|
|
|
static bool compare_mat(const ftype *A, const ftype *B, const uint8_t n)
|
2015-10-26 21:51:43 -03:00
|
|
|
{
|
|
|
|
for(uint8_t i = 0; i < n; i++) {
|
|
|
|
for(uint8_t j = 0; j < n; j++) {
|
2015-12-28 19:19:05 -04:00
|
|
|
if(fabsf(A[i*n + j] - B[i*n + j]) > MAT_ALG_ACCURACY) {
|
2015-10-26 21:51:43 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_matrix_inverse(void)
|
|
|
|
{
|
|
|
|
//fast inverses
|
2021-01-17 20:39:26 -04:00
|
|
|
ftype test_mat[25],ident_mat[25];
|
|
|
|
ftype out_mat[25], out_mat2[25], mat[25];
|
2015-10-26 21:51:43 -03:00
|
|
|
for(uint8_t i = 0;i<25;i++) {
|
2021-01-17 20:39:26 -04:00
|
|
|
test_mat[i] = powf(-1,i)*get_random()/0.7f;
|
2015-10-26 21:51:43 -03:00
|
|
|
}
|
2015-12-28 19:19:05 -04:00
|
|
|
|
2015-10-26 21:51:43 -03:00
|
|
|
|
|
|
|
//Test for 3x3 matrix
|
2021-01-17 20:39:26 -04:00
|
|
|
mat_identity(ident_mat, 3);
|
|
|
|
if (mat_inverse(test_mat,mat,3) && mat_inverse(mat, out_mat2, 3)) {
|
|
|
|
mat_mul(test_mat, mat, out_mat, 3);
|
2015-10-26 21:51:43 -03:00
|
|
|
} else {
|
|
|
|
hal.console->printf("3x3 Matrix is Singular!\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
printf("\n\n3x3 Test Matrix:\n");
|
|
|
|
show_matrix(test_mat,3);
|
|
|
|
printf("\nInverse of Inverse of matrix\n");
|
|
|
|
show_matrix(mat,3);
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("\nInv(A) * A\n");
|
|
|
|
show_matrix(out_mat,3);
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("\n");
|
2021-01-17 20:39:26 -04:00
|
|
|
|
|
|
|
// compare matrix
|
|
|
|
if (!compare_mat(test_mat, out_mat2, 3)) {
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("Test Failed!!\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-01-17 20:39:26 -04:00
|
|
|
if (!compare_mat(ident_mat, out_mat, 3)) {
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("Identity output Test Failed!!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-26 21:51:43 -03:00
|
|
|
|
|
|
|
//Test for 4x4 matrix
|
2021-01-17 20:39:26 -04:00
|
|
|
mat_identity(ident_mat, 4);
|
|
|
|
if (mat_inverse(test_mat, mat, 4) && mat_inverse(mat, out_mat2, 4)){
|
|
|
|
mat_mul(test_mat, mat, out_mat, 4);
|
2015-10-26 21:51:43 -03:00
|
|
|
} else {
|
2015-12-28 19:19:05 -04:00
|
|
|
hal.console->printf("4x4 Matrix is Singular!\n");
|
2015-10-26 21:51:43 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("\n\n4x4 Test Matrix:\n");
|
|
|
|
show_matrix(test_mat,4);
|
|
|
|
printf("\nInverse of Inverse of matrix\n");
|
|
|
|
show_matrix(mat,4);
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("\nInv(A) * A\n");
|
|
|
|
show_matrix(out_mat,4);
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("\n");
|
2021-01-17 20:39:26 -04:00
|
|
|
if (!compare_mat(test_mat, out_mat2, 4)) {
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("Test Failed!!\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-01-17 20:39:26 -04:00
|
|
|
if (!compare_mat(ident_mat,out_mat,4)) {
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("Identity output Test Failed!!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-26 21:51:43 -03:00
|
|
|
//Test for 5x5 matrix
|
2021-01-17 20:39:26 -04:00
|
|
|
mat_identity(ident_mat, 5);
|
|
|
|
if (mat_inverse(test_mat,mat,5) && mat_inverse(mat, out_mat2, 5)) {
|
|
|
|
mat_mul(test_mat, mat, out_mat, 5);
|
2015-10-26 21:51:43 -03:00
|
|
|
} else {
|
2015-12-28 19:19:05 -04:00
|
|
|
hal.console->printf("5x5 Matrix is Singular!\n");
|
2015-10-26 21:51:43 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n\n5x5 Test Matrix:\n");
|
|
|
|
show_matrix(test_mat,5);
|
|
|
|
printf("\nInverse of Inverse of matrix\n");
|
|
|
|
show_matrix(mat,5);
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("\nInv(A) * A\n");
|
|
|
|
show_matrix(out_mat,5);
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("\n");
|
2021-01-17 20:39:26 -04:00
|
|
|
if (!compare_mat(test_mat, out_mat2, 5)) {
|
2015-10-26 21:51:43 -03:00
|
|
|
printf("Test Failed!!\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-01-17 20:39:26 -04:00
|
|
|
if (!compare_mat(ident_mat, out_mat, 5)) {
|
2015-12-28 19:19:05 -04:00
|
|
|
printf("Identity output Test Failed!!\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-10-26 21:51:43 -03:00
|
|
|
|
|
|
|
hal.console->printf("All tests succeeded!!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setup(void)
|
|
|
|
{
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("Matrix Algebra test\n\n");
|
2015-10-26 21:51:43 -03:00
|
|
|
test_matrix_inverse();
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("Matrix Algebra tests done\n\n");
|
2015-10-26 21:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void) {}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|