为什么时间控制不够?Why Isn't Time-Based Control Enough?
1.1 回顾:用时间控制移动1.1 Review: Moving with Time Control
在 Level 1,我们学过用时间控制机器人走直线:In Level 1, we learned how to make the robot drive straight using time control:
// 向前走 2 秒
LeftMotor.spin(forward);
RightMotor.spin(forward);
wait(2, seconds);
LeftMotor.stop();
RightMotor.stop();
// 向前走 2 秒
LeftMotor.spin(forward);
RightMotor.spin(forward);
wait(2, seconds);
LeftMotor.stop();
RightMotor.stop();
// 向前走 2 秒
left_motor.move(127);
right_motor.move(127);
pros::delay(2000);
left_motor.move(0);
right_motor.move(0);
看起来挺好用的,对吧?但是试试看——Seems to work fine, right? But try this --
1.2 问题来了:跑 3 次,停 3 个位置1.2 The Problem: Run 3 Times, Stop at 3 Different Places
把上面的代码烧进机器人,连续跑 3 次,你会发现:Upload the code above to your robot and run it 3 times in a row. You'll find:
- 第 1 次:停在了 60 厘米的位置Run 1: Stopped at the 60 cm mark
- 第 2 次:停在了 55 厘米的位置Run 2: Stopped at the 55 cm mark
- 第 3 次:停在了 63 厘米的位置Run 3: Stopped at the 63 cm mark
同样的代码,每次停的位置都不一样!Same code, different stopping position every time!
时间控制的问题在于:你只告诉机器人"跑多久",但没告诉它"跑多远"。The problem with time control is: you only tell the robot "how long to run," not "how far to go."
影响距离的因素有很多:Many factors affect the actual distance:
- 电池电量Battery level — 满电跑得快,半电跑得慢,同样 2 秒距离不一样Full battery = faster, half battery = slower. Same 2 seconds, different distance
- 地面摩擦力Surface friction — 光滑地面和粗糙地毯,摩擦力不同Smooth floors vs rough carpet have different friction
- 机器人重量Robot weight — 加了球以后变重,加速变慢Heavier with balls loaded, slower to accelerate
- 惯性Inertia — 电机停了,机器人还会滑一段Motors stop but the robot keeps sliding
1.3 转弯也一样不准1.3 Turning Is Just as Inaccurate
用时间控制转弯更惨:Time-based turning is even worse:
// 想转 90 度
LeftMotor.spin(forward);
RightMotor.spin(reverse);
wait(0.8, seconds); // 试出来的时间
LeftMotor.stop();
RightMotor.stop();
// 想转 90 度
LeftMotor.spin(forward);
RightMotor.spin(reverse);
wait(0.8, seconds); // 试出来的时间
LeftMotor.stop();
RightMotor.stop();
// 想转 90 度
left_motor.move(127);
right_motor.move(-127);
pros::delay(800); // 试出来的时间
left_motor.move(0);
right_motor.move(0);
0.8 秒是你调试时"试出来"的。换了电池、换了场地、甚至换了个轮子,这个数就不准了。0.8 seconds is a value you "figured out" during testing. Change the battery, change the field, or even change a wheel, and that number is no longer accurate.
比赛场上没时间给你一次次试数值。你需要更靠谱的方法。There's no time at a competition to keep tweaking values. You need a more reliable approach.
1.4 解决方案:让机器人"看到"自己1.4 The Solution: Let the Robot "See" Itself
人走路不会闭着眼睛数秒数。你会看自己走到哪了,然后决定要不要继续走。When you walk, you don't close your eyes and count seconds. You look at where you are and decide whether to keep going.
机器人也可以这样做——用传感器。Robots can do the same thing -- with sensors.
传感器让机器人能"感知"自己的状态:Sensors let the robot "feel" its own state:
- 电机编码器Motor Encoder — 知道轮子转了多少度(走了多远)Knows how many degrees the wheels have turned (how far it's gone)
- 陀螺仪 IMUGyroscope IMU — 知道机器人朝哪个方向(转了多少度)Knows which direction the robot is facing (how far it's turned)
- 距离传感器Distance Sensor — 知道前面有多远有东西Knows how far away objects are
- 光学传感器Optical Sensor — 能看到颜色Can see colors
有了传感器,代码从"跑 2 秒"变成"跑到 60 厘米":With sensors, the code changes from "run for 2 seconds" to "run to 60 cm":
- 不管电池剩多少,跑到了就停No matter how much battery is left, it stops when it arrives
- 不管地面滑不滑,跑到了就停No matter how slippery the surface, it stops when it arrives
- 每次都停在同一个位置Stops at the same position every time
接下来几节课,我们一个个来学这些传感器。In the next few lessons, we'll learn about each of these sensors one by one.