ardupilot/libraries/AP_Math/examples/polygon/polygon.cpp
Caio Marcelo de Oliveira Filho 2e464a53c2 AP_HAL: make code not depend on concrete HAL implementations
The switching between different AP_HAL was happening by giving different
definitions of AP_HAL_BOARD_DRIVER, and the programs would use it to
instantiate.

A program or library code would have to explicitly include (and depend)
on the concrete implementation of the HAL, even when using it only via
interface.

The proposed change move this dependency to be link time. There is a
AP_HAL::get_HAL() function that is used by the client code. Each
implementation of HAL provides its own definition of this function,
returning the appropriate concrete instance.

Since this replaces the job of AP_HAL_BOARD_DRIVER, the definition was
removed.

The static variables for PX4 and VRBRAIN were named differently to avoid
shadowing the extern symbol 'hal'.
2015-10-21 09:16:07 +11:00

120 lines
4.0 KiB
C++

/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
//
// Unit tests for the AP_Math polygon code
//
#include <AP_Common/AP_Common.h>
#include <AP_Progmem/AP_Progmem.h>
#include <AP_Param/AP_Param.h>
#include <StorageManager/StorageManager.h>
#include <AP_Math/AP_Math.h>
#include <AP_HAL/AP_HAL.h>
#include <AP_HAL_AVR/AP_HAL_AVR.h>
#include <AP_HAL_Linux/AP_HAL_Linux.h>
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
/*
* this is the boundary of the 2010 outback challenge
* Note that the last point must be the same as the first for the
* Polygon_outside() algorithm
*/
static const Vector2l OBC_boundary[] = {
Vector2l(-265695640, 1518373730),
Vector2l(-265699560, 1518394050),
Vector2l(-265768230, 1518411420),
Vector2l(-265773080, 1518403440),
Vector2l(-265815110, 1518419500),
Vector2l(-265784860, 1518474690),
Vector2l(-265994890, 1518528860),
Vector2l(-266092110, 1518747420),
Vector2l(-266454780, 1518820530),
Vector2l(-266435720, 1518303500),
Vector2l(-265875990, 1518344050),
Vector2l(-265695640, 1518373730)
};
static const struct {
Vector2l point;
bool outside;
} test_points[] = {
{ Vector2l(-266398870, 1518220000), true },
{ Vector2l(-266418700, 1518709260), false },
{ Vector2l(-350000000, 1490000000), true },
{ Vector2l(0, 0), true },
{ Vector2l(-265768150, 1518408250), false },
{ Vector2l(-265774060, 1518405860), true },
{ Vector2l(-266435630, 1518303440), true },
{ Vector2l(-266435650, 1518313540), false },
{ Vector2l(-266435690, 1518303530), false },
{ Vector2l(-266435690, 1518303490), true },
{ Vector2l(-265875990, 1518344049), true },
{ Vector2l(-265875990, 1518344051), false },
{ Vector2l(-266454781, 1518820530), true },
{ Vector2l(-266454779, 1518820530), true },
{ Vector2l(-266092109, 1518747420), true },
{ Vector2l(-266092111, 1518747420), false },
{ Vector2l(-266092110, 1518747421), true },
{ Vector2l(-266092110, 1518747419), false },
{ Vector2l(-266092111, 1518747421), true },
{ Vector2l(-266092109, 1518747421), true },
{ Vector2l(-266092111, 1518747419), false },
};
/*
* polygon tests
*/
void setup(void)
{
unsigned i, count;
bool all_passed = true;
uint32_t start_time;
hal.console->println("polygon unit tests\n");
if (!Polygon_complete(OBC_boundary, ARRAY_SIZE(OBC_boundary))) {
hal.console->println("OBC boundary is not complete!");
all_passed = false;
}
if (Polygon_complete(OBC_boundary, ARRAY_SIZE(OBC_boundary)-1)) {
hal.console->println("Polygon_complete test failed");
all_passed = false;
}
for (i=0; i<ARRAY_SIZE(test_points); i++) {
bool result;
result = Polygon_outside(test_points[i].point,
OBC_boundary, ARRAY_SIZE(OBC_boundary));
hal.console->printf_P(PSTR("%10f,%10f %s %s\n"),
1.0e-7f*test_points[i].point.x,
1.0e-7f*test_points[i].point.y,
result ? "OUTSIDE" : "INSIDE ",
result == test_points[i].outside ? "PASS" : "FAIL");
if (result != test_points[i].outside) {
all_passed = false;
}
}
hal.console->println(all_passed ? "TEST PASSED" : "TEST FAILED");
hal.console->println("Speed test:");
start_time = hal.scheduler->micros();
for (count=0; count<1000; count++) {
for (i=0; i<ARRAY_SIZE(test_points); i++) {
bool result;
result = Polygon_outside(test_points[i].point,
OBC_boundary, ARRAY_SIZE(OBC_boundary));
if (result != test_points[i].outside) {
all_passed = false;
}
}
}
hal.console->printf("%u usec/call\n", (unsigned)((hal.scheduler->micros()
- start_time)/(count*ARRAY_SIZE(test_points))));
hal.console->println(all_passed ? "ALL TESTS PASSED" : "TEST FAILED");
}
void loop(void){}
AP_HAL_MAIN();