Mesaj Bağlantısı#
giriiş#
message_link sınıfı, iki V5 Beyninin isteğe bağlı küçük verilerle birlikte kısa dize mesajları göndererek iletişim kurmasını sağlar. Robotlar arası koordinasyon için tasarlanmıştır; örneğin rutinleri başlatmak, sensör sonuçlarını paylaşmak veya başka bir robottaki davranışları tetiklemek gibi.
send’nin her kullanımı, bağlantılı V5 Beyninin kuyruğuna bir mesaj ekler ve kuyruk ilk giren ilk çıkar (FIFO) prensibiyle okunur. Diğer V5 Beyni receive’i kullanmadan önce birden fazla mesaj gönderilirse, bunlar depolanır ve gönderildikleri sırayla tek tek geri döndürülür. Mesajlar kuyruğa alınabildiğinden, her döngüde aynı durumu tekrar tekrar göndermek birikmeye neden olabilir; zaman açısından kritik mantık için, yalnızca değerler değiştiğinde gönderin.
Önemli: Her iki robot da aynı anda message_link kullanan projeleri çalıştırıyor olmalıdır, aksi takdirde hiçbir mesaj gönderilmez/alınmaz.
Sınıf Oluşturucuları#
message_link(
int32_t index,
const char *name,
linkType type,
bool isWired = false );
Sınıf Yıkıcısı#
message_link nesnesini yok eder ve ilişkili kaynakları serbest bırakır.
virtual ~message_link();
Parametreler#
Parametre |
Tip |
Tanım |
|---|---|---|
|
|
V5 Radyonun veya Akıllı Kablonun bağlı olduğu Akıllı Bağlantı Noktası, |
|
|
Mesaj bağlantısının adı. |
|
|
Bağlantı türü, ya
|
|
|
message_link nesnesinin kablolu olup olmadığı.
|
Örnek#
Robot 1 için Kod
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredRobot 2 için Kod
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
Üye Fonksiyonları#
message_link sınıfı aşağıdaki üye fonksiyonlarını içerir:
isLinked— Bu Beynin eşleştirilmiş Beyne aktif olarak bağlı olup olmadığını döndürür.send— Eşleştirilmiş Beyne bir mesaj gönderir.receive— Bir sonraki gelen mesajı bekler ve geri döndürür.received— Yeni bir mesaj alındığında çağrılacak bir fonksiyonu kaydeder.
Herhangi bir message_link üye fonksiyonunu çağırmadan önce, aşağıda gösterildiği gibi bir message_link örneği oluşturulmalıdır:
Robot 1 için Kod
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredRobot 2 için Kod
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
isLinked#
isLinked yöntemi, bir MessageLink üzerindeki V5 Brain’lerin birbiriyle eşleştirilip eşleştirilmediğini döndürür.
bool isLinked();
Bu fonksiyonun herhangi bir parametresi yoktur.
Return ValuesBu fonksiyon bir boolean değer döndürür.
true– İki V5 Beyni eşleştirilmiş ve bu bağlantı üzerinden iletişim kuruyor.false– Bu bağlantıda iki V5 Beyni eşleştirilmemiş.
Herhangi bir kod çalıştırmadan önce, projenin başlangıcında V5 Brains’in bağlantılı olduğundan emin olmak her zaman iyi bir uygulamadır.
ExamplesRobot 1 için Kod
// 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); }Robot 2 için Kod
// 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#
Bağlantı üzerinden bir mesaj gönderir.
Available FunctionsParameters1 — Bir mesaj dizisi gönderir.
int32_t send( const char *message );2 —
doubleiçeren bir mesaj dizesi gönderir.int32_t send( const char *message, double value );3 —
int32_tvedoubleiçeren bir mesaj dizesi gönderir.int32_t send( const char *message, int32_t index, double value );
Parametre |
Tip |
Tanım |
|---|---|---|
|
|
Bağlantılı diğer V5 Beynine gönderilecek mesaj. |
|
|
Diğer bağlantılı V5 Beynine gönderilecek bir tamsayı. |
|
|
Diğer bağlantılı V5 Beynine gönderilecek çift duyarlıklı sayı. Çift duyarlıklı sayılar 4 ondalık basamağa kadar biçimlendirilir; daha az ondalık basamak verilirse, 4 görünene kadar sonuna sıfırlar eklenir. |
Bu fonksiyon herhangi bir değer döndürmez.
NotesYalnızca message gönderilirse, bağlantılı diğer V5 Beyni tam olarak bu mesajı alır.
Eğer value (ve sağlanmışsa index ) dahil edilirse, alıcı Beyin tüm alanları şu sırayla birleştiren tek bir kodlanmış mesaj alır: .message_index_value
link.send(“string”, 1, 2.55) .string_1_2.5500 olarak alınır.
Robot 1 için Kod
// 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); }Robot 2 için Kod
// 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#
Bağlantıdan bir mesaj alır.
Available Functionsint32_t receive(
char *buffer,
int32_t length,
int32_t timeoutMs = 500 );
Parametre |
Tip |
Tanım |
|---|---|---|
|
|
Alınan mesajı saklamak için kullanılan tampon bellek. |
|
|
Tamponun uzunluğu. |
|
|
|
Bu fonksiyon alınan bayt sayısını döndürür.
Notesreceivediğer bağlantılı V5 Beyninden kuyruğa alınmış bir sonraki mesajı döndürür. Mesajlar FIFO sırasına göre (en eski okunmamış olan önce) okunur.receiveçağrıldığında kuyruk boşsa, belirtilentimeoutkadar yeni bir mesaj bekler. Bu süre içinde hiçbir mesaj gelmezse,receive0döndürür ve daha sonra gönderilen herhangi bir mesaj, bir sonrakireceivekullanıldığında okunmak üzere kuyrukta kalır.Eğer diğer bağlantılı V5 Beyin yalnızca
stringvereceive.Diğer bağlantılı V5 Brain bir tamsayı ve/veya ondalık sayı gönderirse,
receivealanları şu sırayla birleştiren tek bir kodlanmış dize döndürür:.string_integer_floatÖrneğin, diğer bağlantılı V5 Brain
link.send(“string”, 1, 2.55)kullanıyorsa,link.receive()kullanmak.string_1_2.5500gibi kodlanmış bir dize döndürür. Bu kodlanmış dizeyi (aşağıdaki örneklere bakın) bölerek mesaj adını, tamsayı ve ondalık sayı değerlerini çıkarabilirsiniz.Eğer manuel bölme/ayrıştırmadan kaçınmayı tercih ederseniz, belirli bir mesaj adı için bir işleyici kaydetmek üzere
receivedkullanın; işleyici gönderilen değerleri argüman olarak alır.
link.send(“string”, 1, 2.55) .string_1_2.5500 olarak alınır.
Robot 1 için Kod
// 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); }Robot 2 için Kod
// 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 Brain’in gönderilen bir mesajı aldığı her seferinde çağrılacak bir fonksiyonu kaydeder.
Available FunctionsParameters1 Alınan tüm mesajlar (mesaj + değer) için bir geri çağırma işlevi kaydeder.
void received( void (* callback)(const char *, const char *, double) );2 Alınan tüm mesajlar için (mesaj + indeks + değer) bir geri çağırma işlevi kaydeder.
void received( void (* callback)(const char *, const char *, int32_t, double) );3 Belirli bir mesaj (mesaj + değer) için bir geri çağırma kaydeder.
void received( const char *message, void (* callback)(const char *, const char *, double) );4 Belirli bir mesaj için (mesaj + indeks + değer) bir geri çağırma kaydeder.
void received( const char *message, void (* callback)(const char *, const char *, int32_t, double) );
Parametre |
Tip |
Tanım |
|---|---|---|
|
|
Dinlenecek mesajın adı. Atlanırsa, geri çağırma işlevi alınan tüm mesajlar için çalışır. |
|
|
Mesaj alındığında çalışan bir fonksiyon. |
Bu fonksiyon herhangi bir değer döndürmez.
Callback Signature1 Gönderenin adını, mesaj adını ve değeri alır.
void callback(const char *, const char *, double)2 Gönderenin adını, mesaj adını, dizini ve değeri alır.
void callback(const char *, const char *, int32_t, double)
Tip |
Tanım |
|---|---|
1st |
Gönderen bağlantısının adı. |
2nd |
Alınan mesajın adı. |
|
Mesajla ilişkili indeks. |
|
Mesajla ilişkilendirilen değer. |
Robot 1 için Kod
// 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); } }Robot 2 için Kod
// 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); } }