diff --git a/3rd/dfoc.c b/3rd/dfoc.c index bd9c19d..90e7030 100644 --- a/3rd/dfoc.c +++ b/3rd/dfoc.c @@ -15,27 +15,19 @@ #define _1_SQRT3 0.57735026919f #define _2_SQRT3 1.15470053838f +#define VOLTAGE_POWER_SUPPLY 12.0f +#define VOLTAGE_LIMIT 6 +#define PWM_COMPARE_VALUE 750.0f + volatile float Ua = 0, Ub = 0, Uc = 0, Ualpha, Ubeta = 0, dc_a = 0, dc_b = 0, dc_c = 0; -const float voltage_limit = 5.5; -const float voltage_power_supply = 12.0f; + volatile float zero_electric_Angle = 0.0; extern int pp; extern int Dir; -void Motor_en() { - // GPIO_InitTypeDef GPIO_InitStructure; - // - // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); - // - // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; - // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; - // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - // GPIO_Init(GPIOA,&GPIO_InitStructure); - // - // GPIO_SetBits(GPIOA, GPIO_Pin_8); -} +void Motor_en() {} // 限制幅值 float constrain(float amt, float low, float high) { @@ -57,18 +49,20 @@ void SetPwm(float Ua, float Ub, float Uc) { volatile float U_b = 0.0; volatile float U_c = 0.0; - U_a = constrain(Ua, 0.0f, voltage_limit); - U_b = constrain(Ub, 0.0f, voltage_limit); - U_c = constrain(Uc, 0.0f, voltage_limit); - // printf("Ua : %f -- Ub : %f -- Uc : %f -- U_a : %f -- U_b : %f -- U_c : %f - // \n",Ua,Ub,Uc,U_a,U_b,U_c); - dc_a = constrain(U_a / voltage_power_supply, 0.0f, 1.0f); - dc_b = constrain(U_b / voltage_power_supply, 0.0f, 1.0f); - dc_c = constrain(U_c / voltage_power_supply, 0.0f, 1.0f); + U_a = constrain(Ua, 0.0f, VOLTAGE_LIMIT); + U_b = constrain(Ub, 0.0f, VOLTAGE_LIMIT); + U_c = constrain(Uc, 0.0f, VOLTAGE_LIMIT); - PWM_Channel1(dc_a * 500.0f); // 频率15k - PWM_Channel2(dc_b * 500.0f); - PWM_Channel3(dc_c * 500.0f); + dc_a = constrain(U_a / VOLTAGE_POWER_SUPPLY, 0.0f, 1.0f); + dc_b = constrain(U_b / VOLTAGE_POWER_SUPPLY, 0.0f, 1.0f); + dc_c = constrain(U_c / VOLTAGE_POWER_SUPPLY, 0.0f, 1.0f); + if (DEBUG_ENABLED & DEBUG_DFOC_ENABLED) { + + printf("dc_a : %f -- dc_b : %f -- dc_c : %f \n", dc_a, dc_b, dc_c); + } + PWM_Channel1((1 - dc_a) * PWM_COMPARE_VALUE); // 频率15k + PWM_Channel2((1 - dc_b) * PWM_COMPARE_VALUE); + PWM_Channel3((1 - dc_c) * PWM_COMPARE_VALUE); } // FOC核心算法,克拉克逆变换/帕克逆变换 @@ -81,9 +75,9 @@ void SetPhaseVoltage(float Uq, float Ud, float angle_el) { Ualpha = -Uq * sin(angle_el); Ubeta = Uq * cos(angle_el); - Ua = Ualpha + voltage_power_supply / 2; - Ub = (sqrt(3) * Ubeta - Ualpha) / 2 + voltage_power_supply / 2; - Uc = -(Ualpha + sqrt(3) * Ubeta) / 2 + voltage_power_supply / 2; + Ua = Ualpha + VOLTAGE_POWER_SUPPLY / 2; + Ub = (sqrt(3) * Ubeta - Ualpha) / 2 + VOLTAGE_POWER_SUPPLY / 2; + Uc = -(Ualpha + sqrt(3) * Ubeta) / 2 + VOLTAGE_POWER_SUPPLY / 2; SetPwm(Ua, Ub, Uc); @@ -94,16 +88,15 @@ void Check_Sensor(void) { // SetPhaseVoltage(3, 0, _3PI_2); // delay_ms(3000); zero_electric_Angle = electricAngle(); + printf("Check_Sensor zero_electric_Angle is %f \n", zero_electric_Angle); // SetPhaseVoltage(0, 0, _3PI_2); // delay_ms(500); } -void FOC_Init(float power) { - // voltage_power_supply = power; +void FOC_Init() { // PWM_Init(); // CurrSense_Init(); // AS5600_Init(); - Check_Sensor(); } @@ -114,8 +107,8 @@ void Set_Angle(float Target) { printf("angle readback in dfoc.c is %f \n", langle); } volatile float Uq = - PID_Controller(0.067, 0.01, 0, (Target - Dir * langle) * 180 / PI); - // volatile float Uq = PID_Controller(0.06, 0, 0, (Target - Dir * langle) * + PID_Controller(0.03, 0.01, 0, (Target - Dir * langle) * 180 / PI); + // volatile float Uq = PID_Controller(0.133, 0, 0, (Target - Dir * langle) * // 180 / PI); if (DEBUG_ENABLED & DEBUG_DFOC_ENABLED) { printf("Uq is %f \n", Uq); @@ -147,7 +140,7 @@ float velocityopenloop(float target) { printf("shaft_angle : %f -- Ts : %f \n", shaft_angle, Ts); } - Uq = voltage_limit; + Uq = VOLTAGE_LIMIT; SetPhaseVoltage(Uq, 0, shaft_angle); openloop_timestamp = now_ts; diff --git a/empty.c b/empty.c index 38bcad8..6826d6f 100644 --- a/empty.c +++ b/empty.c @@ -48,7 +48,7 @@ extern bool gIsI2cError; const float num_f = 0.123456f; volatile uint16_t count = 0; -volatile float Target = 15; // 串口目标值 +volatile float Target = 20; // 串口目标值 const int pp = 7; // 电机极对数 const int Dir = -1; // 电机编码器方向 diff --git a/empty.syscfg b/empty.syscfg index e2015b7..78c8f01 100644 --- a/empty.syscfg +++ b/empty.syscfg @@ -26,20 +26,40 @@ const UART1 = UART.addInstance(); /** * Write custom configuration values to the imported modules. */ +const divider2 = system.clockTree["HFCLK4MFPCLKDIV"]; +divider2.divideValue = 10; + const divider7 = system.clockTree["PLL_PDIV"]; divider7.divideValue = 2; +const divider9 = system.clockTree["UDIV"]; +divider9.divideValue = 2; + const gate7 = system.clockTree["MFCLKGATE"]; gate7.enable = true; +const gate8 = system.clockTree["MFPCLKGATE"]; +gate8.enable = true; + const multiplier2 = system.clockTree["PLL_QDIV"]; multiplier2.multiplyValue = 4; const mux4 = system.clockTree["EXHFMUX"]; mux4.inputSelect = "EXHFMUX_XTAL"; -const pinFunction4 = system.clockTree["HFXT"]; -pinFunction4.inputFreq = 40; +const mux8 = system.clockTree["HSCLKMUX"]; +mux8.inputSelect = "HSCLKMUX_SYSPLL2X"; + +const mux10 = system.clockTree["MFPCLKMUX"]; +mux10.inputSelect = "MFPCLKMUX_HFCLK"; + +const mux12 = system.clockTree["SYSPLLMUX"]; +mux12.inputSelect = "zSYSPLLMUX_HFCLK"; + +const pinFunction4 = system.clockTree["HFXT"]; +pinFunction4.inputFreq = 40; +pinFunction4.enable = true; +pinFunction4.HFXTStartup = 20; GPIO1.$name = "LED"; GPIO1.associatedPins[0].$name = "PA0"; @@ -73,8 +93,9 @@ I2C1.sclPinConfig.$name = "ti_driverlib_gpio_GPIOPinGeneric3"; PWM1.$name = "PWM_0"; PWM1.ccIndex = [0,1,2]; -PWM1.clockDivider = 2; PWM1.pwmMode = "CENTER_ALIGN"; +PWM1.clockDivider = 4; +PWM1.timerCount = 2000; PWM1.PWM_CHANNEL_0.$name = "ti_driverlib_pwm_PWMTimerCC0"; PWM1.PWM_CHANNEL_1.$name = "ti_driverlib_pwm_PWMTimerCC1"; PWM1.ccp0PinConfig.$name = "ti_driverlib_gpio_GPIOPinGeneric4"; @@ -87,7 +108,7 @@ PWM1.PWM_CHANNEL_2.$name = "ti_driverlib_pwm_PWMTimerCC2"; PWM1.ccp2PinConfig.$name = "ti_driverlib_gpio_GPIOPinGeneric6"; SYSCTL.forceDefaultClkConfig = true; -SYSCTL.peripheral.$assign = "SYSCTL"; +SYSCTL.clockTreeEn = true; SYSTICK.periodEnable = true; SYSTICK.systickEnable = true; @@ -105,13 +126,13 @@ TIMER1.peripheral.$assign = "TIMG0"; UART1.$name = "UART_0"; UART1.rxFifoThreshold = "DL_UART_RX_FIFO_LEVEL_ONE_ENTRY"; UART1.targetBaudRate = 115200; -UART1.uartClkDiv = "8"; UART1.enableFIFO = true; UART1.txFifoThreshold = "DL_UART_TX_FIFO_LEVEL_ONE_ENTRY"; UART1.enabledDMATXTriggers = "DL_UART_DMA_INTERRUPT_TX"; UART1.enabledInterrupts = ["DMA_DONE_RX"]; UART1.enabledDMARXTriggers = "DL_UART_DMA_INTERRUPT_RX"; UART1.interruptPriority = "1"; +UART1.uartClkDiv = "3"; UART1.peripheral.$assign = "UART0"; UART1.peripheral.rxPin.$assign = "PA11"; UART1.peripheral.txPin.$assign = "PA10"; @@ -136,5 +157,8 @@ ProjectConfig.migrationCondition = true; * version of the tool will not impact the pinmux you originally saw. These lines can be completely deleted in order to * re-solve from scratch. */ -I2C1.peripheral.$suggestSolution = "I2C1"; -UART1.DMA_CHANNEL_RX.peripheral.$suggestSolution = "DMA_CH1"; +pinFunction4.peripheral.$suggestSolution = "SYSCTL"; +pinFunction4.peripheral.hfxInPin.$suggestSolution = "PA5"; +pinFunction4.peripheral.hfxOutPin.$suggestSolution = "PA6"; +I2C1.peripheral.$suggestSolution = "I2C1"; +UART1.DMA_CHANNEL_RX.peripheral.$suggestSolution = "DMA_CH1";