ข้อความลิงก์#
การแนะนำ#
MessageLink ช่วยให้หุ่นยนต์ EXP Brain สองตัวสื่อสารกันได้โดยการส่งข้อความสั้นๆ พร้อมข้อมูลขนาดเล็กเพิ่มเติม ออกแบบมาเพื่อการประสานงานระหว่างหุ่นยนต์ เช่น การเริ่มต้นรูทีน การแบ่งปันผลลัพธ์จากเซ็นเซอร์ หรือการสั่งการให้หุ่นยนต์อีกตัวแสดงพฤติกรรมบางอย่าง
ทุกครั้งที่ใช้ send จะเพิ่มข้อความลงในคิวของ EXP Brain ที่เชื่อมโยง และคิวจะถูกอ่านแบบเข้าก่อนออกก่อน (FIFO) หากมีการส่งข้อความหลายข้อความก่อนที่ EXP Brain อื่นจะใช้ receive ข้อความเหล่านั้นจะถูกจัดเก็บและส่งคืนทีละข้อความตามลำดับที่ส่ง เนื่องจากข้อความสามารถอยู่ในคิวได้ การส่งสถานะเดียวกันซ้ำๆ ในแต่ละรอบอาจทำให้เกิดงานค้าง สำหรับตรรกะที่สำคัญต่อเวลา ให้ส่งเฉพาะเมื่อค่าเปลี่ยนแปลงเท่านั้น
สำคัญ: หุ่นยนต์ทั้งสองตัวต้องรันโปรเจกต์ที่ใช้ MessageLink พร้อมกัน มิฉะนั้นจะไม่มีการส่ง/รับข้อความใดๆ
หน้านี้ใช้ลิงก์ link เป็นตัวอย่างชื่อ MessageLink MessageLink แทนที่ด้วยชื่อที่คุณกำหนดเองได้ตามต้องการ
ด้านล่างนี้คือรายการวิธีการที่มีให้เลือกใช้:
สร้างลิงก์#
สร้าง MessageLink บน EXP Brain ทั้งสองตัวเพื่อสร้างช่องทางการสื่อสารระหว่างกัน ไม่ว่าจะเป็นแบบไร้สายโดยใช้ V5 Robot Radios หรือผ่าน Smart Cable แบบมีสาย เพื่อให้หุ่นยนต์สามารถส่งข้อความถึงกันได้
MessageLink(smartport, name, linktype, wired)
พารามิเตอร์ |
คำอธิบาย |
|---|---|
|
พอร์ตอัจฉริยะที่ใช้สำหรับลิงก์นี้ คือพอร์ตที่เชื่อมต่อกับวิทยุ V5 Robot (แบบไร้สาย) หรือเสียบสายอัจฉริยะ (แบบมีสาย) เขียนในรูปแบบ |
|
ชื่อเฉพาะที่จะตั้งให้กับลิงก์นี้เป็นสตริง |
|
บทบาทที่ Brain นี้จะใช้สำหรับคู่ลิงก์นี้ แต่ละคู่ต้องประกอบด้วย ผู้จัดการหนึ่งคน และ ผู้ปฏิบัติงานหนึ่งคน:
|
|
ตัวเลือกเสริม การเชื่อมต่อนี้จะเป็นแบบมีสายหรือไร้สาย:
|
รหัสสำหรับหุ่นยนต์ตัวที่ 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 จะคืนค่าว่า EXP Brains บน MessageLink นั้นจับคู่กันหรือไม่
True– สมอง EXP ทั้งสองตัวจับคู่กันและสื่อสารกันผ่านลิงก์นี้False– EXP Brain ทั้งสองตัวไม่ได้จับคู่กันในลิงก์นี้
หมายเหตุ: ควรตรวจสอบให้แน่ใจเสมอว่า 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 Brain ที่เชื่อมต่ออีกตัวหนึ่ง
Usage:
send(string, integer, float)
พารามิเตอร์ |
คำอธิบาย |
|---|---|
|
ข้อความที่จะส่งไปยัง EXP Brain ที่เชื่อมต่ออีกตัวหนึ่ง |
|
ตัวเลือกเสริม: จำนวนเต็มที่จะส่งไปยัง EXP Brain ที่เชื่อมโยงอีกตัวหนึ่ง |
|
ตัวเลือกเสริม: ค่าทศนิยมที่จะส่งไปยัง EXP Brain ที่เชื่อมโยงอีกตัวหนึ่ง ค่าทศนิยมจะถูกจัดรูปแบบเป็น 4 ตำแหน่ง หากระบุจำนวนตำแหน่งทศนิยมน้อยกว่านั้น จะมีการเพิ่มเลขศูนย์ต่อท้ายจนกว่าจะแสดงครบ 4 ตำแหน่ง |
หากส่งสตริง string เพียงสตริงเดียว EXP 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 ส่งคืนข้อความถัดไปที่อยู่ในคิวจาก EXP Brain ที่เชื่อมโยงอีกตัว ข้อความจะถูกอ่านตามลำดับ FIFO (ข้อความที่เก่าที่สุดที่ยังไม่ได้อ่านจะถูกอ่านก่อน) หากคิวว่างเมื่อเรียกใช้ receive ระบบจะรอข้อความใหม่จนถึงเวลาหมดเวลา receive หากไม่มีข้อความใดมาถึงในช่วงเวลานั้น receive จะส่งคืนค่า timeoutและข้อความใดๆ ที่ส่งมาหลังจากนั้นจะยังคงอยู่ในคิวเพื่อรออ่านในครั้งถัดไปที่ใช้ None
ถ้า EXP Brain ที่เชื่อมโยงอีกตัวส่งสตริง string, receive จะส่งสตริงนั้นกลับมา
หาก EXP Brain ที่เชื่อมโยงอีกตัวส่งค่าจำนวนเต็มและ/หรือทศนิยมมา receive และส่งคืนค่าสตริงที่เข้ารหัสเพียงค่าเดียวซึ่งรวมฟิลด์ทั้งหมดตามลำดับนี้: .string_integer_float
ตัวอย่างเช่น หาก EXP Brain ที่เชื่อมโยงอีกตัวใช้ 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 Brain ได้รับข้อความที่ส่งมา
Usage:
link.received(message, arg)
พารามิเตอร์ |
คำอธิบาย |
|---|---|
|
The message name (string) to match. When a received message’s name matches this value, the |
|
A previously defined function that runs when the EXP Brain receives a message matching |
ได้รับ received ฟังก์ชัน โดยมีอาร์กิวเมนต์ 4 ตัว:
รูปแบบการเรียกใช้ฟังก์ชัน Callback:
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)