Αισθητήρας χρώματος#
Εισαγωγή#
The colorsensor class is used to access data from the Color Sensor. It uses reflected light to detect objects, identify colors, and measure brightness and hue.
Κατασκευαστής κλάσης#
colorsensor Color1 = colorsensor(index);
Καταστροφέας κλάσης#
Destroys the colorsensor object and releases associated resources.
virtual ~colorsensor();
Παράμετροι#
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
The Smart Port that the Color Sensor is connected to, written as |
Παραδείγματα#
// Create an colorsensor instance in Port 1
colorsensor Color1 = colorsensor(PORT1);
Συναρτήσεις Μελών#
The colorsensor class includes the following member functions:
setLight– Turns the Color Sensor’s LED on, off, or to a set brightness level.isNearObject– Returns whether or not an object is close to the Color Sensor.detects– Returns whether or not a specified color is detected by the Color Sensor.colorname– Returns the name of the color detected by the Color Sensor.brightness– Returns the detected brightness of an object.hue– Returns the detected hue value.
Before calling any colorsensor member functions, an colorsensor instance must be created, as shown below:
/* This constructor is required when using VS Code.
Color Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create an colorsensor instance in Port 1
colorsensor Color1 = colorsensor(PORT1);
setLight#
Ρυθμίζει την ισχύ φωτός της λυχνίας LED του αισθητήρα χρώματος. Το φως μπορεί να βοηθήσει τον αισθητήρα χρώματος να ανιχνεύει αντικείμενα και χρώματα με μεγαλύτερη σαφήνεια.
Εάν το φως του αισθητήρα χρώματος είναι σβηστό, η ρύθμιση της ισχύος φωτός πάνω από 0% θα ανάψει το φως.
Εάν το φως του αισθητήρα χρώματος είναι αναμμένο, η ρύθμιση της ισχύος φωτός στο 0% θα το σβήσει.
Ένα υψηλότερο ποσοστό κάνει το φως πιο φωτεινό. Ένα χαμηλότερο ποσοστό κάνει το φως πιο αμυδρό.
Available Functions1 — Turns the led on or off using
ledState.void setLight( ledState state );
Parameters2 — Ανάβει το led σε μια συγκεκριμένη ένταση.
void setLight( int32_t intensity, percentUnits units = percent );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Sets the LED on or off. |
|
|
Το επίπεδο ισχύος για να ρυθμίσετε το φως από 0 έως 100. |
|
|
The unit that represents the light intensity: |
Αυτή η συνάρτηση δεν επιστρέφει τιμή.
Examples// Set the light power to 50 percent.
Color1.setLight(50, percent);
isNearObject#
Επιστρέφει εάν ο αισθητήρας χρώματος ανιχνεύσει ένα κοντινό αντικείμενο.
Available Functionsbool isNearObject();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΑυτή η συνάρτηση επιστρέφει μια λογική τιμή που υποδεικνύει εάν ανιχνεύεται ένα κοντινό αντικείμενο:
true– The Color Sensor detects an object.false– The Color Sensor does not detect an object.
// If an object is detected by the Color Sensor, print
// "near object".
if (Color1.isNearObject()){
Brain.Screen.print("near object");
}
detects#
Επιστρέφει εάν ο αισθητήρας χρώματος ανιχνεύει ένα συγκεκριμένο χρώμα.
Available Functions1 — Detects a
colorType.bool detects( colorType color );
Parameters2 — Detects a
vex::color.bool detects( vex::color color );
Παράμετροι |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
A valid color:
|
|
|
Ένα έγκυρο προσαρμοσμένο χρώμα ή ένα από τα προκαθορισμένα χρώματα που αναφέρονται παραπάνω. |
true– The Color Sensor detects the color.false– The Color Sensor does not detect the color.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Drive until a red object is seen
Drivetrain.drive(forward);
while (true) {
if (Color1.detects(red)) {
Drivetrain.stop();
break;
}
wait(0.5, seconds);
}
}
colorname#
Επιστρέφει το πλησιέστερο αντίστοιχο χρώμα που εντοπίστηκε.
Available FunctionscolorType colorname();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΌταν εκτυπώνεται απευθείας, εμφανίζει την αριθμητική τιμή που σχετίζεται με αυτό το χρώμα:
Χρώμα |
Αριθμητική τιμή |
|---|---|
|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
The function
convertColorToStringis automatically created by VEXcode when a Color Sensor is configured in the Devices window. It is only available inside VEXcode IQ projects and will not work in other environments.When using
colornameoutside of VEXcode, comparisons should be made directly with acolorTypevalue — any color that begins withcolorType::(for example,colorType::greenorcolorType::red).
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Continuously check the detected color
while (true) {
// Compare the color name with "red"
if (convertColorToString(Color1.colorname()) == "red") {
Brain.Screen.print("Red object!");
} else {
Brain.Screen.print("Not red");
}
wait(0.5, seconds);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Continuously check the detected color
while (true) {
// Compare the color name with the colorType
if (Color1.colorname() == colorType::red) {
Brain.Screen.print("Red object!");
} else {
Brain.Screen.print("Not red");
}
wait(0.5, seconds);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
}
brightness#
Επιστρέφει την ποσότητα φωτός που ανακλάται από το αντικείμενο.
Ένα υψηλότερο ποσοστό σημαίνει ότι περισσότερο φως ανακλάται πίσω στον Αισθητήρα Χρώματος. Ένα χαμηλότερο ποσοστό σημαίνει ότι λιγότερο φως ανακλάται πίσω.
Available Functionsdouble brightness(bool bRaw = false);
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
A Boolean value to read raw brightness data instead of percentage. The default is |
Ένα διπλό που αντιπροσωπεύει τη φωτεινότητα που ανιχνεύεται από τον αισθητήρα χρώματος στο εύρος 0 - 100% ή ένα διπλό που αντιπροσωπεύει τα ακατέργαστα δεδομένα που ανιχνεύονται από τον αισθητήρα.
Examples// Set a variable, brightness, to the value of the brightness
// detected by the Color Sensor
double brightness = Color1.brightness();
// Print the brightness detected by the Color Sensor to the
// Brain's screen
Brain.Screen.print(brightness);
hue#
Επιστρέφει την απόχρωση που ανιχνεύεται από τον αισθητήρα χρώματος.
Η απόχρωση είναι ένας τρόπος περιγραφής χρώματος χρησιμοποιώντας αριθμούς γύρω από έναν χρωματικό κύκλο.

double hue();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΈνα διπλό που αντιπροσωπεύει την τιμή της απόχρωσης που ανιχνεύεται από τον Αισθητήρα Χρώματος στην περιοχή 0 — 359,00.
Examples// Set a variable, hue, to the value of the hue detected
// by the Color Sensor.
double hue = Color1.hue();