0
$\begingroup$

Rosanswers logo

What is needed to publish a How to work with std_msgs::Int16MultiArray message?

I tried

std_msgs::Int16MultiArray msg;
int16_t *velocities;
msg.data = velocities;

but just those few lines gives me errors while compiling

error: no match for ‘operator=’ (operand types are ‘std_msgs::Int16MultiArray_<std::allocator<void> >::_data_type {aka std::vector<short int, std::allocator<short int> >}’ and ‘int16_t* {aka short int*}’)
         msg.data = velocities;

Originally posted by kump on ROS Answers with karma: 308 on 2019-01-28

Post score: 0


Original comments

Comment by gvdhoorn on 2019-01-28:
I believe this is a duplicate of #q37185.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

This works:

# definition in header
std::vector<physics::JointPtr> joints;
ros::Publisher rosPub;
std::unique_ptr<ros::NodeHandle> rosNode;

# initialization
this->rosNode.reset(new ros::NodeHandle( node_name ));
this->rosPub = this->rosNode->advertise<std_msgs::Float32MultiArray>("/velocity",1);

# callback function
std_msgs::Float32MultiArray msg;
msg.data.push_back( this->joints[0]->GetVelocity(0) );
msg.data.push_back( this->joints[1]->GetVelocity(0) );
msg.data.push_back( this->joints[2]->GetVelocity(0) );

this->rosPub.publish(msg);

Originally posted by kump with karma: 308 on 2019-01-29

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by gvdhoorn on 2019-01-29:
Please note that you're not initialising the layout field here. Subscribers that expect that field to describe the size, dimensionality and shape of the data you're publishing will not work correctly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.