You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.9 KiB
77 lines
1.9 KiB
#include "encoder.h"
|
|
#include "zboard.h"
|
|
#include <stdio.h>
|
|
#include "zport.h"
|
|
|
|
static encoder_t m_uarts[] = {
|
|
{&camera_encoder, TIM_CHANNEL_1 | TIM_CHANNEL_2}, // 相机编码器
|
|
{&driven_encoder_gear, TIM_CHANNEL_1 | TIM_CHANNEL_2} // 从动编码器
|
|
};
|
|
|
|
void encoder_all_start(void)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
|
|
{
|
|
HAL_TIM_Encoder_Start(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
|
|
}
|
|
}
|
|
|
|
void encoder_all_stop(void)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
|
|
{
|
|
HAL_TIM_Encoder_Stop(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
|
|
}
|
|
}
|
|
|
|
bool encoder_clear_counter(encoder_usage_t encoder)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
|
|
{
|
|
if (encoder == i)
|
|
{
|
|
__HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void encoder_all_clear_counter(void)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
|
|
{
|
|
__HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0; // 计数器值重新置位
|
|
}
|
|
}
|
|
|
|
bool encoder_read_with_encoder(encoder_usage_t encoder, uint32_t *encoder_value)
|
|
{
|
|
bool get_encoder_value_flag = false;
|
|
switch (encoder)
|
|
{
|
|
case CAMERA_ENCODER:
|
|
*encoder_value = __HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
|
|
get_encoder_value_flag = true;
|
|
break;
|
|
case DRIVEN_ENCODER_GEAR:
|
|
*encoder_value = __HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
|
|
get_encoder_value_flag = true;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return get_encoder_value_flag;
|
|
}
|
|
|
|
void encoder_light_schedule(void)
|
|
{
|
|
static uint32_t lastprocess = 0;
|
|
if (sys_haspassedms(lastprocess) > 500)
|
|
{
|
|
lastprocess = HAL_GetTick();
|
|
HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
|
|
}
|
|
}
|