2012-07-03 20:19:36 -03:00
|
|
|
//
|
|
|
|
// Unit tests for the AP_Math polygon code
|
|
|
|
//
|
|
|
|
|
2018-05-03 22:40:36 -03:00
|
|
|
#define ALLOW_DOUBLE_MATH_FUNCTIONS
|
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2019-01-02 06:55:02 -04:00
|
|
|
#include <AP_Common/Location.h>
|
2013-07-29 08:21:41 -03: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-07-03 20:19:36 -03:00
|
|
|
|
|
|
|
static const struct {
|
2012-08-17 03:20:14 -03:00
|
|
|
Vector2f wp1, wp2, location;
|
|
|
|
bool passed;
|
2012-07-03 20:19:36 -03:00
|
|
|
} test_points[] = {
|
2015-04-24 00:50:50 -03:00
|
|
|
{ Vector2f(-35.3647759314918f, 149.16265692810987f),
|
|
|
|
Vector2f(-35.36279922658029f, 149.16352169591426f),
|
|
|
|
Vector2f(-35.36214956969903f, 149.16461410046492f), true },
|
|
|
|
{ Vector2f(-35.36438601157189f, 149.16613916088568f),
|
|
|
|
Vector2f(-35.364432558610254f, 149.16287313113048f),
|
|
|
|
Vector2f(-35.36491510034746f, 149.16365837225004f), false },
|
2017-04-13 08:31:52 -03:00
|
|
|
{ Vector2f(0.0f, 0.0f),
|
|
|
|
Vector2f(0.0f, 1.0f),
|
|
|
|
Vector2f(0.0f, 2.0f), true },
|
|
|
|
{ Vector2f(0.0f, 0.0f),
|
|
|
|
Vector2f(0.0f, 2.0f),
|
|
|
|
Vector2f(0.0f, 1.0f), false },
|
|
|
|
{ Vector2f(0.0f, 0.0f),
|
|
|
|
Vector2f(1.0f, 0.0f),
|
|
|
|
Vector2f(2.0f, 0.0f), true },
|
|
|
|
{ Vector2f(0.0f, 0.0f),
|
|
|
|
Vector2f(2.0f, 0.0f),
|
|
|
|
Vector2f(1.0f, 0.0f), false },
|
|
|
|
{ Vector2f(0.0f, 0.0f),
|
|
|
|
Vector2f(-1.0f, 1.0f),
|
|
|
|
Vector2f(-2.0f, 2.0f), true },
|
2012-07-03 20:19:36 -03:00
|
|
|
};
|
|
|
|
|
2023-02-02 18:58:39 -04:00
|
|
|
static Location location_from_point(Vector2f pt)
|
2012-07-03 20:19:36 -03:00
|
|
|
{
|
2023-02-02 18:58:39 -04:00
|
|
|
Location loc = {};
|
2015-05-02 06:10:44 -03:00
|
|
|
loc.lat = pt.x * 1.0e7f;
|
|
|
|
loc.lng = pt.y * 1.0e7f;
|
2012-08-17 03:20:14 -03:00
|
|
|
return loc;
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_passed_waypoint(void)
|
|
|
|
{
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("waypoint tests starting\n");
|
2017-04-13 08:31:52 -03:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(test_points); i++) {
|
2023-02-02 18:58:39 -04:00
|
|
|
Location loc = location_from_point(test_points[i].location);
|
|
|
|
Location wp1 = location_from_point(test_points[i].wp1);
|
|
|
|
Location wp2 = location_from_point(test_points[i].wp2);
|
2019-04-12 05:23:03 -03:00
|
|
|
if (loc.past_interval_finish_line(wp1, wp2) != test_points[i].passed) {
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->printf("Failed waypoint test %u\n", (unsigned)i);
|
2012-08-17 03:20:14 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-21 00:42:36 -04:00
|
|
|
hal.console->printf("waypoint tests OK\n");
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2023-02-02 18:58:39 -04:00
|
|
|
static void test_one_offset(const Location &loc,
|
2012-08-10 22:56:54 -03:00
|
|
|
float ofs_north, float ofs_east,
|
|
|
|
float dist, float bearing)
|
|
|
|
{
|
2023-02-02 18:58:39 -04:00
|
|
|
Location loc2;
|
2012-08-10 22:56:54 -03:00
|
|
|
float dist2, bearing2;
|
|
|
|
|
|
|
|
loc2 = loc;
|
2015-11-19 23:12:15 -04:00
|
|
|
uint32_t t1 = AP_HAL::micros();
|
2019-02-12 12:38:10 -04:00
|
|
|
loc2.offset(ofs_north, ofs_east);
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->printf("location_offset took %u usec\n",
|
2015-11-19 23:12:15 -04:00
|
|
|
(unsigned)(AP_HAL::micros() - t1));
|
2019-02-12 12:38:10 -04:00
|
|
|
dist2 = loc.get_distance(loc2);
|
2019-04-05 10:02:43 -03:00
|
|
|
bearing2 = loc.get_bearing_to(loc2) * 0.01f;
|
2012-08-10 22:56:54 -03:00
|
|
|
float brg_error = bearing2-bearing;
|
|
|
|
if (brg_error > 180) {
|
|
|
|
brg_error -= 360;
|
|
|
|
} else if (brg_error < -180) {
|
|
|
|
brg_error += 360;
|
|
|
|
}
|
|
|
|
|
2015-04-24 00:50:50 -03:00
|
|
|
if (fabsf(dist - dist2) > 1.0f ||
|
|
|
|
brg_error > 1.0f) {
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->printf("Failed offset test brg_error=%f dist_error=%f\n",
|
2017-04-13 08:31:52 -03:00
|
|
|
(double)brg_error, (double)(dist - dist2));
|
2012-08-10 22:56:54 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
float ofs_north, ofs_east, distance, bearing;
|
|
|
|
} test_offsets[] = {
|
2017-04-13 08:31:52 -03:00
|
|
|
{ 1000.0f, 1000.0f, sqrtf(2.0f) * 1000.0f, 45.0f },
|
|
|
|
{ 1000.0f, -1000.0f, sqrtf(2.0f) * 1000.0f, -45.0f },
|
|
|
|
{ 1000.0f, 0.0f, 1000.0f, 0.0f },
|
|
|
|
{ 0.0f, 1000.0f, 1000.0f, 90.0f },
|
2012-08-10 22:56:54 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void test_offset(void)
|
|
|
|
{
|
2023-02-02 18:58:39 -04:00
|
|
|
Location loc {};
|
2012-08-10 22:56:54 -03:00
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
loc.lat = -35 * 1.0e7f;
|
|
|
|
loc.lng = 149 * 1.0e7f;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(test_offsets); i++) {
|
2012-08-17 03:20:14 -03:00
|
|
|
test_one_offset(loc,
|
|
|
|
test_offsets[i].ofs_north,
|
|
|
|
test_offsets[i].ofs_east,
|
2012-08-10 22:56:54 -03:00
|
|
|
test_offsets[i].distance,
|
|
|
|
test_offsets[i].bearing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 08:21:41 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
test position accuracy for floating point versus integer positions
|
|
|
|
*/
|
|
|
|
static void test_accuracy(void)
|
|
|
|
{
|
2023-02-02 18:58:39 -04:00
|
|
|
Location loc {};
|
2013-07-29 08:21:41 -03:00
|
|
|
|
|
|
|
loc.lat = 0.0e7f;
|
|
|
|
loc.lng = -120.0e7f;
|
|
|
|
|
2023-02-02 18:58:39 -04:00
|
|
|
Location loc2 = loc;
|
2017-04-13 08:31:52 -03:00
|
|
|
Vector2f v((loc.lat * 1.0e-7f), (loc.lng* 1.0e-7f));
|
2013-07-29 08:21:41 -03:00
|
|
|
Vector2f v2;
|
|
|
|
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lat += 10000000;
|
2017-04-13 08:31:52 -03:00
|
|
|
v2 = Vector2f(loc2.lat * 1.0e-7f, loc2.lng * 1.0e-7f);
|
2019-02-12 12:38:10 -04:00
|
|
|
hal.console->printf("1 degree lat dist=%.4f\n", (double)loc.get_distance(loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lng += 10000000;
|
2017-04-13 08:31:52 -03:00
|
|
|
v2 = Vector2f(loc2.lat * 1.0e-7f, loc2.lng * 1.0e-7f);
|
2019-02-12 12:38:10 -04:00
|
|
|
hal.console->printf("1 degree lng dist=%.4f\n", (double)loc.get_distance(loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
for (int32_t i = 0; i < 100; i++) {
|
2013-07-29 08:21:41 -03:00
|
|
|
loc2 = loc;
|
|
|
|
loc2.lat += i;
|
2017-04-13 08:31:52 -03:00
|
|
|
v2 = Vector2f((loc.lat + i) * 1.0e-7f, loc.lng * 1.0e-7f);
|
|
|
|
if (v2 != v) {
|
2019-02-12 12:38:10 -04:00
|
|
|
hal.console->printf("lat v2 != v at i=%d dist=%.4f\n", (int)i, (double)loc.get_distance(loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 08:31:52 -03:00
|
|
|
for (int32_t i = 0; i < 100; i++) {
|
2013-07-29 08:21:41 -03:00
|
|
|
loc2 = loc;
|
|
|
|
loc2.lng += i;
|
2017-04-13 08:31:52 -03:00
|
|
|
v2 = Vector2f(loc.lat * 1.0e-7f, (loc.lng + i) * 1.0e-7f);
|
|
|
|
if (v2 != v) {
|
2019-02-12 12:38:10 -04:00
|
|
|
hal.console->printf("lng v2 != v at i=%d dist=%.4f\n", (int)i, (double)loc.get_distance(loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
for (int32_t i = 0; i < 100; i++) {
|
2013-07-29 08:21:41 -03:00
|
|
|
loc2 = loc;
|
|
|
|
loc2.lat -= i;
|
2017-04-13 08:31:52 -03:00
|
|
|
v2 = Vector2f((loc.lat - i) * 1.0e-7f, loc.lng * 1.0e-7f);
|
|
|
|
if (v2 != v) {
|
2019-02-12 12:38:10 -04:00
|
|
|
hal.console->printf("-lat v2 != v at i=%d dist=%.4f\n", (int)i, (double)loc.get_distance(loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 08:31:52 -03:00
|
|
|
for (int32_t i = 0; i < 100; i++) {
|
2013-07-29 08:21:41 -03:00
|
|
|
loc2 = loc;
|
|
|
|
loc2.lng -= i;
|
2017-04-13 08:31:52 -03:00
|
|
|
v2 = Vector2f(loc.lat * 1.0e-7f, (loc.lng - i) * 1.0e-7f);
|
|
|
|
if (v2 != v) {
|
2019-02-12 12:38:10 -04:00
|
|
|
hal.console->printf("-lng v2 != v at i=%d dist=%.4f\n", (int)i, (double)loc.get_distance(loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 03:08:13 -03:00
|
|
|
static const struct {
|
|
|
|
int32_t v, wv;
|
|
|
|
} wrap_180_tests[] = {
|
|
|
|
{ 32000, -4000 },
|
|
|
|
{ 1500 + 100*36000, 1500 },
|
|
|
|
{ -1500 - 100*36000, -1500 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
int32_t v, wv;
|
|
|
|
} wrap_360_tests[] = {
|
|
|
|
{ 32000, 32000 },
|
|
|
|
{ 1500 + 100*36000, 1500 },
|
|
|
|
{ -1500 - 100*36000, 34500 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
float v, wv;
|
|
|
|
} wrap_PI_tests[] = {
|
2016-02-25 13:13:02 -04:00
|
|
|
{ 0.2f*M_PI, 0.2f*M_PI },
|
|
|
|
{ 0.2f*M_PI + 100*M_PI, 0.2f*M_PI },
|
|
|
|
{ -0.2f*M_PI - 100*M_PI, -0.2f*M_PI },
|
2013-10-02 03:08:13 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void test_wrap_cd(void)
|
|
|
|
{
|
2017-04-13 08:31:52 -03:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(wrap_180_tests); i++) {
|
2013-10-02 03:08:13 -03:00
|
|
|
int32_t r = wrap_180_cd(wrap_180_tests[i].v);
|
|
|
|
if (r != wrap_180_tests[i].wv) {
|
|
|
|
hal.console->printf("wrap_180: v=%ld wv=%ld r=%ld\n",
|
|
|
|
(long)wrap_180_tests[i].v,
|
|
|
|
(long)wrap_180_tests[i].wv,
|
|
|
|
(long)r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(wrap_360_tests); i++) {
|
2013-10-02 03:08:13 -03:00
|
|
|
int32_t r = wrap_360_cd(wrap_360_tests[i].v);
|
|
|
|
if (r != wrap_360_tests[i].wv) {
|
|
|
|
hal.console->printf("wrap_360: v=%ld wv=%ld r=%ld\n",
|
|
|
|
(long)wrap_360_tests[i].v,
|
|
|
|
(long)wrap_360_tests[i].wv,
|
|
|
|
(long)r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 08:31:52 -03:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(wrap_PI_tests); i++) {
|
2013-10-02 03:08:13 -03:00
|
|
|
float r = wrap_PI(wrap_PI_tests[i].v);
|
2015-05-08 15:44:28 -03:00
|
|
|
if (fabsf(r - wrap_PI_tests[i].wv) > 0.001f) {
|
2013-10-02 03:08:13 -03:00
|
|
|
hal.console->printf("wrap_PI: v=%f wv=%f r=%f\n",
|
2017-04-13 08:31:52 -03:00
|
|
|
(double)wrap_PI_tests[i].v,
|
|
|
|
(double)wrap_PI_tests[i].wv,
|
|
|
|
(double)r);
|
2013-10-02 03:08:13 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hal.console->printf("wrap_cd tests done\n");
|
|
|
|
}
|
2013-07-29 08:21:41 -03:00
|
|
|
|
2014-04-03 19:44:56 -03:00
|
|
|
static void test_wgs_conversion_functions(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#define D2R DEG_TO_RAD_DOUBLE
|
|
|
|
|
|
|
|
/* Maximum allowable error in quantities with units of length (in meters). */
|
2017-04-13 08:31:52 -03:00
|
|
|
static const double MAX_DIST_ERROR_M = 1e-6;
|
2014-04-03 19:44:56 -03:00
|
|
|
/* Maximum allowable error in quantities with units of angle (in sec of arc).
|
|
|
|
* 1 second of arc on the equator is ~31 meters. */
|
2017-04-13 08:31:52 -03:00
|
|
|
static const double MAX_ANGLE_ERROR_SEC = 1e-7;
|
|
|
|
static const double MAX_ANGLE_ERROR_RAD = (MAX_ANGLE_ERROR_SEC * (D2R / (double)3600.0));
|
2014-04-03 19:44:56 -03:00
|
|
|
|
|
|
|
/* Semi-major axis. */
|
2017-04-13 08:31:52 -03:00
|
|
|
static const double EARTH_A = 6378137.0;
|
2014-04-03 19:44:56 -03:00
|
|
|
/* Semi-minor axis. */
|
2017-04-13 08:31:52 -03:00
|
|
|
static const double EARTH_B = 6356752.31424517929553985595703125;
|
2014-04-03 19:44:56 -03:00
|
|
|
|
|
|
|
|
|
|
|
#define NUM_COORDS 10
|
|
|
|
Vector3d llhs[NUM_COORDS];
|
|
|
|
llhs[0] = Vector3d(0, 0, 0); /* On the Equator and Prime Meridian. */
|
|
|
|
llhs[1] = Vector3d(0, 180*D2R, 0); /* On the Equator. */
|
|
|
|
llhs[2] = Vector3d(0, 90*D2R, 0); /* On the Equator. */
|
|
|
|
llhs[3] = Vector3d(0, -90*D2R, 0); /* On the Equator. */
|
|
|
|
llhs[4] = Vector3d(90*D2R, 0, 0); /* North pole. */
|
|
|
|
llhs[5] = Vector3d(-90*D2R, 0, 0); /* South pole. */
|
|
|
|
llhs[6] = Vector3d(90*D2R, 0, 22); /* 22m above the north pole. */
|
|
|
|
llhs[7] = Vector3d(-90*D2R, 0, 22); /* 22m above the south pole. */
|
|
|
|
llhs[8] = Vector3d(0, 0, 22); /* 22m above the Equator and Prime Meridian. */
|
|
|
|
llhs[9] = Vector3d(0, 180*D2R, 22); /* 22m above the Equator. */
|
|
|
|
|
|
|
|
Vector3d ecefs[NUM_COORDS];
|
|
|
|
ecefs[0] = Vector3d(EARTH_A, 0, 0);
|
|
|
|
ecefs[1] = Vector3d(-EARTH_A, 0, 0);
|
|
|
|
ecefs[2] = Vector3d(0, EARTH_A, 0);
|
|
|
|
ecefs[3] = Vector3d(0, -EARTH_A, 0);
|
|
|
|
ecefs[4] = Vector3d(0, 0, EARTH_B);
|
|
|
|
ecefs[5] = Vector3d(0, 0, -EARTH_B);
|
|
|
|
ecefs[6] = Vector3d(0, 0, (EARTH_B+22));
|
|
|
|
ecefs[7] = Vector3d(0, 0, -(EARTH_B+22));
|
|
|
|
ecefs[8] = Vector3d((22+EARTH_A), 0, 0);
|
|
|
|
ecefs[9] = Vector3d(-(22+EARTH_A), 0, 0);
|
|
|
|
|
|
|
|
hal.console->printf("TESTING wgsllh2ecef\n");
|
|
|
|
for (int i = 0; i < NUM_COORDS; i++) {
|
|
|
|
|
|
|
|
Vector3d ecef;
|
|
|
|
wgsllh2ecef(llhs[i], ecef);
|
|
|
|
|
|
|
|
double x_err = fabs(ecef[0] - ecefs[i][0]);
|
|
|
|
double y_err = fabs(ecef[1] - ecefs[i][1]);
|
|
|
|
double z_err = fabs(ecef[2] - ecefs[i][2]);
|
|
|
|
if ((x_err < MAX_DIST_ERROR_M) &&
|
|
|
|
(y_err < MAX_DIST_ERROR_M) &&
|
|
|
|
(z_err < MAX_DIST_ERROR_M)) {
|
|
|
|
hal.console->printf("passing llh to ecef test %d\n", i);
|
|
|
|
} else {
|
|
|
|
hal.console->printf("failed llh to ecef test %d: ", i);
|
2017-04-13 08:31:52 -03:00
|
|
|
hal.console->printf("(%f - %f) (%f - %f) (%f - %f) => %.10f %.10f %.10f\n",
|
|
|
|
ecef[0], ecefs[i][0], ecef[1], ecefs[i][1], ecef[2], ecefs[i][2], x_err, y_err, z_err);
|
2014-04-03 19:44:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
hal.console->printf("TESTING wgsecef2llh\n");
|
|
|
|
for (int i = 0; i < NUM_COORDS; i++) {
|
|
|
|
|
|
|
|
Vector3d llh;
|
|
|
|
wgsecef2llh(ecefs[i], llh);
|
|
|
|
|
|
|
|
double lat_err = fabs(llh[0] - llhs[i][0]);
|
|
|
|
double lon_err = fabs(llh[1] - llhs[i][1]);
|
|
|
|
double hgt_err = fabs(llh[2] - llhs[i][2]);
|
|
|
|
if ((lat_err < MAX_ANGLE_ERROR_RAD) &&
|
|
|
|
(lon_err < MAX_ANGLE_ERROR_RAD) &&
|
|
|
|
(hgt_err < MAX_DIST_ERROR_M)) {
|
|
|
|
hal.console->printf("passing exef to llh test %d\n", i);
|
|
|
|
} else {
|
|
|
|
hal.console->printf("failed ecef to llh test %d: ", i);
|
|
|
|
hal.console->printf("%.10f %.10f %.10f\n", lat_err, lon_err, hgt_err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 20:19:36 -03:00
|
|
|
/*
|
2012-08-17 03:20:14 -03:00
|
|
|
* polygon tests
|
2012-07-03 20:19:36 -03:00
|
|
|
*/
|
|
|
|
void setup(void)
|
|
|
|
{
|
|
|
|
test_passed_waypoint();
|
2012-08-10 22:56:54 -03:00
|
|
|
test_offset();
|
2013-07-29 08:21:41 -03:00
|
|
|
test_accuracy();
|
2013-10-02 03:08:13 -03:00
|
|
|
test_wrap_cd();
|
2014-04-03 19:44:56 -03:00
|
|
|
test_wgs_conversion_functions();
|
|
|
|
hal.console->printf("ALL TESTS DONE\n");
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2012-09-18 15:08:18 -03:00
|
|
|
void loop(void){}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|