Οπτικός αισθητήρας#
Εισαγωγή#
The optical class is used to access data from the Optical Sensor. It uses reflected light to detect objects, identify colors, and measure brightness and hue.
Κατασκευαστής κλάσης#
optical Optical = optical(index);
Καταστροφέας κλάσης#
Destroys the optical object and releases associated resources.
virtual ~optical();
Παράμετροι#
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
The Smart Port that the Optical Sensor is connected to, written as |
Παραδείγματα#
// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);
Συναρτήσεις Μελών#
The optical class includes the following member functions:
setLight— Turns the Optical Sensor’s LED on or off.setLightPower— Sets the Optical Sensor’s light power level.isNearObject— Returns whether or not the Optical Sensor detects an object within range.color— Returns the color detected by the Optical Sensor.brightness— Returns the amount of light reflected from the object.hue— Returns the hue detected by the Optical Sensor.objectDetected— Registers a function to be called when the Optical Sensor detects an object.objectLost— Registers a function to be called when the Optical Sensor loses an object.
Before calling any optical member functions, an optical instance must be created, as shown below:
/* This constructor is required when using VS Code.
Optical Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create an optical object in Port 1
optical Optical1 = optical(PORT1);
setLight#
Ενεργοποιεί ή απενεργοποιεί τη λυχνία LED του οπτικού αισθητήρα. Το φως μπορεί να βοηθήσει τον οπτικό αισθητήρα να ανιχνεύει αντικείμενα και χρώματα με μεγαλύτερη σαφήνεια.
Available Functionsvoid setLight(ledState state);
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Sets the LED on or off. |
Αυτή η συνάρτηση δεν επιστρέφει τιμή.
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
Ορίζει πόσο έντονο είναι το φως του οπτικού αισθητήρα. Το φως μπορεί να βοηθήσει τον οπτικό αισθητήρα να ανιχνεύει αντικείμενα και χρώματα με μεγαλύτερη καθαρότητα.
Ένα υψηλότερο ποσοστό κάνει το φως πιο φωτεινό. Ένα χαμηλότερο ποσοστό κάνει το φως πιο αμυδρό.
Εάν το φως του οπτικού αισθητήρα είναι σβηστό, η ρύθμιση της ισχύος φωτός πάνω από 0% θα ανάψει το φως.
Εάν η λυχνία του οπτικού αισθητήρα είναι αναμμένη, η ρύθμιση της ισχύος του φωτός στο 0% θα το σβήσει.
Available Functionsvoid setLightPower(
int32_t intensity,
percentUnits units = percent );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Το επίπεδο ισχύος για να ρυθμίσετε το φως από 0 έως 100. |
|
|
The unit that represents the light intensity: |
Αυτή η συνάρτηση δεν επιστρέφει τιμή.
Examples// Set the light power to 50 percent.
Optical.setLightPower(50, percent);
isNearObject#
Επιστρέφει εάν ο οπτικός αισθητήρας ανιχνεύει ένα αντικείμενο εντός εμβέλειας.
Available Functionsbool isNearObject();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΑυτή η συνάρτηση επιστρέφει μια λογική τιμή που υποδεικνύει εάν ανιχνεύεται ένα κοντινό αντικείμενο:
true— An object is detected nearby.false— An object is not detected.
// If an object is detected by the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
Brain.Screen.print("near object");
}
color#
Επιστρέφει το χρώμα που ανιχνεύεται από τον οπτικό αισθητήρα, με βάση την τιμή απόχρωσης που ανιχνεύεται.
Available Functionsvex::color color();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΤο χρώμα που ανιχνεύεται από τον οπτικό αισθητήρα ως παράδειγμα της κλάσης χρώματος.
Πιθανά χρώματα είναι:
blue– Hue value between 196° and 229°blue_green– Hue value between 140° and 195°blue_violet– Hue value between 230° and 239°green– Hue value between 84° and 139°orange– Hue value between 21° and 30°red– Hue value between 350° and 14°red_orange– Hue value between 15° and 20°red_violet– Hue value between 281° and 350°violet– Hue value between 240° and 280°yellow– Hue value between 54° and 74°yellow_green– Hue value between 75° and 83°yellow_orange– Hue value between 31° and 53°
// Set a variable, detectColor, to the color detected by the
// Optical Sensor
color detectColor = Optical.color();
// Print the color detected by the Optical Sensor
// to the Brain's screen
Brain.Screen.print(detectColor);
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 Optical Sensor
double brightness = Optical.brightness();
// Print the brightness detected by the Optical 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 Optical Sensor.
double hue = Optical.hue();
objectDetected#
Καταγράφει μια συνάρτηση επανάκλησης για την ανίχνευση ενός αντικειμένου.
Available Functionsvoid objectDetected(void (* callback)(void));
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Μια προηγουμένως καθορισμένη συνάρτηση που εκτελείται όταν ο Οπτικός Αισθητήρας ανιχνεύει ένα νέο αντικείμενο. |
Αυτή η συνάρτηση δεν επιστρέφει τιμή.
ExamplesDefine the callback function (outside of
int main())// Called when the Optical Sensor detects an object void detected() { // The Brain will print that the Optical Sensor detected an // object to the Brain's screen. Brain.Screen.print("object detected"); }Register the callback inside
int main()/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run when the Optical Sensor detects an object. Optical.objectDetected(detected); }
objectLost#
Καταχωρεί μια συνάρτηση επανάκλησης για την περίπτωση απώλειας ενός αντικειμένου.
Available Functionsvoid objectLost(void (* callback)(void));
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Μια προηγουμένως καθορισμένη συνάρτηση που εκτελείται όταν ο Οπτικός Αισθητήρας χάνει ένα ανιχνευμένο αντικείμενο. |
Αυτή η συνάρτηση δεν επιστρέφει τιμή.
ExamplesDefine the callback function (outside of
int main())// Called when the Optical Sensor loses an object void lost() { // The Brain will print that the Optical Sensor lost an // object to the Brain's screen. Brain.Screen.print("object lost"); }Register the callback inside
int main()/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run lost when the Optical Sensor loses an object. Optical.objectLost(lost); }