Initial commit: SmartCar Framework v0.1 — 龙芯2K0300智能车开发框架\n\n- HAL: GPIO/PWM/Encoder/Framebuffer 驱动\n- Control: PID/IMU/Motor/Servo 控制\n- Vision: HSV双Otsu→4点标定IPM→洪泛填充→逐行搜线\n- Strategy: 三区前瞻偏差+速度策略\n- Debug: 文件热调参+LCD预览+cv截帧\n- Scheduler: 5ms timerfd+epoll 中央调度器
This commit is contained in:
47
strategy/speed.cpp
Normal file
47
strategy/speed.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "speed.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
static float current_speed = 0.0f;
|
||||
static float max_speed = 40.0f;
|
||||
static float accel_rate = 2.0f;
|
||||
static float decel_rate = 5.0f;
|
||||
|
||||
void speed_strategy_reset()
|
||||
{
|
||||
current_speed = 0.0f;
|
||||
}
|
||||
|
||||
float calc_base_speed(const TrackInfo& info)
|
||||
{
|
||||
float target = max_speed * 0.7f; // 默认中速
|
||||
|
||||
switch (info.scene)
|
||||
{
|
||||
case TRACK_STRAIGHT:
|
||||
target = max_speed;
|
||||
break;
|
||||
case TRACK_GENTLE_CURVE:
|
||||
target = max_speed * 0.6f;
|
||||
break;
|
||||
case TRACK_SHARP_CURVE:
|
||||
target = max_speed * 0.35f;
|
||||
break;
|
||||
case TRACK_LOST_LINE:
|
||||
target = max_speed * 0.2f;
|
||||
break;
|
||||
case TRACK_CROSS:
|
||||
target = max_speed * 0.5f;
|
||||
break;
|
||||
}
|
||||
|
||||
if (std::fabs(info.deviation) > 0.6f)
|
||||
target *= 0.6f;
|
||||
|
||||
float diff = target - current_speed;
|
||||
float rate = (diff > 0) ? accel_rate : decel_rate;
|
||||
current_speed += rate * (diff > 0 ? 1 : -1) * 0.01f;
|
||||
current_speed = std::clamp(current_speed, 0.0f, max_speed);
|
||||
|
||||
return current_speed;
|
||||
}
|
||||
Reference in New Issue
Block a user