色彩传感器#
介绍#
VEX IQ(第二代)的颜色传感器 API 提供了从不同对象返回数据的方法。这使得 Brain 能够检测颜色及其色调和亮度。VEXcode 使用“colorType”来区分颜色传感器检测到的颜色。以下是颜色传感器能够检测的颜色:
颜色 – 枚举值 |
---|
<ul><li>`无` – 0 </li><li>`红色` – 1 </li><li>`绿色` – 2 </li><li>`蓝色` – 3 </li><li>`白色` – 4 </li><li>`黄色` – 5 </li><li>`橙色` – 6<li></li>紫色` – 7 </li><li>`青色` – 8 </li><li>`红紫色` – 9 </li><li>`紫罗兰色` – 10 </li><li>`蓝紫色` – 11 </li><li>`蓝绿色` – 12 </li><li>`黄绿色` – 13 </li><li>`黄橙色` – 14 </li><li>`红橙色` – 15 </li><li>`黑色` – 16 </li><li>`透明` – 17 </li><ul> |
以下是所有可用方法的列表:
方法——控制保险杠开关的行为和输入。
setLight – 打开、关闭颜色传感器的 LED,或将其设置为设定的亮度级别。
isNearObject – 返回物体是否在传感器范围内。
检测 – 返回传感器是否检测到指定的颜色。
colorname – 返回传感器检测到的颜色的名称。
亮度 – 以百分比返回检测到的亮度。
hue – 以浮点数形式返回检测到的色调值,范围是 0 到 359.99 度。
colorname3 – 返回与检测到的颜色最接近的红色、蓝色或绿色。
color – 以字符串形式返回检测到的颜色的十六进制代码。
installed – 返回颜色传感器是否连接到大脑。
构造函数——手动初始化保险杠开关。
colorsensor – 创建颜色传感器。
方法#
设置灯光#
setLight
设置颜色传感器 LED 的状态。
用法:
setLight(value)
参数 |
描述 |
---|---|
价值 |
有效的 LED 状态:
|
// Blink the Color Sensor LED.
while (true){
Color1.setLight(ledState::on);
wait(0.5, seconds);
Color1.setLight(ledState::off);
wait(0.5, seconds);
}
过载
setLight(强度,单位)
setLight(intensity, units)
将颜色传感器 LED 设置为特定的光强度。
参数 |
描述 |
---|---|
强度 |
将光的强度设置为 0 - 100 之间的值。 |
单位 |
可选。强度的唯一有效单位是“百分比”。 |
// Change the intensity of the LED
while (true){
Color1.setLight(25, percent);
wait(0.5, seconds);
Color1.setLight(50);
wait(0.5, seconds);
Color1.setLight(100, percent);
wait(0.5, seconds);
}
isNearObject#
如果接近传感器检测到近距离物体,则返回“isNearObject”。
1
– 颜色传感器检测到近处物体。0
– 颜色传感器未检测到近处物体。
用法:
isNearObject()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Stop when a close object is detected
Drivetrain.drive(forward);
while (true){
if (Color1.isNearObject()){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Drivetrain.stop();
Brain.Screen.print("Detected");
wait(0.25, seconds);
}
}
检测#
“检测”检查颜色传感器是否检测到指定的颜色。
1
– 颜色传感器检测颜色。0
– 颜色传感器未检测到颜色。
用法:
检测(颜色)
参数 |
描述 |
---|---|
颜色 |
colorType 表示要检测的颜色。 |
// Stop when a red object is detected
Drivetrain.drive(forward);
while (true){
if (Color1.detects(red)){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Drivetrain.stop();
Brain.Screen.print("Red detected");
wait(0.25, seconds);
}
}
颜色名称#
colorname
返回与预设的 colorType 值最接近的颜色的名称。
用法:
colorname()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display if the detected color is orange
while (true){
// Get the color detected
vex::colorType colorVal = Color1.colorname();
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
// Check if color is orange
if (colorVal == vex::color::orange) {
Brain.Screen.print("Orange!");
} else {
Brain.Screen.print("Not orange...");
}
wait(0.5, seconds);
}
亮度#
“亮度”从颜色传感器读取亮度值。
用法:
brightness()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Slowly turn and display detected brightness
Drivetrain.setTurnVelocity(20, percent);
Drivetrain.turn(right);
while (true){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Brightness: ");
Brain.Screen.print("%d", Color1.brightness());
wait(0.25, seconds);
}
过载:
亮度(bRaw)
参数 |
描述 |
---|---|
bRaw |
是否返回颜色传感器检测到的原始值。默认值为 false。 |
色调#
hue
返回颜色传感器检测到的色调值。
用法:
hue()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Slowly turn and display detected hue
Drivetrain.setTurnVelocity(20, percent);
Drivetrain.turn(right);
while (true){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Hue: ");
Brain.Screen.print("%d", Color1.hue());
wait(0.25, seconds);
}
颜色名称3#
colorname3
将检测到的最接近红色、绿色或蓝色的 colorType 分别返回为 1、2 或 3。
用法:
colorname3()
参数 |
描述 |
---|---|
该方法没有参数。 |
while (true){
// Get the closest color detected
vex::colorType colorVal = Color1.colorname3();
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
// Display the closest color
if (colorVal == vex::color::red) {
Brain.Screen.print("Red");
} else if (colorVal == vex::color::green) {
Brain.Screen.print("Green");
} else if (colorVal == vex::color::blue) {
Brain.Screen.print("Blue");
} else {
Brain.Screen.print("None");
}
wait(0.5, seconds);
}
颜色#
color
从颜色传感器读取检测到的颜色。
用法:
color()
参数 |
描述 |
---|---|
该方法没有参数。 |
while (true){
// Get the color with .color()
vex::color colorVal = Color1.color();
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
// Check if color is red, green, or blue
if (colorVal == vex::color::red) {
Brain.Screen.print("Red");
} else if (colorVal == vex::color::green) {
Brain.Screen.print("Green");
} else if (colorVal == vex::color::blue) {
Brain.Screen.print("Blue");
} else {
Brain.Screen.print("None");
}
wait(0.5, seconds);
}
已安装#
installed
返回一个整数,指示颜色传感器当前是否连接到 IQ(第二代)大脑。
1
– 颜色传感器连接到 IQ(第二代)大脑。0
– 颜色传感器未连接到 IQ(第二代)大脑。
用法:
installed()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display a message if the Color Sensor is detected
if (Color1.installed()){
Brain.Screen.print("Installed!");
}
构造函数#
构造函数用于手动创建“colorsensor”对象,这对于在 VEXcode 之外配置颜色传感器是必需的。
对于下面的示例,配置的颜色传感器将被命名为“Color1”,并且在整个 API 文档的所有后续示例中引用“colorsensor”类方法时将使用它。
颜色传感器#
colorsensor
创建一个颜色传感器。
用法:
colorsensor(port)
范围 |
描述 |
---|---|
|
颜色传感器连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。 |
// Construct a Color Sensor "Color1" with the
// colorsensor class.
colorsensor Color1 = colorsensor(PORT1);
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Change the intensity of the LED
while (true){
Color1.setLight(25, percent);
wait(0.5, seconds);
Color1.setLight(50);
wait(0.5, seconds);
Color1.setLight(100, percent);
wait(0.5, seconds);
}
}