বার্তা লিঙ্ক#
ভূমিকা#
message_link ক্লাসটি দুটি V5 ব্রেইনকে ঐচ্ছিক ছোট ডেটাসহ সংক্ষিপ্ত স্ট্রিং বার্তা পাঠানোর মাধ্যমে একে অপরের সাথে যোগাযোগ করতে দেয়। এটি রোবট-টু-রোবট সমন্বয়ের জন্য ডিজাইন করা হয়েছে — যেমন রুটিন শুরু করা, সেন্সরের ফলাফল শেয়ার করা, বা অন্য রোবটে কোনো আচরণ চালু করা।
send এর প্রতিটি ব্যবহার লিঙ্ক করা V5 ব্রেইনের কিউতে একটি বার্তা যোগ করে, এবং কিউটি ফার্স্ট-ইন, ফার্স্ট-আউট (FIFO) পদ্ধতিতে পড়া হয়। যদি অন্য V5 ব্রেইন receive ব্যবহার করার আগে একাধিক বার্তা পাঠানো হয়, তাহলে সেগুলি সংরক্ষণ করা হবে এবং পাঠানোর ক্রমানুসারে এক এক করে ফেরত দেওয়া হবে। যেহেতু বার্তাগুলি কিউতে জমা হতে পারে, তাই প্রতিটি লুপে বারবার একই স্ট্যাটাস পাঠালে ব্যাকলগ তৈরি হতে পারে; সময়-সংবেদনশীল লজিকের জন্য, শুধুমাত্র মান পরিবর্তন হলেই পাঠান।
গুরুত্বপূর্ণ: উভয় রোবটকে অবশ্যই একই সময়ে এমন প্রজেক্ট চালাতে হবে যা message_link ব্যবহার করে, অন্যথায় কোনো মেসেজ পাঠানো বা গ্রহণ করা যাবে না।
ক্লাস কনস্ট্রাক্টর#
message_link(
int32_t index,
const char *name,
linkType type,
bool isWired = false );
ক্লাস ডেস্ট্রাক্টর#
message_link অবজেক্টটি ধ্বংস করে এবং সংশ্লিষ্ট রিসোর্সসমূহ মুক্ত করে।
virtual ~message_link();
প্যারামিটার#
প্যারামিটার |
প্রকার |
বর্ণনা |
|---|---|---|
|
|
যে স্মার্ট পোর্টের সাথে V5 রেডিও বা স্মার্ট কেবলটি সংযুক্ত থাকে, তা |
|
|
মেসেজ লিঙ্কের নাম। |
|
|
লিঙ্কের ধরণ, হয়
|
|
|
message_link অবজেক্টটি তারযুক্ত কিনা।
|
উদাহরণ#
রোবট ১-এর জন্য কোড
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredরোবট ২ এর কোড
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
সদস্যদের কার্যাবলী#
message_link ক্লাসে নিম্নলিখিত সদস্য ফাংশনগুলো অন্তর্ভুক্ত রয়েছে:
isLinked— এই ব্রেইনটি তার জোড়া ব্রেইনের সাথে সক্রিয়ভাবে সংযুক্ত আছে কিনা তা ফেরত দেয়।send— জোড়া মস্তিষ্কে একটি বার্তা পাঠায়।receive— পরবর্তী আগত বার্তার জন্য অপেক্ষা করে এবং তা ফেরত পাঠায়।received— যখনই একটি নতুন বার্তা পাওয়া যাবে তখন কল করার জন্য একটি ফাংশন নিবন্ধন করে।
যেকোনো message_link মেম্বার ফাংশন কল করার আগে, message_link একটি ইনস্ট্যান্স তৈরি করতে হবে, যেমনটি নিচে দেখানো হয়েছে:
রোবট ১-এর জন্য কোড
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredরোবট ২ এর কোড
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
isLinked#
isLinked মেথডটি রিটার্ন করে যে একটি MessageLink-এ থাকা V5 Brain-গুলো একে অপরের সাথে জোড়বদ্ধ কিনা।
bool isLinked();
এই ফাংশনটির কোনো প্যারামিটার নেই।
Return Valuesএই ফাংশনটি একটি বুলিয়ান মান রিটার্ন করে।
true– দুটি ভি৫ ব্রেইন জোড়া বেঁধেছে এবং এই লিঙ্কে যোগাযোগ করছে।false– এই লিঙ্কে দুটি V5 ব্রেইন জোড়া হিসেবে নেই।
পরবর্তী যেকোনো কোড চালানোর আগে, প্রজেক্ট শুরুর সময়ে V5 ব্রেইনগুলো লিঙ্ক করা আছে কিনা তা সবসময় যাচাই করে নেওয়া একটি ভালো অভ্যাস।
Examplesরোবট ১-এর জন্য কোড
// Tell the other robot when the screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ 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(0.05, seconds); }রোবট ২ এর কোড
// Display if the other robot's screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } // Continuously check if Robot 1 has sent "pressed" while (true){ Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); char msg[32] = {0}; // buffer for incoming message int32_t n = link.receive(msg, sizeof(msg), 100); // wait up to 100ms if (n > 0 && strcmp(msg, "pressed") == 0) { Brain.Screen.newLine(); Brain.Screen.print("Manager is being pressed!"); } wait(0.05, seconds); }
send#
লিঙ্ক সংযোগের মাধ্যমে একটি বার্তা পাঠায়।
Available FunctionsParameters1 — একটি বার্তা স্ট্রিং পাঠায়।
int32_t send( const char *message );2 — একটি
doubleসহ একটি বার্তা স্ট্রিং পাঠায়।int32_t send( const char *message, double value );3 — একটি
int32_tএবং একটিdoubleব্যবহার করে একটি বার্তা স্ট্রিং পাঠায়।int32_t send( const char *message, int32_t index, double value );
প্যারামিটার |
প্রকার |
বর্ণনা |
|---|---|---|
|
|
অন্য সংযুক্ত V5 ব্রেইনটিতে পাঠানোর জন্য বার্তা। |
|
|
অপর সংযুক্ত V5 ব্রেইন-এ পাঠানোর জন্য একটি পূর্ণসংখ্যা। |
|
|
অন্য সংযুক্ত V5 ব্রেইন-এ পাঠানোর জন্য একটি ডাবল সংখ্যা। ডাবল সংখ্যাগুলোকে ৪ দশমিক স্থান পর্যন্ত ফরম্যাট করা হয়; যদি এর চেয়ে কম দশমিক স্থান দেওয়া হয়, তবে শেষে শূন্য যোগ করা হয় যতক্ষণ না ৪টি দশমিক স্থান দেখানো হয়। |
এই ফাংশনটি কোনো মান ফেরত দেয় না।
Notesযদি শুধুমাত্র message পাঠানো হয়, তাহলে অন্য সংযুক্ত V5 ব্রেইনটি ঠিক সেই বার্তাটিই গ্রহণ করে।
যদি value (এবং index যদি প্রদান করা হয়) অন্তর্ভুক্ত থাকে, তাহলে গ্রহণকারী ব্রেইন একটি একক এনকোডেড বার্তা পায় যা এই ক্রমে সমস্ত ক্ষেত্রকে একত্রিত করে: .message_index_value
link.send(“string”, 1, 2.55) পাওয়া যায় .string_1_2.5500.
রোবট ১-এর জন্য কোড
// Tell the other robot when the screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ 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(0.05, seconds); }রোবট ২ এর কোড
// Display if the other robot's screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } // Continuously check if Robot 1 has sent "pressed" while (true){ Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); char msg[32] = {0}; // buffer for incoming message int32_t n = link.receive(msg, sizeof(msg), 100); // wait up to 100ms if (n > 0 && strcmp(msg, "pressed") == 0) { Brain.Screen.newLine(); Brain.Screen.print("Manager is being pressed!"); } wait(0.05, seconds); }
receive#
লিঙ্ক সংযোগ থেকে একটি বার্তা গ্রহণ করে।
Available Functionsint32_t receive(
char *buffer,
int32_t length,
int32_t timeoutMs = 500 );
প্যারামিটার |
প্রকার |
বর্ণনা |
|---|---|---|
|
|
প্রাপ্ত বার্তা সংরক্ষণের জন্য বাফার। |
|
|
বাফারের দৈর্ঘ্য। |
|
|
মিলিসেকেন্ডে কতক্ষণ: |
এই ফাংশনটি প্রাপ্ত বাইটের সংখ্যা ফেরত দেয়।
Notesreceiveঅন্য লিঙ্ক করা V5 ব্রেইন থেকে কিউতে থাকা পরবর্তী মেসেজটি ফেরত দেয়। মেসেজগুলো FIFO ক্রমে (সবচেয়ে পুরোনো অপঠিত মেসেজটি প্রথমে) পড়া হয়।receiveকল করার সময় যদি কিউ খালি থাকে, তবে এটি একটি নতুন মেসেজের জন্য নির্দিষ্টtimeoutপর্যন্ত অপেক্ষা করে। যদি সেই সময়ের মধ্যে কোনো মেসেজ না আসে,receiveফেরত দেয়0, এবং এর পরে পাঠানো যেকোনো মেসেজ কিউতে থেকে যায়, যা পরবর্তীবারreceiveব্যবহার করার সময় পড়া হবে।যদি অন্য সংযুক্ত V5 ব্রেইনটি শুধুমাত্র একটি
stringপাঠায়,receiveঠিক সেই স্ট্রিংটিই ফেরত দেয়।যদি অন্য সংযুক্ত V5 ব্রেইন একটি পূর্ণসংখ্যা এবং/অথবা ফ্লোট পাঠায়,
receiveএকটি একক এনকোডেড স্ট্রিং ফেরত দেয় যা এই ক্রমে সমস্ত ক্ষেত্রকে একত্রিত করে:.string_integer_floatউদাহরণস্বরূপ, যদি অন্য লিঙ্ক করা V5 ব্রেইন
link.send(“string”, 1, 2.55)ব্যবহার করে, তাহলেlink.receive()ব্যবহার করলে.string_1_2.5500এর মতো একটি এনকোডেড স্ট্রিং ফেরত আসে। আপনি এই এনকোডেড স্ট্রিংটি বিভক্ত করে (নীচের উদাহরণগুলি দেখুন) বার্তার নাম, পূর্ণসংখ্যা এবং ফ্লোট মানগুলি বের করতে পারেন।আপনি যদি ম্যানুয়াল বিভাজন/পার্সিং এড়াতে চান, তাহলে একটি নির্দিষ্ট বার্তার নামের জন্য একটি হ্যান্ডলার নিবন্ধন করতে
receivedব্যবহার করুন; হ্যান্ডলারটি প্রেরিত মানগুলিকে আর্গুমেন্ট হিসাবে গ্রহণ করে।
link.send(“string”, 1, 2.55) পাওয়া যায় .string_1_2.5500.
রোবট ১-এর জন্য কোড
// Tell the other robot when the screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ 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(0.05, seconds); }রোবট ২ এর কোড
// Display if the other robot's screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } // Continuously check if Robot 1 has sent "pressed" while (true){ Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); char msg[32] = {0}; // buffer for incoming message int32_t n = link.receive(msg, sizeof(msg), 100); // wait up to 100ms if (n > 0 && strcmp(msg, "pressed") == 0) { Brain.Screen.newLine(); Brain.Screen.print("Manager is being pressed!"); } wait(0.05, seconds); }
received#
যখনই V5 ব্রেইন কোনো প্রেরিত বার্তা গ্রহণ করবে, তখন কল করার জন্য একটি ফাংশন নিবন্ধন করে।
Available FunctionsParameters1 প্রাপ্ত সকল বার্তার (বার্তা + মান) জন্য একটি কলব্যাক নিবন্ধন করে।
void received( void (* callback)(const char *, const char *, double) );2 প্রাপ্ত সকল বার্তার (বার্তা + সূচক + মান) জন্য একটি কলব্যাক নিবন্ধন করে।
void received( void (* callback)(const char *, const char *, int32_t, double) );3 একটি নির্দিষ্ট বার্তার (বার্তা + মান) জন্য একটি কলব্যাক নিবন্ধন করে।
void received( const char *message, void (* callback)(const char *, const char *, double) );4 একটি নির্দিষ্ট বার্তার (বার্তা + সূচক + মান) জন্য একটি কলব্যাক নিবন্ধন করে।
void received( const char *message, void (* callback)(const char *, const char *, int32_t, double) );
প্যারামিটার |
প্রকার |
বর্ণনা |
|---|---|---|
|
|
যে বার্তার জন্য অপেক্ষা করতে হবে তার নাম। এটি বাদ দিলে, প্রাপ্ত সমস্ত বার্তার জন্য কলব্যাকটি চলবে। |
|
|
একটি ফাংশন যা কোনো বার্তা পেলে কার্যকর হয়। |
এই ফাংশনটি কোনো মান ফেরত দেয় না।
Callback Signature1 প্রেরকের নাম, বার্তার নাম এবং মান গ্রহণ করে।
void callback(const char *, const char *, double)2 প্রেরকের নাম, বার্তার নাম, সূচক এবং মান গ্রহণ করে।
void callback(const char *, const char *, int32_t, double)
প্রকার |
বর্ণনা |
|---|---|
1st |
প্রেরক লিঙ্কের নাম। |
2nd |
বার্তার নাম পাওয়া গেছে। |
|
বার্তাটির সাথে সংশ্লিষ্ট সূচক। |
|
বার্তাটির সাথে সংশ্লিষ্ট মান। |
রোবট ১-এর জন্য কোড
// Create the link. message_link link(PORT1, "LeftRightLink", linkType::manager, true); // Send whether screen is pressed on left or right. void screen_pressed() { if (Brain.Screen.xPosition() < 240) { link.send("left"); } else { link.send("right"); } } int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Do not run code UNTIL the Brains are linked. while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.print("Robot 1 - Manager"); Brain.Screen.pressed(screen_pressed); while (true) { wait(10, msec); } }রোবট ২ এর কোড
// Create variable to track where screen is pressed. int lastPress = 0; // Update the variable whenever a new message is received. void message_received(const char* message, const char* linkname, double timestamp) { if (strcmp(message, "left") == 0) lastPress = 1; if (strcmp(message, "right") == 0) lastPress = 2; } int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Create the link. message_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked. while (!link.isLinked()) { wait(0.1, seconds); } link.received(message_received); // Display if Robot 1's screen was pressed on the left or right while (true) { Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); if (lastPress == 1) { Brain.Screen.print("Manager pressed LEFT"); } else if (lastPress == 2) { Brain.Screen.print("Manager pressed RIGHT"); } else { Brain.Screen.print("Waiting..."); } wait(0.1, seconds); } }