turtlebot orientation and moving towards target
Hi everybody,
I am new to ROS and currently I am using turtlebot for my experiment. My aim is to rotate the turtlebot to face my target coordinate and move towards it. However, the turtlebot is not behaving as I expected. The turtlebot is keep rotating or keep moving forward. Here is my source code and kindly give me some advices.
for example destination is (1,-0.5)
curr_loc = msg->pose.pose;
double x=msg->pose.pose.position.x;
double y=msg->pose.pose.position.y;
double z=msg->pose.pose.orientation.z;
double w=msg->pose.pose.orientation.w;
curr_head=atan2(2*(x*y+w*z), w*w + x*x - y*y - z*z);
DestX=1; DestY=-0.5;
left_X=DestX-x;
left_Y=DestY-y;
left_dist=sqrt((left_X * left_X) + (left_Y*left_Y));
if(left_dist <= 0.05)
{
reached=true;
}
if(!reached)
{
heading=atan2(left_locY,left_locX);
}
n_head=curr_head-heading;
if (n_head<0.2 && n_head>-0.2)
facing=true;
else
facing=false;
if (!facing)
cmdvel.linear.x=0.0;
if (n_head>-3.14)
cmdvel.angular.z=-0.25;
else if (n_head>-1.6)
cmdvel.angular.z=-0.10;
else if (n_head<3.14)
cmdvel.angular.z=0.25;
else if (n_head>1.6)
cmdvel.angular.z=0.10;
else if (n_head>3.14)
cmdvel.angular.z=-0.10;
else if (n_head<-3.14)
cmdvel.angular.z=0.10;
if (facing && !reached)
{
cmdvel.angular.z=0.0;
if (left_dist>0.5)
{
cmdvel.linear.x=-0.2;
usleep(1000000);
}
else if (left_dist>0.2)
{
cmdvel.linear.x=-0.15;
usleep(1000000);
}
else if (left_dist>0.08)
{
cmdvel.linear.x=-0.05;
usleep(1000000);
}
}
else if (reached)
{
cmdvel.linear.x=0.0;
n_head=0.0;
ROS_INFO("Turtlebot has reached the destination");
}
Hope anybody can help me out. Thank you!