Herowaree

Messi > CR7

Mar 13th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import rospkg
  2. import nav_msgs
  3.  
  4. from geometry_msgs.msg import PoseStamped
  5.  
  6. from pathlib import Path # Possible conflict with nav_msgs.msg.Path
  7.  
  8. def on_init(): # Put
  9. # Timer
  10. self.timer_pub_path = rospy.Timer(rospy.Rate(5), callback_timer_publish_path)
  11. # Publisher
  12. self.pub_path = rospy.Publisher('/turtlebot_path', nav_msgs.msg.Path, 1)
  13.  
  14. # Timer to publish the path with a frequency of 5 Hz
  15. def callback_timer_publish_path(self, event):
  16. self.pub_path.publish(self.path)
  17.  
  18. def load_trajectory_from_file(self):
  19. # TODO: MAKE THE LAUNCH LOAD THE TRAJECTORY FILE (https://riptutorial.com/ros/example/24423/launch-ros-nodes-and-load-parameters-from-yaml-file)
  20. # Get path to the package directory
  21. rospack = rospkg.RosPack()
  22. my_package_path = rospack.get_path('your_package_name')
  23. print(my_package_path)
  24.  
  25. # Get path to the trajectory file
  26. my_filename = "eight_shape_trajectory_r15.txt"
  27. file_path = Path(my_package_path + '/config/sample_trajectories/') / my_filename
  28. print(file_path)
  29.  
  30. # Read trajectory file
  31. with open(file_path) as f:
  32. contents = f.readlines()
  33. print(contents)
  34.  
  35. # Create path message
  36. self.path = nav_msgs.msg.Path()
  37. self.path.header.frame_id = 'map'
  38. self.path.header.stamp = rospy.Time.now
  39.  
  40. for line in contents:
  41. # Convert each line into a geometry_msgs/Pose message
  42. splitted_line = line.split(" ")
  43. x = float(splitted_line[0])
  44. y = float(splitted_line[1])
  45. heading = float(splitted_line[2])
  46.  
  47. pose_stamped = PoseStamped()
  48. pose_stamped.pose.position.x = x
  49. pose_stamped.pose.position.y = y
  50. pose_stamped.pose.position.z = 0
  51.  
  52. quat = quaternion_from_euler(0, 0, heading)
  53. pose_stamped.pose.orientation = Quaternion(quat[0], quat[1], quat[2], quat[3])
  54.  
  55. # Save trajectory into a nav_msgs/Path message
  56. self.path.poses.append(pose_stamped)
Advertisement
Add Comment
Please, Sign In to add comment