Ask Your Question
0

Import variables from a message

asked 2018-01-16 06:57:28 -0600

Lauran gravatar image

Hello,

In my code I would like to get a variable from my message and use this in my main. Whenever I run this code "x" is said to be 0.00. This should be a different number. In my message I declare my variables as float32. I feel like I don't use x = msg.x the proper way. I know that my message is being send the right way. Because in a different code I was able to ROS_INFO_STREAM(x). This showed that it receives the right x value. Could someone please look at my code and hopefully see where I make a mistake?

#include <iostream>
#include <ros/ros.h>
#include <moveit/move_group_interface/move_group.h>
#include <sstream>
#include <ur5_inf3480/Coor.h>

using namespace std;

class MoveRobot {
    public:
        double x;
    double y;
    double z;
    double roll;
    double pitch;
    double yaw;
    double a;
    void PublishCoordinatesCallback(const ur5_inf3480::Coor msg);
};

void MoveRobot::PublishCoordinatesCallback(const ur5_inf3480::Coor msg) {
    x = msg.x;
    y = msg.y;
    z = msg.z;
    roll = msg.roll;
    pitch = msg.pitch;
yaw = msg.yaw;
a = msg.a;
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "coordinates");
    ros::NodeHandle nh;
    ros::Rate loop_rate(30);
    MoveRobot moverobot;
    ros::Subscriber sub = nh.subscribe<ur5_inf3480::Coor>("topic_subscribed", 1, &MoveRobot::PublishCoordinatesCallback, &moverobot);
    while (ros::ok())
    {
        ROS_INFO("This is x: %.2f", moverobot.x);
    }
    loop_rate.sleep();
    ros::spin();
    return 0;
}

I hope I won't ask a question which has been asked already. I tried to search the forum and the internet first but I can't find a solution. Thank you for your answers!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2018-01-16 07:10:04 -0600

mgruhler gravatar image

The problem is you never service the callbacks. This only happens when you do a spin call.

However, you are looping without calling spin.

Thus, instead of:

while (ros::ok())
{
    ROS_INFO("This is x: %.2f", moverobot.x);
}
loop_rate.sleep();
ros::spin();

You need to have

while (ros::ok())
{
    ROS_INFO("This is x: %.2f", moverobot.x);
    loop_rate.sleep();
    ros::spinOnce();
}

Note: spin() is ´looping itself, thus the call to spinOnce here.

See also: http://wiki.ros.org/roscpp/Overview/C...

edit flag offensive delete link more

Comments

Thank you for your answer! The change in code didn't show any difference. It still just said x was 0.00. But when I started reading your link I saw that I might need to switch loop_rate.sleep(); and ros::spinOnce(); I did this and now it first says x=0.00 and then after a second it gives the right x

Lauran gravatar image Lauran  ( 2018-01-16 07:28:18 -0600 )edit

I am on the right track! Whenever I set my ros::Rate loop_rate(); faster. x goes from 0.00 to the value faster. Is there a clean way to publish my message immediately instead just after a second or something like that?

Lauran gravatar image Lauran  ( 2018-01-16 07:31:25 -0600 )edit

Well, this is the Subscriber side. So how fast this changes also depends on how fast the Publisher side is actually publishing.

switching the sleep and the spinOnce shouldn't make much of a difference after the inital message has been received.

mgruhler gravatar image mgruhler  ( 2018-01-16 07:41:01 -0600 )edit

Okay, I will just make it publish fast then. Thank you for your help and the link. This forum really makes me learn more :)

Lauran gravatar image Lauran  ( 2018-01-16 07:48:21 -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

1 follower

Stats

Asked: 2018-01-16 06:57:28 -0600

Seen: 258 times

Last updated: Jan 16 '18