编辑
2025-02-15
C# 应用
00
请注意,本文编写于 80 天前,最后修改于 80 天前,其中某些信息可能已经过时。

目录

实现原理
完整代码实现
3. 实现说明
3.1 核心参数设置
3.2 时间计算
3.3 位置计算
结语

实现原理

速度曲线规划的实现原理基于三个连续的运动阶段,通过精确控制加速度和速度来实现平滑的运动过程。

在匀加速阶段,物体从静止状态以恒定加速度Amax加速,直到达到最大速度Vmax;随后进入匀速阶段,物体保持最大速度Vmax匀速运动;最后是匀减速阶段,以相同的加速度Amax减速直至停止。整个过程的关键参数包括最大速度Vmax(决定运动的快慢)、最大加速度Amax(控制加减速的剧烈程度)以及目标位置Pf(确定运动的终点)。这些参数共同决定了运动的特性和总时长,确保运动过程既能保持效率,又能实现平稳过渡。

完整代码实现

示例代码中设定的参数值为:最大速度200像素/秒,最大加速度100像素/秒²,目标位置500像素,通过这些参数可以精确计算出每个阶段的时间和位移。

image.png

C#
public partial class Form1 : Form { private Timer animationTimer; private float currentTime = 0; private float currentPosition = 0; // T曲线参数 private const float Vmax = 200; // 最大速度(像素/秒) private const float Amax = 100; // 最大加速度(像素/秒²) private const float Pf = 500; // 目标位置(像素) // 计算关键时间点 private readonly float Ta; // 加速时间 private readonly float Tm; // 匀速时间 private readonly float Tf; // 总时间 private const int BallRadius = 20; private readonly Brush ballBrush = Brushes.Blue; public Form1() { InitializeComponent(); this.DoubleBuffered = true; // 计算运动参数 Ta = Vmax / Amax; float Pa = 0.5f * Amax * Ta * Ta; Tm = (Pf - 2 * Pa) / Vmax; Tf = Tm + 2 * Ta; // 设置定时器 animationTimer = new Timer(); animationTimer.Interval = 16; // ~60fps animationTimer.Tick += AnimationTimer_Tick; this.Paint += TCurveForm_Paint; this.Load += (s, e) => animationTimer.Start(); } private void AnimationTimer_Tick(object sender, EventArgs e) { currentTime += 0.016f; // 增加时间 // 计算当前位置 if (currentTime <= Ta) { // 加速阶段 currentPosition = 0.5f * Amax * currentTime * currentTime; } else if (currentTime <= Ta + Tm) { // 匀速阶段 float t = currentTime - Ta; currentPosition = 0.5f * Amax * Ta * Ta + Vmax * t; } else if (currentTime <= Tf) { // 减速阶段 float t = currentTime - Ta - Tm; currentPosition = 0.5f * Amax * Ta * Ta + Vmax * Tm + Vmax * t - 0.5f * Amax * t * t; } else { // 运动结束,重置动画 currentTime = 0; currentPosition = 0; } this.Invalidate(); } private void TCurveForm_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 绘制轨迹线 g.DrawLine(Pens.Gray, 50, 200, 750, 200); // 绘制小球 float x = 50 + currentPosition; g.FillEllipse(ballBrush, x - BallRadius / 2, 200 - BallRadius / 2, BallRadius, BallRadius); // 绘制信息 string info = $"Time: {currentTime:F2}s\nPosition: {currentPosition:F2}px"; g.DrawString(info, SystemFonts.DefaultFont, Brushes.Black, 10, 10); } }

3. 实现说明

3.1 核心参数设置

C#
private const float Vmax = 200; // 最大速度(像素/秒) private const float Amax = 100; // 最大加速度(像素/秒²) private const float Pf = 500; // 目标位置(像素)

3.2 时间计算

C#
Ta = Vmax / Amax; // 加速时间 float Pa = 0.5f * Amax * Ta * Ta; // 加速距离 Tm = (Pf - 2 * Pa) / Vmax; // 匀速时间 Tf = Tm + 2 * Ta; // 总时间

3.3 位置计算

根据不同阶段计算小球位置:

  • 加速阶段:P = 1/2 * a * t²
  • 匀速阶段:P = Pa + v * t
  • 减速阶段:P = Pa + v * Tm + v * t - 1/2 * a * t²

结语

本文详细介绍了速度曲线规划的实现原理及其在运动控制中的应用。通过匀加速、匀速、匀减速三个阶段的精确计算,结合最大速度、最大加速度和目标位置等关键参数,能够实现平滑、高效的运动过程。示例代码展示了如何利用这些原理在动画中实现小球的平稳运动,确保启停过程自然流畅。这种曲线规划方法不仅适用于动画效果,还广泛应用于工业机器人和自动化设备的运动控制中,为实现高精度、高效率的运动提供了可靠的解决方案。

本文作者:rick

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!