संदेश लिंक#
परिचय#
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();
पैरामीटर#
पैरामीटर |
प्रकार |
विवरण |
|---|---|---|
|
|
स्मार्ट पोर्ट जिससे वी5 रेडियो या स्मार्ट केबल जुड़ा हुआ है, उसे |
|
|
संदेश लिंक का नाम। |
|
|
लिंक का प्रकार, या तो
|
|
|
क्या message_link ऑब्जेक्ट वायर्ड है?
|
उदाहरण#
रोबोट 1 का कोड
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredरोबोट 2 के लिए कोड
// 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 इंस्टेंस बनाना आवश्यक है, जैसा कि नीचे दिखाया गया है:
रोबोट 1 का कोड
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredरोबोट 2 के लिए कोड
// 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 Brains एक दूसरे के साथ युग्मित हैं।
bool isLinked();
इस फ़ंक्शन में कोई पैरामीटर नहीं है।
Return Valuesयह फ़ंक्शन एक बूलियन मान लौटाता है।
true– दोनों V5 ब्रेन इस लिंक पर युग्मित और संचार कर रहे हैं।false– इस लिंक पर दो V5 ब्रेन युग्मित नहीं हैं।
किसी भी प्रोजेक्ट को शुरू करने से पहले, आगे कोई भी कोड चलाने से पहले, यह सुनिश्चित करने के लिए हमेशा जांच करना एक अच्छी प्रथा है कि V5 ब्रेन लिंक किए गए हैं।
Examplesरोबोट 1 का कोड
// 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); }रोबोट 2 के लिए कोड
// 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 ब्रेन को भेजने के लिए एक डबल मान। डबल मान 4 दशमलव स्थानों तक स्वरूपित होते हैं; यदि कम दशमलव स्थान दिए जाते हैं, तो 4 दशमलव स्थान दिखने तक अंत में शून्य जोड़े जाते हैं। |
यह फ़ंक्शन कोई मान वापस नहीं करता है।
Notesयदि केवल message भेजा जाता है, तो दूसरा जुड़ा हुआ V5 ब्रेन ठीक वही संदेश प्राप्त करता है।
यदि value (और index यदि प्रदान किया गया हो) शामिल हैं, तो प्राप्तकर्ता ब्रेन को एक एकल एन्कोडेड संदेश प्राप्त होता है जो सभी फ़ील्ड को इस क्रम में संयोजित करता है: .message_index_value
link.send(“string”, 1, 2.55) .string_1_2.5500 के रूप में प्राप्त किया जाता है।
रोबोट 1 का कोड
// 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); }रोबोट 2 के लिए कोड
// 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तक नए संदेश की प्रतीक्षा करता है। यदि उस समयावधि में कोई संदेश नहीं आता है, तोreceive0लौटाता है, और उसके बाद भेजे गए कोई भी संदेश अगली बार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 के रूप में प्राप्त किया जाता है।
रोबोट 1 का कोड
// 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); }रोबोट 2 के लिए कोड
// 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 |
प्राप्त संदेश का नाम। |
|
संदेश से संबंधित सूचकांक। |
|
संदेश से जुड़ा मान। |
रोबोट 1 का कोड
// 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); } }रोबोट 2 के लिए कोड
// 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); } }