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.
285 lines
8.9 KiB
285 lines
8.9 KiB
#include "encoder.h"
|
|
#include "zboard.h"
|
|
#include <stdio.h>
|
|
#include "zport.h"
|
|
#include "udpclient.h"
|
|
|
|
static encoder_light_state_t camera_encoder_state = STANDBY;
|
|
static encoder_light_state_t driven_encoder_gear_state = STANDBY;
|
|
static uint32_t camera_encoder_lastprocess = 0;
|
|
static uint32_t driven_encoder_gear_lastprocess = 0;
|
|
|
|
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 += (short)__HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
|
|
__HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler) = 0; /* 清零 */
|
|
get_encoder_value_flag = true;
|
|
break;
|
|
case DRIVEN_ENCODER_GEAR:
|
|
*encoder_value += (short)__HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
|
|
__HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler) = 0; /* 清零 */
|
|
get_encoder_value_flag = true;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return get_encoder_value_flag;
|
|
}
|
|
|
|
void encoder_light_schedule(void)
|
|
{
|
|
static bool camera_encoder_light_state;
|
|
static bool driven_encoder_light_state;
|
|
if (camera_encoder_state == STANDBY)
|
|
{
|
|
if (sys_haspassedms(camera_encoder_lastprocess) > 500)
|
|
{
|
|
camera_encoder_lastprocess = HAL_GetTick();
|
|
if (camera_encoder_light_state)
|
|
{
|
|
encoder_light_switch_set_color_and_brightness(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_BLUE, encoder_light_max_brightness);
|
|
}
|
|
else
|
|
{
|
|
encoder_switch_close_light(CAMERA_ENCODER);
|
|
}
|
|
camera_encoder_light_state = !camera_encoder_light_state;
|
|
}
|
|
}
|
|
|
|
if (driven_encoder_gear_state == STANDBY)
|
|
{
|
|
if (sys_haspassedms(driven_encoder_gear_lastprocess) > 500)
|
|
{
|
|
driven_encoder_gear_lastprocess = HAL_GetTick();
|
|
if (driven_encoder_light_state)
|
|
{
|
|
encoder_light_switch_set_color_and_brightness(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_BLUE, encoder_light_max_brightness);
|
|
}
|
|
else
|
|
{
|
|
encoder_switch_close_light(DRIVEN_ENCODER_GEAR);
|
|
}
|
|
driven_encoder_light_state = !driven_encoder_light_state;
|
|
}
|
|
}
|
|
}
|
|
|
|
void encoder_set_state(encoder_usage_t encoder, encoder_light_state_t state)
|
|
{
|
|
/* 主动上报的时候常亮,接收到触发指令直接翻转led,待机中0.5s周期闪烁 */
|
|
if (encoder == CAMERA_ENCODER)
|
|
{
|
|
camera_encoder_state = state;
|
|
if (state == STANDBY)
|
|
{
|
|
camera_encoder_lastprocess = HAL_GetTick();
|
|
// HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
|
|
}
|
|
else if (state == WORKING)
|
|
{
|
|
/* 如果工作状态的时候触发了genlock等,灯的状态不应该变成待机状态,只有同为待机的时候闪烁,上层写的时候需要加判断 */
|
|
// HAL_GPIO_WritePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin, GPIO_PIN_RESET);
|
|
}
|
|
}
|
|
else if (encoder == DRIVEN_ENCODER_GEAR)
|
|
{
|
|
driven_encoder_gear_state = state;
|
|
if (state == STANDBY)
|
|
{
|
|
driven_encoder_gear_lastprocess = HAL_GetTick();
|
|
// HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
|
|
}
|
|
else if (state == WORKING)
|
|
{
|
|
// HAL_GPIO_WritePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin, GPIO_PIN_SET);
|
|
}
|
|
}
|
|
}
|
|
|
|
encoder_light_state_t encoder_get_state(encoder_usage_t encoder)
|
|
{
|
|
if (encoder == CAMERA_ENCODER)
|
|
{
|
|
return camera_encoder_state;
|
|
}
|
|
else if (encoder == DRIVEN_ENCODER_GEAR)
|
|
{
|
|
return driven_encoder_gear_state;
|
|
}
|
|
return ENCODER_STATE_NUMBER_AND_ERR_STATE;
|
|
}
|
|
|
|
void encoder_all_encoder_clear_counter_and_structer_count(void)
|
|
{
|
|
udp_client_get_active_report_data_structer()->encoder_1_count = 0;
|
|
udp_client_get_active_report_data_structer()->encoder_2_count = 0;
|
|
encoder_all_clear_counter();
|
|
}
|
|
|
|
void encoder_switch_encoder_clear_count_and_structer_count(encoder_usage_t encoder)
|
|
{
|
|
|
|
switch (encoder)
|
|
{
|
|
case CAMERA_ENCODER:
|
|
udp_client_get_active_report_data_structer()->encoder_1_count = 0;
|
|
encoder_clear_counter(CAMERA_ENCODER);
|
|
break;
|
|
case DRIVEN_ENCODER_GEAR:
|
|
udp_client_get_active_report_data_structer()->encoder_2_count = 0;
|
|
encoder_clear_counter(DRIVEN_ENCODER_GEAR);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
R1:tim2-->TIM_CHANNEL_4
|
|
G1:tim2-->TIM_CHANNEL_1
|
|
B1:tim3-->TIM_CHANNEL_1
|
|
|
|
R2:tim2-->TIM_CHANNEL_3
|
|
G2:tim4-->TIM_CHANNEL_3
|
|
B2:tim4-->TIM_CHANNEL_4
|
|
*/
|
|
void encoder_light_switch_set_color_and_brightness(encoder_usage_t encoder, encoder_light_color_table_t color, uint16_t brightness)
|
|
{
|
|
uint16_t count;
|
|
if (encoder == CAMERA_ENCODER)
|
|
{
|
|
switch (color)
|
|
{
|
|
case ENCODER_LIGHT_COLOR_RED:
|
|
count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim2);
|
|
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, count);
|
|
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_GREEN:
|
|
count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim2);
|
|
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, count);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
|
|
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_BLUE:
|
|
count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim3);
|
|
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, count);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
|
|
break;
|
|
|
|
default:
|
|
/* not find color */
|
|
break;
|
|
}
|
|
}
|
|
else if (encoder == DRIVEN_ENCODER_GEAR)
|
|
{
|
|
switch (color)
|
|
{
|
|
case ENCODER_LIGHT_COLOR_RED:
|
|
count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim2);
|
|
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, count);
|
|
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_GREEN:
|
|
count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim4);
|
|
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, count);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_BLUE:
|
|
count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim4);
|
|
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_4, count);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
|
|
break;
|
|
|
|
default:
|
|
/* not find color */
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// error
|
|
}
|
|
}
|
|
|
|
void encoder_switch_close_light(encoder_usage_t encoder)
|
|
{
|
|
if (encoder == CAMERA_ENCODER)
|
|
{
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
|
|
}
|
|
else if (encoder == DRIVEN_ENCODER_GEAR)
|
|
{
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
|
|
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
|
|
}
|
|
else
|
|
{
|
|
// error
|
|
}
|
|
}
|