久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5959|回復: 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, 下載次數: 122)



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏4 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 99久久精品免费 | 久久久久中文字幕 | 国产精品中文字幕一区二区三区 | 久久亚洲国产 | 精国产品一区二区三区 | 男女一区二区三区 | 欧美色综合天天久久综合精品 | 五月天天丁香婷婷在线中 | 91大神xh98xh系列全部 | 国产日韩欧美一区二区 | 一本色道精品久久一区二区三区 | 国产日产久久高清欧美一区 | 亚洲精品永久免费 | 日韩精品一二三 | 欧美一级网站 | 久久精彩视频 | 999观看免费高清www | www.天天操.com | 亚洲精品国产成人 | 人人澡视频 | 99久久久国产精品免费消防器 | 精品伊人| 日本在线黄色 | 国产精品一区在线 | 国产九九精品视频 | 久久精品亚洲精品国产欧美 | 国产乱码精品一区二区三区忘忧草 | 中文字幕动漫成人 | 天天草草草 | 天天碰日日操 | 日韩免费高清视频 | 免费不卡视频 | av黄色在线观看 | 国产九九九 | 日韩av一区二区在线观看 | 999久久久 | av官网在线 | 亚洲欧美激情四射 | 久久久久国| 婷婷色国产偷v国产偷v小说 | 欧美视频在线一区 |