0
$\begingroup$

Rosanswers logo

I need to use three variables that I subscribe from another topic and use it in the main loop. I know that I can use boost function but I don't understand how to use in my case:


double theta1f;
double theta2f;
double theta3f;

class Listener
{
public:
    void callback(const std_msgs::Float32MultiArray::ConstPtr& msg);
};
void Listener::callback(const std_msgs::Float32MultiArray::ConstPtr& msg)
{
    theta1f = msg->data[0];
    theta2f = msg->data[1];
    theta3f = msg->data[2];
}
int main(int argc, char **argv)
{
    ros::init(argc, argv, "node_joint");
    ros::NodeHandle nh;
    ros::Rate loop_rate(30); 
    Listener listener;
    ros::Subscriber sub = nh.subscribe("topic_subscribed", 1, &Listener::callback, &listener);
while (ros::ok())
    {  
      cout  theta1f endl;
      }
    loop_rate.sleep();
    ros::spin();
    return 0;
}

I need to use theta1f in this main (cout is only an example and i know that is missing the <<). How can I do it using boost function? There are other solutions better than this?

Thank you for your answers.


Originally posted by nikkk on ROS Answers with karma: 13 on 2017-01-23

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Boost does not seem necessary in this case. You can simply declare theta1f, theta2f, and theta3f as properties of your class listener. Since you create a Listener object in main, you would then have access to its properties. Basically, whenever you have a callback function for messages, you will need to use a class to utilize the data from that callback somewhere else. Like this:

class Listener 
{
  public:
    double theta1f;
    double theta2f;
    double theta3f;
        
    void callback(const std_msgs::Float32MultiArray::ConstPtr& msg);
};

void Listener::callback(const std_msgs::Float32MultiArray::ConstPtr& msg) 
{
    theta1f = msg->data[0];
    theta2f = msg->data[1];
    theta3f = msg->data[2];
}

int main(int argc, char **argv) 
{
    ros::init(argc, argv, "node_joint");
    ros::NodeHandle nh;
    ros::Rate loop_rate(30); 
    Listener listener;
    ros::Subscriber sub = nh.subscribe<std_msgs::float32multiarray>("topic_subscribed", 1, &Listener::callback, &listener);
    while (ros::ok()) 
    {
        ros::spinOnce();
        ROS_INFO("This is theta1f: %.2f", listener.theta1f);
        ROS_INFO("This is theta2f: %.2f", listener.theta2f);
        ROS_INFO("This is theta2f: %.2f", listener.theta2f);
        loop_rate.sleep();
    }
    
    return 0;
}

Also, in general it is discouraged to use global variables unless absolutely necessary.


Originally posted by Steven_Daniluk with karma: 606 on 2017-01-23

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by nikkk on 2017-01-23:
Thank you for the answer. This works without problem.

Comment by reben on 2018-02-17:
This did not work for me until I deleted loop_rate.sleep(); , ros::spin(); , return 0;. Then I had to add the following lines inside the while loop: ros::spinOnce; , loop_rate.sleep();.

Comment by Steven_Daniluk on 2018-02-18:
Yep, you are correct. That's my bad, this is what happens when you write code snippets directly in a comment. I've edited my response to correct that mistake.

Comment by dj95 on 2020-02-17:
I'm a bit confused between the difference of ros::spinOnce() and ros::spin()... Why does calling ros::spinOnce(); inside the while loop does not produce the same result as calling ros::spin() outside the while loop here.

$\endgroup$

Your Answer

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