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

Revision history [back]

I think I got it :

In your code, you are calling listen_0.status(); when a user press '0', this function is creating a new subscriber. If you added a ros::spinOnce(), the callback is never called if a user never pressed '0' before. But there is another problem, you recreate the class kuka listen_0(nh); at the beginning of the loop, hence destroying the old class instance and losing your subscriber.

When using ROS, you should only create a subscriber (or publisher) once, often in the constructor of a class or in an init() function, and delete it at the end of your program. This allow your rosnode to "tell" the roscore that there is a new node with a subscriber (or publisher), and he should receive (or send) message (this take some times in background, you can't create a publisher or subscriber and instantly receive or publish a message).

I recommend you to take a look at the ros tutorial about Subscribing to have a better understanding of the ROS ecosystem.

Normally, your code should look like this (I added /// comment where I edited your file):

Be carefull, as this is not optimal at all. Since your cin are blocking, i placed the ros::spinOnce() before the code needs the messages values. You may want to take a look at Async Spinner once you have fully grasped the basics

..skipping includes...
class kuka
{

private:
    ros::Subscriber sub;
    ros::NodeHandle nh_;

    ///We save the last robot coordinate here
    geometry_msgs::PointStamped lastCoordinate;

public:
    kuka(ros::NodeHandle &nh) // constructor
    {
        nh_ = nh;

        ///Create the subscriber here
        sub = nh_.subscribe("/robposi", 1000, &kuka::callback, this);
    }

    void status() // func showing the actual position (case0)
    {
        cout << "status";
        ROS_INFO_STREAM(endl
                        << "RobCoor.x: " << lastCoordinate->point.x << endl
                        << "RobCoor.y: " << lastCoordinate->point.y << endl
                        << "RobCoor.z: " << lastCoordinate->point.z << endl);
    }

    void callback(const geometry_msgs::PointStamped::ConstPtr &rob)
    {
        cout << "callback  ";
        lastCoordinate = rob; ///we save the robot position for later
    }
};

int main(int argc, char **argv)
{
    ros::init(argc, argv, "kuka");
    int j;
    char loop = '*';

    ros::NodeHandle nh;

    ///Creation of the class outside the main loop
    kuka listen_0(nh);

    do
    {
        cout << "-------------------------------------------" << std::endl;
        cout << "|0 |check position of robot               |" << std::endl;
        cout << "|1 |go to home                            |" << std::endl;
        cout << "|2 |go to stored position                 |" << std::endl;
        cout << "|3 |enter the position w,x,y,z            |" << std::endl;
        cout << "|4 |enter the position of joints          |" << std::endl;
        cout << "|5 | exit                                 |" << std::endl;
        cout << "-------------------------------------------" << std::endl;
        cin >> j;

        cout << "i =   " << j;

        ros::spinOnce();///we spin here since this is the optimal place

        switch (j)
        {
        case (0):
        {
            cout << "i hekkddddddddddd   ";
            listen_0.status(); ///Display current position if pressed 0
            cout << "i hekk.lj,hk....   ";
        }

        break;
        }

        cout << "would you like to enter new choice y/n?" << endl;
        cin >> loop;
    } while (loop == 'y' && ros::ok()); ///continue until ros is stopped, ctrl+c is pressed or user didn't entered Y

    return 0;
}