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

Revision history [back]

click to hide/show revision 1
initial version

Hello, I am having the same issue. I have tried to use the code of Jan and implement it in my situation. When I want to display my variable in the terminal it says it's 0.00. Which it shouldn't be. In a different code I was able to see that my variable "x" was send properly so I think the problem isn't in the sending part. Now I want to use my variable in my main. This is my code:

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;
}

Thank you for your help!

Hello, I am having the same issue. I have tried to use the code of Jan and implement it in my situation. When I want to display my variable in the terminal it says it's 0.00. Which it shouldn't be. In a different code I was able to see that my variable "x" was send properly so I think the problem isn't in the sending part. Now I want to use my variable in my main. This is my code:

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;
moverobot;
    ros::Subscriber sub = nh.subscribe<ur5_inf3480::Coor>("topic_subscribed", 1, &MoveRobot::PublishCoordinatesCallback, &MoveRobot);
&moverobot);
    while (ros::ok())
    {
        ROS_INFO("This is x: %.2f", MoveRobot.x);
moverobot.x);
    }
    loop_rate.sleep();
    ros::spin();
    return 0;
}

Thank you for your help!