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

Unable to Publish Float32MultiArray in .cpp

asked 2019-08-29 01:21:06 -0600

BV_Pradeep gravatar image

Hi All,

I wrote the following program based simple Publisher c++ tutorial.

Code :

#include "ros/ros.h"
#include "std_msgs/Float32MultiArray.h"

int main(int argc, char **argv)
{

  ros::init(argc, argv, "waypoint_publisher");
  ros::NodeHandle n;
  ros::Publisher waypoint_pub = n.advertise<std_msgs::Float32MultiArray>("waypoints", 1000);
  ros::Rate loop_rate(1);

  float waypoints[9][3] = {
                {-6.922, 12.437,-1.315},
                {3.067, -17.730, -1.315},
                {3.067, -17.730, -2.956},
                {-1.645, -19.531,-2.956},
                {-1.645, -19.531,1.892},
                {-12.163, 10.458,1.892},
                {-12.163, 10.458,0.351},
                {-6.922, 12.437,0.351},
                {-6.922, 12.437,-1.315}
              };

  while (ros::ok())
  {
    std_msgs::Float32MultiArray msg;
    msg.data = waypoints;

    waypoint_pub.publish(msg);
    ROS_INFO("Published the waypoints");

    ros::spinOnce();
    loop_rate.sleep();

  }
  return 0;
}

I get the following error when I complie the workspace

/home/rnd424/ros_ws/src/visualize_waypoints/src/waypoint_publisher.cpp: In function ‘int main(int, char**)’:
/home/rnd424/ros_ws/src/visualize_waypoints/src/waypoint_publisher.cpp:27:14: error: no match for ‘operator=’ (operand types are ‘std_msgs::Float32MultiArray_<std::allocator<void> >::_data_type {aka std::vector<float>}’ and ‘float [9][3]’)
     msg.data = waypoints;
              ^
In file included from /usr/include/c++/5/vector:69:0,
                 from /usr/include/boost/format.hpp:17,
                 from /usr/include/boost/math/policies/error_handling.hpp:31,
                 from /usr/include/boost/math/special_functions/round.hpp:14,
                 from /opt/ros/kinetic/include/ros/time.h:58,
                 from /opt/ros/kinetic/include/ros/ros.h:38,
                 from /home/rnd424/ros_ws/src/visualize_waypoints/src/waypoint_publisher.cpp:1:
/usr/include/c++/5/bits/vector.tcc:167:5: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = float; _Alloc = std::allocator<float>]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/5/bits/vector.tcc:167:5: note:   no known conversion for argument 1 from ‘float [9][3]’ to ‘const std::vector<float>&’
visualize_waypoints/CMakeFiles/waypoint_publisher.dir/build.make:62: recipe for target 'visualize_waypoints/CMakeFiles/waypoint_publisher.dir/src/waypoint_publisher.cpp.o' failed
make[2]: *** [visualize_waypoints/CMakeFiles/waypoint_publisher.dir/src/waypoint_publisher.cpp.o] Error 1
CMakeFiles/Makefile2:1225: recipe for target 'visualize_waypoints/CMakeFiles/waypoint_publisher.dir/all' failed
make[1]: *** [visualize_waypoints/CMakeFiles/waypoint_publisher.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

How to resolve this error ?

edit retag flag offensive close merge delete

Comments

Were you able to solve this? If yes, do you remember how? I am trying to publish the same thing using python.

panserzap gravatar image panserzap  ( 2021-06-22 08:04:56 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-08-29 01:47:37 -0600

duck-development gravatar image

You may read this answer https://answers.ros.org/question/3718...

The problem you like to assign an 2d array to an std::vector foat.

And you have to say you data layout 9x3

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-08-29 01:21:06 -0600

Seen: 3,056 times

Last updated: Aug 29 '19