Advertisement
rikisu_

Untitled

Oct 16th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. def pid(self):
  2. # ... (previous PID code)
  3.  
  4. # Calculate distance to the next waypoint
  5. distance_to_waypoint = math.sqrt(
  6. (self.drone_position[0] - self.setpoint[0]) ** 2 +
  7. (self.drone_position[1] - self.setpoint[1]) ** 2 +
  8. (self.drone_position[2] - self.setpoint[2]) ** 2
  9. )
  10.  
  11. # Adjust speed based on distance to waypoint
  12. if distance_to_waypoint > 1.0: # Example threshold for reducing speed (adjust as needed)
  13. self.speed_scaling_factor = 0.5 # Reduce speed when far from the waypoint
  14. else:
  15. self.speed_scaling_factor = 1.0 # Normal speed when close to the waypoint
  16.  
  17. # Calculate PID control commands and scale them by the speed scaling factor
  18. self.cmd.rcRoll = int(1500 + self.error[0] * self.Kp[0] * self.speed_scaling_factor + ...)
  19. self.cmd.rcPitch = int(1500 + self.error[1] * self.Kp[1] * self.speed_scaling_factor + ...)
  20. self.cmd.rcThrottle = int(1554 + self.error[2] * self.Kp[2] * self.speed_scaling_factor + ...)
  21.  
  22. # ... (rest of the PID code)
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. def pid(self):
  36. # ... (previous PID code)
  37.  
  38. # Calculate distance to the next waypoint
  39. distance_to_waypoint = math.sqrt(
  40. (self.drone_position[0] - self.setpoints[self.current_setpoint_index][0]) ** 2 +
  41. (self.drone_position[1] - self.setpoints[self.current_setpoint_index][1]) ** 2 +
  42. (self.drone_position[2] - self.setpoints[self.current_setpoint_index][2]) ** 2
  43. )
  44.  
  45. # Check if the drone is within the error range of ±0.2m for the current setpoint
  46. error_range = 0.2
  47. current_setpoint = self.setpoints[self.current_setpoint_index]
  48. if (
  49. abs(self.drone_position[0] - current_setpoint[0]) <= error_range and
  50. abs(self.drone_position[1] - current_setpoint[1]) <= error_range and
  51. abs(self.drone_position[2] - current_setpoint[2]) <= error_range
  52. ):
  53. # Drone is within the error range, move to the next setpoint
  54. self.current_setpoint_index += 1
  55. if self.current_setpoint_index < len(self.setpoints):
  56. # Update setpoint to the next waypoint
  57. self.setpoint = self.setpoints[self.current_setpoint_index]
  58. rospy.loginfo(f"Reached waypoint {self.current_setpoint_index}: {self.setpoint}")
  59. else:
  60. # All waypoints reached
  61. rospy.loginfo("All waypoints reached!")
  62. # Stop the drone or change state as needed
  63.  
  64. # Adjust speed based on distance to waypoint
  65. if distance_to_waypoint > 1.0: # Example threshold for reducing speed (adjust as needed)
  66. self.speed_scaling_factor = 0.5 # Reduce speed when far from the waypoint
  67. else:
  68. self.speed_scaling_factor = 1.0 # Normal speed when close to the waypoint
  69.  
  70. # Calculate PID control commands and scale them by the speed scaling factor
  71. self.cmd.rcRoll = int(1500 + self.error[0] * self.Kp[0] * self.speed_scaling_factor + ...)
  72. self.cmd.rcPitch = int(1500 + self.error[1] * self.Kp[1] * self.speed_scaling_factor + ...)
  73. self.cmd.rcThrottle = int(1554 + self.error[2] * self.Kp[2] * self.speed_scaling_factor + ...)
  74.  
  75. # ... (rest of the PID code)
  76.  
  77.  
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement