From c7ca5089ecee9e9474a95975fd356e5ef3a99950 Mon Sep 17 00:00:00 2001 From: Sahil Kale Date: Mon, 9 Oct 2023 14:13:25 +0200 Subject: [PATCH] [Infra] Reorganize all files - Employ new directory structure - Move some stuff around Topic: directory-reorg Relative: Reviewers: --- .gitignore | 5 +- CMakeLists.txt | 97 ++++++++++++------- .../bldc}/brushless_6step_control_loop.cpp | 2 - .../bldc}/brushless_6step_control_loop.hpp | 2 +- .../bldc}/brushless_foc_control_loop.cpp | 0 .../bldc}/brushless_foc_control_loop.hpp | 0 {src => control_loop/bldc}/math_foc.cpp | 0 {inc => control_loop/bldc}/math_foc.hpp | 0 .../brushed}/brushed_control_loop.cpp | 2 - .../brushed}/brushed_control_loop.hpp | 0 {inc => control_loop}/control_loop.hpp | 0 .../stepper}/stepper_control_loop.cpp | 1 - .../stepper}/stepper_control_loop.hpp | 0 {inc => hal}/hal_adc.hpp | 0 {inc => hal}/hal_clock.hpp | 0 {inc => hal}/hal_common.hpp | 0 {inc => hal}/hal_gpio.hpp | 0 {inc => hal}/hal_serial.hpp | 0 {inc => hal}/hal_timer.hpp | 0 {inc => hwbridge/3phase}/bridge_3phase.hpp | 0 .../3phase}/bridge_3phase_drv8323.hpp | 0 {inc => hwbridge/h_bridge}/bridge_hbridge.hpp | 0 .../h_bridge}/bridge_hbridge_drv8801.hpp | 0 inc/example.hpp | 7 -- inc/param_service.hpp | 92 ------------------ scripts/test.sh | 5 +- src/example.cpp | 5 - test/CMakeLists.txt | 23 ----- test/brushed_control_loop_test.cpp | 1 - test/brushless_6step_control_loop_test.cpp | 1 - test/brushless_foc_control_loop_test.cpp | 1 - test/{ => mocks}/mock_bridge_3phase.hpp | 0 test/{ => mocks}/mock_hal_adc.hpp | 0 test/{ => mocks}/mock_hal_clock.hpp | 0 test/{ => mocks}/mock_hal_serial.hpp | 0 test/{ => mocks}/mock_hal_timer.hpp | 0 test/test.cpp | 4 - {src => util/pid}/pid.cpp | 0 {inc => util/pid}/pid.hpp | 0 39 files changed, 72 insertions(+), 176 deletions(-) rename {src => control_loop/bldc}/brushless_6step_control_loop.cpp (99%) rename {inc => control_loop/bldc}/brushless_6step_control_loop.hpp (99%) rename {src => control_loop/bldc}/brushless_foc_control_loop.cpp (100%) rename {inc => control_loop/bldc}/brushless_foc_control_loop.hpp (100%) rename {src => control_loop/bldc}/math_foc.cpp (100%) rename {inc => control_loop/bldc}/math_foc.hpp (100%) rename {src => control_loop/brushed}/brushed_control_loop.cpp (99%) rename {inc => control_loop/brushed}/brushed_control_loop.hpp (100%) rename {inc => control_loop}/control_loop.hpp (100%) rename {src => control_loop/stepper}/stepper_control_loop.cpp (99%) rename {inc => control_loop/stepper}/stepper_control_loop.hpp (100%) rename {inc => hal}/hal_adc.hpp (100%) rename {inc => hal}/hal_clock.hpp (100%) rename {inc => hal}/hal_common.hpp (100%) rename {inc => hal}/hal_gpio.hpp (100%) rename {inc => hal}/hal_serial.hpp (100%) rename {inc => hal}/hal_timer.hpp (100%) rename {inc => hwbridge/3phase}/bridge_3phase.hpp (100%) rename {inc => hwbridge/3phase}/bridge_3phase_drv8323.hpp (100%) rename {inc => hwbridge/h_bridge}/bridge_hbridge.hpp (100%) rename {inc => hwbridge/h_bridge}/bridge_hbridge_drv8801.hpp (100%) delete mode 100644 inc/example.hpp delete mode 100644 inc/param_service.hpp delete mode 100644 src/example.cpp rename test/{ => mocks}/mock_bridge_3phase.hpp (100%) rename test/{ => mocks}/mock_hal_adc.hpp (100%) rename test/{ => mocks}/mock_hal_clock.hpp (100%) rename test/{ => mocks}/mock_hal_serial.hpp (100%) rename test/{ => mocks}/mock_hal_timer.hpp (100%) delete mode 100644 test/test.cpp rename {src => util/pid}/pid.cpp (100%) rename {inc => util/pid}/pid.hpp (100%) diff --git a/.gitignore b/.gitignore index ee6d40f..b7e7be8 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,7 @@ *.out *.app -build/ \ No newline at end of file +build/ + +# Bazel files +bazel-* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 82a5b38..d8210bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,34 +1,63 @@ -cmake_minimum_required(VERSION 3.8) - -set(This MAINS) -set(BINARY ${CMAKE_PROJECT_NAME}) - -project(${This} C CXX) - -set(CMAKE_C_STANDARD 99) -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) -set(BUILD_GMOCK ON) - -# enable Werror -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") - -enable_testing() - -# add the googletest subdirectory, located in the lib folder tree that is one level above this one -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/googletest ${CMAKE_CURRENT_BINARY_DIR}/googletest) - -# set the HEADERS as everything in the inc folder, as well as test *.hpp files -file(GLOB HEADERS "inc/*.hpp") - -# set the SOURCES as everything in the src folder -file(GLOB SOURCES "src/*.cpp") - -# add_executable(${BINARY}_run ${SOURCES}) -add_library(${This} STATIC ${SOURCES} ${HEADERS}) - -add_subdirectory(test) - -# To find the tests, this is rquired -include_directories(inc) -include_directories(src) \ No newline at end of file +cmake_minimum_required(VERSION 3.14) + +set(This tests) + +set(BINARY ${CMAKE_PROJECT_NAME}) + +project(${This} C CXX) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_C_STANDARD 99) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) +set(BUILD_GMOCK ON) + +#enable Werror, Wextra, Wall, pedantic, and pedantic-errors +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wextra -Wall") + +# enable testing +enable_testing() + +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/googletest ${CMAKE_CURRENT_BINARY_DIR}/googletest) + +# Glob recurse the headers inside hal/ +file(GLOB_RECURSE HAL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/hal/*.hpp) + +# Glob recurse the headers inside hwbridge/ +file(GLOB_RECURSE HWBRIDGE_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/hwbridge/*.hpp) + +# Glob recurse the headers inside control_loop/ +file(GLOB_RECURSE CONTROL_LOOP_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/control_loop/*.hpp) +# Glob recurse the sources inside control_loop/ +file(GLOB_RECURSE CONTROL_LOOP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/control_loop/*.cpp) + +# Glob recurse the headers inside utils/ +file(GLOB_RECURSE UTILS_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/util/*.hpp) +# Glob recurse the sources inside utils/ +file(GLOB_RECURSE UTILS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/util/*.cpp) + +# Add a compiler definition -DUNITTEST to the compiler +add_definitions(-DUNIT_TEST) + +# Glob recurse the headers inside test/ +file(GLOB_RECURSE TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/test/*.hpp) +# Glob recurse the sources inside test/ +file(GLOB_RECURSE TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/test/*.cpp) + +# Include the headers in the above paths and also do so recursively as there is a hierarchy +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hal) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hwbridge/3phase) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hwbridge/h_bridge) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/control_loop) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/control_loop/brushed) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/control_loop/bldc) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/control_loop/stepper) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/util/pid) + +# Mocks +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test/mocks) + +# Add an executable with the above sources +add_executable(${This} ${CONTROL_LOOP_SOURCES} ${UTILS_SOURCES} ${TEST_SOURCES}) + +# Link the executable with the GoogleTest libraries +target_link_libraries(${This} PUBLIC gtest gtest_main gmock) diff --git a/src/brushless_6step_control_loop.cpp b/control_loop/bldc/brushless_6step_control_loop.cpp similarity index 99% rename from src/brushless_6step_control_loop.cpp rename to control_loop/bldc/brushless_6step_control_loop.cpp index c8eeaa7..809d6e1 100644 --- a/src/brushless_6step_control_loop.cpp +++ b/control_loop/bldc/brushless_6step_control_loop.cpp @@ -2,8 +2,6 @@ #include -#include "param_service.hpp" - namespace control_loop { Brushless6StepControlLoop::Brushless6StepControlLoopState Brushless6StepControlLoop::get_desired_state(utime_t current_time_us, diff --git a/inc/brushless_6step_control_loop.hpp b/control_loop/bldc/brushless_6step_control_loop.hpp similarity index 99% rename from inc/brushless_6step_control_loop.hpp rename to control_loop/bldc/brushless_6step_control_loop.hpp index c6653f5..ea3f26e 100644 --- a/inc/brushless_6step_control_loop.hpp +++ b/control_loop/bldc/brushless_6step_control_loop.hpp @@ -35,7 +35,7 @@ class Brushless6StepControlLoop : public ControlLoop { CommutationSignal u; CommutationSignal v; CommutationSignal w; - }; + } phase_commutation_signals; } commutation_step_t; static constexpr uint8_t num_commutation_steps = 6; diff --git a/src/brushless_foc_control_loop.cpp b/control_loop/bldc/brushless_foc_control_loop.cpp similarity index 100% rename from src/brushless_foc_control_loop.cpp rename to control_loop/bldc/brushless_foc_control_loop.cpp diff --git a/inc/brushless_foc_control_loop.hpp b/control_loop/bldc/brushless_foc_control_loop.hpp similarity index 100% rename from inc/brushless_foc_control_loop.hpp rename to control_loop/bldc/brushless_foc_control_loop.hpp diff --git a/src/math_foc.cpp b/control_loop/bldc/math_foc.cpp similarity index 100% rename from src/math_foc.cpp rename to control_loop/bldc/math_foc.cpp diff --git a/inc/math_foc.hpp b/control_loop/bldc/math_foc.hpp similarity index 100% rename from inc/math_foc.hpp rename to control_loop/bldc/math_foc.hpp diff --git a/src/brushed_control_loop.cpp b/control_loop/brushed/brushed_control_loop.cpp similarity index 99% rename from src/brushed_control_loop.cpp rename to control_loop/brushed/brushed_control_loop.cpp index 8548770..c77b26b 100644 --- a/src/brushed_control_loop.cpp +++ b/control_loop/brushed/brushed_control_loop.cpp @@ -1,7 +1,5 @@ #include "brushed_control_loop.hpp" -#include "param_service.hpp" - namespace control_loop { BrushedControlLoop::BrushedControlLoop() {} diff --git a/inc/brushed_control_loop.hpp b/control_loop/brushed/brushed_control_loop.hpp similarity index 100% rename from inc/brushed_control_loop.hpp rename to control_loop/brushed/brushed_control_loop.hpp diff --git a/inc/control_loop.hpp b/control_loop/control_loop.hpp similarity index 100% rename from inc/control_loop.hpp rename to control_loop/control_loop.hpp diff --git a/src/stepper_control_loop.cpp b/control_loop/stepper/stepper_control_loop.cpp similarity index 99% rename from src/stepper_control_loop.cpp rename to control_loop/stepper/stepper_control_loop.cpp index f5bb669..c9318ba 100644 --- a/src/stepper_control_loop.cpp +++ b/control_loop/stepper/stepper_control_loop.cpp @@ -1,7 +1,6 @@ #include "stepper_control_loop.hpp" #include "math.h" -#include "param_service.hpp" namespace control_loop { diff --git a/inc/stepper_control_loop.hpp b/control_loop/stepper/stepper_control_loop.hpp similarity index 100% rename from inc/stepper_control_loop.hpp rename to control_loop/stepper/stepper_control_loop.hpp diff --git a/inc/hal_adc.hpp b/hal/hal_adc.hpp similarity index 100% rename from inc/hal_adc.hpp rename to hal/hal_adc.hpp diff --git a/inc/hal_clock.hpp b/hal/hal_clock.hpp similarity index 100% rename from inc/hal_clock.hpp rename to hal/hal_clock.hpp diff --git a/inc/hal_common.hpp b/hal/hal_common.hpp similarity index 100% rename from inc/hal_common.hpp rename to hal/hal_common.hpp diff --git a/inc/hal_gpio.hpp b/hal/hal_gpio.hpp similarity index 100% rename from inc/hal_gpio.hpp rename to hal/hal_gpio.hpp diff --git a/inc/hal_serial.hpp b/hal/hal_serial.hpp similarity index 100% rename from inc/hal_serial.hpp rename to hal/hal_serial.hpp diff --git a/inc/hal_timer.hpp b/hal/hal_timer.hpp similarity index 100% rename from inc/hal_timer.hpp rename to hal/hal_timer.hpp diff --git a/inc/bridge_3phase.hpp b/hwbridge/3phase/bridge_3phase.hpp similarity index 100% rename from inc/bridge_3phase.hpp rename to hwbridge/3phase/bridge_3phase.hpp diff --git a/inc/bridge_3phase_drv8323.hpp b/hwbridge/3phase/bridge_3phase_drv8323.hpp similarity index 100% rename from inc/bridge_3phase_drv8323.hpp rename to hwbridge/3phase/bridge_3phase_drv8323.hpp diff --git a/inc/bridge_hbridge.hpp b/hwbridge/h_bridge/bridge_hbridge.hpp similarity index 100% rename from inc/bridge_hbridge.hpp rename to hwbridge/h_bridge/bridge_hbridge.hpp diff --git a/inc/bridge_hbridge_drv8801.hpp b/hwbridge/h_bridge/bridge_hbridge_drv8801.hpp similarity index 100% rename from inc/bridge_hbridge_drv8801.hpp rename to hwbridge/h_bridge/bridge_hbridge_drv8801.hpp diff --git a/inc/example.hpp b/inc/example.hpp deleted file mode 100644 index 0434274..0000000 --- a/inc/example.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef EXAMPLE_HPP -#define EXAMPLE_HPP -#include - -int32_t test_add_func(int32_t a, int32_t b); - -#endif diff --git a/inc/param_service.hpp b/inc/param_service.hpp deleted file mode 100644 index 209f456..0000000 --- a/inc/param_service.hpp +++ /dev/null @@ -1,92 +0,0 @@ -#ifdef PARAM_SERVER_HPP -#define PARAM_SERVER_HPP -#include - -#include "hal_common.hpp" - -namespace param_service { - -class ParamServer { - public: - static ParamServer& getInstance() { - static ParamServer instance; - return instance; - } - - struct comm_message_ids { - const uint16_t arm_disarm_msg = 0x0001; - const uint16_t set_target_speed_msg = 0x0002; - }; - - // Compile time parameters - struct compile_params { - const bool enable_usb_interface = false; - const bool enable_can_interface = false; - const bool enable_temperature_sense_monitoring = true; - const float overtemperature_threshold_deg_c = 100; - // Analog values - const float analog_reference_voltage = 3.3; - const uint8_t analog_resolution_bits = 10; - const uint8_t temp_sensor_adc_channel = 0; - const uint8_t bus_voltage_adc_channel = 1; - const uint8_t motor_current_adc_channel = 2; - // Thermistor parameters (TODO: make these reflect reality) - const float temp_sensor_volt_offset = 1; - const float temp_sensor_volt_per_deg = 1; - // Bus voltage parameters - const float max_permissable_bus_voltage = 40; - const float bus_voltage_dividor_resistor_1_ohms = 1000; - const float bus_voltage_dividor_resistor_2_ohms = 1000; - // Current sense resistor value for PCBA - const float max_permissable_motor_current_amps = 60; - const float current_sense_resistor_ohms = 0.1; - const float current_sense_gain = 100; - // Control Loop Parameters - const float control_loop_period_s = 0.001; - const float control_loop_deadband = 1; - const float control_loop_max_output = 100; - const float control_loop_min_output = -100; - // H Bridge parameters - const float h_bridge_dead_time_us = 10; - const bool h_bridge_brake_mode_enabled = false; - // Stepper motor parameters - const float stepper_motor_max_speed_steps_per_s = 1000; - const float stepper_motor_max_current_amps = 1.5; - const bool stepper_motor_simple_switcher_enabled = true; - const bool stepper_motor_disable_current_pid = - true; // TODO: remove this. this will use a liner map from the current scales to the pwm duty cycle - const float stepper_motor_current_to_pwm_duty_cycle_slope = 50; - - // 3 phase motor parameters - const utime_t sensorless_phase_commutation_step_time_us = 4000; - const utime_t sensorless_phase_motor_startup_sequence_time_us = sensorless_phase_commutation_step_time_us * 100; - const float sensorless_speed_deadband_scale = 0.3f; // 10% of max speed, lower and we cannot detect back emf - const float sensorless_startup_speed = 1.0f; - const float sensored_speed_deadband_scale = 0.1f; // 10% of max speed, lower and actuating the motor does not make sense - const bool sensorless_bemf_enable_backemf_skip_overrun = false; - const bool log_zero_crossing_in_sensored_mode = false; - const utime_t bemf_zero_crossing_timeout_us = 1875; // time at which we do not detect a ZC after a commutation step - - // FOC parameters - const uint8_t num_hall_updates_to_start = 10; - const utime_t foc_start_timeout_period = 1000000; // 1 second - const float open_loop_velocity_rad_s = 2000.0f; - - // Observer parameters - const float observer_angle_error_kp = 10.0f; - }; - - compile_params compile_params; - - // Get message ids - // Done this way to allow for dynamic message ids - comm_message_ids get_message_ids() { return message_ids; } - - private: - ParamServer(); - comm_message_ids message_ids; -}; - -} // namespace param_service - -#endif diff --git a/scripts/test.sh b/scripts/test.sh index 3f1a0b8..3370b14 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,6 +2,9 @@ set -exo pipefail +# make a build directory if it doesn't exist +mkdir -p build + # Change to the build directory cd build @@ -25,5 +28,5 @@ then else # If no argument is specified, build and run the tests cmake .. -DCMAKE_BUILD_TYPE=Debug -G "Unix Makefiles" - make all && ./test/tests + make all && ./tests fi diff --git a/src/example.cpp b/src/example.cpp deleted file mode 100644 index 5563875..0000000 --- a/src/example.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "example.hpp" - -#include - -int32_t test_add_func(int32_t a, int32_t b) { return a + b; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dc331a6..e69de29 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,23 +0,0 @@ -cmake_minimum_required(VERSION 3.8) - -set(This tests) - -# set the test sources to everything in this folder that is cpp -file(GLOB SOURCES "*.cpp") -# add the main project sources in ../src to the test sources -file(GLOB MAIN_SOURCES "../src/*.cpp") - -# include the headers from the main project, which is located at "../inc/" -include_directories(../inc) -include_directories((../test/)) - -add_test( - NAME ${This} - COMMAND ${This} -) - -# add compiler definition for UNIT_TEST -add_definitions(-DUNIT_TEST) - -add_executable(${This} ${SOURCES} ${MAIN_SOURCES}) -target_link_libraries(${This} PUBLIC gtest_main gtest gmock) diff --git a/test/brushed_control_loop_test.cpp b/test/brushed_control_loop_test.cpp index 07aaeac..748fcf4 100644 --- a/test/brushed_control_loop_test.cpp +++ b/test/brushed_control_loop_test.cpp @@ -3,7 +3,6 @@ #include "brushed_control_loop.hpp" #include "gmock/gmock.h" -#include "param_service.hpp" namespace control_loop { diff --git a/test/brushless_6step_control_loop_test.cpp b/test/brushless_6step_control_loop_test.cpp index 0ef6332..1fa3945 100644 --- a/test/brushless_6step_control_loop_test.cpp +++ b/test/brushless_6step_control_loop_test.cpp @@ -3,7 +3,6 @@ #include "mock_bridge_3phase.hpp" #include "mock_hal_adc.hpp" #include "mock_hal_clock.hpp" -#include "param_service.hpp" namespace control_loop { using namespace ::testing; diff --git a/test/brushless_foc_control_loop_test.cpp b/test/brushless_foc_control_loop_test.cpp index c57c679..f240ea1 100644 --- a/test/brushless_foc_control_loop_test.cpp +++ b/test/brushless_foc_control_loop_test.cpp @@ -8,7 +8,6 @@ #include "mock_hal_adc.hpp" #include "mock_hal_clock.hpp" #include "mock_hal_timer.hpp" -#include "param_service.hpp" namespace control_loop { using namespace ::testing; diff --git a/test/mock_bridge_3phase.hpp b/test/mocks/mock_bridge_3phase.hpp similarity index 100% rename from test/mock_bridge_3phase.hpp rename to test/mocks/mock_bridge_3phase.hpp diff --git a/test/mock_hal_adc.hpp b/test/mocks/mock_hal_adc.hpp similarity index 100% rename from test/mock_hal_adc.hpp rename to test/mocks/mock_hal_adc.hpp diff --git a/test/mock_hal_clock.hpp b/test/mocks/mock_hal_clock.hpp similarity index 100% rename from test/mock_hal_clock.hpp rename to test/mocks/mock_hal_clock.hpp diff --git a/test/mock_hal_serial.hpp b/test/mocks/mock_hal_serial.hpp similarity index 100% rename from test/mock_hal_serial.hpp rename to test/mocks/mock_hal_serial.hpp diff --git a/test/mock_hal_timer.hpp b/test/mocks/mock_hal_timer.hpp similarity index 100% rename from test/mock_hal_timer.hpp rename to test/mocks/mock_hal_timer.hpp diff --git a/test/test.cpp b/test/test.cpp deleted file mode 100644 index 2f207c4..0000000 --- a/test/test.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "example.hpp" -#include "gtest/gtest.h" - -TEST(ExampleTest, test_add) { EXPECT_EQ(2, test_add_func(1, 1)); } \ No newline at end of file diff --git a/src/pid.cpp b/util/pid/pid.cpp similarity index 100% rename from src/pid.cpp rename to util/pid/pid.cpp diff --git a/inc/pid.hpp b/util/pid/pid.hpp similarity index 100% rename from inc/pid.hpp rename to util/pid/pid.hpp