jcsyshc пре 2 година
родитељ
комит
6864799404

+ 18 - 17
CMakeLists.txt

@@ -1,45 +1,46 @@
-cmake_minimum_required(VERSION 3.0)
+cmake_minimum_required(VERSION 3.13)
 
 project(Sophiar2)
 set(CMAKE_CXX_STANDARD 20)
 
 add_compile_options(-march=native)
 add_compile_options(-mno-avx) # enable avx will cause some stack pointer alignment error with Eigen
+#add_compile_definitions(CORO_SIGNAL2_USE_TIMER)
 
 include_directories(./src)
 
 find_package(Boost REQUIRED COMPONENTS iostreams)
-list(APPEND EXTRA_LIBS ${Boost_LIBRARIES})
+list(APPEND BASIC_LIBS ${Boost_LIBRARIES})
 include_directories(${Boost_INCLUDE_DIRS})
 
 find_package(fmt REQUIRED)
-list(APPEND EXTRA_LIBS fmt::fmt)
+list(APPEND BASIC_LIBS fmt::fmt)
 
 find_package(spdlog REQUIRED)
-list(APPEND EXTRA_LIBS spdlog::spdlog)
+list(APPEND BASIC_LIBS spdlog::spdlog)
 add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)
 
 find_package(Eigen3 REQUIRED)
-list(APPEND EXTRA_LIBS Eigen3::Eigen)
+list(APPEND BASIC_LIBS Eigen3::Eigen)
 
 find_package(nlohmann_json REQUIRED)
-list(APPEND EXTRA_LIBS nlohmann_json::nlohmann_json)
-
-file(GLOB_RECURSE ALGORITHM_IMPL_FILES ./src/algorithm/*.cpp)
-file(GLOB_RECURSE CORE_IMPL_FILES ./src/core/*.cpp)
-file(GLOB_RECURSE ROBOT_IMPL_FILES ./src/robot/*.cpp)
-
-file(GLOB_RECURSE SRC_FILES ./src/*.cpp)
-add_executable(${PROJECT_NAME} ${SRC_FILES})
+list(APPEND BASIC_LIBS nlohmann_json::nlohmann_json)
 
 IF (WIN32)
-    list(APPEND EXTRA_LIBS ws2_32 wsock32 winmm bcrypt)
+    list(APPEND BASIC_LIBS ws2_32 wsock32 winmm bcrypt)
 ELSEIF (UNIX)
-    list(APPEND crypt)
+    list(APPEND BASIC_LIBS crypt)
 ENDIF ()
 
-target_link_libraries(${PROJECT_NAME} ${EXTRA_LIBS})
+include(src/core/CMakeLists.txt)
+include(src/algorithm/CMakeLists.txt)
+include(src/sensor/CMakeLists.txt)
+include(src/robot/CMakeLists.txt)
+
+add_executable(${PROJECT_NAME} src/main.cpp)
+
+target_link_libraries(${PROJECT_NAME} ${BASIC_LIBS})
+target_link_libraries(${PROJECT_NAME} ${SOPHIAR_LIBS})
 
 add_subdirectory(app)
-add_subdirectory(benchmark)
 add_subdirectory(tests)

+ 2 - 5
app/CMakeLists.txt

@@ -1,5 +1,2 @@
-list(FILTER SRC_FILES EXCLUDE REGEX "main.cpp$") # remove global main
-add_executable(kuka_calibration
-        kuka_calibration.cpp
-        ${SRC_FILES})
-target_link_libraries(kuka_calibration ${EXTRA_LIBS})
+add_executable(kuka_calibration kuka_calibration.cpp)
+target_link_libraries(kuka_calibration ${BASIC_LIBS} ${SOPHIAR_LIBS})

+ 0 - 4
benchmark/CMakeLists.txt

@@ -1,4 +0,0 @@
-find_package(benchmark REQUIRED)
-
-add_executable(benchmark_small_obj core/small_obj.cpp ${EXTERN_DEF_FILES})
-target_link_libraries(benchmark_small_obj benchmark::benchmark ${EXTRA_LIBS})

+ 0 - 39
benchmark/core/small_obj.cpp

@@ -1,39 +0,0 @@
-#include "core/small_obj.hpp"
-
-#include <benchmark/benchmark.h>
-
-#include <memory>
-
-template<size_t length>
-static void BM_std_shared_ptr(benchmark::State &state) {
-    struct test_type {
-        double data[length];
-    };
-    for (auto _: state) {
-        benchmark::DoNotOptimize(std::make_shared<test_type>());
-    }
-}
-
-BENCHMARK(BM_std_shared_ptr<0>);
-BENCHMARK(BM_std_shared_ptr<1>);
-BENCHMARK(BM_std_shared_ptr<8>);
-BENCHMARK(BM_std_shared_ptr<16>);
-BENCHMARK(BM_std_shared_ptr<128>);
-
-template<size_t length>
-static void BM_small_obj(benchmark::State &state) {
-    struct test_type : public sophiar::small_obj<test_type> {
-        double data[length];
-    };
-    for (auto _: state) {
-        benchmark::DoNotOptimize(test_type::new_instance());
-    }
-}
-
-BENCHMARK(BM_small_obj<0>);
-BENCHMARK(BM_small_obj<1>);
-BENCHMARK(BM_small_obj<8>);
-BENCHMARK(BM_small_obj<16>);
-BENCHMARK(BM_small_obj<128>);
-
-BENCHMARK_MAIN();

+ 7 - 0
src/algorithm/CMakeLists.txt

@@ -0,0 +1,7 @@
+file(GLOB_RECURSE ALGORITHM_SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
+
+add_library(sophiar_algorithm STATIC ${ALGORITHM_SRC_FILES})
+
+target_link_libraries(sophiar_algorithm ${BASIC_LIBS})
+
+list(APPEND SOPHIAR_LIBS sophiar_algorithm)

+ 7 - 0
src/core/CMakeLists.txt

@@ -0,0 +1,7 @@
+file(GLOB_RECURSE CORE_SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
+
+add_library(sophiar_core STATIC ${CORE_SRC_FILES})
+
+target_link_libraries(sophiar_core ${BASIC_LIBS})
+
+list(APPEND SOPHIAR_LIBS sophiar_core)

+ 7 - 0
src/robot/CMakeLists.txt

@@ -0,0 +1,7 @@
+file(GLOB_RECURSE ROBOT_SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
+
+add_library(sophiar_robot STATIC ${ROBOT_SRC_FILES})
+
+target_link_libraries(sophiar_robot ${BASIC_LIBS})
+
+list(APPEND SOPHIAR_LIBS sophiar_robot)

+ 7 - 0
src/sensor/CMakeLists.txt

@@ -0,0 +1,7 @@
+file(GLOB_RECURSE SENSOR_SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
+
+add_library(sophiar_sensor STATIC ${SENSOR_SRC_FILES})
+
+target_link_libraries(sophiar_sensor ${BASIC_LIBS})
+
+list(APPEND SOPHIAR_LIBS sophiar_sensor)

+ 0 - 0
src/tracker/ndi/ndi_interface.cpp → src/sensor/ndi/ndi_interface.cpp


+ 50 - 0
src/utility/coro_job.hpp

@@ -0,0 +1,50 @@
+#ifndef SOPHIAR2_CORO_JOB_HPP
+#define SOPHIAR2_CORO_JOB_HPP
+
+#include "core/global_defs.h"
+
+#include <boost/asio/awaitable.hpp>
+#include <boost/asio/detached.hpp>
+#include <boost/asio/experimental/concurrent_channel.hpp>
+#include <boost/asio/use_awaitable.hpp>
+#include <boost/system/error_code.hpp>
+
+#include <functional>
+#include <memory>
+#include <thread>
+
+namespace sophiar {
+
+    class coro_job {
+    public:
+
+        template<typename FuncType>
+        explicit coro_job(FuncType &&func)
+                :finish_signal(*global_context) {
+            static_assert(std::is_same_v<decltype(std::declval<FuncType>()()), bool>);
+            worker_thread = std::make_unique<std::thread>([this, func = std::forward<FuncType>(func)]() {
+                finish_signal.async_send({}, func(), boost::asio::detached);
+            });
+        }
+
+        ~coro_job() {
+            if (worker_thread->joinable()) {
+                assert(false);
+            }
+        }
+
+        boost::asio::awaitable<bool> coro_join() {
+            return finish_signal.async_receive(boost::asio::use_awaitable);
+        }
+
+    private:
+        using channel_type = boost::asio::experimental::concurrent_channel<
+                void(boost::system::error_code, bool)>;
+
+        std::unique_ptr<std::thread> worker_thread;
+        channel_type finish_signal;
+    };
+
+}
+
+#endif //SOPHIAR2_CORO_JOB_HPP

+ 13 - 42
tests/CMakeLists.txt

@@ -1,57 +1,28 @@
-set (Boost_USE_STATIC_LIBS OFF)
-find_package (Boost REQUIRED COMPONENTS unit_test_framework)
-include_directories (${Boost_INCLUDE_DIRS})
+set(Boost_USE_STATIC_LIBS OFF)
+find_package(Boost REQUIRED COMPONENTS unit_test_framework)
+include_directories(${Boost_INCLUDE_DIRS})
 
 file(GLOB_RECURSE TEST_CORE_SRC_FILES ./core/*.cpp)
-add_executable(test_core
-        ${TEST_CORE_SRC_FILES}
-        ${CORE_IMPL_FILES})
+add_executable(test_core ${TEST_CORE_SRC_FILES} ${CORE_SRC_FILES})
 target_compile_definitions(test_core PUBLIC SOPHIAR_TEST)
-#target_compile_definitions(test_core PUBLIC CORO_SIGNAL2_USE_TIMER)
-target_link_libraries(test_core ${Boost_LIBRARIES} ${EXTRA_LIBS})
+target_link_libraries(test_core ${Boost_LIBRARIES} ${BASIC_LIBS})
 
 file(GLOB_RECURSE TEST_UTILITY_SRC_FILES ./utility/*.cpp)
-add_executable(test_utility
-        ${TEST_UTILITY_SRC_FILES}
-        ${CORE_IMPL_FILES})
+add_executable(test_utility ${TEST_UTILITY_SRC_FILES} ${CORE_SRC_FILES})
 target_compile_definitions(test_utility PUBLIC SOPHIAR_TEST)
-target_compile_definitions(test_utility PUBLIC CORO_SIGNAL2_USE_TIMER)
-target_link_libraries(test_utility ${Boost_LIBRARIES} ${EXTRA_LIBS})
+target_link_libraries(test_utility ${Boost_LIBRARIES} ${BASIC_LIBS})
 
 file(GLOB_RECURSE TEST_ALGORITHM_SRC_FILES ./algorithm/*.cpp)
-add_executable(test_algorithm
-        ${TEST_ALGORITHM_SRC_FILES}
-        ${ALGORITHM_IMPL_FILES}
-        #        ${ALGORITHM_IMPL_FILES}
-        ${CORE_IMPL_FILES})
+add_executable(test_algorithm ${TEST_ALGORITHM_SRC_FILES} ${CORE_SRC_FILES})
 target_compile_definitions(test_algorithm PUBLIC SOPHIAR_TEST SOPHIAR_TEST_ALGORITHM)
-target_compile_definitions(test_algorithm PUBLIC CORO_SIGNAL2_USE_TIMER)
-target_link_libraries(test_algorithm ${Boost_LIBRARIES} ${EXTRA_LIBS})
+target_link_libraries(test_algorithm ${Boost_LIBRARIES} ${BASIC_LIBS} sophiar_algorithm)
 
 file(GLOB_RECURSE TEST_ROBOT_SRC_FILES ./robot/*.cpp)
-add_executable(test_robot
-        ${TEST_ROBOT_SRC_FILES}
-        ${ROBOT_IMPL_FILES}
-        ${CORE_IMPL_FILES})
+add_executable(test_robot ${TEST_ROBOT_SRC_FILES} ${CORE_SRC_FILES})
 target_compile_definitions(test_robot PUBLIC SOPHIAR_TEST SOPHIAR_TEST_ROBOT)
-target_compile_definitions(test_robot PUBLIC CORO_SIGNAL2_USE_TIMER)
-target_link_libraries(test_robot ${Boost_LIBRARIES} ${EXTRA_LIBS})
-
-file(GLOB_RECURSE TEST_NDI_SRC_FILES ./tracker/ndi*.cpp)
-add_executable(test_ndi
-        ${TEST_NDI_SRC_FILES}
-        ../src/tracker/ndi/ndi_interface.cpp
-        ${ALGORITHM_IMPL_FILES}
-        ${CORE_IMPL_FILES})
-target_compile_definitions(test_ndi PUBLIC SOPHIAR_TEST SOPHIAR_TEST_NDI SOPHIAR_TEST_ALGORITHM)
-#target_compile_definitions(test_ndi PUBLIC CORO_SIGNAL2_USE_TIMER)
-target_link_libraries(test_ndi ${Boost_LIBRARIES} ${EXTRA_LIBS})
+target_link_libraries(test_robot ${Boost_LIBRARIES} ${BASIC_LIBS} sophiar_robot)
 
 file(GLOB_RECURSE TEST_SENSOR_SRC_FILES ./sensor/*.cpp)
-add_executable(test_sensor
-        ${TEST_SENSOR_SRC_FILES}
-        ../src/sensor/optoforce/optoforce_daq.cpp
-        ${CORE_IMPL_FILES})
+add_executable(test_sensor ${TEST_SENSOR_SRC_FILES} ${CORE_SRC_FILES})
 target_compile_definitions(test_sensor PUBLIC SOPHIAR_TEST SOPHIAR_TEST_SENSOR)
-#target_compile_definitions(test_ndi PUBLIC CORO_SIGNAL2_USE_TIMER)
-target_link_libraries(test_sensor ${Boost_LIBRARIES} ${EXTRA_LIBS})
+target_link_libraries(test_sensor ${Boost_LIBRARIES} ${BASIC_LIBS} sophiar_sensor)

+ 0 - 0
tests/tracker/ndi.cpp → tests/sensor/ndi.cpp


+ 0 - 0
tests/tracker/ndi_registration.cpp → tests/sensor/ndi_registration.cpp


+ 0 - 0
tests/tracker/ndi_stabilizer.cpp → tests/sensor/ndi_stabilizer.cpp


+ 0 - 1
tests/sensor/optoforce_daq.cpp

@@ -1,5 +1,4 @@
 #define BOOST_TEST_DYN_LINK
-#define BOOST_TEST_MAIN  // in only one cpp file
 
 #include "core/global_defs.h"
 

+ 3 - 0
tests/utility/coro_job.cpp

@@ -0,0 +1,3 @@
+#include "utility/coro_job.hpp"
+
+// TODO: test unit for coro_job