0 / 5 页已完成

陀螺仪 IMUGyroscope IMU

3.1 IMU 是什么?3.1 What Is an IMU?

IMU(Inertial Measurement Unit,惯性测量单元)就是一个方向传感器An IMU (Inertial Measurement Unit) is a direction sensor.

简单说:它能告诉你机器人现在朝哪个方向Simply put: it tells you which direction the robot is facing.

就像你手机里的指南针一样——不管你怎么转,它都知道你面朝哪边。Just like the compass in your phone -- no matter how you turn, it always knows which way you're facing.

IMU 的安装Installing the IMU

VEX V5 的 IMU 是一个独立的传感器模块,用智能线连到 Brain 的某个端口。The VEX V5 IMU is a standalone sensor module connected to the Brain via a smart cable.

它应该安装在机器人重心附近,尽量水平放置,远离强磁铁和电机。It should be mounted near the robot's center of gravity, as level as possible, and away from strong magnets and motors.

3.2 校准(必须!)3.2 Calibration (Required!)

IMU 在使用前必须校准。校准大约需要 2-3 秒,期间机器人不能动The IMU must be calibrated before use. Calibration takes about 2-3 seconds, during which the robot must stay still.

C++ (VEXcode)
// 创建 IMU(端口 10)
inertial IMU = inertial(PORT10);

// 开始校准
IMU.calibrate();

// 等校准完成(不能动!)
while (IMU.isCalibrating()) {
    wait(50, msec);
}

// 校准完成,可以开始用了
C++ (VS Code)
// 创建 IMU(端口 10)
inertial IMU = inertial(PORT10);

// 开始校准
IMU.calibrate();

// 等校准完成(不能动!)
while (IMU.isCalibrating()) {
    wait(50, msec);
}

// 校准完成,可以开始用了
C++ (PROS)
// 创建 IMU(端口 10)
pros::Imu imu(10);

// 开始校准并等待完成
imu.reset();
// reset() 会自动等待校准完成(约 2 秒)
// 期间机器人不能动!

// 校准完成,可以开始用了
注意Warning

校准期间如果机器人在动(比如有人在搬它),校准结果会不准,后面所有读数都会偏。If the robot moves during calibration (e.g., someone is carrying it), the calibration will be inaccurate and all subsequent readings will be off.

比赛时,通常在程序最开始校准 IMU,此时机器人放在场上不动。During competitions, the IMU is usually calibrated at the very start of the program while the robot sits still on the field.

3.3 heading 和 rotation 的区别3.3 The Difference Between heading and rotation

IMU 有两个常用的读数:headingrotation。它们很容易搞混,所以我们仔细看一下。The IMU has two commonly used readings: heading and rotation. They're easy to mix up, so let's look at them carefully.

C++ (VEXcode)
double h = IMU.heading(degrees);   // 范围:0 ~ 360
double r = IMU.rotation(degrees);  // 范围:无限(可以超过 360)
C++ (VS Code)
double h = IMU.heading(degrees);   // 范围:0 ~ 360
double r = IMU.rotation(degrees);  // 范围:无限(可以超过 360)
C++ (PROS)
double h = imu.get_heading();   // 范围:0 ~ 360
double r = imu.get_rotation();  // 范围:无限(可以超过 360)

heading(航向):就像表盘上的指针,永远在 0 到 360 之间。 (heading): Like a clock hand, always between 0 and 360.

rotation(累积旋转):就像里程表,只管累加。 (cumulative rotation): Like an odometer, it just keeps adding up.

举个例子Example

机器人从正前方开始,向右转了 450 度(转了一圈多 90 度):The robot starts facing forward and turns right 450 degrees (one full turn plus 90 degrees):

3.4 实用代码:转到目标角度3.4 Practical Code: Turn to a Target Angle

最常见的用法:让机器人转到指定角度。The most common use: making the robot turn to a specific angle.

C++ (VEXcode)
// 右转到 90 度
LeftMotor.spin(forward);
RightMotor.spin(reverse);

while (IMU.heading(degrees) < 90) {
    wait(20, msec);
}

LeftMotor.stop();
RightMotor.stop();
C++ (VS Code)
// 右转到 90 度
LeftMotor.spin(forward);
RightMotor.spin(reverse);

while (IMU.heading(degrees) < 90) {
    wait(20, msec);
}

LeftMotor.stop();
RightMotor.stop();
C++ (PROS)
// 右转到 90 度
left_motor.move(80);
right_motor.move(-80);

while (imu.get_heading() < 90) {
    pros::delay(20);
}

left_motor.move(0);
right_motor.move(0);

这段代码比时间控制好多了——不管摩擦力怎么变,转到 90 度就停This code is much better than time control -- no matter how friction changes, it stops at 90 degrees.

3.5 "冲过头"问题3.5 The "Overshoot" Problem

但你可能会发现:机器人经常转过了 90 度,停在 95 度甚至 100 度的位置。But you may notice: the robot often turns past 90 degrees, stopping at 95 or even 100 degrees.

原因是惯性:电机停了,但机器人还在转。就像你跑步突然刹车,身体还会往前冲一点。The reason is inertia: the motors stop but the robot keeps turning. Just like when you're running and suddenly brake, your body still moves forward a bit.

预告Preview

这个"冲过头"的问题,在 Level 4 会学到更好的解决办法(提前减速)。We'll learn a better solution for this "overshoot" problem in Level 4 (slowing down early).

现在你只需要知道:传感器控制比时间控制好得多,但还不是完美的。For now, just know that sensor control is much better than time control, but it's not perfect yet.

检查点:预测 headingCheckpoint: Predict the heading
机器人校准后朝正前方(heading = 0)。先向右转 120 度,再向右转 300 度。现在 heading 的值是多少?After calibration, the robot faces forward (heading = 0). It turns right 120 degrees, then turns right another 300 degrees. What is the heading value now?