Ask Your Question
0

Question about implement class to handle callback for subscriber

asked 2016-03-17 00:56:18 -0600

Thang Nguyen gravatar image

updated 2016-03-17 01:12:35 -0600

mgruhler gravatar image

This question relates to my previous question in this post: http://answers.ros.org/question/22906... Since I accepted the answer so I guess I should make a new question here about implement my solution. If creating a second question is wrong please let me know how to do it properly

Thanks from Stefan's answer, I try to implement the class to store pose information from callback. I defined this class as follow but it has an error. Since I don't have much C++ experience, I need help:

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include "turtlesim/Pose.h"

class PoseReceiver{
    turtlesim::Pose poseInfo;
    public:
        turtlesim::Pose getPose(){return poseInfo;}
        void setPose(const turtlesim::Pose pose);
        void callback(const turtlesim::Pose& msg);
};

void PoseReceiver::setPose(turtlesim::Pose pose){
    poseInfo = pose;
}

void callback(const turtlesim::Pose& msg)
{
    setPose(msg);
}

int main(int argc, char **argv){
    const double FORWARD_SPEED_MPS = 0.1;
    const double PI = 3.14159265358979323846;
    const double TURN_SPEED_RPS = PI/20;

    ros::init(argc, argv, "move_turtle");
    ros::NodeHandle node;
    PoseReceiver poseReceiver;
    geometry_msgs::Twist msg;

    ros::Publisher pub = node.advertise<geometry_msgs::Twist>("turtle1/cmd_vel",10);
    ros::Subscriber sub = node.subscribe("turtle1/pose",10,&PoseReceiver::callback, &poseReceiver);

    ROS_INFO_STREAM("Start Position: " << poseReceiver.getPose().x << ", " 
                                       << poseReceiver.getPose().y << ", Direction: " 
                                       << poseReceiver.getPose().theta);
    //==============================
    //Go straight 1m
    ros::Time start_time = ros::Time::now();
    ros::Rate rate(10);
    while(ros::ok() && ros::Time::now() - start_time < ros::Duration(10)){
        msg.linear.x = FORWARD_SPEED_MPS;
        pub.publish(msg);
        rate.sleep();
    }
    msg.linear.x = 0;
    pub.publish(msg);
    //==============================
    //Turn 45deg
    start_time = ros::Time::now();
    while(ros::ok() && ros::Time::now() - start_time < ros::Duration(5)){
        msg.angular.z = TURN_SPEED_RPS;
        pub.publish(msg);
        rate.sleep();   
    }
    msg.angular.z = 0;
    pub.publish(msg);
    //==============================
    ROS_INFO_STREAM("End Position: " << poseReceiver.getPose().x << ", " 
                                     << poseReceiver.getPose().y << ", Direction: " 
                                     << poseReceiver.getPose().theta);

}

Currently, I have this error:

/home/viki/Documents/Workspace_IntroToRobotics/src/turtle_move_package/move_turtle_node.cpp: In function ‘void callback(const Pose&)’:
/home/viki/Documents/Workspace_IntroToRobotics/src/turtle_move_package/move_turtle_node.cpp:19:13: error: ‘setPose’ was not declared in this scope
  setPose(msg);
             ^

Thank you very much.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-03-17 01:16:16 -0600

mgruhler gravatar image

updated 2016-03-17 01:16:52 -0600

you missed something in the implementation of the callback(...) function.

As this is a member function of PoseReceiver, it should be PoseReceiver::callback(...), as you did with the setPose(...) function.

Off-Topic: For the sake of readability, pleas format your code the next time correctly (the button with 1s and 0s).

edit flag offensive delete link more

Comments

@mig: Thanks for your answer, I will try this when I go home tonight. Could you please tell me how to format correctly for the code when posting question? I don't really want that happens but I don't know how to fix it. Where is the button you talk about? I looked around but I don't see anything.

Thang Nguyen gravatar imageThang Nguyen ( 2016-03-17 07:07:49 -0600 )edit

The code formatting button says "101010" on it. It's right next to the button for external links in comments, and next to the quote button in answers/questions. You can also just highlight your code and press Ctrl+k

jarvisschultz gravatar imagejarvisschultz ( 2016-03-17 07:46:39 -0600 )edit

I have the error fixed by PoseReceiver::callback(...) The sequence is correct now but I still have wrong angle result

[ INFO] [1458270240.810548805]: Start Position: 0, 0, Direction: 0
[ INFO] [1458270255.914283239]: End Position: 0, 0, Direction: 0

Should I add a delay after robot stop

Thang Nguyen gravatar imageThang Nguyen ( 2016-03-17 22:16:02 -0600 )edit

This is the result with rostopic echo /turtle1/pose after robot is stopped:

---
x: 6.46924448013
y: 5.544444561
theta: 0.799221158028
linear_velocity: 0.0
angular_velocity: 0.0
---

so somehow my poseReceiver isn't updated correctly.

Thang Nguyen gravatar imageThang Nguyen ( 2016-03-17 22:24:16 -0600 )edit

Oh, it works now. I forgot the ros::spinOnce()

Thang Nguyen gravatar imageThang Nguyen ( 2016-03-18 00:26:38 -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: 2016-03-17 00:56:18 -0600

Seen: 294 times

Last updated: Mar 17 '16