মেসেজলিঙ্ক#
ভূমিকা#
মেসেজলিঙ্ক দুটি V5 ব্রেইনকে ঐচ্ছিক ক্ষুদ্র ডেটাসহ সংক্ষিপ্ত স্ট্রিং বার্তা পাঠানোর মাধ্যমে একে অপরের সাথে যোগাযোগ করতে দেয়। এটি রোবট-টু-রোবট সমন্বয়ের জন্য ডিজাইন করা হয়েছে — যেমন রুটিন শুরু করা, সেন্সরের ফলাফল শেয়ার করা, বা অন্য কোনো রোবটে নির্দিষ্ট আচরণ চালু করা।
send এর প্রতিটি ব্যবহার লিঙ্ক করা V5 ব্রেইনের কিউতে একটি বার্তা যোগ করে, এবং কিউটি ফার্স্ট-ইন, ফার্স্ট-আউট (FIFO) পদ্ধতিতে পড়া হয়। যদি অন্য V5 ব্রেইন receive ব্যবহার করার আগে একাধিক বার্তা পাঠানো হয়, তাহলে সেগুলি সংরক্ষণ করা হবে এবং পাঠানোর ক্রমানুসারে এক এক করে ফেরত দেওয়া হবে। যেহেতু বার্তাগুলি কিউতে জমা হতে পারে, তাই প্রতিটি লুপে বারবার একই স্ট্যাটাস পাঠালে ব্যাকলগ তৈরি হতে পারে; সময়-সংবেদনশীল লজিকের জন্য, শুধুমাত্র মান পরিবর্তন হলেই পাঠান।
গুরুত্বপূর্ণ: উভয় রোবটকে অবশ্যই একই সময়ে এমন প্রজেক্ট চালাতে হবে যা MessageLink ব্যবহার করে, অন্যথায় কোনো মেসেজ পাঠানো বা গ্রহণ করা যাবে না।
এই পৃষ্ঠাটি উদাহরণ হিসেবে MessageLink নাম হিসেবে link ব্যবহার করেছে। প্রয়োজন অনুযায়ী এটিকে আপনার নিজস্ব কনফিগার করা নাম দিয়ে প্রতিস্থাপন করুন।
উপলব্ধ পদ্ধতিগুলোর একটি তালিকা নিচে দেওয়া হলো:
একটি লিঙ্ক তৈরি করুন#
তাদের মধ্যে একটি যোগাযোগ চ্যানেল স্থাপন করতে উভয় V5 ব্রেইনে একটি মেসেজলিঙ্ক তৈরি করুন—হয় V5 রোবট রেডিওর মাধ্যমে তারবিহীনভাবে অথবা একটি তারযুক্ত স্মার্ট কেবলের মাধ্যমে—যাতে রোবটগুলো একে অপরকে বার্তা পাঠাতে পারে।
MessageLink(smartport, name, linktype, wired)
প্যারামিটার |
বর্ণনা |
|---|---|
|
এই লিঙ্কের জন্য ব্যবহৃত স্মার্ট পোর্ট—যে পোর্টে V5 রোবট রেডিও সংযুক্ত থাকে (ওয়্যারলেস) অথবা স্মার্ট ক্যাবলটি প্লাগ করা থাকে (ওয়্যারড)। এটিকে |
|
এই লিঙ্কটিকে স্ট্রিং হিসেবে যে অনন্য নামটি দেওয়া হবে। |
|
এই ব্রেইনটি এই লিঙ্ক পেয়ারের জন্য যে ভূমিকা পালন করবে। প্রতিটি পেয়ারে অবশ্যই একজন ম্যানেজার এবং একজন ওয়ার্কার থাকতে হবে:
|
|
ঐচ্ছিক। এই সংযোগটি তারযুক্ত নাকি তারবিহীন:
|
রোবট ১-এর জন্য কোড
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)রোবট ২ এর কোড
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
উপলব্ধ পদ্ধতি#
একবার আপনার MessageLink সেট আপ হয়ে গেলে এবং Brain-গুলো লিঙ্ক হয়ে গেলে, আপনি নিচের পদ্ধতিগুলো ব্যবহার করে বার্তা পাঠাতে, গ্রহণ করতে এবং উত্তর দিতে পারবেন।
is_linked#
is_linked মেথডটি রিটার্ন করে যে একটি MessageLink-এ থাকা V5 Brain-গুলো একে অপরের সাথে জোড়বদ্ধ কিনা।
True– দুটি ভি৫ ব্রেইন জোড়া বেঁধেছে এবং এই লিঙ্কে যোগাযোগ করছে।False– এই লিঙ্কে দুটি V5 ব্রেইন জোড়া হিসেবে নেই।
দ্রষ্টব্য: পরবর্তী যেকোনো কোড চালানোর আগে, প্রজেক্ট শুরুর সময়ে V5 ব্রেইনগুলো লিঙ্ক করা আছে কিনা তা সবসময় যাচাই করে নেওয়া একটি ভালো অভ্যাস।
Usage:
link.is_linked()
প্যারামিটার |
বর্ণনা |
|---|---|
এই পদ্ধতিতে কোনো প্যারামিটার নেই। |
রোবট ১-এর জন্য কোড
# 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)রোবট ২ এর কোড
# 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 ব্রেইনটিতে একটি স্ট্রিং এবং/অথবা একটি পূর্ণসংখ্যা এবং/অথবা একটি ফ্লোট পাঠায়।
Usage:
send(string, integer, float)
প্যারামিটার |
বর্ণনা |
|---|---|
|
অন্য সংযুক্ত V5 ব্রেইনটিতে পাঠানোর জন্য স্ট্রিং। |
|
ঐচ্ছিক। অন্য সংযুক্ত V5 ব্রেইন-এ পাঠানোর জন্য একটি পূর্ণসংখ্যা। |
|
ঐচ্ছিক। অন্য সংযুক্ত V5 ব্রেইন-এ পাঠানোর জন্য একটি ফ্লোট। ফ্লোট সংখ্যাগুলোকে ৪ দশমিক স্থান পর্যন্ত ফরম্যাট করা হয়; যদি এর চেয়ে কম দশমিক স্থান দেওয়া হয়, তবে ৪টি দশমিক স্থান প্রদর্শিত না হওয়া পর্যন্ত শেষে শূন্য যোগ করা হয়। |
যদি শুধুমাত্র string পাঠানো হয়, তাহলে অন্য সংযুক্ত V5 ব্রেইনটি ঠিক সেই স্ট্রিংটিই গ্রহণ করে।
যদি integer এবং/অথবা float অন্তর্ভুক্ত থাকে, তাহলে গ্রহণকারী ব্রেইন একটি একক এনকোডেড স্ট্রিং পায় যা এই ক্রমে সমস্ত ফিল্ডকে একত্রিত করে: .string_integer_float
উদাহরণ: link.send(“string”, 1, 2.55) পাওয়া যায় .string_1_2.5500.
রোবট ১-এর জন্য কোড
# 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)রোবট ২ এর কোড
# 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)
রোবট ১-এর জন্য কোড
# 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)রোবট ২ এর কোড
# 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 ব্রেইনটি শুধুমাত্র একটি stringপাঠায়, receive ঠিক সেই স্ট্রিংটিই ফেরত দেয়।
যদি অন্য সংযুক্ত V5 ব্রেইন একটি পূর্ণসংখ্যা এবং/অথবা ফ্লোট পাঠায়, receive একটি একক এনকোডেড স্ট্রিং ফেরত দেয় যা এই ক্রমে সমস্ত ক্ষেত্রকে একত্রিত করে: .string_integer_float
উদাহরণস্বরূপ, যদি অন্য লিঙ্ক করা V5 ব্রেইন link.send(“string”, 1, 2.55)ব্যবহার করে, তাহলে link.receive() ব্যবহার করলে .string_1_2.5500এর মতো একটি এনকোডেড স্ট্রিং ফেরত আসে। আপনি এই এনকোডেড স্ট্রিংটি বিভক্ত করে (নীচের উদাহরণগুলি দেখুন) বার্তার নাম, পূর্ণসংখ্যা এবং ফ্লোট মানগুলি বের করতে পারেন।
আপনি যদি ম্যানুয়াল বিভাজন/পার্সিং এড়াতে চান, তাহলে একটি নির্দিষ্ট বার্তার নামের জন্য একটি হ্যান্ডলার নিবন্ধন করতে received ব্যবহার করুন; হ্যান্ডলারটি প্রেরিত মানগুলিকে আর্গুমেন্ট হিসাবে গ্রহণ করে।
Usage:
receive(timeout)
প্যারামিটার |
বর্ণনা |
|---|---|
|
ঐচ্ছিক। কতক্ষণ (মিলিসেকেন্ডে |
রোবট ১-এর জন্য কোড
# 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)রোবট ২ এর কোড
# 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 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)রোবট ২ এর কোড
# 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)
প্যারামিটার |
বর্ণনা |
|---|---|
|
যে বার্তার নামটি (স্ট্রিং) মেলাতে হবে। যখন প্রাপ্ত কোনো বার্তার নাম এই মানের সাথে মিলে যায়, তখন |
|
পূর্বে সংজ্ঞায়িত একটি ফাংশন যা তখন চলে যখন V5 ব্রেইন |
received কলব্যাক চারটি আর্গুমেন্ট সহ কল করা হয়েছে:
কলব্যাক সিগনেচার:
callback(message, linkname, index, value)
যুক্তি |
বর্ণনা |
|---|---|
|
প্রাপ্ত বার্তার নাম (উদাহরণস্বরূপ, |
|
যে লিঙ্কের নাম থেকে বার্তাটি পাওয়া গেছে। |
|
বার্তার সাথে পাঠানো পূর্ণসংখ্যা মান (যদি প্রদান করা হয়)। |
|
বার্তার সাথে পাঠানো ফ্লোট মান (যদি প্রদান করা হয়)। |
রোবট ১-এর জন্য কোড
# 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)রোবট ২ এর কোড
# 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)