संदेश लिंक#
परिचय#
MessageLink दो EXP Brains को छोटे स्ट्रिंग संदेशों और वैकल्पिक रूप से छोटे डेटा भेजकर संवाद करने की सुविधा देता है। इसे रोबोट-टू-रोबोट समन्वय के लिए डिज़ाइन किया गया है — जैसे कि रूटीन शुरू करना, सेंसर के परिणाम साझा करना या किसी अन्य रोबोट पर व्यवहार को सक्रिय करना।
send के प्रत्येक उपयोग से लिंक्ड EXP ब्रेन की कतार में एक संदेश जुड़ जाता है, और कतार को पहले आओ, पहले जाओ (FIFO) के आधार पर पढ़ा जाता है। यदि दूसरे EXP ब्रेन द्वारा receive का उपयोग करने से पहले कई संदेश भेजे जाते हैं, तो उन्हें संग्रहीत किया जाएगा और भेजे जाने के क्रम में एक-एक करके वापस किया जाएगा। क्योंकि संदेश कतार में लग सकते हैं, प्रत्येक लूप में एक ही स्थिति को बार-बार भेजने से बैकलॉग बन सकता है; समय-संवेदनशील लॉजिक के लिए, केवल तभी भेजें जब मान बदलते हों।
महत्वपूर्ण: दोनों रोबोटों को एक ही समय में MessageLink का उपयोग करने वाले प्रोजेक्ट चलाने होंगे, अन्यथा कोई संदेश भेजा/प्राप्त नहीं किया जाएगा।
इस पृष्ठ पर उदाहरण के तौर पर link MessageLink का नाम दिया गया है। आवश्यकतानुसार इसे अपने द्वारा निर्धारित नाम से बदलें।
नीचे उपलब्ध विधियों की सूची दी गई है:
is_linked– यह बताता है कि क्या यह ब्रेन अपने युग्मित ब्रेन से सक्रिय रूप से जुड़ा हुआ है।send– युग्मित मस्तिष्क को एक संदेश भेजता है।receive– अगले आने वाले संदेश की प्रतीक्षा करता है और उसे लौटाता है।received– जब भी कोई नया संदेश प्राप्त होता है, तो कॉल करने के लिए एक फ़ंक्शन पंजीकृत करता है।
एक लिंक बनाएं#
दोनों EXP ब्रेन पर एक मैसेजलिंक बनाएं ताकि उनके बीच एक संचार चैनल स्थापित हो सके—या तो 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 सेटअप हो जाए और Brains लिंक हो जाएं, तो आप नीचे दिए गए तरीकों का उपयोग करके संदेश भेज सकते हैं, प्राप्त कर सकते हैं और उनका जवाब दे सकते हैं।
is_linked#
is_linked विधि यह लौटाती है कि क्या MessageLink पर EXP Brains एक दूसरे के साथ युग्मित हैं।
True– दोनों EXP ब्रेन युग्मित हैं और इस लिंक पर संवाद कर रहे हैं।False– इस लिंक पर दो EXP ब्रेन युग्मित नहीं हैं।
नोट: किसी भी कोड को चलाने से पहले, प्रोजेक्ट की शुरुआत में यह सुनिश्चित करना हमेशा अच्छा अभ्यास है कि EXP 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 दूसरे लिंक्ड EXP ब्रेन को एक स्ट्रिंग और/या एक पूर्णांक और/या फ्लोट भेजता है।
Usage:
send(string, integer, float)
पैरामीटर |
विवरण |
|---|---|
|
दूसरे लिंक किए गए EXP ब्रेन को भेजने के लिए स्ट्रिंग। |
|
वैकल्पिक। दूसरे लिंक किए गए EXP ब्रेन को भेजने के लिए एक पूर्णांक। |
|
वैकल्पिक। दूसरे लिंक किए गए EXP ब्रेन को भेजने के लिए एक फ्लोट मान। फ्लोट मान 4 दशमलव स्थानों तक स्वरूपित होते हैं; यदि कम दशमलव स्थान दिए जाते हैं, तो 4 दशमलव स्थान दिखने तक अंत में शून्य जोड़े जाते हैं। |
यदि केवल string भेजी जाती है, तो दूसरा लिंक किया गया EXP ब्रेन ठीक वही स्ट्रिंग प्राप्त करता है।
यदि 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 दूसरे लिंक्ड EXP ब्रेन से अगला क्यूड मैसेज लौटाता है। मैसेज FIFO क्रम में पढ़े जाते हैं (सबसे पुराना अपठित पहले)। यदि receive कॉल किए जाने पर क्यू खाली है, तो यह निर्दिष्ट timeout तक नए मैसेज की प्रतीक्षा करता है। यदि उस समय सीमा में कोई मैसेज नहीं आता है, तो receive Noneलौटाता है, और उसके बाद भेजा गया कोई भी मैसेज अगली बार receive उपयोग किए जाने पर पढ़ने के लिए क्यू में रहता है।
यदि दूसरे लिंक्ड EXP ब्रेन ने केवल string, receive को भेजा तो वही सटीक स्ट्रिंग वापस मिलती है।
यदि दूसरे लिंक्ड EXP ब्रेन ने एक पूर्णांक और/या फ्लोट भेजा, तो receive एक एकल एन्कोडेड स्ट्रिंग लौटाता है जो सभी फ़ील्ड को इस क्रम में जोड़ता है: .string_integer_float
उदाहरण के लिए, यदि दूसरा लिंक्ड EXP ब्रेन 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 एक फ़ंक्शन को पंजीकृत करता है जिसे EXP ब्रेन द्वारा भेजा गया संदेश प्राप्त होने पर कॉल किया जाएगा।
Usage:
link.received(message, arg)
पैरामीटर |
विवरण |
|---|---|
|
मिलान करने के लिए संदेश का नाम (स्ट्रिंग)। जब प्राप्त संदेश का नाम इस मान से मेल खाता है, तो |
|
एक पूर्व-परिभाषित फ़ंक्शन जो तब चलता है जब EXP ब्रेन |
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)