2013-04-13 8 views
6

मैं अज्ञात लंबाई के वेक्टर प्रकाशित करना चाहता हूं जिसमें दो पूर्णांक और दो तार होते हैं। क्या आरओएस में कोई प्रकाशक और ग्राहक है जो यह कर सकता है?आप structs के वेक्टर के आरओएस में एक संदेश कैसे प्रकाशित करेंगे?

यदि नहीं, तो मैं tutorial of how to create custom messages को देख रहा था और मैं समझ रहा युक्त एक .msg फ़ाइल बना सकते हैं:

int32 upperLeft 
int32 lowerRight 
string color 
string cameraID 

और पिछले संदेशों की एक सरणी से युक्त एक और .msg फ़ाइल। लेकिन ट्यूटोरियल एरे का उपयोग करने का उदाहरण नहीं देता है, इसलिए मुझे नहीं पता कि दूसरी .msg फ़ाइल में क्या रखा जाए। इसके अलावा, मुझे यकीन नहीं है कि सी ++ प्रोग्राम में इस कस्टम संदेश का उपयोग कैसे करें।

यह कैसे करना है इस पर कोई सुझाव बहुत अच्छा होगा!

उत्तर

3

मान लें कि आपका पहला संदेश माईस्ट्रक्चर कहलाता है। एक संदेश है कि MyStructs की एक सरणी है के लिए, आप क्षेत्र के साथ एक .msg होगा:

MyStruct[] array 

तब कोड में आप एक MyStruct बनाने के लिए और सभी मूल्यों को निर्धारित: फिर

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

एक सरणी दूसरा .msg प्रकार में अपने सरणी, आप push_back उपयोग कर सकते हैं (बस एसटीडी के साथ की तरह :: वेक्टर) को MyStructs जोड़ने के लिए:

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

यह कहा गया कि push_back एम का सदस्य नहीं है ?? क्यूं कर ? – TravellingSalesWoman

+2

हां, यह 'm.array.push_back (temp) होना चाहिए – Avio

7

बस एक छोटा सा विस्तार करने के लिए क्या @Sterling पहले से ही समझाया ...

आप एक परियोजना (और इस प्रकार निर्देशिका) "test_messages" कहा जाता है, और आप 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 

तो आप इस सी लिख सकते हैं ++ कोड:

#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); 
संबंधित मुद्दे