////////////////////////////////////////////////////////////////////////////////

/// FULL_BRIDGE文件
///
///
/// 包含hall捕获、六步驱动等
/// @file    full_bridge.c 
/// @author  LH
/// @date    2022-07-19
/// @version v1.0 

////////////////////////////////////////////////////////////////////////////////

#include "full_bridge.h"
#include "ptz_struct.h"
#include "gd32f4xx_it.h"
#include "rotate_bldc.h"
#include "rotate_plan.h"
#include "device_adc_collect.h"
#include "gd32f4xx_timer.h"
#include "speed_to_bldc.h"


#ifdef Full_bridge

typedef void(*pctr) (void);

pctr h_six_step_commu[6];
pctr v_six_step_commu[6];


HALL_State H_Hall_state;
HALL_State V_Hall_state;


/*
********************************六步换向法***********************************/
static unsigned int h_pwm_duty;
static unsigned int v_pwm_duty;
/*!
    \brief      none pwm占空比设置
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.10.11
*/
//水平
void h_pwm_duty_change(unsigned int pwm_duty)
{
  h_pwm_duty = pwm_duty;
  
}
//垂直
void v_pwm_duty_change(unsigned int pwm_duty)
{
  v_pwm_duty = pwm_duty;
  
}


/*!
    \brief      hall状态获取
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.19
*/
unsigned char h_hall_senser_value_get()
{
  unsigned char hall_value;
  H_Hall_state.Hall_H1_state = gpio_input_bit_get(GPIOE, GPIO_PIN_10);
  H_Hall_state.Hall_H2_state = gpio_input_bit_get(GPIOE, GPIO_PIN_11);
  H_Hall_state.Hall_H3_state = gpio_input_bit_get(GPIOE, GPIO_PIN_12);
  
  hall_value = H_Hall_state.Hall_H1_state 
               |(H_Hall_state.Hall_H2_state << 1)
               |(H_Hall_state.Hall_H3_state << 2);
  return hall_value;
}

unsigned char v_hall_senser_value_get()
{
  unsigned char hall_value;
  V_Hall_state.Hall_H1_state = gpio_input_bit_get(GPIOE, GPIO_PIN_13);
  V_Hall_state.Hall_H2_state = gpio_input_bit_get(GPIOE, GPIO_PIN_14);
  V_Hall_state.Hall_H3_state = gpio_input_bit_get(GPIOE, GPIO_PIN_15);
  
  hall_value = V_Hall_state.Hall_H1_state 
               |(V_Hall_state.Hall_H2_state << 1)
               |(V_Hall_state.Hall_H3_state << 2);
  return hall_value;
}



/*!
    \brief      none U相进，V相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_uh_vl()
{
  H_EN1_OFF;//U
  H_EN2_ON;//V
  H_EN3_OFF;//W
  TIMER_CH0CV(TIMER2) = h_pwm_duty;//U199;//
  TIMER_CH1CV(TIMER2) = 0;//V
  TIMER_CH2CV(TIMER2) = 0;//W
}


/*!
    \brief      none W相进，V相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_wh_vl()
{
  H_EN1_OFF;//U
  H_EN2_ON;//V
  H_EN3_OFF;//W
  TIMER_CH0CV(TIMER2) = 0;//U
  TIMER_CH1CV(TIMER2) = 0;//V
  TIMER_CH2CV(TIMER2) = h_pwm_duty;//W199;//
}


/*!
    \brief      none W相进，U相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_wh_ul()
{
  H_EN1_ON;//U
  H_EN2_OFF;//V
  H_EN3_OFF;//W
  TIMER_CH0CV(TIMER2) = 0;//U
  TIMER_CH1CV(TIMER2) = 0;//V
  TIMER_CH2CV(TIMER2) = h_pwm_duty;//W199;//
}


/*!
    \brief      none V相进，U相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_vh_ul()
{
  H_EN1_ON;//U
  H_EN2_OFF;//V
  H_EN3_OFF;//W
  TIMER_CH0CV(TIMER2) = 0;//U
  TIMER_CH1CV(TIMER2) = h_pwm_duty;//V199;//
  TIMER_CH2CV(TIMER2) = 0;//W
}


/*!
    \brief      none V相进，W相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_vh_wl()
{
  H_EN1_OFF;//U
  H_EN2_OFF;//V
  H_EN3_ON;//W
  TIMER_CH0CV(TIMER2) = 0;//U
  TIMER_CH1CV(TIMER2) = h_pwm_duty;//V199;//
  TIMER_CH2CV(TIMER2) = 0;//W
}


/*!
    \brief      none U相进，W相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_uh_wl()
{
  H_EN1_OFF;//U
  H_EN2_OFF;//V
  H_EN3_ON;//W
  TIMER_CH0CV(TIMER2) = h_pwm_duty;//U199;//
  TIMER_CH1CV(TIMER2) = 0;//V
  TIMER_CH2CV(TIMER2) = 0;//W
}

/*!
    \brief      none 六步换向，对于120°hall，正转顺序为546231，反转顺序为：7-正转顺序,,（最终再减1）
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
pctr h_six_step_commu[6] =
{
  &h_uh_wl, &h_vh_ul, &h_vh_wl, &h_wh_vl, &h_uh_vl, &h_wh_ul
};






/*!
    \brief      none U相进，V相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void v_uh_vl()
{
  V_EN1_OFF;//U
  V_EN2_ON;//V
  V_EN3_OFF;//W
  TIMER_CH0CV(TIMER3) = v_pwm_duty;//U199;//
  TIMER_CH1CV(TIMER3) = 0;//V
  TIMER_CH2CV(TIMER3) = 0;//W
}


/*!
    \brief      none W相进，V相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void v_wh_vl()
{
  V_EN1_OFF;//U
  V_EN2_ON;//V
  V_EN3_OFF;//W
  TIMER_CH0CV(TIMER3) = 0;//U
  TIMER_CH1CV(TIMER3) = 0;//V
  TIMER_CH2CV(TIMER3) = v_pwm_duty;//W199;//
}


/*!
    \brief      none W相进，U相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void v_wh_ul()
{
  V_EN1_ON;//U
  V_EN2_OFF;//V
  V_EN3_OFF;//W
  TIMER_CH0CV(TIMER3) = 0;//U
  TIMER_CH1CV(TIMER3) = 0;//V
  TIMER_CH2CV(TIMER3) = v_pwm_duty;//W199;//
}


/*!
    \brief      none V相进，U相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void v_vh_ul()
{
  V_EN1_ON;//U
  V_EN2_OFF;//V
  V_EN3_OFF;//W
  TIMER_CH0CV(TIMER3) = 0;//U
  TIMER_CH1CV(TIMER3) = v_pwm_duty;//V199;//
  TIMER_CH2CV(TIMER3) = 0;//W
}


/*!
    \brief      none V相进，W相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void v_vh_wl()
{
  V_EN1_OFF;//U
  V_EN2_OFF;//V
  V_EN3_ON;//W
  TIMER_CH0CV(TIMER3) = 0;//U
  TIMER_CH1CV(TIMER3) = v_pwm_duty;//V199;//
  TIMER_CH2CV(TIMER3) = 0;//W
}


/*!
    \brief      none U相进，W相出
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void v_uh_wl()
{
  V_EN1_OFF;//U
  V_EN2_OFF;//V
  V_EN3_ON;//W
  TIMER_CH0CV(TIMER3) = v_pwm_duty;//U199;//
  TIMER_CH1CV(TIMER3) = 0;//V
  TIMER_CH2CV(TIMER3) = 0;//W
}

/*!
    \brief      none 六步换向，对于120°hall，正转顺序为546231，反转顺序为：7-正转顺序,,（最终再减1）
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
pctr v_six_step_commu[6] =
{
  &v_uh_wl, &v_vh_ul, &v_vh_wl, &v_wh_vl, &v_uh_vl, &v_wh_ul
};


void uvw()
{
  V_EN1_OFF;//U
  V_EN2_OFF;//V
  V_EN3_OFF;//W
}

/*!
    \brief      none 水平直流无刷六步换向
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
//中断里调用
void h_bldc_six_step()
{
//  H_ADC2_Phase_current();
  if(g_ptz.hori_start_stop_set == PTZ_HORI_START)
  {//电机处于启动状态
    H_Hall_state.Hall_value = h_hall_senser_value_get();
    if((H_Hall_state.Hall_value <= 6)&&(H_Hall_state.Hall_value >= 1))
    {
//      if(g_ptz.hori_direction_set == PTZ_HORI_DIR_RIGHT)
//      {//电机正转- 1
//        h_six_step_commu[(7-H_Hall_state.Hall_value) -1]();
//      }else{//电机反转
//        h_six_step_commu[H_Hall_state.Hall_value -1]();
//      }
      //直齿云台转向
      if(g_ptz.hori_direction_set == PTZ_HORI_DIR_RIGHT)
      {//电机正转- 1
        
        h_six_step_commu[H_Hall_state.Hall_value -1]();
      }else{//电机反转
        h_six_step_commu[(7-H_Hall_state.Hall_value) -1]();
        
      }
      
    }
  }
}
/*!
    \brief      none 垂直直流无刷六步换向
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
//中断里调用
void v_bldc_six_step()
{
//  V_ADC0_Phase_current();
  if((g_ptz.vert_start_stop_set == PTZ_VERT_START) && (g_ptz.vert_pid.mode == PID_ADJUST_SPEED))
  {//电机处于启动状态
    V_Hall_state.Hall_value = v_hall_senser_value_get();
    if((V_Hall_state.Hall_value <= 6)&&(V_Hall_state.Hall_value >= 1))
    {
      if(g_ptz.vert_direction_set == PTZ_VERT_DIR_UP)
      {//电机正转- 1
        v_six_step_commu[V_Hall_state.Hall_value -1]();
      }else{//电机反转
        v_six_step_commu[(7-V_Hall_state.Hall_value) -1]();
      }
      
      
//      if(g_ptz.vert_pid.PidUT_float < 0)
//      {//换种思路，PID计算为负数时，反向转动
//        if(g_ptz.vert_direction_set == PTZ_VERT_DIR_UP)
//        {//电机正转- 1
//          v_six_step_commu[(7-V_Hall_state.Hall_value) -1]();
//        }else{//电机反转
//          v_six_step_commu[V_Hall_state.Hall_value -1]();
//        }
//      }
      
    }
  }
}





/*!
    \brief      none 垂直方向调速，自锁
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.12.05
*/
void vert_dir_speed_start(unsigned int verf)
{
  V_EN1_OFF;//U
  V_EN2_ON;//V
  V_EN3_ON;//W
  TIMER_CH0CV(TIMER3) = verf;//U199;//
  TIMER_CH1CV(TIMER3) = 0;//V
  TIMER_CH2CV(TIMER3) = 0;//W
  
}

void vert_dir_speed_stop(void)
{
  V_EN1_OFF;//U
  V_EN2_OFF;//V
  V_EN3_OFF;//W
}


void hori_dir_speed_start(unsigned int verf)
{
  H_EN1_OFF;//U
  H_EN2_ON;//V
  H_EN3_ON;//W
  TIMER_CH0CV(TIMER2) = verf;//U199;//
  TIMER_CH1CV(TIMER2) = 0;//V
  TIMER_CH2CV(TIMER2) = 0;//W
  
}

void hori_dir_speed_stop(void)
{
  H_EN1_OFF;//U
  H_EN2_OFF;//V
  H_EN3_OFF;//W
}








/*!
    \brief      none 电机启动
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_start_motor()
{
  //定时器使能
//  timer_enable(TIMER2);
  h_pwm_duty_change(0);
  TIMER_CH0CV(TIMER2) = 0 ;//
  TIMER_CH1CV(TIMER2) = 0 ;//
  TIMER_CH2CV(TIMER2) = 0 ;//
  
  H_EN1_OFF;//U
  H_EN2_OFF;//V
  H_EN3_OFF;//W
  
//  timer_interrupt_enable(TIMER2,TIMER_INT_UP);//定时器更新中断使能
}

void v_start_motor()
{
  //定时器使能
//  timer_enable(TIMER3);
  v_pwm_duty_change(0);
  TIMER_CH0CV(TIMER3) = 0 ;//
  TIMER_CH1CV(TIMER3) = 0 ;//
  TIMER_CH2CV(TIMER3) = 0 ;//
  
  V_EN1_OFF;//U
  V_EN2_OFF;//V
  V_EN3_OFF;//W
  
//  timer_interrupt_enable(TIMER3,TIMER_INT_UP);//定时器更新中断使能
}


/*!
    \brief      none 电机停止
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.21
*/
void h_stop_motor()
{
  h_pwm_duty_change(0);
//  //输出低电平
  TIMER_CH0CV(TIMER2) = 0;
  TIMER_CH1CV(TIMER2) = 0;
  TIMER_CH2CV(TIMER2) = 0;
  
  H_EN1_OFF;//U
  H_EN2_OFF;//V
  H_EN3_OFF;//W
//  //定时器关闭
//  timer_disable(TIMER2);
//  timer_interrupt_disable(TIMER2,TIMER_INT_UP);//定时器更新中断使能
}

void v_stop_motor()
{
  v_pwm_duty_change(0);
//  //输出低电平
  TIMER_CH0CV(TIMER3) = 0;
  TIMER_CH1CV(TIMER3) = 0;
  TIMER_CH2CV(TIMER3) = 0;
  
  V_EN1_OFF;//U
  V_EN2_OFF;//V
  V_EN3_OFF;//W
//  //定时器关闭
//  timer_disable(TIMER3);
//  timer_interrupt_disable(TIMER3,TIMER_INT_UP);//定时器更新中断使能
}






/*!
    \brief      hall引脚初始化
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.10.11
*/
static void hall_io_init(void)
{
//  //配置引脚时钟
//  rcu_periph_clock_enable(RCU_GPIOE);
//  
//  //水平——PE10-H1,PE11-H2,PE12-H3，，垂直PE13-H1,PE14-H2,PE15-H3
//  //设置引脚为输入模式
//  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_10);
//  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_11);
//  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_12);
//  
//  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
//  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_14);
//  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_15);
  
  H_Hall_state.Hall_H1_state = 0;
  H_Hall_state.Hall_H2_state = 0;
  H_Hall_state.Hall_H3_state = 0;
  H_Hall_state.Hall_value = 0;
  
  V_Hall_state.Hall_H1_state = 0;
  V_Hall_state.Hall_H2_state = 0;
  V_Hall_state.Hall_H3_state = 0;
  V_Hall_state.Hall_value = 0;

}

/*!
    \brief      none 驱动桥引脚初始化
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.10.11
*/
void bridge_io_init(void)
{
  rcu_periph_clock_enable(RCU_GPIOA);
  rcu_periph_clock_enable(RCU_GPIOC);
  rcu_periph_clock_enable(RCU_GPIOD);
  rcu_periph_clock_enable(RCU_GPIOE);
  //H_EN1--PC9//H_EN2--PA8//H_EN3--PA11
  gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
  gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);
  gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_11);
  gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8| GPIO_PIN_11);
  gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  //V_EN1--PD10//V_EN2--PD11//V_EN3--PD15
  gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_10);
  gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_11);
  gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_15);
  gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10| GPIO_PIN_11| GPIO_PIN_15);
  
  //GD改版后驱动策略进行了修改————新增驱动桥电源控制引脚，便于驱动器的断电复位等
  gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0);//水平电源控制引脚
  gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);//垂直电源控制引脚
  gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0| GPIO_PIN_1);
  
  H_EN1_OFF;
  H_EN2_OFF;
  H_EN3_OFF;
  
  V_EN1_OFF;
  V_EN2_OFF;
  V_EN3_OFF;
  hall_io_init();
  
  H_BRIDGE_POWER_ON;
  V_BRIDGE_POWER_ON;
    
}






#endif


