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

Revision history [back]

Let's look at the message definition of mavros_msgs/RCOut:

# RAW Servo out state

std_msgs/Header header
uint16[] channels

It means that the mavros_msgs/RCOut

  1. Contains a standard header.
  2. Contains an array of channel.
    • By seeing the message definition, we do not know how many elements this array contains. In other words, we do not know the number of channels.
    • However, we know that the datatype of this array which is unsigned short integer.

Therefore, inside the subscriber function, the safest option is to iterate over channels array as shown below:

void messageCb(const mavros_msgs::RCOut& msg){
 for(size_t i = 0; i < msg.channels.size(); i++);
   ROS_INFO_STREAM("index: " << i << " value: " << msg.channels[i]);
}

Let's look at the message definition of mavros_msgs/RCOut:

# RAW Servo out state

std_msgs/Header header
uint16[] channels

It means that the mavros_msgs/RCOut

  1. Contains a standard header.
  2. Contains an array of channel.
    • By seeing the message definition, we do not know how many elements this array contains. In other words, we do not know the number of channels.
    • However, we know that the datatype of this array which is unsigned short integer.

Therefore, inside the subscriber function, the safest option is to iterate over channels array the channels array. However, as shown below:@Mike Scheutzow mentioned in the comment below, we can not call size() on an array. Instead, we have to use the generated variable channels_length. Please read Section 2.4 Arrays for information on arrays. Below is a sample code to iterate over channels array:

nh.loginfo("Printing channels");
void messageCb(const mavros_msgs::RCOut& msg){
 for(size_t i = 0; i < msg.channels.size(); msg.channels_length; i++);
   ROS_INFO_STREAM("index: " << i << " value: " << msg.channels[i]);
nh.loginfo(msg.channels[i]);
}

Please use the above snippet only for debugging purposes. Once you have confirmed the values and corresponding indices, please disable/remove it, as the page Logging in rosserial suggests that this kind of logging is very expensive.