Nasc Teachtaireachta#
Réamhrá#
Ligeann an rang message_link do dhá Inchinn EXP cumarsáid a dhéanamh trí theachtaireachtaí gearra teaghrán a sheoladh le sonraí beaga roghnacha. Tá sé deartha le haghaidh comhordú róbat go róbat — amhail gnáthaimh a thosú, torthaí braiteoirí a roinnt, nó iompraíochtaí a spreagadh ar róbat eile.
Cuireann gach úsáid a bhaintear as send teachtaireacht le scuaine an EXP Brain nasctha, agus léitear an scuaine isteach ar dtús, amach ar dtús (FIFO). Má sheoltar il-theachtaireachtaí sula n-úsáideann an EXP Brain eile receive, stórálfar agus cuirfear ar ais iad ceann ar cheann san ord inar seoladh iad. Ós rud é gur féidir le teachtaireachtaí scuaine a dhéanamh, d’fhéadfadh sé go gcruthófaí riaráiste má sheoltar an stádas céanna arís agus arís eile i ngach lúb; i gcás loighic ríthábhachtach ó thaobh ama de, seoltar ach amháin nuair a athraíonn luachanna.
Tábhachtach: Caithfidh an dá róbat tionscadail a úsáideann message_link a bheith ag rith ag an am céanna, nó ní sheolfar/nasc_teachtaireachta ar bith.
Tógálaithe Ranga#
message_link(
int32_t index,
const char *name,
linkType type,
bool isWired = false );
Scriostóir Ranga#
Scriosann sé an réad message_link agus scaoileann sé acmhainní gaolmhara.
virtual ~message_link();
Paraiméadair#
Paraiméadar |
Cineál |
Cur síos |
|---|---|---|
|
|
An Port Cliste a bhfuil an Raidió V5, nó an Cábla Cliste, ceangailte leis, scríofa mar |
|
|
Ainm an Nasc Teachtaireachta. |
|
|
An cineál nasc, bíodh sé
|
|
|
Cibé an bhfuil an réad message_link sreangaithe.
|
Sampla#
Cód do Róbat 1
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredCód do Róbat 2
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
Feidhmeanna na mBall#
Áirítear leis an rang message_link na feidhmeanna ball seo a leanas:
isLinked— Tugann sé seo ar ais an bhfuil an Inchinn seo ceangailte go gníomhach lena Inchinn phéireáilte.send— Seolann sé teachtaireacht chuig an Inchinn phéireáilte.receive— Fanann sé leis an gcéad teachtaireacht isteach eile agus seolann sé ar ais í.received— Cláraíonn sé feidhm le glaoch uirthi aon uair a fhaightear teachtaireacht nua.
Sula nglaotar ar aon fheidhmeanna ball message_link , ní mór sampla message_link a chruthú, mar a thaispeántar thíos:
Cód do Róbat 1
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredCód do Róbat 2
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
isLinked#
Tugann an modh isLinked ar ais cibé an bhfuil na Brains EXP ar MessageLink péireáilte le chéile.
bool isLinked();
Níl aon pharaiméadair ag an bhfeidhm seo.
Return ValuesTugann an fheidhm seo luach booléanach ar ais.
true– Tá an dá Inchinn EXP péireáilte agus ag cumarsáid ar an nasc seo.false– Níl an dá Inchinn EXP péireáilte ar an nasc seo.
Is dea-chleachtas é a sheiceáil i gcónaí go bhfuil na EXP Brains nasctha ag tús tionscadail sula ritheann tú aon chód eile.
ExamplesCód do Róbat 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); }Cód do Róbat 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#
Seolann sé teachtaireacht thar an nasc nasc.
Available FunctionsParameters1 — Seolann sé teaghrán teachtaireachta.
int32_t send( const char *message );2 — Seolann teaghrán teachtaireachta le
double.int32_t send( const char *message, double value );3 — Seolann teaghrán teachtaireachta le
int32_tagusdouble.int32_t send( const char *message, int32_t index, double value );
Paraiméadar |
Cineál |
Cur síos |
|---|---|---|
|
|
An teachtaireacht le seoladh chuig an Inchinn EXP nasctha eile. |
|
|
Slánuimhir le seoladh chuig an Inchinn EXP nasctha eile. |
|
|
Dúbailt le seoladh chuig an EXP Brain nasctha eile. Déantar dúbailtí a fhormáidiú go 4 dheachúlacha; má sholáthraítear níos lú deachúlacha, cuirtear nialais ina dhiaidh sin leis go dtí go dtaispeántar 4. |
Ní thugann an fheidhm seo aon luachanna ar ais.
NotesMura seoltar ach message , faigheann an EXP Brain nasctha eile an teachtaireacht chruinn sin.
Má chuirtear luach value (agus innéacs index más soláthraítear) san áireamh, faigheann an Inchinn a fhaigheann teachtaireacht ionchódaithe aonair a chomhcheanglaíonn na réimsí go léir san ord seo: .message_index_value
Ag baint úsáide as link.send(“string”, 1, 2.55) mar .string_1_2.5500.
Cód do Róbat 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); }Cód do Róbat 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#
Faigheann sé teachtaireacht ón nasc nasc.
Available Functionsint32_t receive(
char *buffer,
int32_t length,
int32_t timeoutMs = 500 );
Paraiméadar |
Cineál |
Cur síos |
|---|---|---|
|
|
An maolán chun an teachtaireacht a fuarthas a stóráil. |
|
|
Fad an mhaoláin. |
|
|
Cé chomh fada i milleasoicindí a fhanfaidh |
Tugann an fheidhm seo líon na mbeart a fuarthas ar ais.
Notesreceivereturns the next queued message from the other linked EXP Brain. Messages are read in FIFO order (oldest unread first). If the queue is empty whenreceiveis called, it waits up to the specifiedtimeoutfor a new message. If no message arrives in that window,receivereturns0, and any message sent afterward remains in the queue to be read the next timereceiveis used.Más rud é nár sheol an EXP Brain nasctha eile ach teaghrán
string,receive, tugann sé an teaghrán beacht sin ar ais.Má sheol an EXP Brain nasctha eile slánuimhir agus/nó snámhphointe, tugann
receiveteaghrán ionchódaithe aonair ar ais a chomhcheanglaíonn na réimsí go léir san ord seo:.string_integer_floatMar shampla, má úsáideann an EXP Brain nasctha eile
link.send(“string”, 1, 2.55), ansin má úsáideann túlink.receive(), cuirtear teaghrán ionchódaithe ar ais amhail.string_1_2.5500. Is féidir leat an teaghrán ionchódaithe seo a scoilt (féach na samplaí thíos) chun ainm an teachtaireachta, an tslánuimhir, agus na luachanna snámhphointe a bhaint amach.Más fearr leat scoilt/parsáil láimhe a sheachaint, bain úsáid as
receivedchun láimhseálaí a chlárú le haghaidh ainm teachtaireachta ar leith; faigheann an láimhseálaí na luachanna seolta mar argóintí.
Ag baint úsáide as link.send(“string”, 1, 2.55) mar .string_1_2.5500.
Cód do Róbat 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); }Cód do Róbat 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#
Cláraíonn sé feidhm le glaoch uirthi aon uair a fhaigheann an EXP Brain teachtaireacht sheolta.
Available FunctionsParameters1 Cláraíonn sé seo glao ar ais do gach teachtaireacht a fhaightear (teachtaireacht + luach).
void received( void (* callback)(const char *, const char *, double) );2 Cláraíonn sé seo glao ar ais do gach teachtaireacht a fhaightear (teachtaireacht + innéacs + luach).
void received( void (* callback)(const char *, const char *, int32_t, double) );3 Cláraíonn sé seo glao ar ais le haghaidh teachtaireacht shonrach (teachtaireacht + luach).
void received( const char *message, void (* callback)(const char *, const char *, double) );4 Cláraíonn sé seo glao ar ais le haghaidh teachtaireacht shonrach (teachtaireacht + innéacs + luach).
void received( const char *message, void (* callback)(const char *, const char *, int32_t, double) );
Paraiméadar |
Cineál |
Cur síos |
|---|---|---|
|
|
Ainm an teachtaireachta le héisteacht leis. Mura ndéantar é, ritheann an glao ar ais do gach teachtaireacht a fhaightear. |
|
pointeoir feidhme |
Feidhm a fhorghníomhaítear nuair a fhaightear teachtaireacht. |
Ní thugann an fheidhm seo aon luachanna ar ais.
Callback Signature1 Faigheann sé ainm an tseoltóra, ainm an teachtaireachta, agus luach.
void callback(const char *, const char *, double)2 Faigheann sé ainm an tseoltóra, ainm an teachtaireachta, innéacs agus luach.
void callback(const char *, const char *, int32_t, double)
Cineál |
Cur síos |
|---|---|
1st |
Ainm nasc an tseoltóra. |
2nd |
Ainm an teachtaireachta a fuarthas. |
|
An t-innéacs a bhaineann leis an teachtaireacht. |
|
An luach a bhaineann leis an teachtaireacht. |
Cód do Róbat 1
// 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(); // Create the link message_link link(PORT1, "LeftRightLink", 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); while (true) { wait(10, msec); } }Cód do Róbat 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); } }