221 lines
8.4 KiB
C
221 lines
8.4 KiB
C
|
#ifndef MOTOR_CONTROL_TASK_H__
|
|||
|
#define MOTOR_CONTROL_TASK_H__
|
|||
|
|
|||
|
#include <appConfig.h>
|
|||
|
#include <softTimerTask.h>
|
|||
|
#include <systick_device.h>
|
|||
|
#include <taskManager.h>
|
|||
|
#include <gpio_device.h>
|
|||
|
#include <lin_device.h>
|
|||
|
|
|||
|
//#define Time1_timing 8000U //500*16 :500us
|
|||
|
#define ADC_period 20000U*48U/Time1_timing
|
|||
|
|
|||
|
#if control_type == Divide_2
|
|||
|
|
|||
|
#define VAMP_Cnt 8
|
|||
|
#define ADC_period 20000U*48U/Time1_timing
|
|||
|
|
|||
|
#define Downpwm_APhasePos_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x08U //PA3 ON(Upper bridge arm)
|
|||
|
#define Downpwm_APhasePos_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x08U //PA3 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_APhasePos_Low_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x04U //PA2 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_APhasePos_Low_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x04U //PA2 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Downpwm_APhaseNeg_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x40U //PA6 ON(Upper bridge arm)
|
|||
|
#define Downpwm_APhaseNeg_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x40U //PA6 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_APhaseNeg_Low_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x20U //PA5 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_APhaseNeg_Low_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x20U //PA5 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Downpwm_BPhasePos_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATASET |= 0x04U //PB2 ON(Upper bridge arm)
|
|||
|
#define Downpwm_BPhasePos_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATACLR |= 0x04U //PB2 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_BPhasePos_Low_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATACLR |= 0x02U //PB1 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_BPhasePos_Low_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATASET |= 0x02U //PB1 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Downpwm_BPhaseNeg_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x10U//PA4 ON(Upper bridge arm)
|
|||
|
#define Downpwm_BPhaseNeg_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x10U //PA4 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_BPhaseNeg_Low_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATACLR |= 0x01U //PB0 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_BPhaseNeg_Low_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATASET |= 0x01U //PB0 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Abridge_OFF do{ Downpwm_APhasePos_High_OFF;Downpwm_APhasePos_Low_OFF;Downpwm_APhaseNeg_High_OFF;Downpwm_APhaseNeg_Low_OFF;}while(0)
|
|||
|
#define Bbridge_OFF do{ Downpwm_BPhasePos_High_OFF;Downpwm_BPhasePos_Low_OFF;Downpwm_BPhaseNeg_High_OFF;Downpwm_BPhaseNeg_Low_OFF;}while(0)
|
|||
|
#define Twobridge_OFF do{ Abridge_OFF;Bbridge_OFF;}while(0)
|
|||
|
|
|||
|
#elif control_type == Divide_6
|
|||
|
|
|||
|
#define VAMP_Cnt 24U
|
|||
|
#define PWMAUX_Period 100U*16U
|
|||
|
|
|||
|
#define Downpwm_APhasePos_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x08U //PA3 ON(Upper bridge arm)
|
|||
|
#define Downpwm_APhasePos_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x08U //PA3 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_APhasePos_Low_ON do{PWMAUX_SetMatchValue(PWMAUX_CH5, 0, PWMAUX_Period);}while(0) //PA2 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_APhasePos_Low_OFF do{PWMAUX_SetMatchValue(PWMAUX_CH5, 0, 0);}while(0) //PA2 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Downpwm_APhaseNeg_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x40U //PA6 ON(Upper bridge arm)
|
|||
|
#define Downpwm_APhaseNeg_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x40U //PA6 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_APhaseNeg_Low_ON do{PWMAUX_SetMatchValue(PWMAUX_CH0, 0, PWMAUX_Period);}while(0) //PA5 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_APhaseNeg_Low_OFF do{PWMAUX_SetMatchValue(PWMAUX_CH0, 0, 0);}while(0) //PA5 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Downpwm_BPhasePos_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATASET |= 0x04U //PB2 ON(Upper bridge arm)
|
|||
|
#define Downpwm_BPhasePos_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOB].DATACLR |= 0x04U //PB2 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_BPhasePos_Low_ON do{PWMAUX_SetMatchValue(PWMAUX_CH2, 0, PWMAUX_Period);}while(0) //PB1 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_BPhasePos_Low_OFF do{PWMAUX_SetMatchValue(PWMAUX_CH2, 0, 0);}while(0) //PB1 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Downpwm_BPhaseNeg_High_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATASET |= 0x10U//PA4 ON(Upper bridge arm)
|
|||
|
#define Downpwm_BPhaseNeg_High_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOA].DATACLR |= 0x10U //PA4 OFF(Upper bridge arm)
|
|||
|
#define Downpwm_BPhaseNeg_Low_ON do{PWMAUX_SetMatchValue(PWMAUX_CH1, 0, PWMAUX_Period);}while(0) //PB0 OFF(Lower bridge arm)
|
|||
|
#define Downpwm_BPhaseNeg_Low_OFF do{PWMAUX_SetMatchValue(PWMAUX_CH1, 0, 0);}while(0) //PB0 ON(Lower bridge arm)
|
|||
|
|
|||
|
#define Abridge_OFF do{ Downpwm_APhasePos_High_OFF;Downpwm_APhasePos_Low_OFF;Downpwm_APhaseNeg_High_OFF;Downpwm_APhaseNeg_Low_OFF;}while(0)
|
|||
|
#define Bbridge_OFF do{ Downpwm_BPhasePos_High_OFF;Downpwm_BPhasePos_Low_OFF;Downpwm_BPhaseNeg_High_OFF;Downpwm_BPhaseNeg_Low_OFF;}while(0)
|
|||
|
#define Twobridge_OFF do{ Abridge_OFF;Bbridge_OFF;}while(0)
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
#define MotorPower_ON GPIO_SFRS->GPIO_SET[GROUP_GPIOC].DATASET |= 0x08U
|
|||
|
#define MotorPower_OFF GPIO_SFRS->GPIO_SET[GROUP_GPIOC].DATACLR |= 0x08U
|
|||
|
|
|||
|
#define VDD3V3EXT_ON do{TRIMHV_SFRS->BORTESTMODE.VDD33EXT_EN = 1U;}while(0)
|
|||
|
#define VDD3V3EXT_OFF do{TRIMHV_SFRS->BORTESTMODE.VDD33EXT_EN = 0U;}while(0)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
typedef enum{
|
|||
|
MotorStop = 0U,
|
|||
|
MotorRun,
|
|||
|
}MotorRunState_t;
|
|||
|
|
|||
|
typedef enum{
|
|||
|
MotorNoOC= 0U,
|
|||
|
MotorOC,
|
|||
|
}MotorOCState_t;
|
|||
|
|
|||
|
typedef enum{
|
|||
|
CoilNomal = 0U,
|
|||
|
CoilOpen,
|
|||
|
CoilShort,
|
|||
|
}CoilState_t;
|
|||
|
|
|||
|
typedef enum{
|
|||
|
Constantstage = 0U,
|
|||
|
Climbstage,
|
|||
|
Slowstage,
|
|||
|
}RunStage_t;
|
|||
|
|
|||
|
typedef struct{
|
|||
|
uint32_t T1timercnt;
|
|||
|
uint16_t SlowSteper;
|
|||
|
uint16_t ClimbSteper;
|
|||
|
uint16_t ClimbTime;
|
|||
|
uint16_t SlowTime;
|
|||
|
uint8_t MM_Step;
|
|||
|
uint32_t stepr;
|
|||
|
uint32_t stepn;
|
|||
|
uint32_t s;
|
|||
|
|
|||
|
}Motorpra_t;
|
|||
|
|
|||
|
typedef struct{
|
|||
|
uint8_t M_ON;
|
|||
|
uint16_t Constantspeed;
|
|||
|
uint16_t Startspeed;
|
|||
|
uint16_t Slowspeed;
|
|||
|
uint16_t LockACoilValtage;
|
|||
|
uint16_t LockBCoilValtage;
|
|||
|
uint8_t ClimbTime;
|
|||
|
uint8_t SlowTime;
|
|||
|
uint16_t Position;
|
|||
|
}M_Re_pra_t;
|
|||
|
|
|||
|
//Response master
|
|||
|
typedef struct{
|
|||
|
int16_t Tchip; //chip temperature 1C
|
|||
|
uint16_t vbat; //vbat mv
|
|||
|
|
|||
|
union{
|
|||
|
struct{
|
|||
|
uint8_t tchipstate :1; //0 <20><><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>
|
|||
|
uint8_t Motor_overcur :1;//0<><30>δ<EFBFBD><CEB4><EFBFBD><EFBFBD> 1:<3A><><EFBFBD><EFBFBD>
|
|||
|
uint8_t Motor_coilAsta :2;//0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>·<EFBFBD><C2B7>2<EFBFBD><32><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|||
|
uint8_t Motor_coilBsta :2;//0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>·<EFBFBD><C2B7>2<EFBFBD><32><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|||
|
uint8_t VBAT_sta :2;//VBAT<41><54>ѹ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD> 2<><32><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
uint8_t Motor_runsta :1;//1<><31><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>0<EFBFBD><30>ֹͣ
|
|||
|
uint8_t Motor_runstage :2;//0<><30><EFBFBD><EFBFBD><EFBFBD>ٽΣ<D7B6>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD>ٽΣ<D7B6>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD>ٽ<D9BD>
|
|||
|
|
|||
|
};
|
|||
|
uint8_t byte[2];
|
|||
|
}state;
|
|||
|
uint32_t MotorErrorState10s;
|
|||
|
uint16_t Motor_lockAcoilcurrent; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Acoil<69>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ѹ mv
|
|||
|
uint16_t Motor_lockBcoilcurrent; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Bcoil<69>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ѹ mv
|
|||
|
uint16_t Motor_position; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD><CEBB> <20><>
|
|||
|
}Motor_and_ChipPar_t;
|
|||
|
|
|||
|
|
|||
|
typedef union{
|
|||
|
struct{
|
|||
|
uint8_t nodeAddress;
|
|||
|
uint8_t ON :1; //1:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ
|
|||
|
uint8_t reserve :7;
|
|||
|
uint16_t Position;
|
|||
|
uint8_t reserve1;
|
|||
|
uint8_t reserve2;
|
|||
|
uint8_t reserve3;
|
|||
|
uint8_t reserve4;
|
|||
|
};
|
|||
|
uint8_t data[LIN_BUFF_SIZE]; /*!< data buffer. */
|
|||
|
uint16_t data16[LIN_BUFF_SIZE/2];
|
|||
|
}MotorCtrlFrame_t;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void delay(uint32_t delayus);
|
|||
|
void MOTORunP_24(uint8_t motorstep);
|
|||
|
void StopMotor(void);
|
|||
|
|
|||
|
uint8_t ReturnClearT1cntFlag(void);
|
|||
|
uint8_t Motor_GetMotorState(void);
|
|||
|
uint8_t Motor_GetMotorRunStage(void);
|
|||
|
|
|||
|
uint32_t Motor_GetMotorErrorState10s(void);
|
|||
|
|
|||
|
void Get_MotorprafromLINM(LIN_Device_Frame_t const *frame);
|
|||
|
void Calculation_position(void);
|
|||
|
void Motor_GetSETLockCurrent(uint16_t *data1,uint16_t *data2);
|
|||
|
uint8_t Motor_GetMotorStep(void);
|
|||
|
M_Re_pra_t Motor_GetALLMotorSetparameters (void);
|
|||
|
uint16_t Motor_GetMotorConstantspeed(void);
|
|||
|
uint16_t Motor_GetCurrentPosition(void);
|
|||
|
uint8_t Motor_GetOnestep_csa_adcnt(void);
|
|||
|
void MOTOR_TaskHandler(void);
|
|||
|
|
|||
|
void MotorCtrl_TaskHandler(void);
|
|||
|
|
|||
|
void MOTORunP_8(uint8_t motorstep);
|
|||
|
void MOTORunP_24(uint8_t motorstep);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void Timer1_INTTest(void);
|
|||
|
uint8_t GetVAMPCNT(void);
|
|||
|
void ClearVAMPCNT(void);
|
|||
|
uint8_t GetVBATCNT(void);
|
|||
|
void ClearVBATCNT(void);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#endif
|