0 / 5 页已完成

其他传感器概览Other Sensors Overview

除了编码器和 IMU,VEX V5 还有几种常用的传感器。这节课带你认识一下它们,不要求全部掌握,知道有这些工具、什么时候用就行。Besides encoders and the IMU, VEX V5 has several other commonly used sensors. This lesson introduces them -- you don't need to master all of them, just know what tools are available and when to use them.

4.1 光学传感器(Optical Sensor)4.1 Optical Sensor

光学传感器能"看到"颜色。它会告诉你面前物体的色相值(hue)——一个 0 到 359 的数字,代表不同的颜色。The optical sensor can "see" colors. It tells you the hue value of the object in front of it -- a number from 0 to 359 representing different colors.

典型场景Typical use case:分拣不同颜色的球。比如 PushBack 赛季,红球和蓝球需要区分对待。: Sorting balls by color. For example, in the PushBack season, red and blue balls need to be handled differently.

C++ (VEXcode)
// 创建光学传感器(端口 6)
optical opt = optical(PORT6);

// 打开 LED 灯(照亮被检测的物体)
opt.setLight(ledState::on);

// 检测面前有没有物体
bool hasObject = opt.isNearObject();

// 读取颜色(0~359 的色相值)
int hue = opt.hue();
C++ (VS Code)
// 创建光学传感器(端口 6)
optical opt = optical(PORT6);

// 打开 LED 灯(照亮被检测的物体)
opt.setLight(ledState::on);

// 检测面前有没有物体
bool hasObject = opt.isNearObject();

// 读取颜色(0~359 的色相值)
int hue = opt.hue();
C++ (PROS)
// 创建光学传感器(端口 6)
pros::Optical opt(6);

// 打开 LED 灯(亮度 0~100)
opt.set_led_pwm(100);

// 读取接近度(数值越大越近)
int proximity = opt.get_proximity();

// 读取颜色(色相值)
double hue = opt.get_hue();

4.2 距离传感器(Distance Sensor)4.2 Distance Sensor

距离传感器用超声波或红外线测量前方物体的距离,返回值单位是毫米(mm)The distance sensor uses ultrasonic or infrared waves to measure the distance to objects ahead, returning the value in millimeters (mm).

典型场景Typical use cases:

C++ (VEXcode)
// 创建距离传感器(端口 5)
distance dist = distance(PORT5);

// 读取前方物体的距离(单位:毫米)
double d = dist.objectDistance(mm);

// 例:开到离墙 100mm 停下
while (dist.objectDistance(mm) > 100) {
    LeftMotor.spin(forward);
    RightMotor.spin(forward);
    wait(20, msec);
}
LeftMotor.stop();
RightMotor.stop();
C++ (VS Code)
// 创建距离传感器(端口 5)
distance dist = distance(PORT5);

// 读取前方物体的距离(单位:毫米)
double d = dist.objectDistance(mm);

// 例:开到离墙 100mm 停下
while (dist.objectDistance(mm) > 100) {
    LeftMotor.spin(forward);
    RightMotor.spin(forward);
    wait(20, msec);
}
LeftMotor.stop();
RightMotor.stop();
C++ (PROS)
// 创建距离传感器(端口 5)
pros::Distance dist(5);

// 读取前方物体的距离(单位:毫米)
double d = dist.get();

// 例:开到离墙 100mm 停下
while (dist.get() > 100) {
    left_motor.move(60);
    right_motor.move(60);
    pros::delay(20);
}
left_motor.move(0);
right_motor.move(0);

4.3 限位开关(Bumper / Limit Switch)4.3 Bumper / Limit Switch

限位开关是最简单的传感器——它只有两个状态:按下没按The limit switch is the simplest sensor -- it has only two states: pressed and not pressed.

它连接在 Brain 的三线端口(A~H)上。It connects to the Brain's 3-wire ports (A~H).

典型场景Typical use cases:

C++ (VEXcode)
// 创建限位开关(三线端口 A)
bumper limitSw = bumper(Brain.ThreeWirePort.A);

// 检测是否被按下
bool pressed = limitSw.pressing();

// 例:机械臂往下走,碰到限位开关就停
ArmMotor.spin(reverse);
while (!limitSw.pressing()) {
    wait(20, msec);
}
ArmMotor.stop();
C++ (VS Code)
// 创建限位开关(三线端口 A)
bumper limitSw = bumper(Brain.ThreeWirePort.A);

// 检测是否被按下
bool pressed = limitSw.pressing();

// 例:机械臂往下走,碰到限位开关就停
ArmMotor.spin(reverse);
while (!limitSw.pressing()) {
    wait(20, msec);
}
ArmMotor.stop();
C++ (PROS)
// 创建限位开关(三线端口 A)
pros::ADIDigitalIn limit_sw('A');

// 检测是否被按下(返回 1 表示按下)
bool pressed = limit_sw.get_value();

// 例:机械臂往下走,碰到限位开关就停
arm_motor.move(-80);
while (!limit_sw.get_value()) {
    pros::delay(20);
}
arm_motor.move(0);

4.4 传感器对比表4.4 Sensor Comparison Table

这张表总结了所有传感器的特点,方便你以后查:This table summarizes all the sensors for your future reference:

传感器Sensor 测量什么Measures 返回值Return Value 典型用途Typical Use
电机编码器Motor Encoder 电机转了多少度Motor rotation in degrees 度数(double)Degrees (double) 控制直走距离Control driving distance
IMU 陀螺仪IMU Gyroscope 机器人朝向Robot heading 0~360 度0~360 degrees 控制转弯角度Control turn angle
光学传感器Optical Sensor 物体颜色Object color 色相 0~359Hue 0~359 分拣球的颜色Sort balls by color
距离传感器Distance Sensor 前方物体距离Distance to object ahead 毫米(mm)Millimeters (mm) 对齐墙壁、避障Wall alignment, obstacle avoidance
限位开关Limit Switch 是否被按下Whether it's pressed true / false 机械限位、检测到位Mechanical limits, position detection
检查点:选传感器Checkpoint: Choose the Right Sensor
你的机器人需要自动检测吸入的球是红色还是蓝色。应该用哪个传感器?Your robot needs to automatically detect whether a ball it picked up is red or blue. Which sensor should you use?