ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to add structure in another structure as a member and publish+subscribe it ?

asked 2020-10-07 05:42:20 -0500

yash.j gravatar image

updated 2022-08-07 08:47:17 -0500

lucasw gravatar image

I have created a custom message file : my_msg.msg

uint16 count
my_struct[] data

Also, the contents of fusion are defined in : my_struct.msg

float32 temp
uint16 my_data
  1. How to assign data from the publisher to the my_struct[].data member in my_msg ?
  2. How to subscribe this data from a different ROS node ?
  3. What should be the changes in CMakeLists.txt (apart from adding my_msg.msg & my_struct.msg) ?
edit retag flag offensive close merge delete

Comments

Is this using roscpp or rospy?

jayess gravatar image jayess  ( 2020-10-09 17:38:01 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-10-09 17:59:50 -0500

jayess gravatar image

As far as I can remember, in roscpp arrays in messages' attributes are actually vectors (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.

edit flag offensive delete link more
0

answered 2020-10-09 16:29:27 -0500

duck-development gravatar image
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-10-07 05:42:20 -0500

Seen: 154 times

Last updated: Oct 09 '20