رابط الرسائل#
مقدمة#
تتيح تقنية MessageLink لروبوتين من طراز V5 Brains التواصل عبر إرسال رسائل نصية قصيرة مع بيانات صغيرة اختيارية. وهي مصممة لتنسيق العمليات بين الروبوتات، مثل بدء الإجراءات الروتينية، ومشاركة نتائج المستشعرات، أو تحفيز سلوكيات معينة على روبوت آخر.
كل استخدام للأمر send يُضيف رسالة إلى قائمة انتظار وحدة المعالجة V5 Brain المرتبطة، وتُقرأ قائمة الانتظار وفقًا لأسلوب “الوارد أولًا يُخرج أولًا” (FIFO). إذا أُرسلت عدة رسائل قبل أن تستخدم وحدة المعالجة V5 Brain الأخرى الأمر receive، فسيتم تخزينها وإعادتها واحدة تلو الأخرى بالترتيب الذي أُرسلت به. نظرًا لإمكانية تراكم الرسائل في قائمة الانتظار، فإن إرسال الحالة نفسها بشكل متكرر في كل دورة قد يُؤدي إلى تراكم الرسائل؛ لذا، بالنسبة للمنطق الحساس للوقت، يُنصح بالإرسال فقط عند تغيير القيم.
هام: يجب أن يقوم كلا الروبوتين بتشغيل مشاريع تستخدم MessageLink في نفس الوقت، وإلا فلن يتم إرسال/استقبال أي رسائل.
This page uses link as the example MessageLink name. Replace it with your own configured name as needed.
فيما يلي قائمة بالطرق المتاحة:
إنشاء رابط#
قم بإنشاء رابط رسالة على كل من دماغي V5 لإنشاء قناة اتصال بينهما - إما لاسلكيًا باستخدام أجهزة راديو الروبوت V5 أو من خلال كابل ذكي سلكي - حتى تتمكن الروبوتات من إرسال الرسائل إلى بعضها البعض.
MessageLink(smartport, name, linktype, wired)
المعلمة |
وصف |
|---|---|
|
المنفذ الذكي المستخدم لهذا الاتصال هو المنفذ الذي يتصل به جهاز راديو الروبوت V5 (لاسلكيًا) أو المنفذ الذي يتم توصيل الكابل الذكي به (سلكيًا). يُكتب على النحو التالي: |
|
الاسم الفريد الذي يُطلق على هذا الرابط كسلسلة نصية. |
|
الدور الذي سيستخدمه هذا الدماغ لهذا الزوج من الروابط. يجب أن يتضمن كل زوج مديرًا واحدًا وعاملًا واحدًا:
|
|
اختياري. ما إذا كان هذا الاتصال سلكيًا أم لاسلكيًا:
|
كود للروبوت 1
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)كود للروبوت 2
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
الطرق المتاحة#
بمجرد إعداد MessageLink الخاص بك وربط العقول، يمكنك استخدام الطرق التالية لإرسال الرسائل واستقبالها والرد عليها.
is_linked#
is_linked تُرجع هذه الطريقة ما إذا كانت وحدات V5 Brains الموجودة على MessageLink مقترنة ببعضها البعض.
True- تم إقران دماغي V5 ويتواصلان عبر هذا الرابط.False- لم يتم إقران جهازي V5 Brains على هذا الرابط.
ملاحظة: من الممارسات الجيدة دائمًا التحقق من ربط V5 Brains في بداية المشروع قبل تشغيل أي كود إضافي.
Usage:
link.is_linked()
حدود |
وصف |
|---|---|
لا تحتوي هذه الطريقة على أي معلمات. |
كود للروبوت 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)كود للروبوت 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
send#
يرسل الأمر send V5 Brain الأخرى المرتبطة.
Usage:
send(string, integer, float)
حدود |
وصف |
|---|---|
|
السلسلة المراد إرسالها إلى وحدة التحكم V5 الأخرى المرتبطة. |
|
اختياري. عدد صحيح لإرساله إلى وحدة V5 Brain الأخرى المرتبطة. |
|
اختياري. قيمة عشرية لإرسالها إلى وحدة المعالجة المركزية V5 الأخرى المرتبطة. يتم تنسيق القيم العشرية إلى 4 منازل عشرية؛ إذا تم توفير عدد أقل من المنازل العشرية، تتم إضافة أصفار لاحقة حتى يتم عرض 4 منازل عشرية. |
إذا تم إرسال السلسلة string فإن وحدة V5 Brain الأخرى المرتبطة بها تتلقى تلك السلسلة بالضبط.
إذا تم تضمين integer و/أو float ، فإن الدماغ المستقبل يحصل على سلسلة مشفرة واحدة تجمع جميع الحقول بهذا الترتيب: .string_integer_float
مثال: باستخدام link.send(“string”, 1, 2.55) كـ .string_1_2.5500.
كود للروبوت 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)كود للروبوت 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
كود للروبوت 1
# Tell the other robot where the screen was pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") brain.screen.next_row() brain.screen.print("Tap to send circles") # Send the coordinates whenever the screen is pressed while True: if brain.screen.pressing(): x = brain.screen.x_position() y = brain.screen.y_position() link.send("touch", x, y) wait(10, MSEC)كود للروبوت 2
# Draw a circle where Robot 1's screen was pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") while True: # Get the full encoded string circle_coordinates = link.receive(200) if circle_coordinates: # Split the encoded string by _ parts = circle_coordinates.split("_") if len(parts) == 3: # Convert the coordinates back into numbers x = float(parts[1]) y = float(parts[2]) brain.screen.set_fill_color(Color.WHITE) brain.screen.draw_circle(x, y, 20) wait(10, MSEC)
receive#
يُعيد الأمر receive الرسالة التالية المُدرجة في قائمة الانتظار من وحدة المعالجة V5 الأخرى المرتبطة. تُقرأ الرسائل بترتيب FIFO (الأقدم غير المقروءة أولاً). إذا كانت قائمة الانتظار فارغة عند استدعاء receive ، فإنه ينتظر حتى انتهاء timeout لرسالة جديدة. إذا لم تصل أي رسالة خلال هذه المهلة، يُعيد receive القيمة None، وتبقى أي رسالة مُرسلة بعد ذلك في قائمة الانتظار ليتم قراءتها في المرة التالية التي يُستخدم فيها الأمر receive.
إذا أرسل جهاز V5 Brain المرتبط الآخر سلسلة stringو receive ويعيدان تلك السلسلة بالضبط.
إذا أرسل معالج V5 Brain الآخر المرتبط عددًا صحيحًا و/أو عددًا عشريًا، فإن receive ، ويعيد سلسلة مشفرة واحدة تجمع جميع الحقول بهذا الترتيب: .string_integer_float
على سبيل المثال، إذا استخدم معالج V5 المرتبط الآخر الدالة link.send(“string”, 1, 2.55)، فإن استخدام الدالة link.receive() يُعيد سلسلة مُشفّرة مثل .string_1_2.5500. يمكنك تقسيم هذه السلسلة المُشفّرة (انظر الأمثلة أدناه) لاستخراج اسم الرسالة، والقيم الصحيحة، والقيم العشرية.
إذا كنت تفضل تجنب التقسيم/التحليل اليدوي، فاستخدم received لتسجيل معالج لاسم رسالة معين؛ يتلقى المعالج القيم المرسلة كوسائط.
Usage:
receive(timeout)
حدود |
وصف |
|---|---|
|
اختياري. مدة الانتظار بالمللي ثانية |
كود للروبوت 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)كود للروبوت 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
كود للروبوت 1
# Send a math problem for the other Brain to solve # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ link.send("add", 7, 2.5)كود للروبوت 2
# Solve the math problem that the other Brain sends # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() math_problem = link.receive() if math_problem: # Split the encoded string by _ parts = math_problem.split("_") if len(parts) == 3: # Remove the . from the beginning of the string operator = "".join([c for c in parts[0] if c.isalpha()]) # Separate out the numbers for the operation a = int(parts[1]) b = float(parts[2]) # Use what operator was sent if operator == "add": result = a + b elif operator == "subtract": result = a - b # If neither add or subtract is sent, do nothing else: pass brain.screen.print("Result = ", result)
received#
received يسجل وظيفة ليتم استدعاؤها كلما استقبلت وحدة V5 Brain رسالة مرسلة.
Usage:
link.received(message, arg)
حدود |
وصف |
|---|---|
|
اسم الرسالة (نص) المراد مطابقته. عند مطابقة اسم الرسالة المستلمة مع هذه القيمة، يتم استدعاء دالة رد الاتصال |
|
A previously defined function that runs when the V5 Brain receives a message matching |
تم استلام received دالة رد نداء بأربعة وسائط:
توقيع الاستدعاء:
callback(message, linkname, index, value)
دعوى |
وصف |
|---|---|
|
اسم الرسالة التي تم استلامها (على سبيل المثال، |
|
اسم الرابط الذي تم استلام الرسالة منه. |
|
القيمة العددية المرسلة مع الرسالة (إن وجدت). |
|
القيمة العشرية المرسلة مع الرسالة (إن وجدت). |
كود للروبوت 1
# Send a math problem for the other Brain to solve # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ link.send("add", 7, 2.5)كود للروبوت 2
# Solve the math problem that the other Brain sends # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() # add if "add" is received def add_cmd(message, linkname, a, b): result = a + b brain.screen.print("Result = ", result) # subtract if ""subtract" is received def subtract_cmd(message, linkname, a, b): result = a - b brain.screen.print("Result = ", result) # Create event handlers for "add" and "subtract" link.received("add", add_cmd) link.received("subtract", subtract_cmd)