Question about implement class to handle callback for subscriber
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.