Turtle run = ### ==crate a new package== ``` $ cd ~/catkin_ws/src $ catkin_create_pkg trtlesim_cleaner geometry_msg rospy ``` ### ==build your workspace== ``` $ cd ~/catkin_ws $ catkin_make ``` ### ==create a src folder for your scripts== ``` $ cd ~/catkin_ws/src/turtlesim_cleaner $ mkdir src $ cd ~/catkin_ws $ catkin_make ``` ## ==CODE== Create your **shape.py** file and save it in your **~/catkin_ws/src/turtlesim_cleaner/src** ```python= #!/usr/bin/env python import rospy from geometry_msgs.msg import Twist PI = 3.1415926535897 def shape(): #Starts a new node rospy.init_node('robot_cleaner', anonymous=True) velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) vel_msg = Twist() # Receiveing the user's input speed = input("Input your speed:") shape=input("Input your shape:") distance = input("Type your distance:") time=input("Input time:") angle=360/shape #Converting from angles to radians angular_speed = speed*20*2*PI/360 relative_angle = angle*2*PI/360 #We wont use linear components vel_msg.linear.y=0 vel_msg.linear.z=0 vel_msg.angular.x = 0 vel_msg.angular.y = 0 # Setting the current time for distance calculus for i in range(0,int(shape)*int(time)): t0 = rospy.Time.now().to_sec() current_angle = 0 current_distance = 0 while(current_distance < distance): vel_msg.linear.x = abs(speed) vel_msg.angular.z = 0 velocity_publisher.publish(vel_msg) tr1=rospy.Time.now().to_sec() current_distance= speed*(tr1-t0) while(current_angle < relative_angle): vel_msg.linear.x=0 vel_msg.angular.z = -abs(angular_speed) velocity_publisher.publish(vel_msg) tr2 = rospy.Time.now().to_sec() current_angle = angular_speed*(tr2-tr1) vel_msg.linear.x=0 vel_msg.angular.z = 0 velocity_publisher.publish(vel_msg) #rospy.spin() if __name__ == '__main__': try: # Testing our function shape() except rospy.ROSInterruptException: pass ``` ### ==make your node executable== ``` $ chmod u+x ~/catkin_ws/src/turtlesim_cleaner/src/shape.py $ source devel/setup.bash ``` ### ==Test== In a new **terminal**,run ``` $ roscore ``` In a new **terminal**,run ``` $ rosrun turtlesim turtlesim_node ``` like this: ![](https://i.imgur.com/528VfaA.png) In a new **terminal**,run our code ``` $ rosrun turtlesim_cleaner shape.py ``` type your inputs ``` input("Input your speed:"):10 input("Input your shape:"):6 input("Type your distance:"):2 input("Input time:"):1 ``` The turtle will move like this: ![](https://i.imgur.com/azrX4AQ.png)