From 6083dba2f657bf13b57f11d154c2226622482a5b Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Tue, 19 Sep 2017 18:08:45 -0700 Subject: [PATCH] C++ top level passthrough works --- Inc/version.h | 10 ------- Makefile | 7 ++++- MotorControl/axis.cpp | 23 ++++++++++++++++ MotorControl/axis.hpp | 49 +++++++++++++++++++++++++++++++++ MotorControl/axis_c_interface.h | 14 ++++++++++ MotorControl/low_level.h | 8 ++---- Src/freertos.c | 6 ++-- 7 files changed, 98 insertions(+), 19 deletions(-) delete mode 100644 Inc/version.h create mode 100644 MotorControl/axis.cpp create mode 100644 MotorControl/axis.hpp create mode 100644 MotorControl/axis_c_interface.h diff --git a/Inc/version.h b/Inc/version.h deleted file mode 100644 index b5b3e21d..00000000 --- a/Inc/version.h +++ /dev/null @@ -1,10 +0,0 @@ -/** - ****************************************************************************** - * @file : version.h - * @brief : This file implements printf functionality - ****************************************************************************** -*/ - -#define ODRIVE_FW_VERSION_MAJOR 0 -#define ODRIVE_FW_VERSION_MINOR 1 -#define ODRIVE_FW_VERSION_PATCH 0 diff --git a/Makefile b/Makefile index 081f17b8..8954a9a3 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,9 @@ C_SOURCES = \ Src/usbd_cdc_if.c \ Src/syscalls.c \ MotorControl/utils.c \ - MotorControl/low_level.c + MotorControl/low_level.c +CPP_SOURCES = \ + MotorControl/axis.cpp ASM_SOURCES = \ startup/startup_stm32f405xx.s @@ -155,6 +157,9 @@ vpath %.s $(sort $(dir $(ASM_SOURCES))) $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) @$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ +$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR) + @$(CXX) -c $(CXXFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cpp=.lst)) $< -o $@ + $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR) @$(AS) -c $(CFLAGS) $< -o $@ diff --git a/MotorControl/axis.cpp b/MotorControl/axis.cpp new file mode 100644 index 00000000..fa7dfd49 --- /dev/null +++ b/MotorControl/axis.cpp @@ -0,0 +1,23 @@ + +#include "axis.hpp" + +//TODO: goal of refactor is to kick this out completely +extern "C" { +#include "low_level.h" +} + +// C interface +extern "C" { +void axis_thread_entry(void const* temp_motor_ptr) { + Motor_t* motor = (Motor_t*)temp_motor_ptr; + Axis axis(motor); + axis.StateMachineLoop(); +} +} // extern "C" + +Axis::Axis(Motor_t* legacy_motor_ref) + : legacy_motor_ref_(legacy_motor_ref) {} + +void Axis::StateMachineLoop() { + motor_thread(legacy_motor_ref_); +} \ No newline at end of file diff --git a/MotorControl/axis.hpp b/MotorControl/axis.hpp new file mode 100644 index 00000000..2108289c --- /dev/null +++ b/MotorControl/axis.hpp @@ -0,0 +1,49 @@ +#ifndef __AXIS_HPP +#define __AXIS_HPP + +//TODO: goal of refactor is to kick this out completely +extern "C" { +#include "low_level.h" +} + +//Outside axis: + //command handler + //callback dispatch + + +class Axis { +public: + //thread/os/system management + //timing log + //thread id + //etc. + //state machine + //control mode + //control_en/calib_ok + //error state + //motor + //current controller + //contains rotor phase logic + //motor level calibration routines + //low_level (implementation specifics) + //DRV driver + //adc callback handling + //pwm queueing + //rotor estimator + //kick out rotor phase logic + //pos/vel controller + //step/dir handler + + // Object operation requires ptr to legacy object for now, TODO: get rid of this dep + Axis(Motor_t* legacy_motor_ref); + + // Infinite loop that does calibration and enters main control loop as appropriate + void StateMachineLoop(); + + bool enable_control_; + bool do_calibration_; + + Motor_t* legacy_motor_ref_; +}; + +#endif /* __AXIS_HPP */ diff --git a/MotorControl/axis_c_interface.h b/MotorControl/axis_c_interface.h new file mode 100644 index 00000000..f891e19b --- /dev/null +++ b/MotorControl/axis_c_interface.h @@ -0,0 +1,14 @@ +#ifndef __AXIS_C_INTERFACE_H +#define __AXIS_C_INTERFACE_H + +#ifdef __cplusplus +extern "C" { +#endif + +void axis_thread_entry(void const * temp_motor_ptr); + +#ifdef __cplusplus +} +#endif + +#endif /* __AXIS_C_INTERFACE_H */ diff --git a/MotorControl/low_level.h b/MotorControl/low_level.h index 5f0f7f80..2b0d6501 100644 --- a/MotorControl/low_level.h +++ b/MotorControl/low_level.h @@ -156,16 +156,14 @@ void set_pos_setpoint(Motor_t* motor, float pos_setpoint, float vel_feed_forward void set_vel_setpoint(Motor_t* motor, float vel_setpoint, float current_feed_forward); void set_current_setpoint(Motor_t* motor, float current_setpoint); -void safe_assert(int arg); -void init_motor_control(); void step_cb(uint16_t GPIO_Pin); void pwm_trig_adc_cb(ADC_HandleTypeDef* hadc, bool injected); void vbus_sense_adc_cb(ADC_HandleTypeDef* hadc, bool injected); -//@TODO move motor thread to high level file -void motor_thread(void const * argument); +void safe_assert(int arg); +void init_motor_control(); -//@TODO move cmd parsing to high level file +void motor_thread(void const * argument); void motor_parse_cmd(uint8_t* buffer, int len); #endif //__LOW_LEVEL_H diff --git a/Src/freertos.c b/Src/freertos.c index c445cdd6..b5194ea9 100644 --- a/Src/freertos.c +++ b/Src/freertos.c @@ -54,7 +54,7 @@ /* USER CODE BEGIN Includes */ #include "freertos_vars.h" #include "low_level.h" -#include "version.h" +#include "axis_c_interface.h" /* USER CODE END Includes */ /* Variables -----------------------------------------------------------------*/ @@ -125,8 +125,8 @@ void StartDefaultTask(void const * argument) init_motor_control(); // Start motor threads - osThreadDef(task_motor_0, motor_thread, osPriorityHigh+1, 0, 512); - osThreadDef(task_motor_1, motor_thread, osPriorityHigh, 0, 512); + osThreadDef(task_motor_0, axis_thread_entry, osPriorityHigh+1, 0, 512); + osThreadDef(task_motor_1, axis_thread_entry, osPriorityHigh, 0, 512); thread_motor_0 = osThreadCreate(osThread(task_motor_0), &motors[0]); thread_motor_1 = osThreadCreate(osThread(task_motor_1), &motors[1]);