errolflynn
errolflynn

Reputation: 641

Sending C array on ROS using messages

My ROS message is simple:

int8[64] packet1

I am publishing in my talking node with:

terp::Packet1 msg;
msg.packet1={0,1,0,1,0,1};
ROS_INFO("Packet in string form: %s", msg->packet1);
chatter_pub.publish(msg);
ros::spinOnce();

I am retrieving in my listening node with:

void resolve_input1(const uint8_t msg[]) {
  if (sizeof(msg->packet1)/sizeof(msg->packet1[0])<MAX_MSG_LEN) {
    memcpy(msg1,msg->packet1);
    ROS_INFO("I heard: [%s]\n",msg1);
  } else
    ROS_ERROR("Message from node 1 too long");
}

However, upon making the project, I receive the following error:

listener.cpp:16:19: error: request for member 'packet1' in '*msg',
which is of non-class type 'const uint8_t {aka const unsigned char}
  if (sizeof(msg->packet1)/sizeof(msg->packet1[0])<MAX_MSG_LEN) {

Changed resolve_input1's declaration to:

void resolve_input1(const terp::Packet1 msg) {

EDIT: Got rid of that error, but now a new one appears, which is similar; underlining my confusion about messages:

listener.cpp:16:17 error: base operand of '->' has non-pointer type
'const Packet1 {aka const terp::Packet1_<std::allocator<void> >}'
    if (sizeof(msg->packet1)/sizeof(msg->packet1[0])<MAX_MSG_LEN) {
                  ^

Upvotes: 3

Views: 2051

Answers (1)

Anton Savin
Anton Savin

Reputation: 41341

In resolve_input1 msg is of type const uint8_t*, so you probably have to reinterpret_cast it to const terp::Packet1* before accessing fields.

Update: Given your edit, just change msg-> to msg..

Upvotes: 2

Related Questions