0 / 6 页已完成

用时间写自动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.

C++ (VEXcode)
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();
}
C++ (VS Code)
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();
}
C++ (PROS)
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:

问题:每次跑的结果都不一样!Problem: Different results every run!

同一段代码,跑第一次前进了 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:

动手验证Try It Yourself

写一段"前进 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:

关键认识Key Takeaway

时间控制是自动程序的起点,但不是终点。下一节我们会学用编码器替代时间,让动作精确可重复。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.

检查点:写一段时间控制自动Checkpoint: Write a Time-Based Autonomous

用时间控制写一段自动程序:前进 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?