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

publishing int variable

asked 2017-02-13 03:57:59 -0500

fafa gravatar image

updated 2017-02-13 04:31:57 -0500

Hello,

I want to publish a variable, called targetIdx (type int), but I always get an error message

That's an extract of my publisher:

std_msgs::Int32 targetIdx;
targetIdx.data = targetIdx;
Sensor_Target_Nr_pub.publish(targetIdx);
ROS_INFO("Sensor Target Nr: %d", targetIdx.data);

That's the error message:

cannot convert ‘std_msgs::Int32’ to ‘std_msgs::Int32_<std::allocator<void> >::_data_type {aka int}’ in assignment
         targetIdx.data = targetIdx;

How can I solve the problem?

Thanks

I have declared the variable targetIdx as type int in the beginning of my program.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-02-13 04:10:35 -0500

gvdhoorn gravatar image

updated 2017-02-13 04:11:06 -0500

std_msgs::Int32 targetIdx;

So targetIdx is declared to be of type std_msgs::Int32 here ..

targetIdx.data = targetIdx;

.. and here you assign the just instantiated msg to its own data member.

That is not going to work.

Could it be that you are shadowing your int targetIdx with the newly created std_msgs::Int32 targetIdx?

edit flag offensive delete link more

Comments

Sorry I'm new to programming and ROS...could you please give me an example how to publish an int variable

fafa gravatar image fafa  ( 2017-02-13 04:37:21 -0500 )edit

This is a C++ issue really, not a ROS issue.

You are creating a two variables with the same name. The compiler will only 'remember' the one that you created last. In this case that is the std_msgs::Int32 one, not the int.

gvdhoorn gravatar image gvdhoorn  ( 2017-02-13 05:37:23 -0500 )edit

Ok. The targetIdx variable is delivered by a sensor. So how can I easily publish this delivered variable?

fafa gravatar image fafa  ( 2017-02-13 05:45:29 -0500 )edit

Just give your std_msgs::Int32 instance a different name. Then everyhing should start working (provided you have the rest correctly implemented).

gvdhoorn gravatar image gvdhoorn  ( 2017-02-15 05:57:07 -0500 )edit

Question Tools

Stats

Asked: 2017-02-13 03:57:59 -0500

Seen: 1,161 times

Last updated: Feb 13 '17