Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import rospy
- import time
- from geometry_msgs.msg import Twist
- from turtlesim.srv import TeleportAbsolute
- def move_turtle(turns):
- rospy.init_node('move_turtle_node', anonymous=True)
- velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
- teleport_service = rospy.ServiceProxy('/turtle1/teleport_absolute', TeleportAbsolute)
- rate = rospy.Rate(10)
- # Rotate until the turtle is facing north
- while not rospy.is_shutdown():
- turns ="Left"
- vel_msg = Twist()
- vel_msg.angular.z = 1.0 # Adjust the angular speed as per your requirement
- velocity_publisher.publish(vel_msg)
- # Check if the desired angle is reached (with a tolerance of 0.01 radians)
- if rospy.Time.now().to_sec() > 2.0: # Adjust the time as per your requirement
- break
- rate.sleep()
- for turn in turns:
- turn_command = turn.split()[0].strip() # Extract the first word as the turn command
- if turn_command == "Left":
- twist = Twist()
- twist.angular.z = 1 # Angular velocity for left turn
- duration = rospy.Duration(1.5) # Duration for turning (adjust as needed)
- start_time = rospy.Time.now()
- while rospy.Time.now() - start_time < duration:
- velocity_publisher.publish(twist)
- rate.sleep()
- twist.angular.z = 0.0
- velocity_publisher.publish(twist)
- # Delay for three second
- time.sleep(3)
- # Going straight
- twist.linear.x = 0.3 # Linear velocity for going straight
- duration = rospy.Duration(2.0) # Duration for going straight (adjust as needed)
- start_time = rospy.Time.now()
- while rospy.Time.now() - start_time < duration:
- velocity_publisher.publish(twist)
- rate.sleep()
- # Stop the turtle
- twist.linear.x = 0
- velocity_publisher.publish(twist)
- elif turn_command == "Right":
- twist = Twist()
- twist.angular.z = -1 # Angular velocity for right turn
- duration = rospy.Duration(1.5) # Duration for turning (adjust as needed)
- start_time = rospy.Time.now()
- while rospy.Time.now() - start_time < duration:
- velocity_publisher.publish(twist)
- rate.sleep()
- twist.angular.z = 0
- velocity_publisher.publish(twist)
- # Delay for three second
- time.sleep(3)
- # Going straight
- twist.linear.x = 0.3 # Linear velocity for going straight
- duration = rospy.Duration(2.0) # Duration for going straight (adjust as needed)
- start_time = rospy.Time.now()
- while rospy.Time.now() - start_time < duration:
- velocity_publisher.publish(twist)
- rate.sleep()
- # Stop the turtle
- twist.linear.x = 0
- velocity_publisher.publish(twist)
- elif turn_command == "Straight":
- twist = Twist()
- twist.linear.x = 0.3 # Linear velocity for going straight
- duration = rospy.Duration(2.0) # Duration for going straight (adjust as needed)
- start_time = rospy.Time.now()
- while rospy.Time.now() - start_time < duration:
- velocity_publisher.publish(twist)
- rate.sleep()
- # Stop the turtle
- twist.linear.x = 0
- velocity_publisher.publish(twist)
- elif turn_command == "Stop":
- twist = Twist()
- twist.linear.x = 0.0
- twist.angular.z = 0
- velocity_publisher.publish(twist)
- else:
- rospy.logerr("Invalid turn command: {}".format(turn_command))
- if __name__ == '__main__':
- turns = []
- with open('/home/atharva/catkin_ws/src/task_3/src/task.txt', 'r') as file:
- lines = file.readlines()
- for line in lines:
- row = line.strip()
- turns.append(row)
- move_turtle(turns)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement