用时间控制机构Controlling Mechanisms with Time
1.1 最简单的方法:转几秒停下来1.1 The Simplest Way: Spin for a Few Seconds
在 Level 1 里,我们学会了让电机转动。现在假设机器人有一个升降臂(Lift),我们想让它升到一定高度。In Level 1, we learned how to make motors spin. Now suppose your robot has a lift arm, and we want to raise it to a certain height.
最直觉的方法:让电机转,等几秒,然后停。The most intuitive way: spin the motor, wait a few seconds, then stop.
// 升降臂上升 2 秒
LiftMotor.spin(forward);
wait(2, seconds);
LiftMotor.stop();
// 升降臂上升 2 秒
LiftMotor.spin(forward);
wait(2, seconds);
LiftMotor.stop();
// 升降臂上升 2 秒
LiftMotor.move(127);
pros::delay(2000);
LiftMotor.move(0);
逻辑很简单:开始转 → 等一会儿 → 停。The logic is simple: start spinning → wait → stop.
1.2 先碰壁再教1.2 Learn by Hitting the Wall
时间控制看起来很好用,但你很快会发现一个问题:Time-based control seems great, but you'll quickly discover a problem:
同样是 wait(2, seconds),有时候臂升得高,有时候升得低。为什么?With the same wait(2, seconds), sometimes the arm goes higher, sometimes lower. Why?
因为电池电压会变。充满电的时候电机转得快,电量低的时候电机转得慢。同样的 2 秒,转过的距离不一样。Because battery voltage changes. When fully charged, motors spin faster; when low on battery, motors spin slower. The same 2 seconds covers a different distance.
除了电池电压,还有这些因素会影响结果:Besides battery voltage, these factors also affect the result:
- 负载变化 — 臂上夹着球和空臂,阻力不一样Load changes — the arm holding a ball vs. empty has different resistance
- 摩擦力 — 机械磨损后摩擦变大,同样时间转得更少Friction — mechanical wear increases friction, resulting in less rotation in the same time
- 齿轮打滑 — 偶尔打滑一下,时间到了但没到位Gear slipping — occasional slips mean time is up but the arm hasn't reached the target
时间控制是"开环"的 — 你不知道电机实际转了多少。后面学了传感器(Level 3),就能用"闭环"方式精确控制。Time-based control is "open-loop" — you don't know how far the motor actually moved. After learning sensors (Level 3), you can use "closed-loop" control for precision.
但时间控制也不是完全没用!对精度要求不高的动作(比如吐球、短暂启动),用时间控制完全够了。But time-based control isn't useless! For actions that don't need precision (like ejecting balls or brief starts), it works just fine.
1.3 动手练习1.3 Practice
假设你的机器人有一个吸球电机(Intake),写代码让它:Suppose your robot has an intake motor. Write code to make it:
- 正转吸球 3 秒Spin forward to intake balls for 3 seconds
- 停 1 秒Stop for 1 second
- 反转吐球 1 秒Spin reverse to eject balls for 1 second
- 停下来Stop
spin(forward) → wait(2秒) → stop()。比赛打到第三场,电池只剩 60%。和第一场满电相比,2 秒后臂的位置会怎样?Your lift program is
spin(forward) → wait(2s) → stop().By the third match, the battery is down to 60%. Compared to the first match at full charge, where will the arm be after 2 seconds?