Creating custom rostopic
Is it possible to create a rostopic from a custom multiarray? I have an array of imu data and would like to have a label for each data value in the array when running rostopic echo instead of just an array of the numbers.
Thanks for any help! Found a couple similar questions/solutions but didnt seem to cover what im looking for.
Asked by sh_ec_ks on 2020-11-05 20:22:19 UTC
Answers
It sounds like you want a custom message. See the Creating a ROS msg and srv tutorial for details on how to create and build a message.
Asked by tryan on 2020-11-06 08:18:03 UTC
Comments
So to add more information to @tryan answer in which you can find the know-how for doing this, I will post here a possible solution for your problem:
The approach is to define two types of custom msgs:
The IMU data with a key
# Msg to contain IMU data and key (string)
string key # Key to map IMU data
sensor_msgs/Imu data # IMU data
Then the array to store those msgs:
# Array of IMU mapped msg
imu_map[] data
And use them as kind of "map":
#include <ros/ros.h>
#include <test_pkg/custom_imu.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "test_node");
ros::NodeHandle n;
ros::Publisher imu_pub = n.advertise<test_pkg::custom_imu>("test_node/imu_vector", 1000);
ros::Rate loop_rate(10);
int count = 0;
test_pkg::custom_imu imu_vector;
while (ros::ok())
{
for(const auto &ii: {"imu0", "imu1", "imu2", "imu3", "imu4"})
{
test_pkg::imu_map imu_data;
imu_data.key = ii;
imu_data.data = sensor_msgs::Imu(); //empty IMU msg
imu_vector.data.push_back(imu_data);
}
imu_pub.publish(imu_vector);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
Remember to add the proper directive to the CMakeLists.txt
:
cmake_minimum_required(VERSION 3.0.2)
project(test_pkg)
## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
sensor_msgs
message_generation
)
## Generate messages in the 'msg' folder
add_message_files(
FILES
imu_map.msg
custom_imu.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
sensor_msgs
)
catkin_package(
CATKIN_DEPENDS roscpp message_runtime
)
###########
## Build ##
###########
## Specify additional locations of header files
include_directories(
${catkin_INCLUDE_DIRS}
)
## Declare a C++ executable
add_executable(test_node src/test_node.cpp)
## Add cmake target dependencies of the executable
add_dependencies(test_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
target_link_libraries(test_node ${catkin_LIBRARIES})
Hope that helps.
Regards.
Asked by Weasfas on 2020-11-07 08:11:37 UTC
Comments
thank you so much! exactly what i was looking for!
Asked by sh_ec_ks on 2020-11-07 08:21:05 UTC
This is a nice answer, except that I would not recommend using a custom message here. If key
is supposed to encode from which IMU the data is coming, you should use the header.frame_id
field for this.
The use of the array here is also something to consider not doing, as it's perfectly OK to publish messages coming from multiple sources, or even multiple messages from a single source on a single topic.
Asked by gvdhoorn on 2020-11-07 11:31:02 UTC
Comments
I don't know how to managed to create three duplicates, see #q364897, #q364898 and #q364899 :-)
Asked by mgruhler on 2020-11-06 03:43:24 UTC