Skip to content Skip to sidebar Skip to footer

Error Exchanging List Of Floats In A Topic

I think that the issue is silly. I'd like to run the code on two computers and I need to use a list. I followed this Tutorials I used my PC as a talker and computer of the robot as

Solution 1:

When you exchange messages in ROS is preferred to adopt standard messages if there is something relatively simple. Of course, when you develop more sophisticated systems (or modules), you can implement your own custom messages.

So in the case of float array, Float32MultiArray is your friend. Populating the message in one side will look like that (just an example using a 2 elements float32 array) in C++:

.
.
.
while (ros::ok())
  {
    std_msgs::Float32MultiArray velocities;
    velocities.layout.dim.push_back(std_msgs::MultiArrayDimension());
    velocities.layout.dim[0].label = "velocities";
    velocities.layout.dim[0].size = 2;
    velocities.layout.dim[0].stride = 1;
    velocities.data.clear();
    velocities.data.push_back(count % 255);
    velocities.data.push_back(-(count % 255));
    velocities_demo_pub.publish(velocities);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }
.
.
.

in Python for 8 elements array an example will look like:

. . . while not rospy.is_shutdown():

# compose the multiarray message
pwmVelocities = Float32MultiArray()

myLayout = MultiArrayLayout()

myMultiArrayDimension = MultiArrayDimension()

myMultiArrayDimension.label = "motion_cmd"
myMultiArrayDimension.size = 1
myMultiArrayDimension.stride = 8

myLayout.dim = [myMultiArrayDimension]
myLayout.data_offset = 0

pwmVelocities.layout = myLayout
pwmVelocities.data = [0, 10.0, 0, 10.0, 0, 10.0, 0, 10.0]

# publish the message and log in terminal
pub.publish(pwmVelocities)
rospy.loginfo("I'm publishing: [%f, %f, %f, %f, %f, %f, %f, %f]" % (pwmVelocities.data[0], pwmVelocities.data[1],
  pwmVelocities.data[2], pwmVelocities.data[3], pwmVelocities.data[4], pwmVelocities.data[5],
  pwmVelocities.data[6], pwmVelocities.data[7]))

# repeat
r.sleep()

. . .

and on the other side your callback (in C++), will look like:

.
.
.
void hardware_interface::velocity_callback(const std_msgs::Float32MultiArray::ConstPtr &msg) {
    //velocities.clear();if (velocities.size() == 0) {
        velocities.push_back(msg->data[0]);
        velocities.push_back(msg->data[1]);
    } else {
        velocities[0] = msg->data[0];
        velocities[1] = msg->data[1];
    }
    vel1 = msg->data[0];
    vel2 = msg->data[1];
    //ROS_INFO("Vel_left: [%f] - Vel_right: [%f]", vel1 , vel2);
}
.
.
.

Hope that you got an idea...if you need something more drop me a line!

Post a Comment for "Error Exchanging List Of Floats In A Topic"