diff --git a/Makefile b/Makefile index 89a7d1cd6..ef773096b 100644 --- a/Makefile +++ b/Makefile @@ -638,7 +638,7 @@ uavo-collections_clean: # ############################## -ALL_UNITTESTS := logfs +ALL_UNITTESTS := logfs math # Build the directory for the unit tests UT_OUT_DIR := $(BUILD_DIR)/unit_tests diff --git a/flight/libraries/math/mathmisc.h b/flight/libraries/math/mathmisc.h index 42213e504..823d358b9 100644 --- a/flight/libraries/math/mathmisc.h +++ b/flight/libraries/math/mathmisc.h @@ -31,6 +31,8 @@ #ifndef MATHMISC_H #define MATHMISC_H +#include + // returns min(boundary1,boundary2) if valmax(boundary1,boundary2) // returns val if min(boundary1,boundary2)<=val<=max(boundary1,boundary2) @@ -79,4 +81,42 @@ static inline void vector_normalizef(float *vector, const uint8_t dim) } } +typedef struct pointf { + float x; + float y; +} pointf; + +// Returns the y value, given x, on the line passing through the points p0 and p1. +static inline float y_on_line(float x, pointf *p0, pointf *p1) +{ + // Setup line y = m * x + b. + const float dY1 = p1->y - p0->y; + const float dX1 = p1->x - p0->x; + const float m = dY1 / dX1; // == dY0 / dX0 == (p0.y - b) / (p0.x - 0.0f) ==> + const float b = p0->y - m * p0->x; + + // Get the y value on the line. + return m * x + b; +} + +// Returns the y value, given x, on the curve defined by the points array. +// The fist and last line of the curve extends beyond the first resp. last points. +static inline float y_on_curve(float x, pointf points[], int num_points) +{ + // Find the two points x is within. + // If x is smaller than the first point's x value, use the first line of the curve. + // If x is larger than the last point's x value, user the last line of the curve. + int end_point = num_points - 1; + + for (int i = 1; i < num_points; i++) { + if (x < points[i].x) { + end_point = i; + break; + } + } + + // Find the y value on the selected line. + return y_on_line(x, &points[end_point - 1], &points[end_point]); +} + #endif /* MATHMISC_H */ diff --git a/flight/libraries/math/pid.c b/flight/libraries/math/pid.c index 1ae1e93aa..0523b16dd 100644 --- a/flight/libraries/math/pid.c +++ b/flight/libraries/math/pid.c @@ -141,36 +141,11 @@ void pid_configure(struct pid *pid, float p, float i, float d, float iLim) pid->iLim = iLim; } -float pid_scale_factor_from_line(float x, struct point *p0, struct point *p1) -{ - // Setup line y = m * x + b. - const float dY1 = p1->y - p0->y; - const float dX1 = p1->x - p0->x; - const float m = dY1 / dX1; // == dY0 / dX0 == (p0.y - b) / (p0.x - 0.0f) ==> - const float b = p0->y - m * p0->x; - - // Scale according to given x. - float y = m * x + b; - - return 1.0f + y; -} - float pid_scale_factor(pid_scaler *scaler) { - const int length = sizeof(scaler->points) / sizeof(typeof(scaler->points[0])); + float y = y_on_curve(scaler->x, scaler->points, sizeof(scaler->points) / sizeof(scaler->points[0])); - // Find the two points where is within scaler->x. Use the outer points if - // scaler->x is smaller than the first x value or larger than the last x value. - int end_point = length - 1; - - for (int i = 1; i < length; i++) { - if (scaler->x < scaler->points[i].x) { - end_point = i; - break; - } - } - - return pid_scale_factor_from_line(scaler->x, &scaler->points[end_point - 1], &scaler->points[end_point]); + return 1.0f + (IS_REAL(y) ? y : 0.0f); } float pid_apply_setpoint_scaled(struct pid *pid, const float factor, const float setpoint, const float measured, float dT, diff --git a/flight/libraries/math/pid.h b/flight/libraries/math/pid.h index 64dbe2aa8..cf99e090a 100644 --- a/flight/libraries/math/pid.h +++ b/flight/libraries/math/pid.h @@ -31,6 +31,8 @@ #ifndef PID_H #define PID_H +#include "mathmisc.h" + // ! struct pid { float p; @@ -43,11 +45,8 @@ struct pid { }; typedef struct pid_scaler { - float x; - struct point { - float x; - float y; - } points[5]; + float x; + pointf points[5]; } pid_scaler; // ! Methods to use the pid structures diff --git a/flight/tests/math/Makefile b/flight/tests/math/Makefile new file mode 100644 index 000000000..cb97e89f9 --- /dev/null +++ b/flight/tests/math/Makefile @@ -0,0 +1,37 @@ +############################################################################### +# @file Makefile +# @author PhoenixPilot, http://github.com/PhoenixPilot, Copyright (C) 2012 +# Copyright (c) 2013, The OpenPilot Team, http://www.openpilot.org +# @addtogroup +# @{ +# @addtogroup +# @{ +# @brief Makefile for unit test +############################################################################### +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +ifndef OPENPILOT_IS_COOL + $(error Top level Makefile must be used to build this target) +endif + +include $(ROOT_DIR)/make/firmware-defs.mk + +EXTRAINCDIRS += $(TOPDIR) +EXTRAINCDIRS += $(ROOT_DIR)/flight/libraries/math +EXTRAINCDIRS += $(PIOS)/inc + +include $(ROOT_DIR)/make/unittest.mk diff --git a/flight/tests/math/unittest.cpp b/flight/tests/math/unittest.cpp new file mode 100644 index 000000000..e6523d0c3 --- /dev/null +++ b/flight/tests/math/unittest.cpp @@ -0,0 +1,110 @@ +#include "gtest/gtest.h" + +#include /* printf */ +#include /* abort */ +#include /* memset */ + +extern "C" { +#include "mathmisc.h" +} + +#define epsilon 0.00001f +// From pios_math.h +#define IS_REAL(f) (!isnan(f) && !isinf(f)) +#define length(points_array) (sizeof(points_array) / sizeof(points_array[0])) + +// To use a test fixture, derive a class from testing::Test. +class MathTestRaw : public testing::Test {}; + +TEST_F(MathTestRaw, y_on_line0) { + pointf points[] = { + { 0.0f, -0.30f }, + { 0.5f, 0.30 } + }; + + EXPECT_NEAR(-0.60f, y_on_line(-0.25f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(-0.30f, y_on_line(0.00f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.00f, y_on_line(0.25f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.30f, y_on_line(0.50f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.60f, y_on_line(0.75f, &points[0], &points[1]), epsilon); +} + +TEST_F(MathTestRaw, y_on_line1) { + pointf points[] = { + { 0.25f, -0.30f }, + { 0.50f, 0.30 } + }; + + EXPECT_NEAR(-1.50f, y_on_line(-0.25f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(-0.90f, y_on_line(0.00f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(-0.30f, y_on_line(0.25f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.30f, y_on_line(0.50f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.90f, y_on_line(0.75f, &points[0], &points[1]), epsilon); +} + +TEST_F(MathTestRaw, y_on_line2) { + pointf points[] = { + { -0.25f, -0.30f }, + { 0.50f, 0.30 } + }; + + EXPECT_NEAR(-0.30f, y_on_line(-0.25f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(-0.10f, y_on_line(0.00f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.10f, y_on_line(0.25f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.30f, y_on_line(0.50f, &points[0], &points[1]), epsilon); + EXPECT_NEAR(0.50f, y_on_line(0.75f, &points[0], &points[1]), epsilon); +} + +TEST_F(MathTestRaw, y_on_line3) { + pointf points[] = { + { 0.25f, -0.30f }, + { 0.25f, 0.30 } + }; + + + EXPECT_FALSE(IS_REAL(y_on_line(-0.25f, &points[0], &points[1]))); +} + +TEST_F(MathTestRaw, y_on_curve0) { + pointf points[] = { + { 0.00f, -0.40f }, + { 0.25f, -0.20f }, + { 0.50f, 0.00f }, + { 0.75f, 0.20 }, + { 1.00f, 0.40 } + }; + + EXPECT_NEAR(-0.50f, y_on_curve(-0.125f, points, length(points)), epsilon); + EXPECT_NEAR(-0.40f, y_on_curve(0.000f, points, length(points)), epsilon); + EXPECT_NEAR(-0.30f, y_on_curve(0.125f, points, length(points)), epsilon); + EXPECT_NEAR(-0.20f, y_on_curve(0.250f, points, length(points)), epsilon); + EXPECT_NEAR(-0.10f, y_on_curve(0.375f, points, length(points)), epsilon); + EXPECT_NEAR(0.00f, y_on_curve(0.500f, points, length(points)), epsilon); + EXPECT_NEAR(0.10f, y_on_curve(0.625f, points, length(points)), epsilon); + EXPECT_NEAR(0.20f, y_on_curve(0.750f, points, length(points)), epsilon); + EXPECT_NEAR(0.30f, y_on_curve(0.875f, points, length(points)), epsilon); + EXPECT_NEAR(0.40f, y_on_curve(1.000f, points, length(points)), epsilon); + EXPECT_NEAR(0.50f, y_on_curve(1.125f, points, length(points)), epsilon); +} + + +TEST_F(MathTestRaw, y_on_curve1) { + pointf points[] = { + { -0.25f, 0.10f }, + { 0.00f, 0.20f }, + { 0.50f, 0.30f }, + { 1.00f, -0.30 }, + { 2.00f, -0.50 } + }; + + EXPECT_NEAR(0.00f, y_on_curve(-0.500f, points, length(points)), epsilon); + EXPECT_NEAR(0.10f, y_on_curve(-0.250f, points, length(points)), epsilon); + EXPECT_NEAR(0.15f, y_on_curve(-0.125f, points, length(points)), epsilon); + EXPECT_NEAR(0.20f, y_on_curve(0.000f, points, length(points)), epsilon); + EXPECT_NEAR(0.22f, y_on_curve(0.100f, points, length(points)), epsilon); + EXPECT_NEAR(0.30f, y_on_curve(0.500f, points, length(points)), epsilon); + EXPECT_NEAR(0.00f, y_on_curve(0.750f, points, length(points)), epsilon); + EXPECT_NEAR(-0.30f, y_on_curve(1.000f, points, length(points)), epsilon); + EXPECT_NEAR(-0.35f, y_on_curve(1.250f, points, length(points)), epsilon); + EXPECT_NEAR(-0.50f, y_on_curve(2.000f, points, length(points)), epsilon); +}