Guest User

Python Script to send fake GLOBAL_POSITION_INT messages

a guest
Aug 17th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. from time import sleep, time
  2. from pymavlink import mavutil
  3. import math
  4.  
  5. # Create the connection
  6. connection_one = mavutil.mavlink_connection('udpin:127.0.0.1:14550', source_system=2)
  7. connection_two = mavutil.mavlink_connection('udpin:127.0.0.1:15550', source_system=3)
  8. connection_three = mavutil.mavlink_connection('udpin:127.0.0.1:16550', source_system=4)
  9.  
  10. # Wait a heartbeat before sending commands
  11. connection_one.wait_heartbeat()
  12. connection_two.wait_heartbeat()
  13. connection_three.wait_heartbeat()
  14.  
  15. start_time = time()
  16.  
  17. def time_since_boot_ms():
  18. return (time() * 1000 - start_time * 1000)
  19.  
  20. ang_vel = (2 * math.pi / 30) # rad / sec
  21. dt = 1 # sec
  22.  
  23. start_angle = [0, math.pi / 2, math.pi]
  24. radius = [10000, 8000, 10000]
  25.  
  26. def xy_on_circle(center, radius, theta_rad):
  27. x = radius * math.cos(theta_rad)
  28. y = radius * math.sin(theta_rad)
  29. x += center[0]
  30. y += center[1]
  31. return (x, y)
  32.  
  33. def theta_now(start_angle, ang_vel):
  34. return start_angle + (ang_vel * time_since_boot_ms() / 1000)
  35.  
  36. while True:
  37. theta = theta_now(start_angle[0], ang_vel) # radians
  38. center = (-353633600, 1491632350) # lat lon
  39. latlon = xy_on_circle(center, radius[0], theta)
  40.  
  41. connection_one.mav.global_position_int_send(
  42. int(time_since_boot_ms()),
  43. int(latlon[0]),
  44. int(latlon[1]),
  45. int(684.9 * 1000), # mm alt amsl
  46. 100 * 1000, # relalt mm UP!
  47. 0, # vx
  48. 0, # vy
  49. 0, # vz
  50. int(math.degrees(theta + (math.pi / 2)) * 100) % (36000)# heading
  51. )
  52.  
  53. theta = theta_now(start_angle[1], ang_vel) # radians
  54. center = (-353654700, 1491662400) # lat lon
  55. latlon = xy_on_circle(center, radius[1], theta)
  56. connection_two.mav.global_position_int_send(
  57. int(time_since_boot_ms()),
  58. int(latlon[0]),
  59. int(latlon[1]),
  60. int(684.9 * 1000), # mm alt amsl
  61. 100 * 1000, # relalt mm UP!
  62. 0, # vx
  63. 0, # vy
  64. 0, # vz
  65. int(math.degrees(theta + (math.pi / 2)) * 100) % (36000) # heading
  66. )
  67.  
  68. theta = theta_now(start_angle[2], ang_vel) # radians
  69. center = (-353602800, 1491661300) # lat lon
  70. latlon = xy_on_circle(center, radius[2], theta)
  71. connection_three.mav.global_position_int_send(
  72. int(time_since_boot_ms()),
  73. int(latlon[0]),
  74. int(latlon[1]),
  75. int(684.9 * 1000), # mm alt amsl
  76. 100 * 1000, # relalt mm UP!
  77. 0, # vx
  78. 0, # vy
  79. 0, # vz
  80. int(math.degrees(theta + (math.pi / 2)) * 100) % (36000)# heading
  81. )
  82. sleep(dt)
Add Comment
Please, Sign In to add comment