Seriālā saite#
Ievads#
Klase serial_link ļauj starp robotiem nosūtīt neapstrādātu baitu plūsmu. Tā ir paredzēta robotu savstarpējai saziņai, kur gan raidošais, gan saņemošais robots saprot datu plūsmas saturu.
Katrs send lietojums pievieno datus saistītās V5 Brain rindai, un rinda tiek lasīta pēc principa “pirmais iekšā, pirmais ārā” (FIFO). Ja pirms citu V5 Brain funkciju saņemšanas tiek nosūtīti vairāki ziņojumi, tie tiks saglabāti un atgriezti pa vienam tādā secībā, kādā tie tika nosūtīti. Tā kā ziņojumi var tikt ievietoti rindā, atkārtota viena un tā paša statusa nosūtīšana katrā ciklā var radīt uzkrāšanos; laika ziņā kritiskas loģikas gadījumā sūtiet tikai tad, kad mainās vērtības.
Svarīgi: Abiem robotiem vienlaikus jādarbojas projektos, kas izmanto serial_link , pretējā gadījumā dati netiks nosūtīti/saņemti.
Klases konstruktori#
serial_link(
int32_t index,
const char *name,
linkType type,
bool isWired = false );
Klases iznīcinātājs#
Iznīcina objektu serial_link un atbrīvo ar to saistītos resursus.
~serial_link();
Parametri#
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
Viedports, ar kuru ir savienota ziņojuma saite, kas rakstīts kā |
|
|
Šīs saites nosaukums. Ieteicams, lai šī unikālā virkne būtu pietiekami gara, lai, to jaukjot ar vexos, tiktu izveidots unikāls ID. Nepareizs saites nosaukums būtu kaut kas vispārīgs, piemēram, “vexlink”, jo to varētu izmantot cita komanda. |
|
|
Saites veids —
|
|
|
Vai serial_link objekts ir savienots ar vadu (pēc noklusējuma
|
Piezīmes#
VEXlink atbalsta gan bezvadu, gan vadu saziņu, un vadu savienojumiem ieteicams modificēts viedkabelis, lai novērstu strāvas padeves problēmas.
Bezvadu saziņai katram robotam ir nepieciešams V5 robota radio, kas savienots ar viedportu.
VEXlink radio var izmantot līdzās V5 kontrollera VEXnet radio, kas jāpievieno viedajai pieslēgvietai ar augstāko numuru, lai izvairītos no konfliktiem.
Lai izveidotu VEXlink, abām V5 smadzenēm jābūt savienotām ar V5 robota radio.
Piemērs#
1. robota kods
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredRobots 2 kods
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
Dalībnieku funkcijas#
Klasē serial_link ir iekļautas šādas dalībfunkcijas:
isLinked— Pārbauda, vai V5 smadzenes ir savienotas pārī un sazinās šajā saitē.send— Nosūta ziņojumu pārī savienotajām smadzenēm.receive— Gaida un atgriež nākamo ienākošo ziņojumu.received— Reģistrē funkciju, kas tiks izsaukta ikreiz, kad tiek saņemts jauns ziņojums.
Pirms jebkuru serial_link dalībfunkciju izsaukšanas ir jāizveido serial_link instance, kā parādīts zemāk:
1. robota kods
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredRobots 2 kods
// Create a wireless link in Port 1 serial_link link = serial_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
isLinked#
Atgriež, vai V5 smadzenes seriālajā saitē ir savienotas pārī viena ar otru.
Available Functionsbool isLinked();
Šai funkcijai nav parametru.
Return ValuesAtgriež Būla vērtību, kas norāda, vai V5 smadzenes ir saistītas:
true— The two V5 Brains are paired and communicating on this link.false— The two V5 Brains are not paired on this link.
Pirms jebkāda turpmāka koda palaišanas projekta sākumā vienmēr ir ieteicams pārbaudīt, vai V5 smadzenes ir saistītas.
1. robota kods
// 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));
Robots 2 kods
// 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#
Nosūta ziņojumu pārī savienotajām smadzenēm.
Available Functions1 — Nosūta ziņojumu, izmantojot baitu buferi.
int32_t send( uint8_t *buffer, int32_t length );
Parameters2 — Nosūta ziņojumu, izmantojot rakstzīmju buferi.
int32_t send( const char *buffer, int32_t length );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
Buferis, kas jānosūta uz otru saistīto V5 Brain. |
|
|
Nosūtāmā bufera garums. |
Atgriež int32_t , kas apzīmē nosūtīto baitu skaitu.
1. robota kods
// 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); }
Robots 2 kods
// 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#
Gaida un atgriež nākamo ienākošo ziņojumu.
Available FunctionsParameters1 — Saņem ziņojumu rakstzīmju buferī ar papildu taimautu.
int32_t receive( char *buffer, int32_t length, int32_t timeoutMs = 500);
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
Buferis saņemtā ziņojuma glabāšanai. |
|
|
Bufera garums. |
|
|
Cik ilgi milisekundēs |
Atgriež int32_t , kas apzīmē saņemto baitu skaitu.
receiveatgriež nākamo rindā esošo ziņojumu no otras saistītās V5 smadzenes.Ziņojumi tiek lasīti FIFO secībā (vispirms vecākie nelasītie).
Ja rinda ir tukša, kad tiek izsaukta komanda
receive, tā gaida jaunu ziņojumu līdz norādītajamtimeoutMslaikam.Ja šajā logā netiek saņemts neviens ziņojums,
receiveatgriež0, un visi pēc tam nosūtītie ziņojumi paliek rindā, lai tos lasītu nākamajā reizē, kad tiks izmantotareceive.
1. robota kods
// 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); }
Robots 2 kods
// 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#
Reģistrē funkciju, kas tiks izsaukta ikreiz, kad V5 Brain saņem datus.
Available FunctionsParameters1 — Reģistrē atzvanīšanas funkciju katram saņemtajam baitu buferim.
void received( void (* callback)(uint8_t *, int32_t) );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
Iepriekš definēta funkcija, kas darbojas, kad V5 Brain saņem datus. |
Šī funkcija neatgriež vērtību.
Notesreceivedatzvanīšanas signāli tiek izsaukti ar diviem argumentiem: baitu buferi un saņemto baitu skaitu.Saņemtie dati, iespējams, nav pabeigti ar nulli. Ja atzvanīšanas funkcija apstrādā datus kā virkni, kopējiet saņemtos baitus rakstzīmju masīvā un pievienojiet nulles pārtrauktāju.
callback(buffer, length)
Arguments |
Apraksts |
|---|---|
|
Rādītājs uz saņemto baitu buferi. |
|
Saņemto baitu skaits. |
1. robota kods
// Create the link. serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // 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(); // 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); } }
Robots 2 kods
// 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..."); while (true) { wait(10, msec); } }