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

How to publish float64 array to a topic?

asked 2022-01-19 13:28:18 -0600

faraziii gravatar image

updated 2022-01-19 16:29:24 -0600

osilva gravatar image

Hello, I am trying to publish fake joint states on a topic. My code compiles successfully, however, it gives an error "Segmentation fault, core dumped" when is run the node. Basically, I want to publish two fake positions (0) and two velocities.

#include<ros/ros.h>
#include<std_msgs/Float64.h>
#include <sensor_msgs/JointState.h>
float lVel = 0, rVel = 0, lPos = 0, rPos = 0;

int main(int argc, char** argv){
    ros::init(argc, argv, "custom_joint_state_publisher");
    ros::NodeHandle n;
    ros::Publisher statePub = n.advertise<sensor_msgs::JointState>("measured_joint_states", 100); 
    sensor_msgs::JointState joint_values;
    ros::Rate r(10);
    while(ros::ok()){
        joint_values.header.stamp = ros::Time::now();
        joint_values.position[0] = 0 ;
        joint_values.position[1] = 0;
        joint_values.velocity.push_back(0);
        joint_values.velocity.push_back(0);
        statePub.publish(joint_values);
        r.sleep();
        ros::spinOnce();
    }
    return 0;
}

Here you can see that I am using the assignment operator and pushback() function. The segmentation fault error appears with the assignment operator only. The issue with push_back function is that instead of publishing one value (0), it publishes multiple values (array of 0s) on the topic. How to publish one 0 at a time to the topic? I am using ROS melodic, Ubuntu 18.04. Thank you!

edit retag flag offensive close merge delete

Comments

Thank you for comment. I actually went through above links but couldn't fix the issue i am facing.

faraziii gravatar image faraziii  ( 2022-01-19 13:57:21 -0600 )edit

Please edit your question so your code is easy readable, only then we could help

ljaniec gravatar image ljaniec  ( 2022-01-19 14:00:33 -0600 )edit
1

Just to note: the OP is not publishing "a float64 topic". This is a regular JointState publisher.

gvdhoorn gravatar image gvdhoorn  ( 2022-01-20 03:32:57 -0600 )edit

Sorry, i didn't know how to do that but someone did that for me. Thanks to him.

faraziii gravatar image faraziii  ( 2022-01-20 03:56:09 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-19 17:44:58 -0600

miura gravatar image

resize will solve this problem.

#include<ros/ros.h>
#include<std_msgs/Float64.h>
#include <sensor_msgs/JointState.h>
float lVel = 0, rVel = 0, lPos = 0, rPos = 0;

int main(int argc, char** argv){
    ros::init(argc, argv, "custom_joint_state_publisher");
    ros::NodeHandle n;
    ros::Publisher statePub = n.advertise<sensor_msgs::JointState>("measured_joint_states", 100); 
    sensor_msgs::JointState joint_values;
    ros::Rate r(10);
    while(ros::ok()){
        joint_values.header.stamp = ros::Time::now();
        joint_values.position.resize(2);
        joint_values.position[0] = 0 ;
        joint_values.position[1] = 0;
        joint_values.velocity.resize(0);
        joint_values.velocity.push_back(0);
        joint_values.velocity.push_back(0);
        statePub.publish(joint_values);
        r.sleep();
        ros::spinOnce();
    }
    return 0;
}

position will be resize at 2 to make room for the assignment.

velocity should be resize by 0 to clear the assigned area. This can also be done with empty.

edit flag offensive delete link more

Comments

Thank you! That's what i was missing.

faraziii gravatar image faraziii  ( 2022-01-20 03:55:27 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-01-19 13:28:18 -0600

Seen: 1,643 times

Last updated: Jan 19 '22