Liên kết tin nhắn#
Giới thiệu#
MessageLink cho phép hai bộ điều khiển V5 Brain giao tiếp bằng cách gửi các tin nhắn chuỗi ngắn kèm theo dữ liệu nhỏ tùy chọn. Nó được thiết kế để phối hợp giữa các robot — chẳng hạn như khởi động các quy trình, chia sẻ kết quả cảm biến hoặc kích hoạt các hành vi trên robot khác.
Mỗi lần sử dụng send sẽ thêm một tin nhắn vào hàng đợi của V5 Brain được liên kết, và hàng đợi được đọc theo thứ tự vào trước ra trước (FIFO). Nếu nhiều tin nhắn được gửi trước khi V5 Brain khác sử dụng receive, chúng sẽ được lưu trữ và trả về từng tin nhắn một theo thứ tự chúng được gửi. Vì tin nhắn có thể được xếp hàng đợi, việc liên tục gửi cùng một trạng thái trong mỗi vòng lặp có thể tạo ra sự tắc nghẽn; đối với logic cần xử lý nhanh, chỉ gửi khi giá trị thay đổi.
Quan trọng: Cả hai robot phải cùng lúc chạy các dự án sử dụng MessageLink MessageLink , nếu không sẽ không có tin nhắn nào được gửi/nhận.
Trang này sử dụng link làm ví dụ cho tên MessageLink Hãy thay thế bằng tên do bạn cấu hình nếu cần.
Dưới đây là danh sách các phương pháp có sẵn:
Tạo liên kết#
Tạo MessageLink trên cả hai bộ điều khiển V5 Brain để thiết lập kênh liên lạc giữa chúng — có thể là không dây bằng V5 Robot Radio hoặc thông qua cáp thông minh có dây — để các robot có thể gửi tin nhắn cho nhau.
MessageLink(smartport, name, linktype, wired)
Tham số |
Sự miêu tả |
|---|---|
|
Cổng thông minh được sử dụng cho liên kết này—cổng mà bộ thu phát vô tuyến V5 Robot được kết nối (không dây) hoặc cáp thông minh được cắm vào (có dây). Được viết dưới dạng |
|
Tên duy nhất để đặt cho liên kết này dưới dạng chuỗi. |
|
Vai trò mà Brain này sẽ sử dụng cho cặp liên kết này. Mỗi cặp phải bao gồm một Manager và một Worker:
|
|
Tùy chọn. Kết nối này là có dây hay không dây:
|
Mã lệnh cho Robot 1
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)Mã lệnh cho Robot 2
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
Các phương pháp có sẵn#
Sau khi MessageLink được thiết lập và các thiết bị Brains được liên kết, bạn có thể sử dụng các phương thức bên dưới để gửi, nhận và trả lời tin nhắn.
is_linked#
Phương thức is_linked trả về trạng thái cho biết liệu các V5 Brains trên MessageLink có được ghép nối với nhau hay không.
True– Hai bộ não V5 được ghép nối và đang giao tiếp trên liên kết này.False– Hai bộ não V5 không được ghép nối trên liên kết này.
Lưu ý: Tốt nhất là luôn kiểm tra để đảm bảo các bộ xử lý V5 Brains đã được liên kết trước khi chạy bất kỳ đoạn mã nào khác khi bắt đầu dự án.
Usage:
link.is_linked()
Thông số |
Sự miêu tả |
|---|---|
Phương pháp này không có tham số. |
Mã lệnh cho Robot 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)Mã lệnh cho Robot 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 gửi một chuỗi ký tự và/hoặc một số nguyên và/hoặc một số thực đến bộ não V5 được liên kết khác.
Usage:
send(string, integer, float)
Thông số |
Sự miêu tả |
|---|---|
|
Chuỗi ký tự cần gửi đến bộ não V5 được liên kết khác. |
|
Tùy chọn. Một số nguyên để gửi đến bộ điều khiển V5 Brain được liên kết khác. |
|
Tùy chọn. Một số thực để gửi đến bộ xử lý V5 Brain được liên kết khác. Số thực được định dạng với 4 chữ số thập phân; nếu cung cấp ít hơn số chữ số thập phân, các số 0 ở cuối sẽ được thêm vào cho đến khi hiển thị đủ 4 chữ số. |
Nếu chỉ gửi chuỗi string , thì bộ xử lý V5 Brain được liên kết còn lại sẽ nhận được chính xác chuỗi đó.
Nếu bao gồm integer và/hoặc float , Brain nhận được một chuỗi mã hóa duy nhất kết hợp tất cả các trường theo thứ tự này: .string_integer_float
Ví dụ: Sử dụng link.send(“string”, 1, 2.55) được nhận dưới dạng .string_1_2.5500.
Mã lệnh cho Robot 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)Mã lệnh cho Robot 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)
Mã lệnh cho Robot 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)Mã lệnh cho Robot 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 trả về tin nhắn tiếp theo trong hàng đợi từ bộ xử lý V5 Brain được liên kết khác. Tin nhắn được đọc theo thứ tự FIFO (tin nhắn cũ nhất chưa đọc trước). Nếu hàng đợi trống khi receive được gọi, nó sẽ đợi tối đa đến thời gian chờ receive để nhận tin nhắn mới. Nếu không có tin nhắn nào đến trong khoảng thời gian đó, receive trả về timeout, và bất kỳ tin nhắn nào được gửi sau đó vẫn nằm trong hàng đợi để được đọc vào lần tiếp theo None được sử dụng.
Nếu bộ não V5 được liên kết khác chỉ gửi chuỗi string, receive và trả về chính xác chuỗi đó.
Nếu bộ xử lý V5 Brain được liên kết khác gửi một số nguyên và/hoặc số thực, thì receive sẽ trả về một chuỗi được mã hóa duy nhất kết hợp tất cả các trường theo thứ tự này: .string_integer_float
Ví dụ, nếu V5 Brain được liên kết khác sử dụng link.send(“string”, 1, 2.55), thì việc sử dụng link.receive() sẽ trả về một chuỗi được mã hóa như .string_1_2.5500. Bạn có thể tách chuỗi được mã hóa này (xem các ví dụ bên dưới) để trích xuất tên thông báo, giá trị số nguyên và số thực.
Nếu bạn muốn tránh việc tách/phân tích cú pháp thủ công, hãy sử dụng received để đăng ký trình xử lý cho một tên tin nhắn cụ thể; trình xử lý sẽ nhận các giá trị đã gửi dưới dạng đối số.
Usage:
receive(timeout)
Thông số |
Sự miêu tả |
|---|---|
|
Tùy chọn. Thời gian chờ (tính bằng mili giây) |
Mã lệnh cho Robot 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)Mã lệnh cho Robot 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)
Mã lệnh cho Robot 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)Mã lệnh cho Robot 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 đăng ký một hàm sẽ được gọi bất cứ khi nào V5 Brain nhận được một tin nhắn đã gửi.
Usage:
link.received(message, arg)
Thông số |
Sự miêu tả |
|---|---|
|
The message name (string) to match. When a received message’s name matches this value, the |
|
Một hàm được định nghĩa trước đó chạy khi Bộ não V5 nhận một thông báo khớp với thông báo |
received hàm gọi lại được gọi với bốn đối số:
Chữ ký hàm gọi lại:
callback(message, linkname, index, value)
Lý lẽ |
Sự miêu tả |
|---|---|
|
Tên tin nhắn đã nhận được (ví dụ: |
|
Tên liên kết mà tin nhắn được nhận từ đó. |
|
Giá trị số nguyên được gửi kèm theo tin nhắn (nếu có). |
|
Giá trị số thực được gửi kèm theo tin nhắn (nếu có). |
Mã lệnh cho Robot 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)Mã lệnh cho Robot 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)