رسالة#

مقدمة#

يمكن ربط روبوتين من نوع VEX AIM Coding Robots للتواصل أثناء تنفيذ مشروع. تتيح طرق المراسلة لأحد الروبوتين إرسال رسالة إلى الآخر. يمكن أن تتضمن الرسالة نصًا فقط، أو نصًا مع ما يصل إلى ثلاثة أرقام.

تُخزَّن الرسائل المستلمة في قائمة انتظار وفقًا لأسلوب “الأول في الأول خارج” (FIFO). وهذا يعني أن أول رسالة يتم استلامها هي أول رسالة ستُقرأ.

استخدم is_message_available للتحقق من جاهزية الرسالة للقراءة. ثم استخدم get_message لقراءة الرسالة النصية التالية. إذا كانت الرسالة تتضمن أرقامًا أيضًا، فاستخدم get_message_and_data لقراءة النص والأرقام معًا.

ملاحظة: لكي يتم إرسال واستقبال الرسائل، يجب ربط كلا الروبوتين ويجب أن يقوم كلا الروبوتين بتشغيل مشاريع تستخدم طرق الرسائل.

فيما يلي قائمة بجميع الطرق:

الإجراء — إرسال الرسائل بين الروبوتات.

  • send_message — يرسل نصًا وما يصل إلى ثلاثة أرقام إلى الروبوت المرتبط.

المستقبِلون — قراءة الرسائل أو التحقق من حالة الاتصال.

  • get_message — تُرجع الرسالة النصية التالية التي تم استلامها.

  • get_message_and_data — تُرجع الرسالة النصية والأرقام المستلمة التالية.

  • is_connected — تُرجع ما إذا كان الروبوت مرتبطًا بروبوت آخر.

  • is_message_available — تُرجع ما إذا كانت الرسالة متاحة للقراءة.

  • get_name — يُرجع اسم الروبوت المرتبط.

الوظائف المستدعاة — تشغيل الوظائف عند حدوث أحداث الرسائل.

  • handle_message — يقوم بتشغيل وظيفة عند استلام رسالة معينة.

  • connected — يقوم بتشغيل وظيفة عندما يكون الروبوت مرتبطًا بروبوت آخر.

  • disconnected — يقوم بتشغيل وظيفة عندما لا يعود الروبوت مرتبطًا بروبوت آخر.

فعل#

send_message#

يرسل الأمر send_message نصًا وثلاثة أرقام كحد أقصى إلى الروبوت المرتبط. استخدم هذه الطريقة مع أحد الروبوتات عندما يحتاج الروبوت الآخر إلى تلقي أمر أو إشارة أو معلومة موجزة. أرسل النص فقط عندما تكون الرسالة كافية. أضف الأرقام عندما تحتاج الرسالة إلى قيم مرتبطة، مثل إرسال ` “heading” مع قيمة عنوان الروبوت الحالي.

في الروبوت المُستقبِل، استخدم is_message_available للتحقق من وصول الرسالة. ثم استخدم get_message لرسالة نصية فقط، أو get_message_and_data لرسالة تتضمن أرقامًا.

Usage:
robot.link.send_message(message, arg1, arg2, arg3)

المعلمة

وصف

message

النص المراد إرساله إلى الروبوت المرتبط.

arg1

اختياري. رقم لإرساله مع الرسالة.

arg2

اختياري. رقم ثانٍ لإرساله مع الرسالة.

arg3

اختياري. رقم ثالث لإرساله مع الرسالة.

مثال

رمز للروبوت 1

# Press the screen to tell Robot 2 which way to turn
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("right")

رمز للروبوت 2

# Turn in the direction that Robot 1 sends
robot.screen.print("Robot 2")

while not robot.link.is_message_available():
    wait(5, MSEC)

direction = robot.link.get_message()

if direction == "left":
    robot.turn_for(LEFT, 90)
elif direction == "right":
    robot.turn_for(RIGHT, 90)

# Robot 1: continuously send the current heading to Robot 2
robot.screen.print("Robot 1")

while True:
    robot.link.send_message("heading", robot.inertial.get_heading())
    wait(50, MSEC)

# Robot 2: turn to match Robot 1's heading
robot.screen.print("Robot 2")

while True:
    if robot.link.is_message_available():
        message, heading = robot.link.get_message_and_data()
        robot.turn_to(heading)

    wait(50, MSEC)

المحصلون#

get_message#

get_message returns the next received text message from the message queue. Use this method on the receiving robot when the linked robot sent a text-only message. A common order is to use is_message_available to check that a message has arrived, then use get_message before displaying, comparing, or using the returned text in another method.

استخدم send_message على الروبوت الآخر لإرسال رسالة نصية فقط.

Usage:
robot.link.get_message(timeout)

المعلمة

وصف

timeout

اختياري. الحد الأقصى للوقت، بالمللي ثانية، للانتظار حتى وصول رسالة قبل المتابعة. القيمة الافتراضية هي 1000.

مثال

رمز للروبوت 1

# Press the screen to send a message to Robot 2
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("Hello, Robot 2!")

رمز للروبوت 2

# Display the message sent by Robot 1
robot.screen.print("Robot 2")
robot.screen.next_row()

while not robot.link.is_message_available():
    wait(5, MSEC)

message = robot.link.get_message()
robot.screen.print(message)

get_message_and_data#

get_message_and_data returns the next received text message and numbers from the message queue as a tuple. Use this method on the receiving robot when the linked robot sent text with one or more numbers. A common order is to use is_message_available to check that a message has arrived, then use get_message_and_data before using the returned text and numbers.

استخدم send_message على الروبوت الآخر لإرسال النص والأرقام معًا.

Usage:
robot.link.get_message_and_data(timeout)

المعلمة

وصف

timeout

اختياري. الحد الأقصى للوقت، بالمللي ثانية، للانتظار حتى وصول رسالة قبل المتابعة. القيمة الافتراضية هي 1000.

مثال

رمز للروبوت 1

# Continuously send the current heading to Robot 2
robot.screen.print("Robot 1")

while True:
    robot.link.send_message("heading", robot.inertial.get_heading())
    wait(50, MSEC)

رمز للروبوت 2

# Turn to match Robot 1's heading
robot.screen.print("Robot 2")

while True:
    if robot.link.is_message_available():
        message, heading = robot.link.get_message_and_data()
        robot.turn_to(heading)

    wait(50, MSEC)

is_connected#

تُشير الدالة is_connected is_connected إلى ما إذا كان الروبوت متصلاً بروبوت آخر. استخدم هذه الدالة قبل إرسال الرسائل أو انتظارها إذا كان المشروع بحاجة إلى التحقق من جاهزية اتصال الروبوت بالروبوت الآخر.

  • True — الروبوت مرتبط بروبوت آخر.

  • False — الروبوت غير مرتبط بروبوت آخر.

Usage:
robot.link.is_connected()

حدود

وصف

لا تحتوي هذه الطريقة على أي معلمات.

# Display whether the robot is connected
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(robot.link.is_connected())
    wait(50, MSEC)

is_message_available#

is_message_available returns whether a message is waiting in the message queue. Use this method before get_message or get_message_and_data so the receiving robot only tries to read a message after one has arrived.

  • True — هناك رسالة متاحة للقراءة.

  • False — لا توجد رسائل متاحة للقراءة.

Usage:
robot.link.is_message_available()

حدود

وصف

لا تحتوي هذه الطريقة على أي معلمات.

مثال

رمز للروبوت 1

# Press the screen to send a message to Robot 2
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("Hello, Robot 2!")

رمز للروبوت 2

# Wait until a message is available, then display it
robot.screen.print("Robot 2")
robot.screen.next_row()

while not robot.link.is_message_available():
    wait(5, MSEC)

message = robot.link.get_message()
robot.screen.print(message)

get_name#

get_name تُعيد اسم الروبوت المرتبط. استخدم هذه الطريقة لعرض الروبوت المتصل قبل إرسال أو استقبال الرسائل. إذا لم يكن هناك روبوت مرتبط، تُعيد هذه الطريقة None.

Usage:
robot.link.get_name()

حدود

وصف

لا تحتوي هذه الطريقة على أي معلمات.

# Display the name of the linked robot
robot.screen.print(robot.link.get_name())

معاودة الاتصال#

handle_message#

يُسجّل handle_message دالةً تُنفّذ عند تلقّي الروبوت رسالة نصية مُحدّدة. استخدم هذه الطريقة عندما يُريد الروبوت الاستجابة تلقائيًا لرسالة معروفة بدلًا من فحص قائمة الرسائل في حلقة تكرارية. حدّد دالة الاستدعاء أولًا، ثم سجّلها باستخدام handle_message ونص الرسالة المراد مراقبتها.

Usage:
robot.link.handle_message(callback, message)

المعلمة

وصف

callback

دالة مُعرَّفة مسبقاً يتم تشغيلها عندما تتطابق الرسالة المستلمة message.

message

النص الذي يتم التحقق منه في الرسائل الواردة.

مثال

رمز للروبوت 1

# Press the screen to send "smile" to Robot 2
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("smile")

رمز للروبوت 2

# Show a happy emoji when Robot 1 sends "smile"
def robot_smile():
    robot.screen.show_emoji(HAPPY)

robot.link.handle_message(robot_smile, "smile")

while True:
    wait(50, MSEC)

connected#

connected يسجل دالة يتم تشغيلها عند ربط الروبوت بروبوت آخر. استخدم هذه الطريقة عندما يتطلب المشروع الاستجابة فور إنشاء اتصال الروبوت بالروبوت الآخر. حدد دالة الاستدعاء أولاً، ثم سجلها باستخدام connected.

Usage:
robot.link.connected(callback, args)

المعلمة

وصف

callback

وظيفة محددة مسبقًا يتم تشغيلها عندما يكون الروبوت مرتبطًا بروبوت آخر.

args

اختياري. مجموعة تحتوي على الوسائط التي يتم تمريرها إلى دالة الاستدعاء. راجع استخدام الدوال ذات المعاملات لمزيد من المعلومات.

# Show a happy emoji when connected
def robot_smile():
    robot.screen.show_emoji(HAPPY)

robot.link.connected(robot_smile)

disconnected#

disconnected يسجل دالة يتم تشغيلها عندما ينقطع اتصال الروبوت بروبوت آخر. استخدم هذه الطريقة عندما يجب أن يستجيب المشروع فور فقدان الاتصال بين الروبوتين. حدد دالة الاستدعاء أولاً، ثم سجلها باستخدام disconnected.

Usage:
robot.link.disconnected(callback, args)

المعلمة

وصف

callback

وظيفة محددة مسبقًا يتم تشغيلها عندما لا يعود الروبوت مرتبطًا بروبوت آخر.

args

اختياري. مجموعة تحتوي على الوسائط التي يتم تمريرها إلى دالة الاستدعاء. راجع استخدام الدوال ذات المعاملات لمزيد من المعلومات.

# Show a sad emoji when disconnected
def robot_sad():
    robot.screen.show_emoji(SAD)

robot.link.disconnected(robot_sad)