请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【NUCLEO-C031C6】FOC开环测试

[复制链接]
vecang 发布时间:2024-3-28 00:22

前言

  • 使用stm32cubemx配置外设,Matlab搭建FOC开环模型,并导出代码用于FOC。对导出的FOC加以修改,已完成了对于FOC的开环控制。

CUBEMX

  • 配置cubemx,这里选择芯片型号时,直接选择了开发板只有的配置。

TIM 配置

image.png

1、首先最重要的就是配置TIM定时器啦~

2、这里选择了三路互补的PWM来驱动电机的6路MOS信号

3、第四路PWM用于触发ADC采样

4、计数模式选择了中心对齐模式,由于使用svpwm调制,设置为该模式可以减少开关频率

5、这里系统时钟为48MHz,因此设置一个10k的PWM信号。由于中心对称,所以需要除2

6、用于死区的配置

7、这里也是因为中心对称,CCR等于ARR时会触发一次,这里RCR为1可以把计数到ARR时忽略

8、将触发事件配置为第四路PWM信号触发,当产生触发事件后告诉ADC进行采样

9、这里默认三陆互补PWM为mode1

10、PWM4设置为mode2

  1. image.png

11、NVIC设置事件更新中断

image.png

ADC

VGK1FPP%~HW2YIZ1B_~FWM.png

1、选择ADC

2、启用两路ADC用于测量母线电流。根据KCL定律,已知两路便可得到第三路

3,4、使能扫描和DMA,需要先配置下面的操作

image.png

5、配置触发方式,为定时器PWM触发

UART

image.png

这里配置了串口一,结果发现板子默认是串口二。这里大家可以根据自己的板子修改,我是去掉了两个连接串口2的电阻转而连接在串口1上。

Clion

FOC驱动代码

// foc.c
#include "foc.h"

ExtU rtU;
ExtY rtY;

/* Model step function */
void foc_step(void) {
    int sector = 0;
    float T1;
    float rtb_ualpha;
    float rtb_ubeta;

    T1 = sinf(rtU.theta);
    rtb_ubeta = cosf(rtU.theta);
    rtb_ualpha = (float) rtU.ud * rtb_ubeta - (float) rtU.uq * T1;
    rtb_ubeta = (float) rtU.ud * T1 + (float) rtU.uq * rtb_ubeta;

    // 由 U_alpha 和 U_beta 计算出对于扇区
    if (rtb_ubeta > 0.0F) { sector = 1; }
    if ((1.73205078F * rtb_ualpha - rtb_ubeta) / 2.0F > 0.0F) { sector += 2; }
    if ((-1.73205078F * rtb_ualpha - rtb_ubeta) / 2.0F > 0.0F) { sector += 4; }

    switch (sector) {
        case 1:
            T1 = (-1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc);
            rtb_ualpha = (1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc);
            break;

        case 2:
            T1 = (1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc);
            rtb_ualpha = -(1.73205078F * rtb_ubeta * rtU.Tpwm / rtU.udc);
            break;

        case 3:
            T1 = -((-1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc));
            rtb_ualpha = 1.73205078F * rtb_ubeta * rtU.Tpwm / rtU.udc;
            break;

        case 4:
            T1 = -(1.73205078F * rtb_ubeta * rtU.Tpwm / rtU.udc);
            rtb_ualpha = (-1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc);
            break;

        case 5:
            T1 = 1.73205078F * rtb_ubeta * rtU.Tpwm / rtU.udc;
            rtb_ualpha = -((1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc));
            break;

        default:
            T1 = -((1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm / rtU.udc));
            rtb_ualpha = -((-1.5F * rtb_ualpha + 0.866025388F * rtb_ubeta) * (rtU.Tpwm /
                                                                              rtU.udc));
            break;
    }

    rtb_ubeta = T1 + rtb_ualpha;
    if (rtb_ubeta > rtU.Tpwm) {
        T1 /= rtb_ubeta;
        rtb_ualpha /= T1 + rtb_ualpha;
    }

    rtb_ubeta = (rtU.Tpwm - (T1 + rtb_ualpha)) / 4.0F;
    T1 = T1 / 2.0F + rtb_ubeta;
    switch (sector) {
        case 1:
            rtY.Tcmp1 = T1;
            rtY.Tcmp2 = rtb_ubeta;
            rtY.Tcmp3 = rtb_ualpha / 2.0F + T1;
            break;

        case 2:
            rtY.Tcmp1 = rtb_ubeta;
            rtY.Tcmp2 = rtb_ualpha / 2.0F + T1;
            rtY.Tcmp3 = T1;
            break;

        case 3:
            rtY.Tcmp1 = rtb_ubeta;
            rtY.Tcmp2 = T1;
            rtY.Tcmp3 = rtb_ualpha / 2.0F + T1;
            break;

        case 4:
            rtY.Tcmp1 = rtb_ualpha / 2.0F + T1;
            rtY.Tcmp2 = T1;
            rtY.Tcmp3 = rtb_ubeta;
            break;

        case 5:
            rtY.Tcmp1 = rtb_ualpha / 2.0F + T1;
            rtY.Tcmp2 = rtb_ubeta;
            rtY.Tcmp3 = T1;
            break;

        case 6:
            rtY.Tcmp1 = T1;
            rtY.Tcmp2 = rtb_ualpha / 2.0F + T1;
            rtY.Tcmp3 = rtb_ubeta;
            break;
    }

    rtY.sector = (float) sector;
}
// foc.h
#ifndef RTW_HEADER_foc_h_
#define RTW_HEADER_foc_h_
#include <math.h>

typedef struct {
  double ud,uq;
  float theta, udc,Tpwm;
} ExtU;

typedef struct {
  float Tcmp1,Tcmp2,Tcmp3;
  float sector;
} ExtY;

extern ExtU rtU;
extern ExtY rtY;
extern void foc_step(void);

#endif                                 /* RTW_HEADER_foc_h_ */
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "foc.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
#define PI 3.1415926
uint16_t adc1_val_buf1[4] = {0};
extern ExtU rtU;
extern ExtY rtY;
extern DMA_HandleTypeDef hdma_adc1;
uint16_t a,b,c,d,e,f;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void dmaAdcCallback(struct __DMA_HandleTypeDef *hdma);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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 */
    rtU.ud = 0;
    rtU.uq = 2;
    rtU.Tpwm = 4800;
    rtU.udc = 12;

  /* 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_DMA_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */

    HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&adc1_val_buf1,2);
    hdma_adc1.XferCpltCallback = dmaAdcCallback;
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);
  /* USER CODE END 2 */

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

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

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void dmaAdcCallback(struct __DMA_HandleTypeDef *hdma) {
    rtU.theta += 0.01;
    if (rtU.theta >= 2.0f * PI) {
        rtU.theta -= 2.0f * PI;
    }
    foc_step();
    htim1.Instance->CCR1 = (uint16_t) rtY.Tcmp3;
    htim1.Instance->CCR2 = (uint16_t) rtY.Tcmp2;
    htim1.Instance->CCR3 = (uint16_t) rtY.Tcmp1;

    a=htim1.Instance->CCR1;
    b=htim1.Instance->CCR2;
    c=htim1.Instance->CCR3;
    d=htim1.Instance->CCMR1;
    e=htim1.Instance->CCMR2;
    f=htim1.Instance->CCMR3;

}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

点击编译,烧录。

image.png

此时,电机开始旋转咯 ^^~

查看DEBUG

使用STM32CubeMonitor软件image.png

双击打开

image.png

点击编辑

image.png

添加路径和变量

image.png

点击更新,完成

image.pngimage.png

完成部署后运行

image.png

image.png

后序

不知道是不是Cmake配置的问题,32kflah写到这就基本快写完了。加个串口,内存就完全不够用了。。。

image.png
image.png
image.png
image.png
image.png
收藏 评论4 发布时间:2024-3-28 00:22

举报

4个回答
网络孤客 回答时间:2024-3-29 08:38:02

你用什么IDE?有没有把编译器的代码优化选上

Optimization.jpg

vecang 回答时间:2024-3-29 12:39:01
网络孤客 发表于 2024-3-29 08:389 q: g$ U$ a" t! u1 _' ]
[md]你用什么IDE?有没有把编译器的代码优化选上
7 o2 _0 x! \# O# V8 O
. [0 u9 V9 p1 ^: G" n+ X* v+ o2 k% |![Optimization.jpg](data/attachment/forum/202403/29/0 ...

! I$ Y% K( h1 ^; e( P6 C用的clion,cubemx导出的是cubeide。实测下来使用老版本的cubemx输出sw4stm32可以解决这个问题。估计是cmake配置的问题了
网络孤客 回答时间:2024-3-31 09:29:26

vecang 发表于 2024-3-29 12:39
用的clion,cubemx导出的是cubeide。实测下来使用老版本的cubemx输出sw4stm32可以解决这个问题。估计是cm ...

我直接用STM32CUBEIDE,编译时根本没有压缩,要设置一下。

vecang 回答时间:2024-3-31 12:07:44

网络孤客 发表于 2024-3-31 09:29
我直接用STM32CUBEIDE,编译时根本没有压缩,要设置一下。

您好,我添加了 add_compile_options(-Os) 后,仍然占用了很大部分的flash,STM32CUBEIDE占用要77%,Clion要84%

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版