修改位置参数
This commit is contained in:
parent
fcd6b4cfbf
commit
8880969627
File diff suppressed because it is too large
Load Diff
|
@ -26,9 +26,9 @@ static uint8_t business_inspection(struct flow *fl)
|
|||
case WORK_MOTOR_POS:
|
||||
ssd1306_clear_buffer();
|
||||
ssd1306_f8x16_string(0, 0, " TAR:");
|
||||
ssd1306_f8x16_number(48, 0, work.target_pos, 1);
|
||||
ssd1306_f8x16_number(48, 0, work.target_pos, 0);
|
||||
ssd1306_f8x16_string(0, 2, " CUR:");
|
||||
ssd1306_f8x16_number(48, 2, work.encoder_cnt, 1);
|
||||
ssd1306_f8x16_number(48, 2, work.encoder_cnt, 0);
|
||||
ssd1306_update_screen();
|
||||
break;
|
||||
case WORK_MOTOR_SPEED:
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "work.h"
|
||||
#include "board.h"
|
||||
#define OUTPUT_INFORMATION_CYCLE_BASE 100
|
||||
#define DISTANCE 1000
|
||||
work_t work;
|
||||
|
||||
/**
|
||||
|
@ -17,7 +18,7 @@ static void output_information(void)
|
|||
|
||||
if (work.type == WORK_MOTOR_POS)
|
||||
{
|
||||
rst = snprintf(data, ARRAY_LEN(data), "%f,%d,%f\r\n", work.target_pos, work.encoder_cnt, work.pwm_percent);
|
||||
rst = snprintf(data, ARRAY_LEN(data), "%d,%d,%f\r\n", work.target_pos, work.encoder_cnt, work.pwm_percent);
|
||||
}
|
||||
else if (work.type == WORK_MOTOR_SPEED)
|
||||
{
|
||||
|
@ -81,8 +82,8 @@ static void pwm_map_key_handle(button_id_e id)
|
|||
|
||||
static void motot_pos_key_handle(button_id_e id)
|
||||
{
|
||||
int8_t min = -100, max = 100;
|
||||
int8_t step = 10;
|
||||
int16_t min = 0, max = DISTANCE;
|
||||
int16_t step = 100;
|
||||
switch (id)
|
||||
{
|
||||
case KEY_ADD:
|
||||
|
@ -98,16 +99,11 @@ static void motot_pos_key_handle(button_id_e id)
|
|||
}
|
||||
break;
|
||||
case KEY_S:
|
||||
work.target_pos = 50;
|
||||
work.target_pos = 500;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (work.target_pos == 0)
|
||||
{
|
||||
work.encoder_cnt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void motor_speed_key_handle(button_id_e id)
|
||||
|
@ -134,6 +130,13 @@ void work_key_handle_cb(button_id_e id)
|
|||
}
|
||||
}
|
||||
|
||||
static void work_data_update(void)
|
||||
{
|
||||
work.pid.pid_u.fuzzy.set_kp(&work.pid.pid_u.fuzzy, work.pid_params.kp);
|
||||
work.pid.pid_u.fuzzy.set_ki(&work.pid.pid_u.fuzzy, work.pid_params.ki);
|
||||
work.pid.pid_u.fuzzy.set_kd(&work.pid.pid_u.fuzzy, work.pid_params.kd);
|
||||
}
|
||||
|
||||
void work_process(void)
|
||||
{
|
||||
switch (work.type)
|
||||
|
@ -154,10 +157,7 @@ void work_process(void)
|
|||
{
|
||||
output_information();
|
||||
}
|
||||
|
||||
work.pid.pid_u.fuzzy.set_kp(&work.pid.pid_u.fuzzy, work.pid_params.kp);
|
||||
work.pid.pid_u.fuzzy.set_ki(&work.pid.pid_u.fuzzy, work.pid_params.ki);
|
||||
work.pid.pid_u.fuzzy.set_kd(&work.pid.pid_u.fuzzy, work.pid_params.kd);
|
||||
work_data_update();
|
||||
}
|
||||
|
||||
void work_encoder_exti(void)
|
||||
|
@ -178,12 +178,12 @@ void work_init(void)
|
|||
PWM_START(PWM_TIM, PWM_CHANNEL);
|
||||
PWM_SET_DUTY(PWM_TIM, PWM_CHANNEL, 0);
|
||||
work.pwm_feq = PWM_GET_FREQ(PWM_TIM);
|
||||
|
||||
work.pwm_arr = PWM_GET_ARR(PWM_TIM);
|
||||
work.type = WORK_MOTOR_POS;
|
||||
work.pid_params.kp = 1;
|
||||
work.pid_params.ki = 0.01f;
|
||||
work.pid_params.kd = 5;
|
||||
work.pid_params.dead_zone = 0.5f;
|
||||
work.pid_params.kd = 10;
|
||||
work.pid_params.dead_zone = 0.5;
|
||||
// PID初始化
|
||||
|
||||
{
|
||||
|
@ -196,7 +196,7 @@ void work_init(void)
|
|||
work.pid.pid_u.fuzzy.set_kd_enable(&work.pid.pid_u.fuzzy, TRUE);
|
||||
}
|
||||
|
||||
work.target_pos = 20;
|
||||
work.target_pos = 500;
|
||||
work.timer_cycle = TIM_CYCLE(WORK_TIM);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -19,11 +19,11 @@ typedef struct
|
|||
{
|
||||
work_e type;
|
||||
uint8_t timer_cycle; // 定时器周期
|
||||
uint32_t enter_cnt; // 进入次数
|
||||
int32_t encoder_cnt; // 编码器位置
|
||||
float32 pwm_percent; // PWM占空比
|
||||
uint32_t pwm_feq; // PWM频率
|
||||
|
||||
uint32_t enter_cnt; // 进入次数
|
||||
int32_t encoder_cnt; // 编码器位置
|
||||
float32 pwm_percent; // PWM占空比
|
||||
uint32_t pwm_feq; // PWM频率
|
||||
uint16_t pwm_arr; // PWM自动重装载值
|
||||
pid_t pid;
|
||||
struct
|
||||
{
|
||||
|
@ -33,9 +33,8 @@ typedef struct
|
|||
float32 dead_zone;
|
||||
} pid_params;
|
||||
|
||||
float32 target_pos; // 目标位置
|
||||
int32_t target_pos; // 目标位置
|
||||
float32 target_speed; // 目标速度
|
||||
|
||||
} work_t;
|
||||
extern work_t work;
|
||||
|
||||
|
|
|
@ -673,49 +673,49 @@ void ssd1306_f8x16_number(uint8_t x, uint8_t y, float32 num, uint8_t dot_num)
|
|||
if (num >= 10000)
|
||||
{
|
||||
ch[i++] = num / 10000 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 10000 / 1000 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 1000 / 100 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10000 / 1000 + 48;
|
||||
ch[i++] = (int32_t)(num) % 1000 / 100 + 48;
|
||||
ch[i++] = (int32_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10 + 48;
|
||||
}
|
||||
else if (num >= 1000)
|
||||
{
|
||||
ch[i++] = (uint8_t)(num) % 10000 / 1000 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 1000 / 100 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10000 / 1000 + 48;
|
||||
ch[i++] = (int32_t)(num) % 1000 / 100 + 48;
|
||||
ch[i++] = (int32_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10 + 48;
|
||||
}
|
||||
else if (num >= 100)
|
||||
{
|
||||
ch[i++] = (uint8_t)(num) % 1000 / 100 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 1000 / 100 + 48;
|
||||
ch[i++] = (int32_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10 + 48;
|
||||
}
|
||||
else if (num >= 10)
|
||||
{
|
||||
ch[i++] = (uint8_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (uint8_t)(num) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 100 / 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10 + 48;
|
||||
}
|
||||
else if (num >= 0)
|
||||
{
|
||||
ch[i++] = (uint8_t)(num) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num) % 10 + 48;
|
||||
}
|
||||
if (dot_num > 0 && i < 7)
|
||||
{
|
||||
ch[i++] = '.';
|
||||
num = num - (uint8_t)num;
|
||||
num = num - (int32_t)num;
|
||||
|
||||
if (dot_num == 1 && i < 8)
|
||||
{
|
||||
num = num * 10;
|
||||
ch[i++] = (uint8_t)(num + 0.5) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num + 0.5) % 10 + 48;
|
||||
}
|
||||
if (dot_num >= 2 && i < 8)
|
||||
{
|
||||
num = num * 100;
|
||||
ch[i++] = (uint8_t)num % 100 / 10 + 48;
|
||||
ch[i++] = (int32_t)num % 100 / 10 + 48;
|
||||
if (i < 8)
|
||||
ch[i++] = (uint8_t)(num + 0.5) % 10 + 48;
|
||||
ch[i++] = (int32_t)(num + 0.5) % 10 + 48;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,9 @@
|
|||
LL_TIM_CC_DisableChannel(TIMx, CHx); \
|
||||
} while (__LINE__ == -1)
|
||||
|
||||
#define PWM_GET_ARR(TIMx) LL_TIM_GetAutoReload(TIMx)
|
||||
#define PWM_GET_PSC(TIMx) LL_TIM_GetPrescaler(TIMx)
|
||||
|
||||
/**
|
||||
* @brief Sets the PWM frequency
|
||||
* @param TIMx: TIM instance
|
||||
|
@ -87,4 +90,30 @@ static inline uint32_t PWM_GET_FREQ(TIM_TypeDef *TIMx)
|
|||
return SystemCoreClock / (LL_TIM_GetPrescaler(TIMx) + 1) / (LL_TIM_GetAutoReload(TIMx) + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取指定通道的PWM比较值
|
||||
*
|
||||
* 根据给定的定时器指针和通道编号,获取该通道的PWM比较值。
|
||||
*
|
||||
* @param TIMx 定时器指针,指向一个TIM_TypeDef结构体
|
||||
* @param CHx 通道编号,取值范围为LL_TIM_CHANNEL_CH1到LL_TIM_CHANNEL_CH4
|
||||
*
|
||||
* @return 返回一个uint16_t类型的值,表示指定通道的PWM比较值。如果通道编号无效,则返回0。
|
||||
*/
|
||||
static inline uint16_t PWM_GET_COMPARE(TIM_TypeDef *TIMx, uint32_t CHx)
|
||||
{
|
||||
switch (CHx)
|
||||
{
|
||||
case LL_TIM_CHANNEL_CH1:
|
||||
return LL_TIM_OC_GetCompareCH1(TIMx);
|
||||
case LL_TIM_CHANNEL_CH2:
|
||||
return LL_TIM_OC_GetCompareCH2(TIMx);
|
||||
case LL_TIM_CHANNEL_CH3:
|
||||
return LL_TIM_OC_GetCompareCH3(TIMx);
|
||||
case LL_TIM_CHANNEL_CH4:
|
||||
return LL_TIM_OC_GetCompareCH4(TIMx);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif ///< __PWMS_H__
|
||||
|
|
Loading…
Reference in New Issue