lib/matrix: delete unused and add testing to PX4

This commit is contained in:
Daniel Agar
2021-11-16 12:27:14 -05:00
parent 4a3d64f1d7
commit ab07f5300b
15 changed files with 52 additions and 793 deletions
+1
View File
@@ -49,6 +49,7 @@ add_subdirectory(hysteresis)
add_subdirectory(l1)
add_subdirectory(landing_slope)
add_subdirectory(led)
add_subdirectory(matrix)
add_subdirectory(mathlib)
add_subdirectory(mixer)
add_subdirectory(mixer_module)
-9
View File
@@ -1,9 +0,0 @@
Checks: 'clang-diagnostic-*,clang-analyzer-*,*,
,-cppcoreguidelines-pro-type-vararg
,-cppcoreguidelines-pro-bounds-array-to-pointer-decay
,-cppcoreguidelines-pro-bounds-constant-array-index
,-cppcoreguidelines-pro-bounds-pointer-arithmetic
'
WarningsAsErrors: '*'
HeaderFilterRegex: '*.h, *.hpp, *.hh, *.hxx'
-53
View File
@@ -1,53 +0,0 @@
name: Tests
on:
push:
branches:
- 'master'
pull_request:
branches:
- '*'
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false # don't cancel if a job from the matrix fails
matrix:
compiler:
- { compiler: GNU, CC: gcc, CXX: g++, FORMAT: ON }
- { compiler: LLVM, CC: clang, CXX: clang++, FORMAT: OFF }
#flavor: [ Release, Coverage ] # TODO
flavor: [ Release ]
exclude:
- compiler: { compiler: LLVM, CC: clang, CXX: clang++ }
flavor: Coverage
steps:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Install Dependencies
run: |
sudo apt install -y clang cmake g++ gcc lcov
- name: Fetch coveralls
if: matrix.flavor == 'Coverage'
run: |
pip install --user cpp-coveralls
- name: Build & Run
run: |
cmake -DCMAKE_BUILD_TYPE=${{ matrix.flavor }} -DSUPPORT_STDIOSTREAM=ON -DTESTING=ON -DFORMAT=${{ matrix.compiler.FORMAT }} .
make check
# TODO: enable
# - name: Coveralls build
# if: matrix.flavor == 'Coverage'
# run: |
# cpp-coveralls -i matrix
# - name: Coveralls
# if: matrix.flavor == 'Coverage'
# uses: coverallsapp/github-action@master
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
-39
View File
@@ -1,39 +0,0 @@
*.orig
*.swp
*~
astyle/
build*/
cmake_install.cmake
CMakeCache.txt
CMakeFiles/
compile_commands.json
CPackConfig.cmake
CPackSourceConfig.cmake
CTestTestfile.cmake
Makefile
test/attitude
test/cmake_install.cmake
test/CMakeFiles/
test/copyto
test/coverage.info
test/CTestTestfile.cmake
test/filter
test/hatvee
test/helper
test/integration
test/inverse
test/Makefile
test/matrixAssignment
test/matrixMult
test/matrixScalarMult
test/out/
test/pseudoinverse
test/setIdentity
test/slice
test/squareMatrix
test/transpose
test/vector
test/vector2
test/vector3
test/vectorAssignment
Testing/
View File
+34 -178
View File
@@ -1,180 +1,36 @@
cmake_minimum_required(VERSION 2.8)
############################################################################
#
# Copyright (c) 2021 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
set(VERSION_MAJOR "1")
set(VERSION_MINOR "0")
set(VERSION_PATCH "2")
project(matrix CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (NOT CMAKE_BUILD_TYPE)
if(TESTING)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE)
else()
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type" FORCE)
endif()
message(STATUS "set build type to ${CMAKE_BUILD_TYPE}")
if(BUILD_TESTING)
add_subdirectory(test)
endif()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel;Coverage")
option(SUPPORT_STDIOSTREAM "If enabled provides support for << operator (as used with std::cout)" OFF)
option(TESTING "Enable testing" OFF)
option(FORMAT "Enable formatting" OFF)
option(COV_HTML "Display html for coverage" OFF)
option(ASAN "Enable address sanitizer" OFF)
option(UBSAN "Enable undefined behaviour sanitizer" OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(${CMAKE_SOURCE_DIR})
if(SUPPORT_STDIOSTREAM)
add_definitions(-DSUPPORT_STDIOSTREAM)
endif()
if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang"))
set(CMAKE_CXX_FLAGS_COVERAGE
"--coverage -ftest-coverage -fdiagnostics-absolute-paths -O0 -fprofile-arcs -fno-inline-functions -fno-elide-constructors"
CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
"-ftest-coverage -fdiagnostics-absolute-paths"
CACHE STRING "Flags used for linking binaries during coverage builds" FORCE)
else()
set(CMAKE_CXX_FLAGS_COVERAGE
# Add when GCC 9 or later is available as part of the default toolchain: -fprofile-abs-path
"--coverage -fprofile-arcs -ftest-coverage -O0 -fno-default-inline -fno-inline -fno-inline-small-functions -fno-elide-constructors"
CACHE STRING "Flags used by the C++ compiler during coverage builds" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
"--coverage -ftest-coverage -lgcov"
CACHE STRING "Flags used for linking binaries during coverage builds" FORCE)
endif()
mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE CMAKE_C_FLAGS_COVERAGE CMAKE_EXE_LINKER_FLAGS_COVERAGE)
add_compile_options(
-pedantic
-Wall
-Warray-bounds
-Wcast-align
-Wcast-qual
-Wconversion
-Wctor-dtor-privacy
-Wdisabled-optimization
-Werror
-Wextra
-Wfloat-equal
-Wformat-security
-Wformat=2
-Winit-self
-Wlogical-op
-Wmissing-declarations
-Wmissing-include-dirs
-Wno-sign-compare
-Wno-unused
-Wno-unused-parameter
-Wnoexcept
-Wold-style-cast
-Woverloaded-virtual
-Wpointer-arith
-Wredundant-decls
-Wreorder
-Wshadow
-Wsign-conversion
-Wsign-promo
-Wstrict-null-sentinel
-Wswitch-default
-Wundef
-Wuninitialized
-Wunused-variable
)
# clang tolerate unknown gcc options
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(
-Wno-error=unused-command-line-argument-hard-error-in-future
-Wno-unknown-warning-option
)
else()
add_compile_options(
-Wstrict-overflow=5
)
endif()
# santiziers (ASAN, UBSAN)
if(ASAN)
message(STATUS "address sanitizer enabled")
# environment variables
# ASAN_OPTIONS=detect_stack_use_after_return=1
# ASAN_OPTIONS=check_initialization_order=1
add_compile_options(
-fsanitize=address
-g3
-O1
-fno-omit-frame-pointer
)
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address)
elseif(UBSAN)
message(STATUS "undefined behaviour sanitizer enabled")
add_compile_options(
-fsanitize=undefined
-fsanitize=integer
-g3
-O1
-fno-omit-frame-pointer
)
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined)
endif()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)
if(TESTING)
enable_testing()
add_subdirectory(test)
add_dependencies(check test_build)
add_custom_target(clang-tidy COMMAND clang-tidy -p . ${CMAKE_SOURCE_DIR}/test/*.cpp)
add_dependencies(clang-tidy test_build)
endif()
if(FORMAT)
set(astyle_exe ${CMAKE_BINARY_DIR}/astyle/src/bin/astyle)
add_custom_command(OUTPUT ${astyle_exe}
COMMAND wget http://sourceforge.net/projects/astyle/files/astyle/astyle%202.06/astyle_2.06_linux.tar.gz -O /tmp/astyle.tar.gz
COMMAND tar -xvf /tmp/astyle.tar.gz
COMMAND cd astyle/src && make -f ../build/gcc/Makefile
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_target(check_format
COMMAND scripts/format.sh ${astyle_exe} 0
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
DEPENDS ${astyle_exe}
VERBATIM
)
add_custom_target(format
COMMAND scripts/format.sh ${astyle_exe} 1
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
VERBATIM
DEPENDS ${astyle_exe}
)
add_dependencies(check check_format)
endif()
set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
set(CPACK_PACKAGE_CONTACT "james.goppert@gmail.com")
include(CPack)
# vim: set et fenc=utf-8 ft=cmake ff=unix sts=0 sw=4 ts=4 :
-29
View File
@@ -1,29 +0,0 @@
BSD 3-Clause License
Copyright (c) 2015, PX4 Pro Drone Autopilot
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-146
View File
@@ -1,146 +0,0 @@
# matrix [![Build Status](https://travis-ci.org/PX4/Matrix.svg?branch=master)](https://travis-ci.org/PX4/Matrix) [![Coverage Status](https://coveralls.io/repos/PX4/Matrix/badge.svg?branch=master&service=github)](https://coveralls.io/github/PX4/Matrix?branch=master)
A simple and efficient template based matrix library.
## License
* (BSD) The Matrix library is licensed under a permissive 3-clause BSD license. Contributions must be made under the same license.
## Features
* Compile time size checking.
* Euler angle (321), DCM, Quaternion conversion through constructors.
* High testing coverage.
## Limitations
* No dynamically sized matrices.
## Library Overview
* matrix/math.hpp : Provides matrix math routines.
* Matrix (MxN)
* Square Matrix (MxM, has inverse)
* Vector (Mx1)
* Scalar (1x1)
* Quaternion
* Dcm
* Euler (Body 321)
* Axis Angle
* matrix/filter.hpp : Provides filtering routines.
* kalman_correct
* matrix/integrate.hpp : Provides integration routines.
* integrate_rk4 (Runge-Kutta 4th order)
## Testing
The tests can be executed as following:
```
mkdir build
cd build
cmake -DTESTING=ON ..
make check
```
## Formatting
The format can be checked as following:
```
mkdir build
cd build
cmake -DFORMAT=ON ..
make check_format
```
## Example
See the test directory for detailed examples. Some simple examples are included below:
```c++
// define an euler angle (Body 3(yaw)-2(pitch)-1(roll) rotation)
float roll = 0.1f;
float pitch = 0.2f;
float yaw = 0.3f;
Eulerf euler(roll, pitch, yaw);
// convert to quaternion from euler
Quatf q_nb(euler);
// convert to DCM from quaternion
Dcmf dcm(q_nb);
// you can assign a rotation instance that already exist to another rotation instance, e.g.
dcm = euler;
// you can also directly create a DCM instance from euler angles like this
dcm = Eulerf(roll, pitch, yaw);
// create an axis angle representation from euler angles
AxisAngle<float> axis_angle = euler;
// use axis angle to initialize a DCM
Dcmf dcm2(AxisAngle(1, 2, 3));
// use axis angle with axis/angle separated to init DCM
Dcmf dcm3(AxisAngle(Vector3f(1, 0, 0), 0.2));
// do some kalman filtering
const size_t n_x = 5;
const size_t n_y = 3;
// define matrix sizes
SquareMatrix<float, n_x> P;
Vector<float, n_x> x;
Vector<float, n_y> y;
Matrix<float, n_y, n_x> C;
SquareMatrix<float, n_y> R;
SquareMatrix<float, n_y> S;
Matrix<float, n_x, n_y> K;
// define measurement matrix
C = zero<float, n_y, n_x>(); // or C.setZero()
C(0,0) = 1;
C(1,1) = 2;
C(2,2) = 3;
// set x to zero
x = zero<float, n_x, 1>(); // or x.setZero()
// set P to identity * 0.01
P = eye<float, n_x>()*0.01;
// set R to identity * 0.1
R = eye<float, n_y>()*0.1;
// measurement
y(0) = 1;
y(1) = 2;
y(2) = 3;
// innovation
r = y - C*x;
// innovations variance
S = C*P*C.T() + R;
// Kalman gain matrix
K = P*C.T()*S.I();
// S.I() is the inverse, defined for SquareMatrix
// correction
x += K*r;
// slicing
float data[9] = {0, 2, 3,
4, 5, 6,
7, 8, 10
};
SquareMatrix<float, 3> A(data);
// Slice a 3,3 matrix starting at row 1, col 0,
// with size 2 x 3, warning, no size checking
Matrix<float, 2, 3> B(A.slice<2, 3>(1, 0));
// this results in:
// 4, 5, 6
// 7, 8, 10
```
<!-- vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 : -->
-1
View File
@@ -1 +0,0 @@
.ipynb_checkpoints/
File diff suppressed because one or more lines are too long
Binary file not shown.
-30
View File
@@ -1,30 +0,0 @@
#!/bin/bash
echo pwd:$PWD
astyle=$1
format=$2
format_wildcards="""
./matrix/*.*pp
./test/*.*pp
"""
#echo astyle: $astyle
#echo format: $format
if [[ $format -eq 1 ]]
then
echo formatting
$astyle ${format_wildcards}
else
echo checking format...
$astyle --dry-run ${format_wildcards} | grep Formatted &>/dev/null
if [[ $? -eq 0 ]]
then
echo Error: need to format
echo "From cmake build directory run: 'make format'"
exit 1
fi
echo no formatting needed
exit 0
fi
# vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :
+17 -38
View File
@@ -1,4 +1,9 @@
include(gtest.cmake)
#include(gtest.cmake)
add_compile_options(
-Wno-double-promotion
-Wno-float-equal
)
set(tests
setIdentity
@@ -15,7 +20,6 @@ set(tests
attitude
filter
integration
sparseVector
squareMatrix
helper
hatvee
@@ -24,44 +28,19 @@ set(tests
upperRightTriangle
dual
pseudoInverse
)
)
add_custom_target(test_build)
foreach(test_name ${tests})
add_executable(${test_name}
${test_name}.cpp)
add_test(test_${test_name} ${test_name})
add_dependencies(test_build ${test_name})
add_executable(test-matrix_${test_name} ${test_name}.cpp)
add_test(NAME test-matrix_${test_name}
COMMAND test-matrix_${test_name}
WORKING_DIRECTORY ${PX4_BINARY_DIR}
)
add_dependencies(test_build test-matrix_${test_name})
add_dependencies(test_results test-matrix_${test_name})
endforeach()
target_link_libraries(sparseVector gtest_main)
if (${CMAKE_BUILD_TYPE} STREQUAL "Coverage")
add_custom_target(coverage_build
COMMAND ${CMAKE_CTEST_COMMAND}
COMMAND lcov --capture --directory . --output-file coverage.info --rc lcov_branch_coverage=1
COMMAND lcov --rc lcov_branch_coverage=1 --summary coverage.info
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS test_build
)
add_custom_target(coverage_html
COMMAND genhtml coverage.info --output-directory out --branch-coverage
COMMAND x-www-browser out/index.html
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS coverage_build
)
set(coverage_deps
coverage_build)
if (COV_HTML)
list(APPEND coverage_deps coverage_html)
endif()
add_custom_target(coverage
DEPENDS ${coverage_deps}
)
endif()
# vim: set et fenc=utf-8 ft=cmake ff=unix sts=0 sw=4 ts=4 :
px4_add_unit_gtest(SRC sparseVector.cpp)
-18
View File
@@ -1,18 +0,0 @@
cmake_minimum_required(VERSION 2.8.4)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
URL https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
# Wrap download, configure and build steps in a script to log output
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
)
-12
View File
@@ -1,12 +0,0 @@
set_directory_properties(PROPERTIES COMPILE_OPTIONS "")
# Download and unpack googletest at configure time
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
if(result1 OR result2)
message(FATAL_ERROR "Preparing googletest failed: ${result1} ${result2}")
endif()
# Add googletest, defines gtest and gtest_main targets
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)