diff --git a/src/App/main.c b/src/App/main.c index 566eed8..966b243 100644 --- a/src/App/main.c +++ b/src/App/main.c @@ -11,6 +11,9 @@ /* Includes ------------------------------------------------------------------*/ #include "main.h" +hall_handle_t hall_handle; + + /* Function ------------------------------------------------------------------*/ /** diff --git a/src/Bsp/board.h b/src/Bsp/board.h index d3eba69..231559e 100644 --- a/src/Bsp/board.h +++ b/src/Bsp/board.h @@ -23,6 +23,8 @@ extern "C" #include "usart.h" #include "gpio.h" +#include "hall.h" + /* Functions ----------------------------------------------------------------*/ extern void board_init(void); diff --git a/src/Foc/hall.c b/src/Foc/hall.c index bd72d3f..88a0080 100644 --- a/src/Foc/hall.c +++ b/src/Foc/hall.c @@ -12,13 +12,13 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) case HALL_STATE_5: if (handle->state == HALL_STATE_4) { - handle->Direction = POSITIVE; - handle->MeasuredElAngle = handle->PhaseShift; + handle->direction = POSITIVE; + handle->measure_elec_angle = handle->phase_shift; } else if (handle->state == HALL_STATE_1) { - handle->Direction = NEGATIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift + S16_60_PHASE_SHIFT); + handle->direction = NEGATIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift + S16_60_PHASE_SHIFT); } else { @@ -28,13 +28,13 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) case HALL_STATE_1: if (handle->state == HALL_STATE_5) { - handle->Direction = POSITIVE; - handle->MeasuredElAngle = handle->PhaseShift + S16_60_PHASE_SHIFT; + handle->direction = POSITIVE; + handle->measure_elec_angle = handle->phase_shift + S16_60_PHASE_SHIFT; } else if (handle->state == HALL_STATE_3) { - handle->Direction = NEGATIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift + S16_120_PHASE_SHIFT); + handle->direction = NEGATIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift + S16_120_PHASE_SHIFT); } else { @@ -44,14 +44,14 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) case HALL_STATE_3: if (handle->state == HALL_STATE_1) { - handle->Direction = POSITIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift + S16_120_PHASE_SHIFT); + handle->direction = POSITIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift + S16_120_PHASE_SHIFT); } else if (handle->state == HALL_STATE_2) { - handle->Direction = NEGATIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift + S16_120_PHASE_SHIFT + - S16_60_PHASE_SHIFT); + handle->direction = NEGATIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift + S16_120_PHASE_SHIFT + + S16_60_PHASE_SHIFT); } else { @@ -62,13 +62,13 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) case HALL_STATE_2: if (handle->state == HALL_STATE_3) { - handle->Direction = POSITIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift + S16_120_PHASE_SHIFT + S16_60_PHASE_SHIFT); + handle->direction = POSITIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift + S16_120_PHASE_SHIFT + S16_60_PHASE_SHIFT); } else if (handle->state == HALL_STATE_6) { - handle->Direction = NEGATIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift - S16_120_PHASE_SHIFT); + handle->direction = NEGATIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift - S16_120_PHASE_SHIFT); } else { @@ -78,13 +78,13 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) case HALL_STATE_6: if (handle->state == HALL_STATE_2) { - handle->Direction = POSITIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift - S16_120_PHASE_SHIFT); + handle->direction = POSITIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift - S16_120_PHASE_SHIFT); } else if (handle->state == HALL_STATE_4) { - handle->Direction = NEGATIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift - S16_60_PHASE_SHIFT); + handle->direction = NEGATIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift - S16_60_PHASE_SHIFT); } else { @@ -94,13 +94,13 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) case HALL_STATE_4: if (handle->state == HALL_STATE_6) { - handle->Direction = POSITIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift - S16_60_PHASE_SHIFT); + handle->direction = POSITIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift - S16_60_PHASE_SHIFT); } else if (handle->state == HALL_STATE_5) { - handle->Direction = NEGATIVE; - handle->MeasuredElAngle = (int16_t)(handle->PhaseShift); + handle->direction = NEGATIVE; + handle->measure_elec_angle = (int16_t)(handle->phase_shift); } else { @@ -109,7 +109,7 @@ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) default: /* Bad hall sensor configutarion so update the speed reliability */ - handle->SensorIsReliable = 0; + // handle->SensorIsReliable = 0; break; } diff --git a/src/Foc/hall.h b/src/Foc/hall.h index c3524f5..b9613a8 100644 --- a/src/Foc/hall.h +++ b/src/Foc/hall.h @@ -30,9 +30,6 @@ enum typedef struct { - // ||| - // ||| - // ||| uint8_t state; // 霍尔状态真值表 int16_t phase_shift; // 同步电角度 diff --git a/src/Foc/svpwm.c b/src/Foc/svpwm.c index 42dc1b4..b5e404f 100644 --- a/src/Foc/svpwm.c +++ b/src/Foc/svpwm.c @@ -1,10 +1,11 @@ #include "svpwm.h" -void svpwm(int16_t alpha, int16_t beta) + +void svpwm(foc_handle_t *handle, int16_t alpha, int16_t beta) { int32_t wX, wY, wZ, wUAlpha, wUBeta, wTimePhA, wTimePhB, wTimePhC; - wUAlpha = alpha * (int32_t)pHandle->hT_Sqrt3; - wUBeta = -(beta * (int32_t)(pHandle->PWMperiod)) * 2; + wUAlpha = alpha * (int32_t)handle->hT_Sqrt3; + wUBeta = -(beta * (int32_t)(handle->PWMperiod)) * 2; wX = wUBeta; wY = (wUBeta + wUAlpha) / 2; @@ -15,7 +16,7 @@ void svpwm(int16_t alpha, int16_t beta) { if (wZ < 0) { - pHandle->Sector = SECTOR_5; + handle->sector = SECTOR_5; wTimePhA = (int32_t)(pHandle->PWMperiod) / 4 + ((wY - wZ) / (int32_t)262144); wTimePhB = wTimePhA + wZ / 131072; wTimePhC = wTimePhA - wY / 131072; @@ -26,7 +27,7 @@ void svpwm(int16_t alpha, int16_t beta) else /* wZ >= 0 */ if (wX <= 0) { - pHandle->Sector = SECTOR_4; + handle->sector = SECTOR_4; wTimePhA = (int32_t)(pHandle->PWMperiod) / 4 + ((wX - wZ) / (int32_t)262144); wTimePhB = wTimePhA + wZ / 131072; wTimePhC = wTimePhB - wX / 131072; @@ -36,7 +37,7 @@ void svpwm(int16_t alpha, int16_t beta) } else /* wX > 0 */ { - pHandle->Sector = SECTOR_3; + handle->sector = SECTOR_3; wTimePhA = (int32_t)(pHandle->PWMperiod) / 4 + ((wY - wX) / (int32_t)262144); wTimePhC = wTimePhA - wY / 131072; wTimePhB = wTimePhC + wX / 131072; @@ -49,7 +50,7 @@ void svpwm(int16_t alpha, int16_t beta) { if (wZ >= 0) { - pHandle->Sector = SECTOR_2; + handle->sector = SECTOR_2; wTimePhA = (int32_t)(pHandle->PWMperiod) / 4 + ((wY - wZ) / (int32_t)262144); wTimePhB = wTimePhA + wZ / 131072; wTimePhC = wTimePhA - wY / 131072; @@ -60,7 +61,7 @@ void svpwm(int16_t alpha, int16_t beta) else /* wZ < 0 */ if (wX <= 0) { - pHandle->Sector = SECTOR_6; + handle->sector = SECTOR_6; wTimePhA = (int32_t)(pHandle->PWMperiod) / 4 + ((wY - wX) / (int32_t)262144); wTimePhC = wTimePhA - wY / 131072; wTimePhB = wTimePhC + wX / 131072; @@ -70,7 +71,7 @@ void svpwm(int16_t alpha, int16_t beta) } else /* wX > 0 */ { - pHandle->Sector = SECTOR_1; + handle->sector = SECTOR_1; wTimePhA = (int32_t)(pHandle->PWMperiod) / 4 + ((wX - wZ) / (int32_t)262144); wTimePhB = wTimePhA + wZ / 131072; wTimePhC = wTimePhB - wX / 131072; @@ -80,8 +81,7 @@ void svpwm(int16_t alpha, int16_t beta) } } - pHandle->CntPhA = (uint16_t)wTimePhA; - pHandle->CntPhB = (uint16_t)wTimePhB; - pHandle->CntPhC = (uint16_t)wTimePhC; - + handle->pwmA = (uint16_t)wTimePhA; + handle->pwmA = (uint16_t)wTimePhB; + handle->pwmA = (uint16_t)wTimePhC; } \ No newline at end of file diff --git a/src/Foc/svpwm.h b/src/Foc/svpwm.h index 13dd56c..bae512c 100644 --- a/src/Foc/svpwm.h +++ b/src/Foc/svpwm.h @@ -15,4 +15,14 @@ #define SECTOR_3 2u #define SECTOR_4 3u #define SECTOR_5 4u -#define SECTOR_6 5u \ No newline at end of file +#define SECTOR_6 5u + +typedef struct +{ + uint8_t sector; // 扇区 + + uint16_t pwmA; + uint16_t pwmB; + uint16_t pwmC; + +} foc_handle_t; \ No newline at end of file