सीरियल लिंक#
परिचय#
serial_link क्लास रोबोटों के बीच रॉ बाइट्स की एक स्ट्रीम भेजने की अनुमति देती है। इसे रोबोट-टू-रोबोट संचार के लिए डिज़ाइन किया गया है, जहाँ भेजने वाला और प्राप्त करने वाला दोनों रोबोट डेटा स्ट्रीम की सामग्री को समझते हैं।
send के प्रत्येक उपयोग से लिंक्ड EXP ब्रेन की कतार में डेटा जुड़ जाता है, और कतार को फर्स्ट-इन, फर्स्ट-आउट (FIFO) के आधार पर पढ़ा जाता है। यदि दूसरे EXP ब्रेन द्वारा रिसीव का उपयोग करने से पहले कई संदेश भेजे जाते हैं, तो उन्हें संग्रहीत किया जाएगा और भेजे जाने के क्रम में एक-एक करके वापस किया जाएगा। क्योंकि संदेश कतार में लग सकते हैं, प्रत्येक लूप में एक ही स्थिति को बार-बार भेजने से बैकलॉग बन सकता है; समय-संवेदनशील लॉजिक के लिए, केवल तभी भेजें जब मान बदलते हों।
महत्वपूर्ण: दोनों रोबोटों को एक ही समय में serial_link का उपयोग करने वाले प्रोजेक्ट चलाने होंगे, अन्यथा कोई डेटा भेजा/प्राप्त नहीं किया जाएगा।
क्लास कंस्ट्रक्टर#
serial_link(
int32_t index,
const char *name,
linkType type,
bool isWired = false );
क्लास डिस्ट्रक्टर#
यह serial_link ऑब्जेक्ट को नष्ट करता है और उससे जुड़े संसाधनों को मुक्त करता है।
~serial_link();
पैरामीटर#
पैरामीटर |
प्रकार |
विवरण |
|---|---|---|
|
|
स्मार्ट पोर्ट जिससे संदेश लिंक जुड़ा है, उसे |
|
|
इस लिंक का नाम। यह सलाह दी जाती है कि यह विशिष्ट स्ट्रिंग इतनी लंबी हो कि वेक्सोस द्वारा हैश किए जाने पर एक विशिष्ट आईडी बन सके। “वेक्सलिंक” जैसा सामान्य लिंक नाम उपयुक्त नहीं होगा, क्योंकि इसका उपयोग किसी अन्य टीम द्वारा भी किया जा सकता है। |
|
|
लिंक का प्रकार, या तो
|
|
|
क्या serial_link ऑब्जेक्ट वायर्ड है (डिफ़ॉल्ट
|
नोट्स#
VEXlink वायरलेस और वायर्ड दोनों प्रकार के संचार का समर्थन करता है, और वायर्ड कनेक्शन के लिए बिजली आपूर्ति संबंधी समस्याओं से बचने के लिए एक संशोधित स्मार्ट केबल का उपयोग करने की सलाह दी जाती है।
वायरलेस संचार के लिए, प्रत्येक रोबोट को स्मार्ट पोर्ट से जुड़ा एक V5 रोबोट रेडियो चाहिए होता है।
VEXlink रेडियो का उपयोग EXP कंट्रोलर के VEXnet रेडियो के साथ किया जा सकता है, जिसे टकराव से बचने के लिए उच्चतम संख्या वाले स्मार्ट पोर्ट से जोड़ा जाना चाहिए।
VEXlink बनाने के लिए, दोनों EXP Brains को V5 रोबोट रेडियो से कनेक्ट होना चाहिए।
उदाहरण#
रोबोट 1 का कोड
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredरोबोट 2 के लिए कोड
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
सदस्य कार्य#
serial_link क्लास में निम्नलिखित सदस्य फ़ंक्शन शामिल हैं:
isLinked— जाँच करता है कि क्या EXP ब्रेन इस लिंक पर युग्मित और संचार कर रहे हैं।send— युग्मित मस्तिष्क को एक संदेश भेजता है।receive— अगले आने वाले संदेश की प्रतीक्षा करता है और उसे लौटाता है।received— जब भी कोई नया संदेश प्राप्त होता है, तो कॉल करने के लिए एक फ़ंक्शन पंजीकृत करता है।
किसी भी serial_link सदस्य फ़ंक्शन को कॉल करने से पहले, एक serial_link इंस्टेंस बनाना आवश्यक है, जैसा कि नीचे दिखाया गया है:
रोबोट 1 का कोड
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredरोबोट 2 के लिए कोड
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
isLinked#
यह बताता है कि सीरियल लिंक पर मौजूद EXP ब्रेन एक दूसरे के साथ युग्मित हैं या नहीं।
Available Functionsbool isLinked();
इस फ़ंक्शन में कोई पैरामीटर नहीं है।
Return Valuesयह एक बूलियन मान लौटाता है जो यह दर्शाता है कि EXP ब्रेन आपस में जुड़े हुए हैं या नहीं:
true— The two EXP Brains are paired and communicating on this link.false— The two EXP Brains are not paired on this link.
किसी भी प्रोजेक्ट को शुरू करने से पहले, आगे कोई भी कोड चलाने से पहले, यह सुनिश्चित करने के लिए हमेशा जांच करना एक अच्छी प्रथा है कि EXP Brains लिंक किए गए हैं।
रोबोट 1 का कोड
// Create the link serial_link 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"); Brain.Screen.newLine(); Brain.Screen.print("Sending message..."); // Message to send const char* message = "Hello Robot 2"; // Send the message link.send((uint8_t*)message, strlen(message));
रोबोट 2 के लिए कोड
// Create the link serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); Brain.Screen.print("Waiting..."); // Buffer to store incoming bytes uint8_t buffer[32] = {0}; int32_t n = link.receive(buffer, sizeof(buffer) - 1); // Null-terminate if (n > 0) { buffer[n] = 0; // Display the message that was sent Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); Brain.Screen.print((char*)buffer); } else { Brain.Screen.newLine(); Brain.Screen.print("No message received"); }
send#
यह संबंधित मस्तिष्क को एक संदेश भेजता है।
Available Functions1 — बाइट बफर का उपयोग करके संदेश भेजता है।
int32_t send( uint8_t *buffer, int32_t length );
Parameters2 — कैरेक्टर बफर का उपयोग करके संदेश भेजता है।
int32_t send( const char *buffer, int32_t length );
पैरामीटर |
प्रकार |
विवरण |
|---|---|---|
|
|
दूसरे लिंक किए गए EXP ब्रेन को भेजने के लिए बफर। |
|
|
भेजने के लिए बफर की लंबाई। |
भेजे गए बाइट्स की संख्या को दर्शाते हुए int32_t लौटाता है।
रोबोट 1 का कोड
// Create the link serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 1 - Manager"); // Track last sent state uint8_t lastState = 255; // impossible initial value // Tell Robot 2 when the screen is being pressed while (true) { uint8_t state = Brain.Screen.pressing() ? 1 : 0; // Only send if state changed if (state != lastState) { link.send(&state, 1); lastState = state; } wait(0.02, seconds); }
रोबोट 2 के लिए कोड
// Create the link serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); Brain.Screen.print("Waiting..."); // Track the last state uint8_t lastShown = 255; while (true) { // Variable stores 1 - pressed, 0 - not pressed uint8_t state = 0; int32_t n = link.receive(&state, 1, 200); // Only update screen if state changes if (n == 1 && state != lastShown) { Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); // Display text if Robot 1's screen is being pressed if (state == 1) { Brain.Screen.print("Manager IS being pressed!"); } else { Brain.Screen.print("Manager is NOT being pressed"); } lastShown = state; } wait(0.02, seconds); }
receive#
अगले आने वाले संदेश की प्रतीक्षा करता है और उसे वापस भेजता है।
Available FunctionsParameters1 — वैकल्पिक टाइमआउट के साथ एक कैरेक्टर बफर में एक संदेश प्राप्त करता है।
int32_t receive( char *buffer, int32_t length, int32_t timeoutMs = 500);
पैरामीटर |
प्रकार |
विवरण |
|---|---|---|
|
|
प्राप्त संदेश को संग्रहित करने के लिए बफर। |
|
|
बफर की लंबाई। |
|
|
मिलीसेकंड में कितना समय लगेगा? |
प्राप्त बाइट्स की संख्या को दर्शाते हुए int32_t लौटाता है।
receiveअन्य लिंक किए गए EXP ब्रेन से अगला कतारबद्ध संदेश लौटाता है।संदेशों को FIFO क्रम में पढ़ा जाता है (सबसे पुराना अपठित संदेश पहले)।
यदि
receiveकॉल करते समय कतार खाली है, तो यह एक नए संदेश के लिए निर्दिष्टtimeoutMsतक प्रतीक्षा करता है।यदि उस समयावधि में कोई संदेश नहीं आता है,
receive0लौटाता है, और उसके बाद भेजा गया कोई भी संदेश अगली बारreceiveका उपयोग किए जाने पर पढ़ने के लिए कतार में रहता है।
रोबोट 1 का कोड
// Create the link serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 1 - Manager"); // Track last sent state uint8_t lastState = 255; // impossible initial value // Tell Robot 2 when the screen is being pressed while (true) { uint8_t state = Brain.Screen.pressing() ? 1 : 0; // Only send if state changed if (state != lastState) { link.send(&state, 1); lastState = state; } wait(0.02, seconds); }
रोबोट 2 के लिए कोड
// Create the link serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); Brain.Screen.print("Waiting..."); // Track the last state uint8_t lastShown = 255; while (true) { // Variable stores 1 - pressed, 0 - not pressed uint8_t state = 0; int32_t n = link.receive(&state, 1, 200); // Only update screen if state changes if (n == 1 && state != lastShown) { Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); // Display text if Robot 1's screen is being pressed if (state == 1) { Brain.Screen.print("Manager IS being pressed!"); } else { Brain.Screen.print("Manager is NOT being pressed"); } lastShown = state; } wait(0.02, seconds); }
received#
EXP ब्रेन द्वारा भेजे गए संदेश को प्राप्त होने पर कॉल किए जाने वाले फ़ंक्शन को पंजीकृत करता है।
Available Functions1 — प्रत्येक प्राप्त संदेश के लिए एक कॉलबैक फ़ंक्शन पंजीकृत करता है।
void received( void (* callback)(const char *, const char *, double) );
2 — प्रत्येक प्राप्त संदेश के लिए एक कॉलबैक को पंजीकृत करता है जिसमें एक अतिरिक्त int32_t फ़ील्ड शामिल होता है।
void received( void (* callback)(const char *, const char *, int32_t, double) );
3 — केवल प्राप्त संदेशों के लिए कॉलबैक पंजीकृत करता है जिनका नाम संदेश से मेल खाता है।
void received( const char *message, void (* callback)(const char *, const char *, double) );
Parameters4 — केवल प्राप्त संदेशों के लिए कॉलबैक पंजीकृत करता है जिनका नाम संदेश से मेल खाता है, जहां संदेश पेलोड में एक अतिरिक्त int32_t फ़ील्ड शामिल है।
void received( const char *message, void (* callback)(const char *, const char *, int32_t, double) );
पैरामीटर |
प्रकार |
विवरण |
|---|---|---|
|
|
मिलान करने के लिए संदेश का नाम (स्ट्रिंग)। जब प्राप्त संदेश का नाम इस मान से मेल खाता है, तो |
|
|
एक पूर्व-परिभाषित फ़ंक्शन जो तब चलता है जब EXP ब्रेन को |
इस कार्य पर कीमत के लिहाज से कुछ भी रिटर्न नहीं मिलता है।
Notesreceivedको चार तर्कों के साथ कॉल किया जाता है:
callback(message, linkname, index, value)
तर्क |
विवरण |
|---|---|
|
प्राप्त संदेश का नाम (उदाहरण के लिए, |
|
जिस लिंक से संदेश प्राप्त हुआ था, उसका नाम। |
|
संदेश के साथ भेजा गया पूर्णांक मान (यदि प्रदान किया गया हो)। |
|
संदेश के साथ भेजा गया फ्लोट मान (यदि प्रदान किया गया हो)। |
रोबोट 1 का कोड
// Send left or right when pressed void screen_pressed() { if (Brain.Screen.xPosition() < 240) { link.send((uint8_t*)"left", 4); } else { link.send((uint8_t*)"right", 5); } } int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Create the link serial_link 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"); Brain.Screen.pressed(screen_pressed); }
रोबोट 2 के लिए कोड
// Called when message received void received_callback(uint8_t *buffer, int32_t length) { char msg[32]; // Copy safely and null terminate int n = (length < 31) ? length : 31; memcpy(msg, buffer, n); msg[n] = '\0'; Brain.Screen.clearScreen(); Brain.Screen.setCursor(1,1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); // Display if Robot 1's screen is pressed on left or right if (strcmp(msg, "left") == 0) { Brain.Screen.print("Manager pressed LEFT"); } else if (strcmp(msg, "right") == 0) { Brain.Screen.print("Manager pressed RIGHT"); } } int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Create the link serial_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(received_callback); Brain.Screen.clearScreen(); Brain.Screen.setCursor(1,1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); Brain.Screen.print("Waiting..."); }