167 lines
6.4 KiB
C
Raw Normal View History

2026-05-10 21:35:40 +08:00
#include "dehy_test.h"
#include "mw_soft_timer.h"
2026-05-11 10:11:13 +08:00
/*
2026-05-10 21:35:40 +08:00
2026-05-11 10:11:13 +08:00
current_speed取值范围为0至800 target_speed为0400500600700800result_speed后speed_set_valspeed_set_val来让current_speed达到speed_set_val的速度current_speed会掉下去
1. result_speed的初值设为此时的current_speedacc_val的值往上加acc_valresult_speed1 result_speed = result_speed+acc_valresult_speed继续加acc_valresult_speed超过了target_speedresult_speed改为目标速度result_speed一直保持在target_speed
2. target_speed突然变小current_speed大于新的target_speedresult_speed一直保持在当前速度不改变result_speed继续根据加速度a来加
3. target_speed突然变大result_speed继续根据加速度acc_val来增加target_speed
4. target_speed为0result_speed就要变成0.
5.
6. result_speed不要下降
*/
2026-05-10 21:35:40 +08:00
/* 脱水速度设置 枚举变量 */
2026-05-10 21:35:40 +08:00
typedef enum
{
DEHY_SPEEDSET_INIT, // 初始化
DEHY_SPEEDSET_SPEED_UP, // 加速
DEHY_SPEEDSET_SPEED_KEEP, // 稳速
DEHY_SPEEDSET_SPEED_STOP, // 超震停机
2026-05-10 21:35:40 +08:00
DEHY_SPEEDSET_NUM
}SPEEDUP_ENUM_T;
2026-05-10 21:35:40 +08:00
/* 外部调用 速度设置值 */
static uint16_t speed_set_val = 0u;
/*************************************************************************************
* @brief
* @param[in/out] val
*
* @warning
* @note
*************************************************************************************/
void motor_speed_set(uint16_t val)
2026-05-10 21:35:40 +08:00
{
speed_set_val = val;
2026-05-10 21:35:40 +08:00
}
void dehy_speed_up(LOGIC_STATE_ENUM_T init_cmd, uint16_t acc_val, uint16_t current_speed, uint16_t target_speed)
2026-05-10 21:35:40 +08:00
{
static uint32_t timetick = 0; // 时间戳
static SPEEDUP_ENUM_T add_speed_state = DEHY_SPEEDSET_INIT; // 本函数的状态机
2026-05-10 21:35:40 +08:00
static uint16_t result_speed = 0; // 预设速度
static uint16_t target_speed_cache = 0; // 缓存目标速度
// static uint16_t current_speed_cache = 0; // 缓存当前速度
if(init_cmd == LOGIC_INIT)
{
add_speed_state = DEHY_SPEEDSET_INIT;
}
else
2026-05-10 21:35:40 +08:00
{
;
2026-05-10 21:35:40 +08:00
}
switch(add_speed_state)
{
// init
case DEHY_SPEEDSET_INIT:
2026-05-10 21:35:40 +08:00
// 时间戳 初始化
timetick = get_systick_ms();
// 预设速度 初始化
result_speed = current_speed;
// 目标速度缓存 初始化
target_speed_cache = target_speed;
// // 刷新 当前速度缓存
// current_speed_cache = current_speed;
// 进入下一阶段
add_speed_state = DEHY_SPEEDSET_SPEED_UP;
2026-05-10 21:35:40 +08:00
break;
// 加速
case DEHY_SPEEDSET_SPEED_UP:
2026-05-10 21:35:40 +08:00
// 当前 触发超震
if(target_speed == 0)
{
add_speed_state = DEHY_SPEEDSET_SPEED_STOP;
2026-05-10 21:35:40 +08:00
result_speed = 0;
}
// 周期到
else if(get_systick_ms() - timetick > 1000u)
{
timetick = get_systick_ms();
// 目标速度 小于 目标缓存速度,说明外面超震大 目标速度变小了 高4-> 高3
if(target_speed < target_speed_cache)
{
// 若当前速度 小于 现在的目标速度 则没事 更新一下目标速度缓存 当前就不加速了
if(current_speed < target_speed)
{
// // 刷新 目标速度缓存
// target_speed_cache = target_speed;
// // 刷新 当前速度缓存
// current_speed_cache = current_speed;
}
else
{
// 否则保持当前缓存的速度 稳住
// result_speed = current_speed_cache;
// 记录当前速度
result_speed = current_speed;
// 切到速度保持
add_speed_state = DEHY_SPEEDSET_SPEED_KEEP;
2026-05-10 21:35:40 +08:00
}
}
else
{
// // 刷新 目标速度缓存
// target_speed_cache = target_speed;
// // 刷新 当前速度缓存
// current_speed_cache = current_speed;
// 更新 预设速度
result_speed += acc_val;
// 进下一阶段判断
if(result_speed >= target_speed)
{
result_speed = target_speed;
add_speed_state = DEHY_SPEEDSET_SPEED_KEEP;
2026-05-10 21:35:40 +08:00
}
}
// 刷新 目标速度缓存
target_speed_cache = target_speed;
}
break;
// 速度保持
case DEHY_SPEEDSET_SPEED_KEEP:
2026-05-10 21:35:40 +08:00
// 超震停机
if(target_speed == 0)
{
result_speed = 0;
add_speed_state = DEHY_SPEEDSET_SPEED_STOP;
2026-05-10 21:35:40 +08:00
}
// 目标速度变好了 外面超震小了
else if(target_speed > target_speed_cache)
{
// 若预定速度 小于 新的目标速度,则回去加速
if(result_speed < target_speed)
{
// 回加速
add_speed_state = DEHY_SPEEDSET_SPEED_UP;
}
2026-05-10 21:35:40 +08:00
target_speed_cache = target_speed;
// // 刷新 当前速度缓存
// current_speed_cache = current_speed;
}
break;
// 超震停机
case DEHY_SPEEDSET_SPEED_STOP:
2026-05-10 21:35:40 +08:00
result_speed = 0;
break;
default:
break;
}
/* 设置速度 */
motor_speed_set(result_speed);
2026-05-10 21:35:40 +08:00
}