最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - How Optimize SPWM for BLDC Motor in STM32 - Stack Overflow

programmeradmin2浏览0评论

I made a BLDC motor driver in the past months but I am not very good at software unfortunately I am doing some things in software for the first time. I have been trying to drive the bldc motor at home with my own driver for the last 3 days but it vibrates and heats up more compared to the ready BLDC ESC driver at home. When I looked at the ESC card I saw that it uses a JY01 integrated circuit and I understood that it drives with the pwm method in the picture. How can I create this in stm32 or if I can do it with my own code how can I optimize it and drive my motor properly?

#define PI 3.14159265359
#define PWM_MAX 100 // Assume TIM8 ARR = 999 for 100% duty cycle
#define TABLE_SIZE 360

int sineTable[360] = { //I generated to manual  
    0, 24, 51, 49, 73, 97, 122, 146, 170, 194,
    218, 242, 266, 289, 313, 336, 359, 382, 404, 427,
    449, 470, 492, 513, 534, 555, 575, 595, 614, 633,
    652, 670, 688, 706, 723, 740, 756, 772, 787, 802,
    816, 830, 844, 856, 869, 881, 892, 903, 913, 922,
    932, 940, 948, 955, 962, 969, 974, 979, 984, 988,
    991, 994, 996, 997, 998, 999, 998, 997, 996, 994,
    991, 988, 984, 979, 974, 969, 962, 955, 948, 940,
    932, 922, 913, 903, 892, 881, 869, 856, 844, 830,
    816, 802, 787, 772, 756, 740, 723, 706, 688, 670,
    652, 633, 614, 595, 575, 555, 534, 513, 492, 470,
    449, 427, 404, 382, 359, 336, 313, 289, 266, 242,
    218, 194, 170, 146, 122, 97, 73, 49, 24, 0,
    -24, -49, -73, -97, -122, -146, -170, -194, -218, -242,
    -266, -289, -313, -336, -359, -382, -404, -427, -449, -470,
    -492, -513, -534, -555, -575, -595, -614, -633, -652, -670,
    -688, -706, -723, -740, -756, -772, -787, -802, -816, -830,
    -844, -856, -869, -881, -892, -903, -913, -922, -932, -940,
    -948, -955, -962, -969, -974, -979, -984, -988, -991, -994,
    -996, -997, -998, -999, -998, -997, -996, -994, -991, -988,
    -984, -979, -974, -969, -962, -955, -948, -940, -932, -922,
    -913, -903, -892, -881, -869, -856, -844, -830, -816, -802,
    -787, -772, -756, -740, -723, -706, -688, -670, -652, -633,
    -614, -595, -575, -555, -534, -513, -492, -470, -449, -427,
    -404, -382, -359, -336, -313, -289, -266, -242, -218, -194,
    -170, -146, -122, -97, -73, -49, -24, 0
};

 // Precomputed sine lookup table
uint16_t stepIndex = 0; // Angle step for sine wave generation

void generateSPWM() {
    // Update phase shift
    stepIndex = (stepIndex + 1) % TABLE_SIZE;

    // Get sine wave values for three phases with 120° phase shift
    int SINE_A_PWM = sineTable[stepIndex];
    int SINE_B_PWM = sineTable[(stepIndex + 120) % TABLE_SIZE];
    int SINE_C_PWM = sineTable[(stepIndex + 240) % TABLE_SIZE];

    // Set the PWM duty cycles for each phase
    __HAL_TIM_SET_COMPARE(&htim8, TIM_CHANNEL_1, SINE_A_PWM * 0.12);
    __HAL_TIM_SET_COMPARE(&htim8, TIM_CHANNEL_2, SINE_B_PWM * 0.12);
    __HAL_TIM_SET_COMPARE(&htim8, TIM_CHANNEL_3, SINE_C_PWM * 0.12);

}

// Timer Interrupt Handler
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    if (htim->Instance == TIM8) {
        generateSPWM(); // Generate SPWM on Timer overflow
    }
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  MX_SPI3_Init();
  MX_TIM2_Init();
  MX_ADC1_Init();
  MX_I2C2_Init();
  MX_TIM8_Init();
  MX_USART1_UART_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_GPIO_WritePin(GPIOB, RUNL_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET); // Enable Half1
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); // Enable Half2
  HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_3);
  // Start PWM signals
  HAL_TIM_Base_Start_IT(&htim8);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while(1){

  }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  /* USER CODE END 3 */
}

JY01 SPWM Drive

With the help of ChatGPT and some people, I wrote certain codes, but while it should normally rotate with less vibration and without heating, it rotated with more vibration, noise and heating, and the current values ​​were slightly higher.

发布评论

评论列表(0)

  1. 暂无评论