# 速度平滑器 **Repository Path**: aionoiya/speed-smoother ## Basic Information - **Project Name**: 速度平滑器 - **Description**: 项目基于恒定加速度和t型加速度的速度平滑器 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 18 - **Forks**: 0 - **Created**: 2025-07-29 - **Last Updated**: 2026-01-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 速度平滑器:一个类来处理速度的平滑加速和减速 1. 类设计 1.1初始化:设置加速度和减速度 1.2更新函数:每次传入目标速度 1.3可以设置最大速度、加速度等参数 2.数据结构 2.1结构体Velocity 结构体 成员 类型 Velocity linear Double angular Double 2.2结构体AccelerationLimits AccelerationLimits 参数 说明 max_linear 默认最大线速度 max_angular 默认最大角速度 linear_acc 默认线加速度 linear_deacc 默认线减速度 angular_acc 默认角加速度 angular_deacc 默认角减速度 2.3函数void VelocitySmoother::updateTarget() updateTarget 参数 说明 double linear 目标线速度 double angular 目标角速度 2.4函数getCurrentVelocity() getCurrentVelocity 参数 说明 获取当前平滑后的速度 2.5函数reset() reset 参数 说明 处理急停 2.6内部变量 变量 类型 描述 mutex_ mutable std::mutex 线程安全锁 limits_ AccelerationLimits 当前加速度限制参数 target_linear_ double 目标线速度 target_angular_ double 目标角速度 current_linear_ double 当前平滑线速度 current_angular_ double 当前平滑角速度 last_update_time_ std::chrono::steady_clock::time_point 上次更新时间戳 error double 速度误差 = 目标速度 - 当前速度 abs_error double 速度误差的绝对值 max_acc AccelerationLimits 最大加速度(来自配置) max_deacc AccelerationLimits 最大减速度(来自配置) dt double 时间间隔(秒) t_acc double 计算得到的实际加速度值 3.算法切换 enum AlgorithmType { CONSTANT_ACCELERATION, // 恒定加速度算法 T_CURVE_ACCELERATION // T型加速度算法 }; // 4.使用示例 int main() { // 创建平滑器实例 VelocitySmoother smoother; //smoother.setAlgorithm(VelocitySmoother::T_CURVE_ACCELERATION); // 设置目标速度 smoother.updateTarget(1.5, 0.8); // 控制循环 for (int i = 0; i < 10; i++) { // 更新目标(实际应用中目标可能变化) smoother.updateTarget(1.5, 0.8); // 获取当前平滑速度 auto velocity = smoother.getCurrentVelocity(); printf("Step %d: Linear=%.2f m/s, Angular=%.2f rad/s\n", i, velocity.linear, velocity.angular); } return 0;} 4.自定义参数 VelocitySmoother::AccelerationLimits custom_params( 2.0, // max_linear 1.0, // max_angular 1.0, // linear_acc 1.5, // linear_deacc 1.2, // angular_acc 1.8 // angular_deacc); // 使用自定义参数创建平滑器 VelocitySmoother custom_smoother(custom_params); 5.紧急处理速度平滑器:一个类来处理速度的平滑加速和减速 1. 类设计 1.1初始化:设置加速度和减速度 1.2更新函数:每次传入目标速度 1.3可以设置最大速度、加速度等参数 2.数据结构 2.1结构体Velocity 结构体 成员 类型 Velocity linear Double angular Double 2.2结构体AccelerationLimits AccelerationLimits 参数 说明 max_linear 默认最大线速度 max_angular 默认最大角速度 linear_acc 默认线加速度 linear_deacc 默认线减速度 angular_acc 默认角加速度 angular_deacc 默认角减速度 2.3函数void VelocitySmoother::updateTarget() updateTarget 参数 说明 double linear 目标线速度 double angular 目标角速度 2.4函数getCurrentVelocity() getCurrentVelocity 参数 说明 获取当前平滑后的速度 2.5函数reset() reset 参数 说明 处理急停 2.6内部变量 变量 类型 描述 mutex_ mutable std::mutex 线程安全锁 limits_ AccelerationLimits 当前加速度限制参数 target_linear_ double 目标线速度 target_angular_ double 目标角速度 current_linear_ double 当前平滑线速度 current_angular_ double 当前平滑角速度 last_update_time_ std::chrono::steady_clock::time_point 上次更新时间戳 error double 速度误差 = 目标速度 - 当前速度 abs_error double 速度误差的绝对值 max_acc AccelerationLimits 最大加速度(来自配置) max_deacc AccelerationLimits 最大减速度(来自配置) dt double 时间间隔(秒) t_acc double 计算得到的实际加速度值 3.算法切换 enum AlgorithmType { CONSTANT_ACCELERATION, // 恒定加速度算法 T_CURVE_ACCELERATION // T型加速度算法 }; // 4.使用示例 int main() { // 创建平滑器实例 VelocitySmoother smoother; //smoother.setAlgorithm(VelocitySmoother::T_CURVE_ACCELERATION); // 设置目标速度 smoother.updateTarget(1.5, 0.8); // 控制循环 for (int i = 0; i < 10; i++) { // 更新目标(实际应用中目标可能变化) smoother.updateTarget(1.5, 0.8); // 获取当前平滑速度 auto velocity = smoother.getCurrentVelocity(); printf("Step %d: Linear=%.2f m/s, Angular=%.2f rad/s\n", i, velocity.linear, velocity.angular); } return 0; } 4.自定义参数 VelocitySmoother::AccelerationLimits custom_params( 2.0, // max_linear 1.0, // max_angular 1.0, // linear_acc 1.5, // linear_deacc 1.2, // angular_acc 1.8 // angular_deacc); // 使用自定义参数创建平滑器 VelocitySmoother custom_smoother(custom_params); 5.紧急处理