0 / 5 页已完成

综合挑战Combined Challenge

5.1 挑战目标5.1 Challenge Goal

在 Level 1,你学会了用时间控制机器人直走和转弯。现在,用你在 Level 3 学到的传感器知识,重写那段代码In Level 1, you learned to make the robot drive and turn using time control. Now, use the sensor knowledge from Level 3 to rewrite that code.

任务Task

让机器人完成以下动作:Make the robot complete these actions:

  1. 向前走 600 度(用编码器控制)Drive forward 600 degrees (using encoder)
  2. 右转 90 度(用 IMU 控制)Turn right 90 degrees (using IMU)
  3. 再向前走 400 度(用编码器控制)Drive forward another 400 degrees (using encoder)
  4. 停下Stop

5.2 要求5.2 Requirements

5.3 思路提示5.3 Hints

不知道怎么开始?点开看提示:Not sure where to start? Open the hints below:

提示 1:整体结构Hint 1: Overall Structure
提示 2:编码器直走的模板Hint 2: Encoder Driving Template
提示 3:IMU 转弯的模板Hint 3: IMU Turning Template

5.4 参考答案5.4 Reference Answer

自己写完了再看!Write your own code first before looking!

完整参考代码Complete Reference Code
检查点:代码审查Checkpoint: Code Review
下面的代码有一个 Bug。机器人先走了 600 度,然后想再走 400 度,但第二段直走不动了。为什么?The code below has a bug. The robot drives 600 degrees first, then tries to drive 400 more degrees, but the second drive doesn't move. Why?
C++ (VEXcode)
// 第一段
LeftMotor.setPosition(0, degrees);
LeftMotor.spin(forward);
while (LeftMotor.position(degrees) < 600) {
    wait(20, msec);
}
LeftMotor.stop();

// 第二段(Bug 在这里!)
LeftMotor.spin(forward);
while (LeftMotor.position(degrees) < 400) {
    wait(20, msec);
}
LeftMotor.stop();
C++ (VS Code)
// 第一段
LeftMotor.setPosition(0, degrees);
LeftMotor.spin(forward);
while (LeftMotor.position(degrees) < 600) {
    wait(20, msec);
}
LeftMotor.stop();

// 第二段(Bug 在这里!)
LeftMotor.spin(forward);
while (LeftMotor.position(degrees) < 400) {
    wait(20, msec);
}
LeftMotor.stop();
C++ (PROS)
// 第一段
left_motor.set_zero_position(0);
left_motor.move(80);
while (left_motor.get_position() < 600) {
    pros::delay(20);
}
left_motor.move(0);

// 第二段(Bug 在这里!)
left_motor.move(80);
while (left_motor.get_position() < 400) {
    pros::delay(20);
}
left_motor.move(0);