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.
202 lines
6.2 KiB
202 lines
6.2 KiB
#include "encoder.h"
|
|
#include "zboard.h"
|
|
#include <stdio.h>
|
|
#include "zport.h"
|
|
#include "udpclient.h"
|
|
|
|
static uint32_t camera_encoder_lastprocess = 0;
|
|
static uint32_t driven_encoder_gear_lastprocess = 0;
|
|
|
|
static encoder_t m_encoders[] = {
|
|
{&camera_encoder, TIM_CHANNEL_1 | TIM_CHANNEL_2, 0, 0}, // 相机编码器
|
|
{&driven_encoder_gear, TIM_CHANNEL_1 | TIM_CHANNEL_2, 0, 0} // 从动编码器
|
|
};
|
|
|
|
void encoder_all_start(void)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_encoders) / sizeof(encoder_t)); i++)
|
|
{
|
|
HAL_TIM_Encoder_Start(m_encoders[i].tim_handler, m_encoders[i].tim_channel);
|
|
}
|
|
}
|
|
|
|
void encoder_all_stop(void)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_encoders) / sizeof(encoder_t)); i++)
|
|
{
|
|
HAL_TIM_Encoder_Stop(m_encoders[i].tim_handler, m_encoders[i].tim_channel);
|
|
}
|
|
}
|
|
|
|
void encoder_all_clear_counter(void)
|
|
{
|
|
for (uint8_t i = 0; i < (sizeof(m_encoders) / sizeof(encoder_t)); i++)
|
|
{
|
|
m_encoders[i].count = 0;
|
|
}
|
|
}
|
|
|
|
void encoder_switch_clear_counter(encoder_usage_t encoder)
|
|
{
|
|
/* 需要注意别传入ENCODER_NUMBER,否则数组越界 */
|
|
m_encoders[encoder].count = 0;
|
|
}
|
|
|
|
void encoder_read_with_encoder(encoder_usage_t encoder, uint32_t *encoder_value)
|
|
{
|
|
switch (encoder)
|
|
{
|
|
case CAMERA_ENCODER:
|
|
*encoder_value = m_encoders[CAMERA_ENCODER].count;
|
|
break;
|
|
case DRIVEN_ENCODER_GEAR:
|
|
*encoder_value = m_encoders[DRIVEN_ENCODER_GEAR].count;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void encoder_light_schedule(bool netif_link_status, bool *camera_encoder_flag, bool *driven_encoder_gear_flag)
|
|
{
|
|
static uint8_t camera_encoder_light_flicker_count = 0;
|
|
static uint8_t driven_encoder_gear_light_flicker_count = 0;
|
|
if (netif_link_status)
|
|
{
|
|
if (*camera_encoder_flag)
|
|
{
|
|
if (sys_haspassedms(camera_encoder_lastprocess) > 100)
|
|
{
|
|
camera_encoder_lastprocess = HAL_GetTick();
|
|
HAL_GPIO_TogglePin(DIS_G1_GPIO_Port, DIS_G1_Pin);
|
|
camera_encoder_light_flicker_count += 1;
|
|
}
|
|
if (camera_encoder_light_flicker_count > 6)
|
|
{
|
|
encoder_light_switch_set_color(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_GREEN);
|
|
camera_encoder_light_flicker_count = 0;
|
|
*camera_encoder_flag = false;
|
|
}
|
|
}
|
|
|
|
if (*driven_encoder_gear_flag)
|
|
{
|
|
if (sys_haspassedms(driven_encoder_gear_lastprocess) > 100)
|
|
{
|
|
driven_encoder_gear_lastprocess = HAL_GetTick();
|
|
HAL_GPIO_TogglePin(DIS_G2_GPIO_Port, DIS_G2_Pin);
|
|
driven_encoder_gear_light_flicker_count += 1;
|
|
}
|
|
if (driven_encoder_gear_light_flicker_count > 6)
|
|
{
|
|
encoder_light_switch_set_color(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_GREEN);
|
|
driven_encoder_gear_light_flicker_count = 0;
|
|
*driven_encoder_gear_flag = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
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(encoder_usage_t encoder, encoder_light_color_table_t color)
|
|
{
|
|
if (encoder == CAMERA_ENCODER)
|
|
{
|
|
switch (color)
|
|
{
|
|
case ENCODER_LIGHT_COLOR_RED:
|
|
/* 红色 */
|
|
HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_GREEN:
|
|
/* 绿色 */
|
|
HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_BLUE:
|
|
/* 蓝色 */
|
|
HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_RESET);
|
|
break;
|
|
|
|
default:
|
|
/* not find color */
|
|
break;
|
|
}
|
|
}
|
|
else if (encoder == DRIVEN_ENCODER_GEAR)
|
|
{
|
|
switch (color)
|
|
{
|
|
case ENCODER_LIGHT_COLOR_RED:
|
|
/* 红色 */
|
|
HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_GREEN:
|
|
/* 绿色 */
|
|
HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
|
|
break;
|
|
case ENCODER_LIGHT_COLOR_BLUE:
|
|
/* 蓝色 */
|
|
HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_RESET);
|
|
break;
|
|
|
|
default:
|
|
/* not find color */
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// error
|
|
}
|
|
}
|
|
|
|
void encoder_switch_close_light(encoder_usage_t encoder)
|
|
{
|
|
if (encoder == CAMERA_ENCODER)
|
|
{
|
|
HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
|
|
}
|
|
else if (encoder == DRIVEN_ENCODER_GEAR)
|
|
{
|
|
HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
|
|
}
|
|
else
|
|
{
|
|
// error
|
|
}
|
|
}
|
|
|
|
encoder_t *encoder_get_camera_encoder_structer(void)
|
|
{
|
|
return &m_encoders[CAMERA_ENCODER];
|
|
}
|
|
|
|
encoder_t *encoder_get_driven_encoder_gear_structer(void)
|
|
{
|
|
return &m_encoders[DRIVEN_ENCODER_GEAR];
|
|
}
|