2013-04-13 21 views
6

Desidero pubblicare un vettore di lunghezza sconosciuta di strutture che contengono due numeri interi e due stringhe. C'è un editore e un abbonato in ROS che può fare questo?Come pubblicheresti un messaggio in ROS di un vettore di strutture?

In caso contrario, ho cercato al tutorial of how to create custom messages e Immagino che posso fare una .msg file contenente:

int32 upperLeft 
int32 lowerRight 
string color 
string cameraID 

e un altro .msg file che contiene una serie di messaggi precedenti. Ma il tutorial non fornisce un esempio di come utilizzare gli array, quindi non so cosa inserire nel secondo file .msg. Inoltre, non sono sicuro di come utilizzare questo messaggio personalizzato in un programma C++.

Qualsiasi suggerimento su come fare sarebbe fantastico!

risposta

3

Diciamo che il tuo primo messaggio si chiama MyStruct. Per avere un msg che è un array di MyStructs, si avrebbe un .msg con il campo:

MyStruct[] array 

Poi nel codice si effettua una MyStruct e impostare tutti i valori:

MyStruct temp; 
temp.upperLeft = 3 
temp.lowerRight = 4 
temp.color = some_color 
temp.cameraID = some_id 

Poi per aggiungere MyStructs ad un array l'array nel secondo tipo msg, è possibile utilizzare push_back (proprio come con std :: vector):

MySecondMsg m; 
m.push_back(temp); 
my_publisher.publish(m); 
+0

Ha detto che push_back non è un membro di m ?? perché ? – TravellingSalesWoman

+2

Sì, dovrebbe essere 'm.array.push_back (temp)' – Avio

7

giusto per espandere un po 'quello che @Sterling già spiegato ...

Se si dispone di un progetto (e quindi directory) denominata "test_messages", e si dispone di questi due tipi di messaggio in test_messages/msg:

#> cat test.msg 
string first_name 
string last_name 
uint8 age 
uint32 score 

#> cat test_vector.msg 
string vector_name 
uint32 vector_len   # not really necessary, just for testing 
test[] vector_test 

È quindi possibile scrivere questo codice C++:

#include "test_messages/test.h" 
#include "test_messages/test_vector.h" 

... 

    ::test_messages::test test_msg; 

    test_msg.age   = 29; 
    test_msg.first_name = "Firstname"; 
    test_msg.last_name = "Lastname"; 
    test_msg.score  = 79; 

    test_pub.publish(test_msg); 


    ::test_messages::test_vector test_vec; 

    test_vec.vector_len = 5; 
    test_vec.vector_name = std::string("test vector name"); 

    for (int idx = 0; idx < test_vec.vector_len; idx++) 
    { 
     test_msg.age   = 50; 
     test_msg.score  = 100; 
     test_msg.first_name = std::string("aaaa"); 
     test_msg.last_name = std::string("bbbb"); 

     test_vec.vector_test.push_back(test_msg); 
    } 

    test_vector_pub.publish(test_vec);