Οπτικός αισθητήρας#
Εισαγωγή#
Ο οπτικός αισθητήρας V5 ανιχνεύει το χρώμα, τη φωτεινότητα και την παρουσία κοντινών αντικειμένων χρησιμοποιώντας έναν συνδυασμό αισθητήρα χρώματος RGB και αισθητήρα εγγύτητας. Μπορεί επίσης να χρησιμοποιήσει την ενσωματωμένη λυχνία LED για να φωτίσει αντικείμενα για πιο ακριβή ανίχνευση.

This page uses optical_sensor as the example Optical Sensor name. Replace it with your own configured name as needed.
Παρακάτω είναι μια λίστα με τις διαθέσιμες μεθόδους:
set_light– Turns the Optical Sensor’s LED on or off.set_light_power– Sets the Optical Sensor’s light power level.is_near_object– Returns whether 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.object_detected– Registers a function to be called when the Optical Sensor detects an object.object_lost– Registers a function to be called when the Optical Sensor loses an object.
Κατασκευαστής – Χειροκίνητη αρχικοποίηση ενός οπτικού αισθητήρα.
Optical– Creates an Optical Sensor.
set_light#
set_light turns the Optical Sensor’s LED on or off. This can help increase the sensor’s accuracy when detecting colors.
Usage:
optical_sensor.set_light(state)
Παράμετροι |
Περιγραφή |
|---|---|
κατάσταση |
Sets the LED on or off. |
# Turn on the sensor's LED
optical_sensor.set_light(LEDStateType.ON)
set_light_power#
set_light_power sets the brightness of the Optical Sensor’s LED. The light can help the Optical Sensor detect objects and colors more clearly.
Εάν το φως του οπτικού αισθητήρα είναι σβηστό, η ρύθμιση της ισχύος φωτός πάνω από 0% θα ανάψει το φως.
Εάν η λυχνία του οπτικού αισθητήρα είναι αναμμένη, η ρύθμιση της ισχύος του φωτός στο 0% θα το σβήσει.
Ένα υψηλότερο ποσοστό κάνει το φως πιο φωτεινό. Ένα χαμηλότερο ποσοστό κάνει το φως πιο αμυδρό.
Usage:
optical_sensor.set_light_power(percent, units)
Παράμετροι |
Περιγραφή |
|---|---|
τοις εκατό |
Η φωτεινότητα στην οποία θα οριστεί η λυχνία LED ως ακέραιος αριθμός. |
μονάδες |
Optional. The unit to represent the brightness: |
# Turn on the sensor's light at half brightness
optical_sensor.set_light_power(50, PERCENT)
είναι_κοντά_στο_αντικείμενο#
is_near_object returns a Boolean indicating whether or not the Optical Sensor detects an object within range.
True– The Optical Sensor detects an object.False– The Optical Sensor does not detect an object.
Usage:
optical_sensor.is_near_object()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
# Drive forward until an object is found
drivetrain.drive(FORWARD)
while not optical_sensor.is_near_object():
pass
drivetrain.stop()
χρώμα#
color returns an instance of a Color class, based on the detected hue value. These can be compared to predefined Color objects to create conditional statements.
Πιθανά χρώματα είναι:
Color.BLACKColor.WHITEColor.RED- A hue value between 340° - 20°Color.GREEN- A hue value between 80° - 140°Color.BLUE- A hue value between 200° - 240°Color.YELLOW- A hue value between 40° - 60°Color.ORANGE- A hue value between 20° - 40°Color.PURPLE- A hue value between 240° - 280°Color.CYAN- A hue value between 140° - 200°
Note: The Optical Sensor is looking for hue ranges that match the specified color. For detecting specific hue ranges, see hue.
Usage:
optical_sensor.color()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
# Drive forward until red is found
drivetrain.drive(FORWARD)
while not optical_sensor.color() == Color.RED:
pass
drivetrain.stop()
λάμψη#
brightness returns the amount of light reflected from the object as a decimal (float) representing a percent.
Ένα υψηλότερο ποσοστό σημαίνει ότι περισσότερο φως ανακλάται πίσω στον οπτικό αισθητήρα. Ένα χαμηλότερο ποσοστό σημαίνει ότι λιγότερο φως ανακλάται πίσω.
Usage:
optical_sensor.brightness()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
απόχρωση#
hue returns the hue value of the detected color as a decimal (float) from 0 to 359.
Η απόχρωση είναι ένας τρόπος περιγραφής χρώματος χρησιμοποιώντας αριθμούς γύρω από έναν χρωματικό κύκλο.

Usage:
optical_sensor.hue()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
# Look for the color pink using hue
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
if 300 < optical_sensor.hue() < 359:
brain.screen.print("This is pink!")
else:
brain.screen.print("No pink detected!")
ανιχνευμένο_αντικείμενο#
object_detected registers a function to be called whenever the Optical Sensor detects a new object.
Usage:
optical_sensor.object_detected(callback, arg)
Παράμετροι |
Περιγραφή |
|---|---|
|
Μια προηγουμένως καθορισμένη συνάρτηση που εκτελείται όταν ο Οπτικός Αισθητήρας ανιχνεύει ένα νέο αντικείμενο. |
|
Προαιρετικό. Μια πλειάδα που περιέχει ορίσματα για να μεταβιβαστούν στη συνάρτηση επανάκλησης. Δείτε Χρήση Συναρτήσεων με Παραμέτρους για περισσότερες πληροφορίες. |
def my_function():
brain.screen.print("Optical Sensor detected an object")
# Call my_function whenever optical_sensor detects an object
optical_sensor.object_detected(my_function)
αντικείμενο_χαμένο#
object_lost registers a function to be called whenever the Optical Sensor loses a detected object.
Usage:
optical_sensor.object_lost(callback, arg)
Παράμετροι |
Περιγραφή |
|---|---|
|
Μια προηγουμένως καθορισμένη συνάρτηση που εκτελείται όταν ο Οπτικός Αισθητήρας χάνει ένα ανιχνευμένο αντικείμενο. |
|
Προαιρετικό. Μια πλειάδα που περιέχει ορίσματα για να μεταβιβαστούν στη συνάρτηση επανάκλησης. Δείτε Χρήση Συναρτήσεων με Παραμέτρους για περισσότερες πληροφορίες. |
def my_function():
brain.screen.print("Optical Sensor lost an object")
# Call my_function whenever optical_sensor loses an object
optical_sensor.object_lost(my_function)
Κατασκευαστής#
Constructors are used to manually create Optical objects, which are necessary for configuring an Optical Sensor outside of VEXcode.
Optical#
Optical creates an Optical Sensor.
Usage:
Optical(smartport)
Παράμετρος |
Περιγραφή |
|---|---|
|
The Smart Port that the Optical Sensor is connected to, written as |
# Create an Optical Sensor in Port 10
optical_sensor = Optical(Ports.PORT10)