Advertisement
fuccpuff

Untitled

Nov 19th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import rospy
  5. from clover import srv
  6. from std_srvs.srv import Trigger
  7. import math
  8. import csv
  9.  
  10. rospy.init_node('flight')
  11.  
  12. # Define service proxies
  13. get_telemetry = rospy.ServiceProxy('get_telemetry', srv.GetTelemetry)
  14. navigate = rospy.ServiceProxy('navigate', srv.Navigate)
  15. land = rospy.ServiceProxy('land', Trigger)
  16.  
  17. def wait_arrival(tolerance=0.2, debug=False, log_writer=None):
  18. """
  19. Waits until the drone reaches the target position within a certain tolerance.
  20. Logs telemetry data to the provided CSV writer during the wait.
  21. """
  22. rate = rospy.Rate(5) # 5 Hz
  23. while not rospy.is_shutdown():
  24. telem = get_telemetry(frame_id='navigate_target')
  25. distance = math.sqrt(telem.x ** 2 + telem.y ** 2 + telem.z ** 2)
  26.  
  27. if debug:
  28. rospy.loginfo(f"Current distance to target: {distance}")
  29.  
  30. if log_writer:
  31. global_telem = get_telemetry(frame_id='map')
  32. log_writer.writerow([rospy.get_time(), global_telem.x, global_telem.y, global_telem.z, global_telem.yaw])
  33.  
  34. if distance < tolerance:
  35. break
  36. rate.sleep()
  37. rospy.sleep(2)
  38.  
  39. def navigate_and_wait(x, y, z, frame_id='body', auto_arm=False, debug=False, log_writer=None):
  40. """
  41. Sends the drone to the specified coordinates and waits until arrival.
  42. """
  43. navigate(x=x, y=y, z=z, frame_id=frame_id, auto_arm=auto_arm)
  44. if debug:
  45. rospy.loginfo(f"Navigating to: x={x}, y={y}, z={z}, frame_id={frame_id}")
  46. wait_arrival(debug=debug, log_writer=log_writer)
  47.  
  48. # Main mission loop
  49. for flight_num in range(1, 4):
  50. rospy.loginfo(f"Starting flight test {flight_num}")
  51.  
  52. filename = f'flight_log_{flight_num}.csv'
  53. with open(filename, 'w', newline='') as csvfile:
  54. log_writer = csv.writer(csvfile)
  55. log_writer.writerow(['time', 'x', 'y', 'z', 'yaw']) # CSV header
  56.  
  57. # Flight mission
  58. # 1. Take off to 1 meter
  59. navigate_and_wait(x=0, y=0, z=1, frame_id='body', auto_arm=True, debug=True, log_writer=log_writer)
  60. rospy.loginfo("Reached altitude of 1 meter")
  61.  
  62. # 2. Fly to (1, 0, 1)
  63. navigate_and_wait(x=1, y=0, z=1, frame_id='map', debug=True, log_writer=log_writer)
  64. rospy.loginfo("Reached point (1, 0, 1)")
  65.  
  66. # 3. Fly to (1, 1, 1)
  67. navigate_and_wait(x=1, y=1, z=1, frame_id='map', debug=True, log_writer=log_writer)
  68. rospy.loginfo("Reached point (1, 1, 1)")
  69.  
  70. # 4. Fly to (0, 1, 1)
  71. navigate_and_wait(x=0, y=1, z=1, frame_id='map', debug=True, log_writer=log_writer)
  72. rospy.loginfo("Reached point (0, 1, 1)")
  73.  
  74. # 5. Land at (0, 0, 0)
  75. navigate_and_wait(x=0, y=0, z=0.5, frame_id='map', debug=True, log_writer=log_writer)
  76. land()
  77. rospy.loginfo("Landed at (0, 0, 0)")
  78.  
  79. rospy.loginfo(f"Completed flight test {flight_num}")
  80. rospy.sleep(5)
  81.  
  82. # Analyze flight data
  83. for flight_num in range(1, 4):
  84. filename = f'flight_log_{flight_num}.csv'
  85. rospy.loginfo(f"Analyzing flight data from {filename}")
  86.  
  87. with open(filename, 'r') as csvfile:
  88. log_reader = csv.reader(csvfile)
  89. next(log_reader) # Skip header
  90.  
  91. times = []
  92. xs = []
  93. ys = []
  94. zs = []
  95. yaws = []
  96.  
  97. for row in log_reader:
  98. times.append(float(row[0]))
  99. xs.append(float(row[1]))
  100. ys.append(float(row[2]))
  101. zs.append(float(row[3]))
  102. yaws.append(float(row[4]))
  103.  
  104. if times:
  105. total_time = times[-1] - times[0]
  106. rospy.loginfo(f"Flight {flight_num} total time: {total_time:.2f} seconds")
  107.  
  108. total_distance = 0
  109. for i in range(1, len(xs)):
  110. dx = xs[i] - xs[i-1]
  111. dy = ys[i] - ys[i-1]
  112. dz = zs[i] - zs[i-1]
  113. total_distance += math.sqrt(dx**2 + dy**2 + dz**2)
  114. rospy.loginfo(f"Flight {flight_num} total distance: {total_distance:.2f} meters")
  115.  
  116. average_speed = total_distance / total_time if total_time > 0 else 0
  117. rospy.loginfo(f"Flight {flight_num} average speed: {average_speed:.2f} m/s")
  118. else:
  119. rospy.loginfo(f"No data recorded for flight {flight_num}")
  120.  
  121. rospy.loginfo("Mission completed")
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement