/**
|
* @brief ʹÄÜ»ô¶û´«¸ÐÆ÷
|
* @param ÎÞ
|
* @retval ÎÞ
|
*/
|
void hall_enable(void)
|
{
|
/* ʹÄÜ»ô¶û´«¸ÐÆ÷½Ó¿Ú */
|
__HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_TRIGGER);
|
__HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_UPDATE);
|
|
HAL_TIMEx_HallSensor_Start(&htimx_hall);
|
|
LED1_OFF;
|
|
HAL_TIM_TriggerCallback(&htimx_hall); // Ö´ÐÐÒ»´Î»»Ïà
|
|
motor_drive.enable_flag = 1;
|
}
|
|
/**
|
* @brief ½ûÓûô¶û´«¸ÐÆ÷
|
* @param ÎÞ
|
* @retval ÎÞ
|
*/
|
void hall_disable(void)
|
{
|
/* ½ûÓûô¶û´«¸ÐÆ÷½Ó¿Ú */
|
__HAL_TIM_DISABLE_IT(&htimx_hall, TIM_IT_TRIGGER);
|
__HAL_TIM_DISABLE_IT(&htimx_hall, TIM_IT_UPDATE);
|
HAL_TIMEx_HallSensor_Stop(&htimx_hall);
|
motor_drive.enable_flag = 0;
|
motor_drive.speed = 0;
|
}
|
|
uint8_t get_hall_state(void)
|
{
|
uint8_t state = 0;
|
|
#if 1
|
/* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ U µÄ״̬ */
|
if (HAL_GPIO_ReadPin(HALL_INPUTU_GPIO_PORT, HALL_INPUTU_PIN) != GPIO_PIN_RESET)
|
{
|
state |= 0x01U << 0;
|
}
|
|
/* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ V µÄ״̬ */
|
if (HAL_GPIO_ReadPin(HALL_INPUTV_GPIO_PORT, HALL_INPUTV_PIN) != GPIO_PIN_RESET)
|
{
|
state |= 0x01U << 1;
|
}
|
|
/* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ W µÄ״̬ */
|
if (HAL_GPIO_ReadPin(HALL_INPUTW_GPIO_PORT, HALL_INPUTW_PIN) != GPIO_PIN_RESET)
|
{
|
state |= 0x01U << 2;
|
}
|
#else
|
state = (GPIOH->IDR >> 10) & 7; // ¶Á 3 ¸ö»ô¶û´«¸ÐÆ÷µÄ״̬
|
#endif
|
|
return state; // ·µ»Ø´«¸ÐÆ÷״̬
|
}
|
|
static void update_motor_speed(uint8_t dir_in, uint32_t time)
|
{
|
int speed_temp = 0;
|
static int flag = 0;
|
float f = 0;
|
|
/* ¼ÆËãËÙ¶È£º
|
µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(72000000.0/128.0)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(72000000.0/128.0) * time)Ϊʱ¼ä³¤¡£
|
*/
|
|
if (time == 0)
|
motor_drive.speed_group[count++] = 0;
|
else
|
{
|
f = (1.0f / (72000000.0f / HALL_PRESCALER_COUNT) * time);
|
f = (1.0f / 12.0f) / (f / 60.0f);
|
motor_drive.speed_group[count++] = f;
|
}
|
update_speed_dir(dir_in);
|
// motor_drive.speed = motor_drive.speed_group[count-1];
|
if (count >= SPEED_FILTER_NUM)
|
{
|
flag = 1;
|
count = 0;
|
}
|
// return ;
|
speed_temp = 0;
|
|
/* ¼ÆËã½ü SPEED_FILTER_NUM ´ÎµÄËÙ¶ÈÆ½¾ùÖµ£¨Â˲¨£© */
|
if (flag)
|
{
|
for (uint8_t c = 0; c < SPEED_FILTER_NUM; c++)
|
{
|
speed_temp += motor_drive.speed_group[c];
|
}
|
|
motor_drive.speed = speed_temp / SPEED_FILTER_NUM;
|
}
|
else
|
{
|
for (uint8_t c = 0; c < count; c++)
|
{
|
speed_temp += motor_drive.speed_group[c];
|
}
|
|
motor_drive.speed = speed_temp / count;
|
}
|
}
|
|
/**
|
* @brief »ñÈ¡µç»úתËÙ
|
* @param time:»ñÈ¡µÄʱ¼ä¼ä¸ô
|
* @retval ·µ»Øµç»úתËÙ
|
*/
|
float get_motor_speed(void)
|
{
|
return motor_drive.speed;
|
}
|
|
/**
|
* @brief ¸üеç»úʵ¼ÊËÙ¶È·½Ïò
|
* @param dir_in£º»ô¶ûÖµ
|
* @retval ÎÞ
|
*/
|
static void update_speed_dir(uint8_t dir_in)
|
{
|
uint8_t step[6] = {1, 3, 2, 6, 4, 5};
|
|
static uint8_t num_old = 0;
|
uint8_t step_loc = 0; // ¼Ç¼µ±Ç°»ô¶ûλÖÃ
|
int8_t dir = 1;
|
|
for (step_loc = 0; step_loc < 6; step_loc++)
|
{
|
if (step[step_loc] == dir_in) // ÕÒµ½µ±Ç°»ô¶ûµÄλÖÃ
|
{
|
break;
|
}
|
}
|
|
/* ¶Ëµã´¦Àí */
|
if (step_loc == 0)
|
{
|
if (num_old == 1)
|
{
|
dir = 1;
|
}
|
else if (num_old == 5)
|
{
|
dir = -1;
|
}
|
}
|
/* ¶Ëµã´¦Àí */
|
else if (step_loc == 5)
|
{
|
if (num_old == 0)
|
{
|
dir = 1;
|
}
|
else if (num_old == 4)
|
{
|
dir = -1;
|
}
|
}
|
else if (step_loc > num_old)
|
{
|
dir = -1;
|
}
|
else if (step_loc < num_old)
|
{
|
dir = 1;
|
}
|
|
num_old = step_loc;
|
// motor_drive.speed *= dir;;
|
motor_drive.speed_group[count - 1] *= dir;
|
}
|
|
/**
|
* @brief »ô¶û´«¸ÐÆ÷´¥·¢»Øµ÷º¯Êý
|
* @param htim:¶¨Ê±Æ÷¾ä±ú
|
* @retval ÎÞ
|
*/
|
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
|
{
|
/* »ñÈ¡»ô¶û´«¸ÐÆ÷Òý½Å״̬,×÷Ϊ»»ÏàµÄÒÀ¾Ý */
|
uint8_t step = 0;
|
step = get_hall_state();
|
|
if (htim == &htimx_hall) // ÅжÏÊÇ·ñÓÉ´¥·¢ÖжϲúÉú
|
{
|
update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1));
|
motor_drive.timeout = 0;
|
}
|
|
if (get_bldcm_direction() == MOTOR_FWD)
|
{
|
switch (step)
|
{
|
case 1: /* U+ W- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse); // ͨµÀ 1 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 2: /* V+ U- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse); // ͨµÀ 2 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
|
break;
|
|
case 3: /* V+ W- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse); // ͨµÀ 2 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 4: /* W+ V- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse); // ͨµÀ 3 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 5: /* U+ V -*/
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse); // ͨµÀ 1 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 6: /* W+ U- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse); // ͨµÀ 3 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
}
|
}
|
else
|
{
|
switch (step)
|
{
|
case 1: /* W+ U- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse); // ͨµÀ 3 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 2: /* U+ V -*/
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse); // ͨµÀ 1 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 3: /* W+ V- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse); // ͨµÀ 3 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
|
break;
|
|
case 4: /* V+ W- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse); // ͨµÀ 2 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 5: /* V+ U- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0); // ͨµÀ 1 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse); // ͨµÀ 2 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
|
case 6: /* U+ W- */
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0); // ͨµÀ 2 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0); // ͨµÀ 3 ÅäÖÃΪ 0
|
HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
|
|
__HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse); // ͨµÀ 1 ÅäÖõÄÕ¼¿Õ±È
|
HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
|
break;
|
}
|
}
|
HAL_TIM_GenerateEvent(&htimx_bldcm, TIM_EVENTSOURCE_COM); // Èí¼þ²úÉú»»Ïàʼþ£¬´Ëʱ²Å½«ÅäÖÃдÈë
|
}
|