00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "ros/ros.h"
00010 #include "geometry_msgs/Twist.h"
00011 #include "turtlesim/Pose.h"
00012 #include <sstream>
00013
00014 using namespace std;
00015
00016 ros::Publisher velocity_publisher;
00017 ros::Subscriber pose_subscriber;
00018 turtlesim::Pose turtlesim_pose;
00019
00020
00021 const double x_min = 0.0;
00022 const double y_min = 0.0;
00023 const double x_max = 11.0;
00024 const double y_max = 11.0;
00025
00026 const double PI = 3.14159265359;
00027
00028 void move(double speed, double distance, bool isForward);
00029 void rotate (double angular_speed, double angle, bool clockwise);
00030 double degrees2radians(double angle_in_degrees);
00031 void setDesiredOrientation (double desired_angle_radians);
00032 void poseCallback(const turtlesim::Pose::ConstPtr & pose_message);
00033 void moveGoal(turtlesim::Pose goal_pose, double distance_tolerance);
00034 void gridClean();
00035 void spiralClean();
00036
00037 int main(int argc, char **argv)
00038 {
00039
00040 ros::init(argc, argv, "turtlesim_cleaner");
00041 ros::NodeHandle n;
00042 double speed, angular_speed;
00043 double distance, angle;
00044 bool isForward, clockwise;
00045
00046
00047 velocity_publisher = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 1000);
00048 pose_subscriber = n.subscribe("/turtle1/pose", 10, poseCallback);
00049
00050
00051
00052
00053
00055 ROS_INFO("\n\n\n******START TESTING************\n");
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 ros::Rate loop_rate(0.5);
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 ros::Rate loop(0.5);
00091 turtlesim::Pose pose;
00092 pose.x=1;
00093 pose.y=1;
00094 pose.theta=0;
00095 moveGoal(pose, 0.01);
00096
00097 pose.x=6;
00098 pose.y=6;
00099 pose.theta=0;
00100 moveGoal(pose, 0.01);
00101
00102
00103
00104 loop.sleep();
00105
00106
00107 ros::spin();
00108
00109 return 0;
00110 }
00111
00116 void move(double speed, double distance, bool isForward){
00117 geometry_msgs::Twist vel_msg;
00118
00119 if (isForward)
00120 vel_msg.linear.x =abs(speed);
00121 else
00122 vel_msg.linear.x =-abs(speed);
00123 vel_msg.linear.y =0;
00124 vel_msg.linear.z =0;
00125
00126 vel_msg.angular.x = 0;
00127 vel_msg.angular.y = 0;
00128 vel_msg.angular.z =0;
00129
00130 double t0 = ros::Time::now().toSec();
00131 double current_distance = 0.0;
00132 ros::Rate loop_rate(100);
00133 do{
00134 velocity_publisher.publish(vel_msg);
00135 double t1 = ros::Time::now().toSec();
00136 current_distance = speed * (t1-t0);
00137 ros::spinOnce();
00138 loop_rate.sleep();
00139
00140 }while(current_distance<distance);
00141 vel_msg.linear.x =0;
00142 velocity_publisher.publish(vel_msg);
00143
00144 }
00145
00146
00147 void rotate (double angular_speed, double relative_angle, bool clockwise){
00148
00149 geometry_msgs::Twist vel_msg;
00150
00151 vel_msg.linear.x =0;
00152 vel_msg.linear.y =0;
00153 vel_msg.linear.z =0;
00154
00155 vel_msg.angular.x = 0;
00156 vel_msg.angular.y = 0;
00157
00158 if (clockwise)
00159 vel_msg.angular.z =-abs(angular_speed);
00160 else
00161 vel_msg.angular.z =abs(angular_speed);
00162
00163 double current_angle = 0.0;
00164 double t0 = ros::Time::now().toSec();
00165 ros::Rate loop_rate(10);
00166 do{
00167 velocity_publisher.publish(vel_msg);
00168 double t1 = ros::Time::now().toSec();
00169 current_angle = angular_speed * (t1-t0);
00170 ros::spinOnce();
00171 loop_rate.sleep();
00172 }while(current_angle<relative_angle);
00173
00174 vel_msg.angular.z =0;
00175 velocity_publisher.publish(vel_msg);
00176
00177 }
00178
00179 double degrees2radians(double angle_in_degrees){
00180 return angle_in_degrees *PI /180.0;
00181 }
00182
00183
00184 void setDesiredOrientation (double desired_angle_radians){
00185 double relative_angle_radians = desired_angle_radians - turtlesim_pose.theta;
00186 bool clockwise = ((relative_angle_radians<0)?true:false);
00187
00188 rotate (degrees2radians(10), abs(relative_angle_radians), clockwise);
00189
00190 }
00191
00192 void poseCallback(const turtlesim::Pose::ConstPtr & pose_message){
00193 turtlesim_pose.x=pose_message->x;
00194 turtlesim_pose.y=pose_message->y;
00195 turtlesim_pose.theta=pose_message->theta;
00196 }
00197
00198 double getDistance(double x1, double y1, double x2, double y2){
00199 return sqrt(pow((x1-x2),2)+pow((y1-y2),2));
00200 }
00201
00202 void moveGoal(turtlesim::Pose goal_pose, double distance_tolerance){
00203
00204 geometry_msgs::Twist vel_msg;
00205
00206 ros::Rate loop_rate(100);
00207 double E = 0.0;
00208 do{
00209
00210
00211 double Kp=1.0;
00212 double Ki=0.02;
00213
00214
00215 double e = getDistance(turtlesim_pose.x, turtlesim_pose.y, goal_pose.x, goal_pose.y);
00216 double E = E+e;
00217
00218 vel_msg.linear.x = (Kp*e);
00219 vel_msg.linear.y =0;
00220 vel_msg.linear.z =0;
00221
00222 vel_msg.angular.x = 0;
00223 vel_msg.angular.y = 0;
00224 vel_msg.angular.z =4*(atan2(goal_pose.y-turtlesim_pose.y, goal_pose.x-turtlesim_pose.x)-turtlesim_pose.theta);
00225
00226 velocity_publisher.publish(vel_msg);
00227
00228 ros::spinOnce();
00229 loop_rate.sleep();
00230
00231 }while(getDistance(turtlesim_pose.x, turtlesim_pose.y, goal_pose.x, goal_pose.y)>distance_tolerance);
00232 cout<<"end move goal"<<endl;
00233 vel_msg.linear.x =0;
00234 vel_msg.angular.z = 0;
00235 velocity_publisher.publish(vel_msg);
00236 }
00237
00238
00239
00240 void gridClean(){
00241
00242 ros::Rate loop(0.5);
00243 turtlesim::Pose pose;
00244 pose.x=1;
00245 pose.y=1;
00246 pose.theta=0;
00247 moveGoal(pose, 0.01);
00248 loop.sleep();
00249 setDesiredOrientation(0);
00250 loop.sleep();
00251
00252 move(2, 9, true);
00253 loop.sleep();
00254 rotate(degrees2radians(10), degrees2radians(90), false);
00255 loop.sleep();
00256 move(2, 9, true);
00257
00258
00259 rotate(degrees2radians(10), degrees2radians(90), false);
00260 loop.sleep();
00261 move(2, 1, true);
00262 rotate(degrees2radians(10), degrees2radians(90), false);
00263 loop.sleep();
00264 move(2, 9, true);
00265
00266 rotate(degrees2radians(30), degrees2radians(90), true);
00267 loop.sleep();
00268 move(2, 1, true);
00269 rotate(degrees2radians(30), degrees2radians(90), true);
00270 loop.sleep();
00271 move(2, 9, true);
00272
00273
00274 double distance = getDistance(turtlesim_pose.x, turtlesim_pose.y, x_max, y_max);
00275
00276 }
00277
00278
00279 void spiralClean(){
00280 geometry_msgs::Twist vel_msg;
00281 double count =0;
00282
00283 double constant_speed=4;
00284 double vk = 1;
00285 double wk = 2;
00286 double rk = 0.5;
00287 ros::Rate loop(1);
00288
00289 do{
00290 rk=rk+1.0;
00291 vel_msg.linear.x =rk;
00292 vel_msg.linear.y =0;
00293 vel_msg.linear.z =0;
00294
00295 vel_msg.angular.x = 0;
00296 vel_msg.angular.y = 0;
00297 vel_msg.angular.z =constant_speed;
00298
00299 cout<<"vel_msg.linear.x = "<<vel_msg.linear.x<<endl;
00300 cout<<"vel_msg.angular.z = "<<vel_msg.angular.z<<endl;
00301 velocity_publisher.publish(vel_msg);
00302 ros::spinOnce();
00303
00304 loop.sleep();
00305
00306
00307
00308 cout<<rk<<", "<<vk <<", "<<wk<<endl;
00309 }while((turtlesim_pose.x<10.5)&&(turtlesim_pose.y<10.5));
00310 vel_msg.linear.x =0;
00311 velocity_publisher.publish(vel_msg);
00312 }
00313
00314
00315
00316
00317