2012-07-03 20:19:36 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
//
|
|
|
|
// Unit tests for the AP_Math polygon code
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <AP_Common.h>
|
2012-10-27 00:55:17 -03:00
|
|
|
#include <AP_Progmem.h>
|
2012-08-20 20:22:44 -03:00
|
|
|
#include <AP_Param.h>
|
2012-09-18 15:08:18 -03:00
|
|
|
#include <AP_HAL.h>
|
2012-07-03 20:19:36 -03:00
|
|
|
#include <AP_Math.h>
|
2013-07-29 08:21:41 -03:00
|
|
|
#include <Filter.h>
|
|
|
|
#include <AP_ADC.h>
|
2013-12-16 20:50:58 -04:00
|
|
|
#include <AP_Notify.h>
|
|
|
|
#include <AP_InertialSensor.h>
|
|
|
|
#include <AP_GPS.h>
|
|
|
|
#include <DataFlash.h>
|
2014-01-29 22:33:46 -04:00
|
|
|
#include <AP_Baro.h>
|
2013-07-29 08:21:41 -03:00
|
|
|
#include <GCS_MAVLink.h>
|
|
|
|
#include <AP_Declination.h>
|
2012-07-03 20:19:36 -03:00
|
|
|
|
2012-09-18 15:08:18 -03:00
|
|
|
#include <AP_HAL_AVR.h>
|
2013-07-29 08:21:41 -03:00
|
|
|
#include <AP_HAL_AVR_SITL.h>
|
|
|
|
#include <AP_HAL_Empty.h>
|
|
|
|
|
2012-12-18 21:26:58 -04:00
|
|
|
const AP_HAL::HAL& hal = AP_HAL_BOARD_DRIVER;
|
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[] = {
|
2012-08-17 03:20:14 -03:00
|
|
|
{ Vector2f(-35.3647759314918, 149.16265692810987),
|
|
|
|
Vector2f(-35.36279922658029, 149.16352169591426),
|
|
|
|
Vector2f(-35.36214956969903, 149.16461410046492), true },
|
|
|
|
{ Vector2f(-35.36438601157189, 149.16613916088568),
|
|
|
|
Vector2f(-35.364432558610254, 149.16287313113048),
|
|
|
|
Vector2f(-35.36491510034746, 149.16365837225004), false },
|
|
|
|
{ Vector2f(0, 0),
|
|
|
|
Vector2f(0, 1),
|
|
|
|
Vector2f(0, 2), true },
|
|
|
|
{ Vector2f(0, 0),
|
|
|
|
Vector2f(0, 2),
|
|
|
|
Vector2f(0, 1), false },
|
|
|
|
{ Vector2f(0, 0),
|
|
|
|
Vector2f(1, 0),
|
|
|
|
Vector2f(2, 0), true },
|
|
|
|
{ Vector2f(0, 0),
|
|
|
|
Vector2f(2, 0),
|
|
|
|
Vector2f(1, 0), false },
|
|
|
|
{ Vector2f(0, 0),
|
|
|
|
Vector2f(-1, 1),
|
|
|
|
Vector2f(-2, 2), true },
|
2012-07-03 20:19:36 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
#define ARRAY_LENGTH(x) (sizeof((x))/sizeof((x)[0]))
|
|
|
|
|
|
|
|
static struct Location location_from_point(Vector2f pt)
|
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
struct Location loc = {0};
|
|
|
|
loc.lat = pt.x * 1.0e7;
|
|
|
|
loc.lng = pt.y * 1.0e7;
|
|
|
|
return loc;
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_passed_waypoint(void)
|
|
|
|
{
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->println("waypoint tests starting");
|
2012-08-17 03:20:14 -03:00
|
|
|
for (uint8_t i=0; i<ARRAY_LENGTH(test_points); i++) {
|
|
|
|
struct Location loc = location_from_point(test_points[i].location);
|
|
|
|
struct Location wp1 = location_from_point(test_points[i].wp1);
|
|
|
|
struct Location wp2 = location_from_point(test_points[i].wp2);
|
|
|
|
if (location_passed_point(loc, 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;
|
|
|
|
}
|
|
|
|
}
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->println("waypoint tests OK");
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2013-03-26 08:58:54 -03:00
|
|
|
static void test_one_offset(const struct Location &loc,
|
2012-08-10 22:56:54 -03:00
|
|
|
float ofs_north, float ofs_east,
|
|
|
|
float dist, float bearing)
|
|
|
|
{
|
|
|
|
struct Location loc2;
|
|
|
|
float dist2, bearing2;
|
|
|
|
|
|
|
|
loc2 = loc;
|
2012-09-18 15:08:18 -03:00
|
|
|
uint32_t t1 = hal.scheduler->micros();
|
2013-08-04 23:37:51 -03:00
|
|
|
location_offset(loc2, ofs_north, ofs_east);
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->printf("location_offset took %u usec\n",
|
2012-12-18 20:18:00 -04:00
|
|
|
(unsigned)(hal.scheduler->micros() - t1));
|
2013-08-04 21:14:33 -03:00
|
|
|
dist2 = get_distance(loc, loc2);
|
2013-08-04 23:37:51 -03:00
|
|
|
bearing2 = get_bearing_cd(loc, loc2) * 0.01;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:42:24 -04:00
|
|
|
if (fabsf(dist - dist2) > 1.0 ||
|
2012-08-10 22:56:54 -03:00
|
|
|
brg_error > 1.0) {
|
2012-09-18 15:08:18 -03:00
|
|
|
hal.console->printf("Failed offset test brg_error=%f dist_error=%f\n",
|
2012-08-10 22:56:54 -03:00
|
|
|
brg_error, dist-dist2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
float ofs_north, ofs_east, distance, bearing;
|
|
|
|
} test_offsets[] = {
|
|
|
|
{ 1000, 1000, sqrt(2.0)*1000, 45 },
|
|
|
|
{ 1000, -1000, sqrt(2.0)*1000, -45 },
|
|
|
|
{ 1000, 0, 1000, 0 },
|
|
|
|
{ 0, 1000, 1000, 90 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_offset(void)
|
|
|
|
{
|
|
|
|
struct Location loc;
|
|
|
|
|
|
|
|
loc.lat = -35*1.0e7;
|
|
|
|
loc.lng = 149*1.0e7;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
for (uint8_t i=0; i<ARRAY_LENGTH(test_offsets); i++) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct Location loc;
|
|
|
|
|
|
|
|
loc.lat = 0.0e7f;
|
|
|
|
loc.lng = -120.0e7f;
|
|
|
|
|
|
|
|
struct Location loc2 = loc;
|
|
|
|
Vector2f v((loc.lat*1.0e-7f), (loc.lng*1.0e-7f));
|
|
|
|
Vector2f v2;
|
|
|
|
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lat += 10000000;
|
|
|
|
v2 = Vector2f(loc2.lat*1.0e-7f, loc2.lng*1.0e-7f);
|
2013-08-29 02:05:38 -03:00
|
|
|
hal.console->printf("1 degree lat dist=%.4f\n", get_distance(loc, loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lng += 10000000;
|
|
|
|
v2 = Vector2f(loc2.lat*1.0e-7f, loc2.lng*1.0e-7f);
|
2013-08-29 02:05:38 -03:00
|
|
|
hal.console->printf("1 degree lng dist=%.4f\n", get_distance(loc, loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
|
|
|
|
for (int32_t i=0; i<100; i++) {
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lat += i;
|
|
|
|
v2 = Vector2f((loc.lat+i)*1.0e-7f, loc.lng*1.0e-7f);
|
|
|
|
if (v2.x != v.x || v2.y != v.y) {
|
2013-08-29 02:05:38 -03:00
|
|
|
hal.console->printf("lat v2 != v at i=%d dist=%.4f\n", (int)i, get_distance(loc, loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<100; i++) {
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lng += i;
|
|
|
|
v2 = Vector2f(loc.lat*1.0e-7f, (loc.lng+i)*1.0e-7f);
|
|
|
|
if (v2.x != v.x || v2.y != v.y) {
|
2013-08-29 02:05:38 -03:00
|
|
|
hal.console->printf("lng v2 != v at i=%d dist=%.4f\n", (int)i, get_distance(loc, loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i=0; i<100; i++) {
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lat -= i;
|
|
|
|
v2 = Vector2f((loc.lat-i)*1.0e-7f, loc.lng*1.0e-7f);
|
|
|
|
if (v2.x != v.x || v2.y != v.y) {
|
2013-08-29 02:05:38 -03:00
|
|
|
hal.console->printf("-lat v2 != v at i=%d dist=%.4f\n", (int)i, get_distance(loc, loc2));
|
2013-07-29 08:21:41 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<100; i++) {
|
|
|
|
loc2 = loc;
|
|
|
|
loc2.lng -= i;
|
|
|
|
v2 = Vector2f(loc.lat*1.0e-7f, (loc.lng-i)*1.0e-7f);
|
|
|
|
if (v2.x != v.x || v2.y != v.y) {
|
2013-08-29 02:05:38 -03:00
|
|
|
hal.console->printf("-lng v2 != v at i=%d dist=%.4f\n", (int)i, get_distance(loc, 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[] = {
|
|
|
|
{ 0.2f*PI, 0.2f*PI },
|
|
|
|
{ 0.2f*PI + 100*PI, 0.2f*PI },
|
|
|
|
{ -0.2f*PI - 100*PI, -0.2f*PI },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_wrap_cd(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<sizeof(wrap_180_tests)/sizeof(wrap_180_tests[0]); i++) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<sizeof(wrap_360_tests)/sizeof(wrap_360_tests[0]); i++) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<sizeof(wrap_PI_tests)/sizeof(wrap_PI_tests[0]); i++) {
|
|
|
|
float r = wrap_PI(wrap_PI_tests[i].v);
|
|
|
|
if (fabs(r - wrap_PI_tests[i].wv) > 0.001f) {
|
|
|
|
hal.console->printf("wrap_PI: v=%f wv=%f r=%f\n",
|
|
|
|
wrap_PI_tests[i].v,
|
|
|
|
wrap_PI_tests[i].wv,
|
|
|
|
r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hal.console->printf("wrap_cd tests done\n");
|
|
|
|
}
|
2013-07-29 08:21:41 -03:00
|
|
|
|
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();
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2012-09-18 15:08:18 -03:00
|
|
|
void loop(void){}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|