From 945ba42f7550d5a203e43d82b43ed82dc981d9e9 Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Wed, 30 Jun 2021 22:05:52 +0800
Subject: [PATCH] 霍尔输入中断正常,需要调试马达转动

---
 USR/SRC/bldc_ctrl.c |  104 +++++++++++++++++++++++-----------------------------
 1 files changed, 46 insertions(+), 58 deletions(-)

diff --git a/USR/SRC/bldc_ctrl.c b/USR/SRC/bldc_ctrl.c
index 04f1b7d..2f3d428 100644
--- a/USR/SRC/bldc_ctrl.c
+++ b/USR/SRC/bldc_ctrl.c
@@ -1,6 +1,11 @@
 
 #include <math.h>
 #include <stdlib.h>
+#include "bldc_ctrl.h"
+#include "motor.h"
+#include "gpio.h"
+#include "pwm.h"
+#include "bsp_pid.h"
 
 /* ˽�б��� */
 static bldcm_data_t bldcm_data;
@@ -15,9 +20,9 @@
   */
 void bldcm_init(void)
 {
-    PWM_TIMx_Configuration(); // ������ƶ�ʱ�������ų�ʼ��
-    hall_tim_config();        // ������������ʼ��
-    sd_gpio_config();         // sd ���ų�ʼ��
+  // PWM_TIMx_Configuration(); // ������ƶ�ʱ�������ų�ʼ��
+  // hall_tim_config();        // ������������ʼ��
+  sd_gpio_config(); // sd ���ų�ʼ��
 }
 
 /**
@@ -27,24 +32,7 @@
   */
 static void sd_gpio_config(void)
 {
-    GPIO_InitTypeDef GPIO_InitStruct;
-
-    /* ��ʱ��ͨ���������Ŷ˿�ʱ��ʹ�� */
-    SHUTDOWN_GPIO_CLK_ENABLE();
-
-    /* ����IO��ʼ�� */
-    /*�����������*/
-    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-    /*������������ */
-    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
-    /*ѡ��Ҫ���Ƶ�GPIO����*/
-    GPIO_InitStruct.Pin = SHUTDOWN_PIN;
-
-    /*���ÿ⺯����ʹ���������õ�GPIO_InitStructure��ʼ��GPIO*/
-    HAL_GPIO_Init(SHUTDOWN_GPIO_PORT, &GPIO_InitStruct);
-
-    BLDCM_ENABLE_SD(); // Ĭ�Ͽ���
-    HAL_Delay(1);
+  Set_SDH2136_Disable();
 }
 
 /**
@@ -54,9 +42,9 @@
   */
 void set_bldcm_speed(uint16_t v)
 {
-    bldcm_data.dutyfactor = v;
+  bldcm_data.dutyfactor = v;
 
-    set_pwm_pulse(v); // �����ٶ�
+  set_pwm_pulse(v); // �����ٶ�
 }
 
 /**
@@ -66,7 +54,7 @@
   */
 void set_bldcm_direction(motor_dir_t dir)
 {
-    bldcm_data.direction = dir;
+  bldcm_data.direction = dir;
 }
 
 /**
@@ -76,7 +64,7 @@
   */
 motor_dir_t get_bldcm_direction(void)
 {
-    return bldcm_data.direction;
+  return bldcm_data.direction;
 }
 
 /**
@@ -86,8 +74,8 @@
   */
 void set_bldcm_enable(void)
 {
-    bldcm_data.is_enable = 1;
-    hall_enable();
+  bldcm_data.is_enable = 1;
+  hall_enable();
 }
 
 /**
@@ -97,14 +85,14 @@
   */
 void set_bldcm_disable(void)
 {
-    /* ���û����������ӿ� */
-    hall_disable();
+  /* ���û����������ӿ� */
+  hall_disable();
 
-    /* ֹͣ PWM ��� */
-    stop_pwm_output();
+  /* ֹͣ PWM ��� */
+  stop_pwm_output();
 
-    /* �ر� MOS �� */
-    bldcm_data.is_enable = 0;
+  /* �ر� MOS �� */
+  bldcm_data.is_enable = 0;
 }
 
 /**
@@ -114,32 +102,32 @@
   */
 void bldcm_pid_control(void)
 {
-    int32_t speed_actual = get_motor_speed(); // �����ת�ĵ�ǰ�ٶ�
+  int32_t speed_actual = get_motor_speed(); // �����ת�ĵ�ǰ�ٶ�
 
-    if (bldcm_data.is_enable)
+  if (bldcm_data.is_enable)
+  {
+    float cont_val = 0; // ��ǰ����ֵ
+
+    cont_val = PID_realize(speed_actual);
+
+    if (cont_val < 0)
     {
-        float cont_val = 0; // ��ǰ����ֵ
-
-        cont_val = PID_realize(speed_actual);
-
-        if (cont_val < 0)
-        {
-            cont_val = -cont_val;
-            bldcm_data.direction = MOTOR_REV;
-        }
-        else
-        {
-            bldcm_data.direction = MOTOR_FWD;
-        }
-
-        cont_val = (cont_val > PWM_PERIOD_COUNT) ? PWM_PERIOD_COUNT : cont_val; // ���޴���
-
-        set_bldcm_speed(cont_val);
-
-#ifdef PID_ASSISTANT_EN
-        set_computer_value(SEND_FACT_CMD, CURVES_CH1, &speed_actual, 1); // ��ͨ�� 1 ����ʵ��ֵ
-#else
-        printf("ʵ��ֵ��%d, Ŀ��ֵ��%.0f������ֵ: %.0f\n", speed_actual, get_pid_target(), cont_val);
-#endif
+      cont_val = -cont_val;
+      bldcm_data.direction = MOTOR_REV;
     }
+    else
+    {
+      bldcm_data.direction = MOTOR_FWD;
+    }
+
+    cont_val = (cont_val > PERIOD_CMP) ? PERIOD_CMP : cont_val; // ���޴���
+
+    set_bldcm_speed(cont_val);
+
+    // #ifdef PID_ASSISTANT_EN
+    //     set_computer_value(SEND_FACT_CMD, CURVES_CH1, &speed_actual, 1); // ��ͨ�� 1 ����ʵ��ֵ
+    // #else
+    //     printf("ʵ��ֵ��%d, Ŀ��ֵ��%.0f������ֵ: %.0f\n", speed_actual, get_pid_target(), cont_val);
+    // #endif
+  }
 }

--
Gitblit v1.8.0