用时间写自动Time-Based Autonomous
2.1 最简单的方法:spin + wait + stop2.1 The Simplest Method: spin + wait + stop
写自动程序最直觉的方法:让电机转一段时间,然后停下来。The most intuitive way to write autonomous: spin the motors for a set time, then stop.
就像你告诉朋友:"往前走 2 秒,然后左转 1 秒"——用时间来控制距离和角度。Like telling a friend: "Walk forward for 2 seconds, then turn left for 1 second" -- using time to control distance and angle.
void autonomous() {
// 第 1 步:前进 2 秒
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(2, seconds);
// 第 2 步:停下
LeftMotor.stop();
RightMotor.stop();
// 第 3 步:右转 0.8 秒
LeftMotor.spin(forward, 40, percent);
RightMotor.spin(reverse, 40, percent);
wait(0.8, seconds);
// 第 4 步:停下
LeftMotor.stop();
RightMotor.stop();
// 第 5 步:再前进 1.5 秒
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(1.5, seconds);
// 第 6 步:停
LeftMotor.stop();
RightMotor.stop();
}
void autonomous() {
// 第 1 步:前进 2 秒
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(2, seconds);
// 第 2 步:停下
LeftMotor.stop();
RightMotor.stop();
// 第 3 步:右转 0.8 秒
LeftMotor.spin(forward, 40, percent);
RightMotor.spin(reverse, 40, percent);
wait(0.8, seconds);
// 第 4 步:停下
LeftMotor.stop();
RightMotor.stop();
// 第 5 步:再前进 1.5 秒
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(1.5, seconds);
// 第 6 步:停
LeftMotor.stop();
RightMotor.stop();
}
void autonomous() {
// 第 1 步:前进 2 秒
left_motor.move(64); // 64/127 ≈ 50%
right_motor.move(64);
pros::delay(2000); // 2000 毫秒 = 2 秒
// 第 2 步:停下
left_motor.move(0);
right_motor.move(0);
// 第 3 步:右转 0.8 秒
left_motor.move(50);
right_motor.move(-50);
pros::delay(800);
// 第 4 步:停下
left_motor.move(0);
right_motor.move(0);
// 第 5 步:再前进 1.5 秒
left_motor.move(64);
right_motor.move(64);
pros::delay(1500);
// 第 6 步:停
left_motor.move(0);
right_motor.move(0);
}
模式很简单:spin → wait → stop,重复这个套路就能组合出各种动作。The pattern is simple: spin -> wait -> stop, repeat this to build any sequence of movements.
2.2 先碰壁:跑 3 次结果不一样2.2 Hit the Wall: Different Results Every Time
用时间控制写完之后,你会发现一个大问题:After writing time-based control, you'll discover a big problem:
同一段代码,跑第一次前进了 60 厘米,跑第二次变成 55 厘米,跑第三次又变成 63 厘米。转弯角度也是,有时候转 85 度,有时候转 95 度。Same code: first run goes 60 cm, second run goes 55 cm, third run goes 63 cm. Turning is the same -- sometimes 85 degrees, sometimes 95 degrees.
为什么会这样?因为时间控制依赖太多不稳定的因素:Why does this happen? Because time control depends on too many unstable factors:
- 电池电量Battery level:满电时电机转得快,电量低时转得慢。同样 2 秒,跑的距离不一样: Motors spin faster on full charge, slower on low battery. Same 2 seconds, different distance
- 地面摩擦Surface friction:场地不同区域摩擦力不一样,轮子打滑程度不同: Different areas of the field have different friction, wheels slip differently
- 机器人重量Robot weight:装了新零件后变重,同样速度跑得更慢: Adding new parts makes it heavier, same speed but slower movement
- 轮子磨损Wheel wear:轮子用久了直径变小,同样转速跑的距离变短: Worn wheels have smaller diameter, same RPM but shorter distance
写一段"前进 2 秒"的代码,在同一块场地上连续跑 3 次。用尺子量每次前进的距离,看看差多少。Write a "drive forward for 2 seconds" program and run it 3 times on the same field. Measure the distance each time with a ruler and see how much it varies.
你会发现:差距可能有 5-10 厘米。在比赛中,这个误差可能让你错过得分物。You'll find: the difference can be 5-10 cm. In a match, this error could make you miss a scoring object.
2.3 时间控制适合什么场景?2.3 When Is Time Control Appropriate?
时间控制虽然不精确,但在某些场景下够用:Time control isn't precise, but it works for certain situations:
- 比赛前快速调试Quick pre-match testing:先用时间控制跑通流程,再换成编码器控制精调: Get the routine working with time first, then switch to encoder control for fine-tuning
- 不需要精确定位的动作Actions that don't need precision:比如"冲过去撞一下",差几厘米无所谓: Like "rush forward and ram something" -- a few cm off doesn't matter
- 机构动作Mechanism actions:比如"夹爪关闭 0.5 秒",误差不大: Like "close the claw for 0.5 seconds" -- small error is acceptable
时间控制是自动程序的起点,但不是终点。下一节我们会学用编码器替代时间,让动作精确可重复。Time control is a starting point for autonomous programs, but not the final solution. Next lesson we'll learn to use encoders instead of time for precise, repeatable movements.
用时间控制写一段自动程序:前进 1 秒 → 停下 → 左转 0.5 秒 → 停下。Write a time-based autonomous program: drive forward 1 second -> stop -> turn left 0.5 seconds -> stop.
写完之后回答:时间控制最大的缺点是什么?After writing, answer: What is the biggest drawback of time control?