tao_z
2021-07-14 82e38738a4d532cc3d56cbf80c1a4093f23cdd6a
USR/SRC/Motor.c
@@ -18,9 +18,10 @@
    motor_drive.timeout = 0;
    motor_drive.speed = 0;
    motor_drive.enable_flag = 0;
    motor_pluse = 0;
    memset(motor_drive.speed_group, 0, SPEED_FILTER_NUM);
    hall_enable();
    // hall_enable();
};
/**
  * @brief  Ê¹ÄÜ»ô¶û´«¸ÐÆ÷
@@ -87,14 +88,14 @@
    float f = 0;
    /* ¼ÆËãËÙ¶È£º
     µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(48000000.0/6400)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(48000000.0/6400) * time)Ϊʱ¼ä³¤¡£
     µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(64000000.0/6400)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(64000000.0/6400) * time)Ϊʱ¼ä³¤¡£
  */
    if (time == 0)
        motor_drive.speed_group[count++] = 0;
    else
    {
        // f = (1.0f / (48000000.0f / SPEED_PRESCALER_COUNT) * time);
        // f = (1.0f / (64000000.0f / SPEED_PRESCALER_COUNT) * time);
        // f = (1.0f / 12.0f) / (f / 60.0f);
        f = time;
        motor_drive.speed_group[count++] = f;
@@ -211,25 +212,11 @@
    // /* »ñÈ¡»ô¶û´«¸ÐÆ÷Òý½Å״̬,×÷Ϊ»»ÏàµÄÒÀ¾Ý */
    if (exti_interrupt_flag_get(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI)) // ÅжÏÊÇ·ñÓÉ´¥·¢ÖжϲúÉú
    {
        // update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1)); //TODO Ö÷¶¨Ê±Æ÷´¦»ñµÃ¼ÆÊýʱ¼ä
        update_motor_speed(motor_step, 300u * GetSpeedTimerOutcnt() + timer_counter_read(TIMER2));
        SEGGER_RTT_printf(0, "Hall state is:%d!\n", motor_step);
        // update_motor_speed(motor_step, 300u * GetSpeedTimerOutcnt() + timer_counter_read(TIMER2));
        SEGGER_RTT_printf(0, "motor speed is:%d,%d!\n", GetSpeedTimerOutcnt(), timer_counter_read(TIMER2));
        timer_counter_value_config(TIMER2, 0);
        motor_drive.timeout = 0;
        //     // exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI);
    }
    // if (RESET != exti_interrupt_flag_get(EXTI_4))
    // {
    //     SEGGER_RTT_printf(0, "HALL_A_EXTI  triggle!\n");
    // }
    // else if (RESET != exti_interrupt_flag_get(EXTI_5))
    // {
    //     SEGGER_RTT_printf(0, "HALL_C_EXTI  triggle!\n");
    // }
    // else if (RESET != exti_interrupt_flag_get(EXTI_15))
    // {
    //     SEGGER_RTT_printf(0, "HALL_B_EXTI  triggle!\n");
    // }
    motor_phasechange();
    // HAL_TIM_GenerateEvent(&htimx_bldcm, TIM_EVENTSOURCE_COM); // Èí¼þ²úÉú»»Ïàʼþ£¬´Ëʱ²Å½«ÅäÖÃдÈë
}
@@ -248,18 +235,21 @@
    case 1:
        /*  channel U configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_U, 0);
        MOTOR_U_L_ENABLE;
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        /*  channel V configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_V, 0);
        MOTOR_V_L_DISABLE;
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        /*  channel W configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE);
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_W, motor_pluse);
        MOTOR_W_L_DISABLE;
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_ENABLE);
@@ -268,20 +258,23 @@
    /* next step: step 6 configuration .u-v` breakover---------------------------- */
    case 2:
        /*  channel U configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_ENABLE);
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_U, motor_pluse);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        MOTOR_U_L_DISABLE;
        /*  channel V configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_V, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        MOTOR_V_L_ENABLE;
        /*  channel W configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_W, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_ENABLE);
        MOTOR_W_L_DISABLE;
@@ -291,19 +284,22 @@
    case 3:
        /*  channel U configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_U, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_ENABLE);
        MOTOR_U_L_DISABLE;
        /*  channel V configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_V, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        MOTOR_V_L_ENABLE;
        /*  channel W configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE);
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_W, motor_pluse);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_DISABLE;
@@ -313,19 +309,22 @@
    case 4:
        /*  channel U configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_U, 0);
        MOTOR_U_L_DISABLE;
        /*  channel V configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_V, motor_pluse);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        MOTOR_V_L_DISABLE;
        /*  channel W configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_W, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_ENABLE;
@@ -334,19 +333,22 @@
    /* next step: step 1 configuration .V-U` breakover---------------------------- */
    case 5:
        /*  channel U configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_U, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        MOTOR_U_L_ENABLE;
        /*  channel V configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_V, motor_pluse);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        MOTOR_V_L_DISABLE;
        /*  channel W configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_W, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_DISABLE;
        break;
@@ -354,19 +356,22 @@
    /* next step: step 4 configuration .U-W` breakover---------------------------- */
    case 6:
        /*  channel U configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_ENABLE);
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_ENABLE);
        SetPwmDuty(MOTOR_OUT_CH_U, motor_pluse);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        MOTOR_U_L_DISABLE;
        /*  channel V configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_V, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        MOTOR_V_L_DISABLE;
        /*  channel W configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE);
        // timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE);
        SetPwmDuty(MOTOR_OUT_CH_W, 0);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_ENABLE;
@@ -381,12 +386,15 @@
  */
extern void stop_pwm_output(void)
{
    timer_channel_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCX_DISABLE);
    // timer_channel_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCX_DISABLE);
    // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCXN_DISABLE);
    timer_channel_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCX_DISABLE);
    // timer_channel_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCX_DISABLE);
    // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCXN_DISABLE);
    timer_channel_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCX_DISABLE);
    // timer_channel_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCX_DISABLE);
    // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCXN_DISABLE);
    SetPwmDuty(MOTOR_OUT_CH_U, 0);
    SetPwmDuty(MOTOR_OUT_CH_V, 0);
    SetPwmDuty(MOTOR_OUT_CH_W, 0);
    MOTOR_W_L_DISABLE;
    MOTOR_V_L_DISABLE;
    MOTOR_U_L_DISABLE;
@@ -401,9 +409,9 @@
{
    /* ÉèÖö¨Ê±Æ÷ͨµÀÊä³ö PWM µÄÕ¼¿Õ±È */
    motor_pluse = pulse;
    SetPwmDuty(TIMER_CH_0, motor_pluse);
    SetPwmDuty(TIMER_CH_1, motor_pluse);
    SetPwmDuty(TIMER_CH_2, motor_pluse);
    // SetPwmDuty(TIMER_CH_0, motor_pluse);
    // SetPwmDuty(TIMER_CH_1, motor_pluse);
    // SetPwmDuty(TIMER_CH_2, motor_pluse);
    if (motor_drive.enable_flag)
    {