Go to the documentation of this file.00001 #include <boost/bind.hpp>
00002 #include <ros/ros.h>
00003 #include <turtlesim/Pose.h>
00004 #include <turtlesim/Velocity.h>
00005 #include <std_srvs/Empty.h>
00006
00007 turtlesim::PoseConstPtr g_pose;
00008 turtlesim::Pose g_goal;
00009
00010 enum State
00011 {
00012 FORWARD,
00013 STOP_FORWARD,
00014 TURN,
00015 STOP_TURN,
00016 };
00017
00018 State g_state = FORWARD;
00019 State g_last_state = FORWARD;
00020 bool g_first_goal_set = false;
00021
00022 #define PI 3.141592
00023
00024 void poseCallback(const turtlesim::PoseConstPtr& pose)
00025 {
00026 g_pose = pose;
00027 }
00028
00029 bool hasReachedGoal()
00030 {
00031 return fabsf(g_pose->x - g_goal.x) < 0.1 && fabsf(g_pose->y - g_goal.y) < 0.1 && fabsf(g_pose->theta - g_goal.theta) < 0.01;
00032 }
00033
00034 bool hasStopped()
00035 {
00036 return g_pose->angular_velocity < 0.0001 && g_pose->linear_velocity < 0.0001;
00037 }
00038
00039 void printGoal()
00040 {
00041 ROS_INFO("New goal [%f %f, %f]", g_goal.x, g_goal.y, g_goal.theta);
00042 }
00043
00044 void commandTurtle(ros::Publisher vel_pub, float linear, float angular)
00045 {
00046 turtlesim::Velocity vel;
00047 vel.linear = linear;
00048 vel.angular = angular;
00049 vel_pub.publish(vel);
00050 }
00051
00052 void stopForward(ros::Publisher vel_pub)
00053 {
00054 if (hasStopped())
00055 {
00056 ROS_INFO("Reached goal");
00057 g_state = TURN;
00058 g_goal.x = g_pose->x;
00059 g_goal.y = g_pose->y;
00060 g_goal.theta = fmod(g_pose->theta + PI/2.0, 2*PI);
00061 printGoal();
00062 }
00063 else
00064 {
00065 commandTurtle(vel_pub, 0, 0);
00066 }
00067 }
00068
00069 void stopTurn(ros::Publisher vel_pub)
00070 {
00071 if (hasStopped())
00072 {
00073 ROS_INFO("Reached goal");
00074 g_state = FORWARD;
00075 g_goal.x = cos(g_pose->theta) * 2 + g_pose->x;
00076 g_goal.y = sin(g_pose->theta) * 2 + g_pose->y;
00077 g_goal.theta = g_pose->theta;
00078 printGoal();
00079 }
00080 else
00081 {
00082 commandTurtle(vel_pub, 0, 0);
00083 }
00084 }
00085
00086
00087 void forward(ros::Publisher vel_pub)
00088 {
00089 if (hasReachedGoal())
00090 {
00091 g_state = STOP_FORWARD;
00092 commandTurtle(vel_pub, 0, 0);
00093 }
00094 else
00095 {
00096 commandTurtle(vel_pub, 1.0, 0.0);
00097 }
00098 }
00099
00100 void turn(ros::Publisher vel_pub)
00101 {
00102 if (hasReachedGoal())
00103 {
00104 g_state = STOP_TURN;
00105 commandTurtle(vel_pub, 0, 0);
00106 }
00107 else
00108 {
00109 commandTurtle(vel_pub, 0.0, 0.4);
00110 }
00111 }
00112
00113 void timerCallback(const ros::TimerEvent&, ros::Publisher vel_pub)
00114 {
00115 if (!g_pose)
00116 {
00117 return;
00118 }
00119
00120 if (!g_first_goal_set)
00121 {
00122 g_first_goal_set = true;
00123 g_state = FORWARD;
00124 g_goal.x = cos(g_pose->theta) * 2 + g_pose->x;
00125 g_goal.y = sin(g_pose->theta) * 2 + g_pose->y;
00126 g_goal.theta = g_pose->theta;
00127 printGoal();
00128 }
00129
00130 if (g_state == FORWARD)
00131 {
00132 forward(vel_pub);
00133 }
00134 else if (g_state == STOP_FORWARD)
00135 {
00136 stopForward(vel_pub);
00137 }
00138 else if (g_state == TURN)
00139 {
00140 turn(vel_pub);
00141 }
00142 else if (g_state == STOP_TURN)
00143 {
00144 stopTurn(vel_pub);
00145 }
00146 }
00147
00148 int main(int argc, char** argv)
00149 {
00150 ros::init(argc, argv, "draw_square");
00151 ros::NodeHandle nh;
00152 ros::Subscriber pose_sub = nh.subscribe("turtle1/pose", 1, poseCallback);
00153 ros::Publisher vel_pub = nh.advertise<turtlesim::Velocity>("turtle1/command_velocity", 1);
00154 ros::ServiceClient reset = nh.serviceClient<std_srvs::Empty>("reset");
00155 ros::Timer timer = nh.createTimer(ros::Duration(0.016), boost::bind(timerCallback, _1, vel_pub));
00156
00157 std_srvs::Empty empty;
00158 reset.call(empty);
00159
00160 ros::spin();
00161 }