مستشعر الألوان#

مقدمة#

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);

مُدمِّر الفئة#

يقوم بتدمير الكائن colorsensor وتحرير الموارد المرتبطة به.

virtual ~colorsensor();

حدود#

المعلمة

يكتب

وصف

index

int32_t

المنفذ الذكي الذي يتصل به مستشعر اللون، مكتوبًا على النحو التالي: PORTx، حيث x هو رقم المنفذ (على سبيل المثال، PORT1).

أمثلة#

// Create an colorsensor instance in Port 1
colorsensor Color1 = colorsensor(PORT1);

الدوال الأعضاء#

The colorsensor class includes the following member functions:

  • setLight – يقوم بتشغيل أو إيقاف تشغيل مؤشر LED الخاص بمستشعر اللون أو ضبطه على مستوى سطوع محدد.

  • isNearObject – تُرجع ما إذا كان الكائن قريبًا من مستشعر اللون أم لا.

  • detects – يُرجع ما إذا كان مستشعر اللون قد اكتشف لونًا محددًا أم لا.

  • colorname – يُرجع اسم اللون الذي تم اكتشافه بواسطة مستشعر الألوان.

  • brightness – تُرجع السطوع المكتشف للكائن.

  • hue – تُرجع قيمة اللون المكتشفة.

قبل استدعاء أي من الدوال الأعضاء colorsensor ، يجب إنشاء مثيل colorsensor ، كما هو موضح أدناه:

/* 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 Functions

1 — يقوم بتشغيل أو إيقاف تشغيل مؤشر LED باستخدام ledState.

void setLight( 
  ledState  state );

2 — يقوم بتشغيل مصباح LED بكثافة محددة.

void setLight( 
  int32_t      intensity, 
  percentUnits units = percent );

Parameters

المعلمة

يكتب

وصف

state

ledState

يُشغّل أو يُطفئ مؤشر LED. ledState::on يُشغّل مؤشر LED بكامل سطوعه. ledState::off يُطفئه.

intensity

int32_t

مستوى الطاقة لضبط الإضاءة من 0 إلى 100.

units

percentUnits

The unit that represents the light intensity: percent / pct — percent

Return Values

هذه الدالة لا تُرجع قيمة.

Examples
// Set the light power to 50 percent.
Color1.setLight(50, percent);

isNearObject#

يُرجع قيمة إذا اكتشف مستشعر اللون جسمًا قريبًا.

Available Functions
bool  isNearObject();

Parameters

لا تقبل هذه الدالة أي معلمات.

Return Values

تُرجع هذه الدالة قيمة منطقية تشير إلى ما إذا تم اكتشاف جسم قريب أم لا:

  • true - يكتشف مستشعر اللون وجود جسم.

  • false - لا يكتشف مستشعر الألوان وجود جسم.

Examples
// If an object is detected by the Color Sensor, print
// "near object".
if (Color1.isNearObject()){
  Brain.Screen.print("near object");
}

detects#

يتم إرجاع القيمة إذا كان مستشعر اللون يكتشف لونًا محددًا.

Available Functions

1 — Detects a colorType.

bool detects( 
  colorType  color );

2 — Detects a vex::color.

bool detects( 
  vex::color  color );

Parameters

حدود

يكتب

وصف

color

colorType

الألوان الصالحة:

  • blue - قيمة اللون بين 196° و229°
  • blue_green - قيمة اللون بين 140° و195°
  • blue_violet - قيمة اللون بين 230° و239°
  • green - قيمة اللون بين 84° و139°
  • orange - قيمة اللون بين 21° و30°
  • purple
  • red - قيمة اللون بين 350° و14°
  • red_orange - قيمة اللون بين 15° و20°
  • red_violet - قيمة اللون بين 281° و350°
  • violet – قيمة اللون بين 240° و280°
  • white
  • yellow – قيمة اللون بين 54° و74°
  • yellow_green – قيمة اللون بين 75° و83°
  • yellow_orange – قيمة اللون بين 31° و53°

color

vex::color

A valid custom color, or one of the predefined colors listed above.

Return Values
  • true - يقوم مستشعر اللون باكتشاف اللون.

  • false - لا يكتشف مستشعر اللون اللون.

Examples
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 Functions
colorType colorname();

Parameters

لا تقبل هذه الدالة أي معلمات.

Return Values

عند طباعتها مباشرة، فإنها تُظهر القيمة العددية المرتبطة بهذا اللون:

لون

القيمة العددية

none

0

red

1

green

2

blue

3

white

4

yellow

5

orange

6

purple

7

cyan

8

red_violet

9

violet

10

blue_violet

11

blue_green

12

yellow_green

13

yellow_orange

14

red_orange

15

black

16

transparent

17

Notes
  • يتم إنشاء الدالة convertColorToString بواسطة VEXcode عند تهيئة مستشعر الألوان في نافذة الأجهزة. وهي متاحة فقط داخل مشاريع VEXcode IQ ولن تعمل في بيئات أخرى.

  • عند استخدام colorname خارج VEXcode، يجب إجراء المقارنات مباشرة مع قيمة colorType - أي لون يبدأ بـ colorType:: (على سبيل المثال، colorType::green أو colorType::red).

Examples
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 Functions
double  brightness(bool bRaw = false);

Parameters

المعلمة

يكتب

وصف

bRaw

bool

قيمة منطقية لقراءة بيانات السطوع الخام بدلاً من النسبة المئوية. القيمة الافتراضية هي false.

Return Values

قيمة مزدوجة تمثل السطوع الذي تم اكتشافه بواسطة مستشعر الألوان في النطاق من 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#

يعيد اللون الذي تم اكتشافه بواسطة مستشعر الألوان.

اللون هو طريقة لوصف اللون باستخدام الأرقام الموجودة على عجلة الألوان.

عجلة ألوان VEX توضح درجات الألوان حول دائرة، حيث يكون اللون الأحمر عند الدرجة 0 ويتدرج عبر قوس قزح مع زيادة القيمة.

Available Functions
double hue();

Parameters

لا تقبل هذه الدالة أي معلمات.

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();