Ask Your Question
0

Creating custom rostopic

asked 2020-11-05 19:22:19 -0500

sh_ec_ks gravatar image

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.

edit retag flag offensive close merge delete

Comments

I don't know how to managed to create three duplicates, see #q364897, #q364898 and #q364899 :-)

mgruhler gravatar image mgruhler  ( 2020-11-06 02:43:24 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2020-11-07 07:11:37 -0500

Weasfas gravatar image

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.

edit flag offensive delete link more

Comments

thank you so much! exactly what i was looking for!

sh_ec_ks gravatar image sh_ec_ks  ( 2020-11-07 07:21:05 -0500 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2020-11-07 10:31:02 -0500 )edit
0

answered 2020-11-06 07:18:03 -0500

tryan gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-11-05 19:22:19 -0500

Seen: 86 times

Last updated: Nov 07 '20