ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Cannot convert ros::Subscriber to float

asked 2017-01-24 04:19:47 -0600

Milliau gravatar image

updated 2017-01-24 04:36:12 -0600

NEngelhard gravatar image


I have the Problem: error: cannot convert ros::Subscriber to float in assignment

I used 2 Subscirber to listen to my velocities and i want to get this as an float but it doesn't work.

#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include <can_msgs/Frame.h>
#include <socketcan_bridge/topic_to_socketcan.h>
#include <socketcan_interface/socketcan.h>

class VehicleDynamics


   sub_vel_left= n.subscribe<std_msgs::Float64>("left_velocity_controller/command",1, &VehicleDynamics::velCallback, this);
   sub_vel_right= n.subscribe<std_msgs::Float64>("right_velocity_controller/command",1, &VehicleDynamics::velCallback, this);

   pub_ = n.advertise<can_msgs::Frame>("sent_messages", 10);

float calculate()
    static const float calcReduc = m_reduc * 16.6666666f;
    static const float calcDiameter = (m_diameter * M_PI) / 1000.0f;

    velocity_rpm = ((left_wheel+right_wheel)/ 2);

    return (velocity_rpm* calcDiameter/calcReduc);

void velCallback(const std_msgs::Float64::ConstPtr& float_msgs)


    velocity_kmh = calculate();

 can_msgs::Frame msg;
  msg.is_extended = false;
  msg.is_rtr = false;
  msg.is_error = false; = 0x220;
  msg.dlc = 8;[1] = velocity_kmh;

  msg.header.frame_id = "0";  // "0" for no frame.
  msg.header.stamp = ros::Time::now();

  // send the can_frame::Frame message to the sent_messages topic.


ros::NodeHandle n;
ros::Publisher pub_;
ros::Subscriber sub_vel_left;
ros::Subscriber sub_vel_right;
float velocity_rpm;
float velocity_kmh;
float left_wheel;
float right_wheel;
float m_max_velocity;
float m_diameter;

    int m_reduc;


    int main(int argc, char **argv)


    VehicleDynamics SAPObject;
    ros::Rate loop_rate(10000);

    return 0;

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-01-24 04:36:03 -0600

NEngelhard gravatar image

updated 2017-01-24 04:36:31 -0600

The subscriber is a subscriber and not a float value. You can find the float value in the msg you receive. In your case, you need two callback functions in which you can access your float as float_msgs->data. So create two callbacks (leftVelCb, RightVelCb) and read the corresponding msg.

edit flag offensive delete link more


ok, so i have two make a function like this: void velLeftCallback(const std_msgs::Float64::ConstPtr& float_msgs) { float_msgs->data; } is that right? and where i can definded in which variable I write the data?

Milliau gravatar image Milliau  ( 2017-01-24 06:10:28 -0600 )edit

just assign it,,, (float left_vel = float_msgs->data;}

NEngelhard gravatar image NEngelhard  ( 2017-01-24 07:17:15 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2017-01-24 04:19:47 -0600

Seen: 1,702 times

Last updated: Jan 24 '17