找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5146|回复: 2
收起左侧

STM32 VESC DC _ BLDC _ FOC本杰明控制器C源码

[复制链接]
ID:472995 发表于 2019-1-26 00:08 | 显示全部楼层 |阅读模式
VESC DC _ BLDC _ FOC本杰明控制器C源码,分享大家

单片机源程序如下:
  1. /*
  2.         Copyright 2016 Benjamin Vedder        benjamin@vedder.se

  3.         This file is part of the VESC firmware.

  4.         The VESC firmware is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.

  8.     The VESC firmware is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.

  12.     You should have received a copy of the GNU General Public License
  13.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14.     */

  15. #include "ch.h"
  16. #include "hal.h"
  17. #include "stm32f4xx_conf.h"

  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <stdlib.h>

  22. #include "mc_interface.h"
  23. #include "mcpwm.h"
  24. #include "mcpwm_foc.h"
  25. #include "ledpwm.h"
  26. #include "comm_usb.h"
  27. #include "ledpwm.h"
  28. #include "terminal.h"
  29. #include "hw.h"
  30. #include "app.h"
  31. #include "packet.h"
  32. #include "commands.h"
  33. #include "timeout.h"
  34. #include "comm_can.h"
  35. #include "ws2811.h"
  36. #include "led_external.h"
  37. #include "encoder.h"
  38. #include "servo.h"
  39. #include "servo_simple.h"
  40. #include "utils.h"
  41. #include "nrf_driver.h"
  42. #include "rfhelp.h"
  43. #include "spi_sw.h"

  44. /*
  45. * Timers used:
  46. * TIM7: servo
  47. * TIM1: mcpwm
  48. * TIM2: mcpwm
  49. * TIM12: mcpwm
  50. * TIM8: mcpwm
  51. * TIM3: servo_dec/Encoder (HW_R2)/servo_simple
  52. * TIM4: WS2811/WS2812 LEDs/Encoder (other HW)
  53. *
  54. * DMA/stream        Device                Function
  55. * 1, 2                        I2C1                Nunchuk, temp on rev 4.5
  56. * 1, 7                        I2C1                Nunchuk, temp on rev 4.5
  57. * 1, 1                        UART3                HW_R2
  58. * 1, 3                        UART3                HW_R2
  59. * 2, 2                        UART6                Other HW
  60. * 2, 7                        UART6                Other HW
  61. * 2, 4                        ADC                        mcpwm
  62. * 1, 0                        TIM4                WS2811/WS2812 LEDs CH1 (Ch 1)
  63. * 1, 3                        TIM4                WS2811/WS2812 LEDs CH2 (Ch 2)
  64. *
  65. */

  66. // Private variables


  67. static THD_WORKING_AREA(periodic_thread_wa, 1024);
  68. static THD_WORKING_AREA(timer_thread_wa, 128);

  69. static THD_FUNCTION(periodic_thread, arg) {
  70.         (void)arg;

  71.         chRegSetThreadName("Main periodic");

  72.         for(;;) {
  73.                 if (mc_interface_get_state() == MC_STATE_RUNNING) {
  74.                         ledpwm_set_intensity(LED_GREEN, 1.0);
  75.                 } else {
  76.                         ledpwm_set_intensity(LED_GREEN, 0.2);
  77.                 }

  78.                 mc_fault_code fault = mc_interface_get_fault();
  79.                 if (fault != FAULT_CODE_NONE) {
  80.                         for (int i = 0;i < (int)fault;i++) {
  81.                                 ledpwm_set_intensity(LED_RED, 1.0);
  82.                                 chThdSleepMilliseconds(250);
  83.                                 ledpwm_set_intensity(LED_RED, 0.0);
  84.                                 chThdSleepMilliseconds(250);
  85.                         }

  86.                         chThdSleepMilliseconds(500);
  87.                 } else {
  88.                         ledpwm_set_intensity(LED_RED, 0.0);
  89.                 }

  90.                 if (mc_interface_get_state() == MC_STATE_DETECTING) {
  91.                         commands_send_rotor_pos(mcpwm_get_detect_pos());
  92.                 }

  93.                 disp_pos_mode display_mode = commands_get_disp_pos_mode();

  94.                 switch (display_mode) {
  95.                         case DISP_POS_MODE_ENCODER:
  96.                                 commands_send_rotor_pos(encoder_read_deg());
  97.                                 break;

  98.                         case DISP_POS_MODE_PID_POS:
  99.                                 commands_send_rotor_pos(mc_interface_get_pid_pos_now());
  100.                                 break;

  101.                         case DISP_POS_MODE_PID_POS_ERROR:
  102.                                 commands_send_rotor_pos(utils_angle_difference(mc_interface_get_pid_pos_set(), mc_interface_get_pid_pos_now()));
  103.                                 break;

  104.                         default:
  105.                                 break;
  106.                 }

  107.                 if (mc_interface_get_configuration()->motor_type == MOTOR_TYPE_FOC) {
  108.                         switch (display_mode) {
  109.                         case DISP_POS_MODE_OBSERVER:
  110.                                 commands_send_rotor_pos(mcpwm_foc_get_phase_observer());
  111.                                 break;

  112.                         case DISP_POS_MODE_ENCODER_OBSERVER_ERROR:
  113.                                 commands_send_rotor_pos(utils_angle_difference(mcpwm_foc_get_phase_observer(), mcpwm_foc_get_phase_encoder()));
  114.                                 break;

  115.                         default:
  116.                                 break;
  117.                 }
  118.                 }

  119.                 chThdSleepMilliseconds(10);

  120. //                chThdSleepMilliseconds(40);
  121. //                volatile const mc_configuration *conf = mc_interface_get_configuration();
  122. //                float vq = mcpwm_foc_get_vq();
  123. //                float iq = mc_interface_get_tot_current_directional();
  124. //                float linkage = conf->foc_motor_flux_linkage;
  125. //                float speed = ((2.0 * M_PI) / 60.0) * mc_interface_get_rpm();
  126. //
  127. //                if (iq < -6.0) {
  128. //                        float res = vq / (linkage * speed * iq);
  129. //                        res *= 2.0 / 3.0;
  130. //                        static float res_filtered = 0.0;
  131. //                        UTILS_LP_FAST(res_filtered, res, 0.02);
  132. //                        commands_printf("Res: %.4f", (double)res_filtered);
  133. //                }

  134. //                chThdSleepMilliseconds(40);
  135. //                commands_printf("Max: %.2f Min: %.2f",
  136. //                                (double)mc_interface_get_configuration()->lo_current_motor_max_now,
  137. //                                (double)mc_interface_get_configuration()->lo_current_motor_min_now);
  138.         }
  139. }

  140. static THD_FUNCTION(timer_thread, arg) {
  141.         (void)arg;

  142.         chRegSetThreadName("msec_timer");

  143.         for(;;) {
  144.                 packet_timerfunc();
  145.                 chThdSleepMilliseconds(1);
  146.         }
  147. }

  148. int main(void) {
  149.         halInit();
  150.         chSysInit();

  151.         // Initialize the enable pins here and disable them
  152.         // to avoid excessive current draw at boot because of
  153.         // floating pins.
  154. #ifdef HW_HAS_DRV8313
  155.         INIT_BR();
  156. #endif

  157.         chThdSleepMilliseconds(1000);

  158.         hw_init_gpio();
  159.         LED_RED_OFF();
  160.         LED_GREEN_OFF();

  161.         conf_general_init();
  162.         ledpwm_init();

  163.         mc_configuration mcconf;
  164.         conf_general_read_mc_configuration(&mcconf);
  165.         mc_interface_init(&mcconf);

  166.         commands_init();
  167.         comm_usb_init();

  168.         app_configuration appconf;
  169.         conf_general_read_app_configuration(&appconf);
  170.         app_set_configuration(&appconf);

  171. #ifdef HW_HAS_PERMANENT_NRF
  172.         conf_general_permanent_nrf_found = nrf_driver_init();
  173.         if (conf_general_permanent_nrf_found) {
  174.                 rfhelp_restart();
  175.         } else {
  176.                 nrf_driver_stop();
  177.                 // Set the nrf SPI pins to the general SPI interface so that
  178.                 // an external NRF can be used with the NRF app.
  179.                 spi_sw_change_pins(
  180.                                 HW_SPI_PORT_NSS, HW_SPI_PIN_NSS,
  181.                                 HW_SPI_PORT_SCK, HW_SPI_PIN_SCK,
  182.                                 HW_SPI_PORT_MOSI, HW_SPI_PIN_MOSI,
  183.                                 HW_SPI_PORT_MISO, HW_SPI_PIN_MISO);
  184.         }
  185. #endif

  186.         timeout_init();
  187.         timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current);

  188. #if CAN_ENABLE
  189.         comm_can_init();
  190. #endif

  191. #if WS2811_ENABLE
  192.         ws2811_init();
  193. #if !WS2811_TEST
  194.         led_external_init();
  195. #endif
  196. #endif

  197. #if SERVO_OUT_ENABLE
  198. #if SERVO_OUT_SIMPLE
  199.         servo_simple_init();
  200. #else
  201.         servo_init();
  202. #endif
  203. #endif

  204.         // Threads
  205.         chThdCreateStatic(periodic_thread_wa, sizeof(periodic_thread_wa), NORMALPRIO, periodic_thread, NULL);
  206.         chThdCreateStatic(timer_thread_wa, sizeof(timer_thread_wa), NORMALPRIO, timer_thread, NULL);

  207. #if WS2811_TEST
  208.         unsigned int color_ind = 0;
  209.         const int num = 4;
  210.         const uint32_t colors[] = {COLOR_RED, COLOR_GOLD, COLOR_GRAY, COLOR_MAGENTA, COLOR_BLUE};
  211.         const int brightness_set = 100;

  212.         for (;;) {
  213.                 chThdSleepMilliseconds(1000);

  214.                 for (int i = 0;i < brightness_set;i++) {
  215.                         ws2811_set_brightness(i);
  216.                         chThdSleepMilliseconds(10);
  217.                 }

  218.                 chThdSleepMilliseconds(1000);

  219.                 for(int i = -num;i <= WS2811_LED_NUM;i++) {
  220.                         ws2811_set_led_color(i - 1, COLOR_BLACK);
  221.                         ws2811_set_led_color(i + num, colors[color_ind]);

  222.                         ws2811_set_led_color(0, COLOR_RED);
  223.                         ws2811_set_led_color(WS2811_LED_NUM - 1, COLOR_GREEN);

  224.                         chThdSleepMilliseconds(50);
  225.                 }

  226.                 for (int i = 0;i < brightness_set;i++) {
  227.                         ws2811_set_brightness(brightness_set - i);
  228.                         chThdSleepMilliseconds(10);
  229.                 }

  230.                 color_ind++;
  231.                 if (color_ind >= sizeof(colors) / sizeof(uint32_t)) {
  232.                         color_ind = 0;
  233.                 }

  234.                 static int asd = 0;
  235.                 asd++;
  236.                 if (asd >= 3) {
  237.                         asd = 0;

  238.                         for (unsigned int i = 0;i < sizeof(colors) / sizeof(uint32_t);i++) {
  239.                                 ws2811_set_all(colors[i]);

  240.                                 for (int i = 0;i < brightness_set;i++) {
  241.                                         ws2811_set_brightness(i);
  242.                                         chThdSleepMilliseconds(2);
  243.                                 }

  244.                                 chThdSleepMilliseconds(100);

  245.                                 for (int i = 0;i < brightness_set;i++) {
  246.                                         ws2811_set_brightness(brightness_set - i);
  247.                                         chThdSleepMilliseconds(2);
  248.                                 }
  249.                         }
  250.                 }
  251.         }
  252. #endif

  253.         for(;;) {
  254.                 chThdSleepMilliseconds(10);

  255.                 if (encoder_is_configured()) {
  256.                         //                comm_can_set_pos(0, encoder_read_deg());
  257.                 }
  258.         }
  259. }

复制代码

所有资料51hei提供下载:
bldc-master.7z (760.42 KB, 下载次数: 114)
回复

使用道具 举报

ID:1 发表于 2019-1-26 00:41 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

ID:324611 发表于 2019-9-18 14:39 | 显示全部楼层
好东西,学习了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表