Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
08a1422
Add the drivers for the GY89 MPU and barometer BMP 180
shrit Jun 20, 2026
cdffaa3
Adding a Makefile for the collect dataset project, use ncurses
shrit Jun 20, 2026
15e633a
Adding the collect binary that collect data w/o TUI
shrit Jun 20, 2026
cdf960b
Adding the CMake configuration that is needed for cross compilation
shrit Jun 21, 2026
9c3a920
Adding necessary CMakeLists file needed for the cross compilation
shrit Jun 21, 2026
711fa01
Fix capture the next arg, and also make TUI by default unless otherwise
shrit Jun 21, 2026
434635d
Make ncurses compiles by default unless the user say otherwise
shrit Jun 28, 2026
39ed301
Adding OpenBLAS patch in CMakelist before the compilation
shrit Jun 28, 2026
26d1747
Remove makefiles as pre review
shrit Jul 13, 2026
0c792dd
Remove completely ncurses
shrit Jul 13, 2026
6278731
Simplify the code largely to make it easier to read
shrit Jul 13, 2026
a2160d7
Move manually the sensor code into its own class
shrit Jul 13, 2026
05fb789
Inline mag calibration class
shrit Jul 13, 2026
84d8402
Remove the cpp from the list of files to compile
shrit Jul 13, 2026
32544aa
Write openblas function that apply the low memory patch modification
shrit Jul 14, 2026
be89ac9
Move the parse sensor to sensor_board class since it is being used by…
shrit Jul 14, 2026
0d61be2
Simplify the argc argv input according to rcurtin comment
shrit Jul 14, 2026
bef370a
Fix the architecture flag, and match our mlpack modifications
shrit Jul 14, 2026
0a8753f
Fix the author name
shrit Jul 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions cpp/movement_recognition/CMake/ConfigureCrossCompile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This file adds the necessary configurations to cross compile
# mlpack for embedded systems. You need to set the following variables
# from the command line: CMAKE_SYSROOT and TOOLCHAIN_PREFIX.
# This file will compile OpenBLAS if it is downloaded and it is not
# available on your system in order to find the BLAS library. If OpenBLAS will
# be compiled, the OPENBLAS_TARGET variable must be set. This can be done
# by, e.g., setting ARCH_NAME (which will set OPENBLAS_TARGET in
# `flags-config.cmake`).

if (CMAKE_CROSSCOMPILING)
include(CMake/crosscompile-arch-config.cmake)
if (NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX))
message(FATAL_ERROR "Neither CMAKE_SYSROOT nor TOOLCHAIN_PREFIX are set; please set both of them and try again.")
elseif(NOT CMAKE_SYSROOT)
message(FATAL_ERROR "Cannot configure: CMAKE_SYSROOT must be set when performing cross-compiling!")
elseif(NOT TOOLCHAIN_PREFIX)
message(FATAL_ERROR "Cannot configure: TOOLCHAIN_PREFIX must be set when performing cross-compiling!")
endif()

# Now make sure that we can still compile a simple test program.
# (This ensures we didn't add any bad CXXFLAGS.)
# Note that OUTPUT_VARIABLE is only available in newer versions of CMake!
# CMake 3.23 (silently) introduced the variable.
include(CheckCXXSourceCompiles)
if (CMAKE_VERSION VERSION_LESS "3.22.0")
check_cxx_source_compiles("int main() { return 0; }" COMPILE_SUCCESS)
if (NOT COMPILE_SUCCESS)
message(FATAL_ERROR "The C++ cross-compiler at ${CMAKE_CXX_COMPILER} is "
"not able to compile a trivial test program. Check the CXXFLAGS!")
endif ()
else ()
check_cxx_source_compiles("int main() { return 0; }" COMPILE_SUCCESS
OUTPUT_VARIABLE COMPILE_OUTPUT)
if (NOT COMPILE_SUCCESS)
message(FATAL_ERROR "The C++ cross-compiler at ${CMAKE_CXX_COMPILER} is "
"not able to compile a trivial test program. Compiler output:\n\n"
"${COMPILE_OUTPUT}")
endif ()
endif ()
endif()

macro(search_openblas version)
set(BLA_STATIC ON)
find_package(BLAS)
if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES))
if(NOT OPENBLAS_TARGET)
message(FATAL_ERROR "Cannot compile OpenBLAS: OPENBLAS_TARGET is not set. Either set that variable, or set BOARD_NAME correctly!")
endif()
get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz)
if (NOT MSVC)
if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a")
set(ENV{COMMON_OPT} "${CMAKE_OPENBLAS_FLAGS}") # Pass our flags to OpenBLAS

# NN-on-device memory fit (riscv64). OpenBLAS lazily allocates a per-GEMM
# scratch buffer (BUFFER_SIZE -- 32 MB on riscv64) sized for its default
# N-block (SGEMM_DEFAULT_R = 12288). That single 32 MB allocation does
# not fit on a ~28 MB device, so the first f32 matrix-multiply -- e.g. the
# neural network's dense layers -- is OOM-killed at startup. (Random
# forest and KNN avoid that big GEMM path, which is why only the NN
# failed.) Our matrices are tiny, so shrink the N-block to 2048 and the
# buffer to 8 MB for the generic riscv64 target; there is no measurable
# speed cost. See README.md / BINARY_SIZE.md for the full investigation.
if(OPENBLAS_TARGET STREQUAL "RISCV64_GENERIC")
execute_process(COMMAND sed -i
"/#ifdef RISCV64_GENERIC/,/#endif/{s/_DEFAULT_R 12288/_DEFAULT_R 2048/;s/_DEFAULT_R 8192/_DEFAULT_R 2048/;s/_DEFAULT_R 4096/_DEFAULT_R 2048/}"
"${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/param.h")
execute_process(COMMAND sed -i
"s/( 32 << 20)/( 8 << 20)/"
"${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/common_riscv64.h")
message(STATUS
"OpenBLAS(riscv64): shrank GEMM buffer 32MB->8MB and N-block "
"12288->2048 so the f32 neural net fits in ~28 MB RAM.")
endif()
# USE_THREAD=0 / NUM_THREADS=1 / USE_OPENMP=0: build a single-threaded
# OpenBLAS. On a single-core, 64 MB target (Milk-V Duo) the threaded
# build spawns worker threads that busy-wait (spin) at startup, which
# starves the main thread on one core and hangs the program before it
# even runs. Single-threaded also removes the per-thread GEMM buffers.
execute_process(COMMAND make TARGET=${OPENBLAS_TARGET} BINARY=${OPENBLAS_BINARY} HOSTCC=gcc CC=${CMAKE_C_COMPILER} FC=${CMAKE_FORTRAN_COMPILER} NO_SHARED=1 USE_THREAD=0 NUM_THREADS=1 USE_OPENMP=0
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version})
endif()
file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a")
set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES})
set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES})
set(BLA_VENDOR OpenBLAS)
set(BLAS_FOUND ON)
endif()
endif()
find_library(GFORTRAN NAMES libgfortran.a)
find_library(PTHREAD NAMES libpthread.a)
set(CROSS_COMPILE_SUPPORT_LIBRARIES ${GFORTRAN} ${PTHREAD})
endmacro()
95 changes: 95 additions & 0 deletions cpp/movement_recognition/CMake/crosscompile-arch-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# This function provides a set of specific flags for each supported board
# depending on the processor type. The objective is to optimize for size.
# Thus, all of the following flags are chosen carefully to reduce binary
# footprints.

# Set generic minimization flags for all platforms.
# These flags are the same for all cross-compilation cases and they are
# mainly to reduce the binary footprint.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fomit-frame-pointer -fno-unwind-tables")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-asynchronous-unwind-tables -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fshort-enums -finline-small-functions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -findirect-inlining -fno-common")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmerge-all-constants -fno-ident")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-unroll-loops -fno-math-errno")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-stack-protector")
set(CMAKE_OPENBLAS_FLAGS "${CMAKE_CXX_FLAGS}") # OpenBLAS does not supoport flto
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--hash-style=gnu -Wl,--build-id=none")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,norelro")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
## Keep the following flag in comment, they might be relevant in the case of MCU's
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-nmagic,-Bsymbolic -nostartfiles")

# BOARD_NAME is deprecated and will be removed in mlpack 5.
# please use ARCH_NAME instead.
set(BOARD_NAME "" CACHE STRING "Specify Board name to optimize for.")
set(ARCH_NAME "" CACHE STRING "Name of embedded architecture to optimize for.")

if (BOARD_NAME)
set(ARCH_NAME "${BOARD_NAME}")
endif()

string(TOUPPER ${ARCH_NAME} ARCH)

# Set specific platforms CMAKE CXX flags.
if(ARCH STREQUAL "RPI2" OR ARCH STREQUAL "CORTEXA7")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a7")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=hard -mfpu=neon-vfpv4")
set(OPENBLAS_TARGET "ARMV7")
set(OPENBLAS_BINARY "32")
elseif(ARCH STREQUAL "CORTEXA8")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a8")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=hard -mfpu=neon")
set(OPENBLAS_TARGET "ARMV7")
set(OPENBLAS_BINARY "32")
elseif(ARCH STREQUAL "CORTEXA9")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a9")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=hard -mfpu=neon")
set(OPENBLAS_TARGET "CORTEXA9")
set(OPENBLAS_BINARY "32")
elseif(ARCH STREQUAL "CORTEXA15")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a15")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=hard -mfpu=neon")
set(OPENBLAS_TARGET "CORTEXA15")
set(OPENBLAS_BINARY "32")
elseif(ARCH STREQUAL "RPI3" OR ARCH STREQUAL "CORTEXA53")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a53 -ftree-vectorize")
set(OPENBLAS_TARGET "CORTEXA53")
set(OPENBLAS_BINARY "64")
elseif(ARCH STREQUAL "RPI4" OR ARCH STREQUAL "CORTEXA72")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8.2-a+crypto+fp16+rcpc+dotprod -fasynchronous-unwind-tables")
set(OPENBLAS_TARGET "CORTEXA72")
set(OPENBLAS_BINARY "64")
elseif(ARCH STREQUAL "JETSONAGX" OR ARCH STREQUAL "CORTEXA76")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a76 -ftree-vectorize")
set(OPENBLAS_TARGET "CORTEXA76")
set(OPENBLAS_BINARY "64")
elseif(ARCH STREQUAL "BV")
set(OPENBLAS_TARGET "RISCV64_GENERIC")
set(OPENBLAS_BINARY "64")
elseif(ARCH STREQUAL "C906")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=thead-c906")
set(OPENBLAS_TARGET "RISCV64_GENERIC")
set(OPENBLAS_BINARY "64")
elseif(ARCH STREQUAL "x280")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=sifive-x280")
set(OPENBLAS_TARGET "x280")
set(OPENBLAS_BINARY "64")
elseif(ARCH STREQUAL "KATAMI")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3")
set(OPENBLAS_TARGET "KATAMI")
set(OPENBLAS_BINARY "32")
elseif(ARCH STREQUAL "COPPERMINE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3")
set(OPENBLAS_TARGET "COPPERMINE")
set(OPENBLAS_BINARY "32")
elseif(ARCH STREQUAL "NORTHWOOD")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium4")
set(OPENBLAS_TARGET "NORTHWOOD")
set(OPENBLAS_BINARY "32")
elseif(ARCH)
## TODO: update documentation with a list of the supported boards.
message(FATAL_ERROR "Given ARCH_NAME is not known; please choose a supported board from the list")
endif()
40 changes: 40 additions & 0 deletions cpp/movement_recognition/CMake/crosscompile-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## This file handles cross-compilation configurations for any architecture.
## The objective of this file is to find and assign cross-compiler and the
## entire toolchain.
##
## This configuration works best with the buildroot toolchain. When using this
## file, be sure to set the TOOLCHAIN_PREFIX and CMAKE_SYSROOT variables,
## preferably via the CMake configuration command (e.g. `-DCMAKE_SYSROOT=<...>`).
##
## You can use any toochain to produce the cross compiled binaries. However,
## we recommend using buildroot toolchain for cross-compilation. Here is the
## link to download the toolchains: https://toolchains.bootlin.com/

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSROOT)
set(TOOLCHAIN_PREFIX "" CACHE STRING "Path for toolchain for cross compiler and other compilation tools.")

# Ensure that CMake tries to build static libraries when testing the compiler.
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_AR "${TOOLCHAIN_PREFIX}gcc-ar" CACHE FILEPATH "" FORCE)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}ld)
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH true)
set(CMAKE_FORTRAN_COMPILER ${TOOLCHAIN_PREFIX}gfortran)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool")
set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool")

## Here are the standard ROOT_PATH if you are using the standard toolchain
## if you are using a different toolchain you have to specify that too.
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_SYSROOT}" CACHE INTERNAL "" FORCE)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
Loading
Loading