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

Revision history [back]

click to hide/show revision 1
initial version

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.