As far as I can remember, in roscpp
arrays in messages' attributes are actually vector
s (I'd appreciate it if someone can help me with the docs on this or a correction), so you'd treat any message attribute that's an array as a vector
.
1). How to assign data from the publisher to the my_struct[].data
member in my_msg
?
Just like you would with a vector (e.g., use the vector's push_back
method).
2). How to subscribe this data from a different ROS node ?
Just like you would with any other subscriber., create a callback function and and create a subscriber.
Callback:
void myCallback(const std_msgs::my_struct::ConstPtr& msg)
{
// process data here
}
Subscriber:
ros::Subscriber sub = n.subscribe("my_struct_topic", 1000, myCallback);
3). What should be the changes in CMakeLists.txt
(apart from adding my_msg.msg
& my_struct.msg
) ?
Without seeing your CMakeLists.txt
it's impossible to say exactly but you'd need to use the genmsg
and std_msgs
, add your message files, and then generate the messages that you added
# include any other packages you're using
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs genmsg)
# Declare ROS messages (assuming the message files are in a directory called msg
add_message_files(DIRECTORY msg FILES my_struct.msg my_struct.msg)
# Generate added messages
generate_messages(DEPENDENCIES std_msgs)
By the way, this all found in the tutorials that @duck-development pointed you to (specifically this one). I'm not currently at a computer with ROS installed, so if there's any issues here please let me know. This is basically the gist of it though.
Is this using
roscpp
orrospy
?