paramus

Drone autopilot [Sable]

Jun 15th, 2026 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | Gaming | 0 0
  1. local data = {}
  2. local destination = {x=0, z=0}
  3. local tiltAngle = 10 -- angle the drone will tilt towards objective
  4. local triggerangle = 2 -- margin of angle tilt to consider the drone, stable
  5.  
  6. function TiltTowards()
  7.     local location = data.position
  8.     local velocity = sublevel.getVelocity()
  9.    
  10.     local angleY = data.orientation.v.y * 180
  11.     local angleX = data.orientation.v.x * 180 * angleY/180
  12.     local angleZ = data.orientation.v.z * 180 * angleY/180
  13.    
  14.     --Face destination
  15.     if (math.abs(location.x-destination.x) < 200 and math.abs(location.z-destination.z) < 200) then
  16.         tiltAngle = 3
  17.     end
  18.    
  19.     local enginePower = 0
  20.     if (location.y > -20) then
  21.        
  22.         if (location.x > destination.x) then
  23.             angleX = angleX+tiltAngle
  24.         elseif (location.x < destination.x) then
  25.             angleX = angleX-tiltAngle
  26.         end
  27.  
  28.         if (location.z > destination.z) then
  29.             angleZ = angleZ+tiltAngle
  30.         elseif (location.z < destination.z) then
  31.             angleZ = angleZ-tiltAngle
  32.         end
  33.        
  34.     end
  35.    
  36.     --Landing
  37.     if (math.abs(location.x - destination.x) < 100 and math.abs(location.z - destination.z) < 100) then
  38.         enginePower = 10
  39.     end
  40.    
  41.     -- X stabilization
  42.     if (angleX >= triggerangle) then
  43.         redstone.setAnalogOutput("back", enginePower+1)
  44.     else
  45.         redstone.setAnalogOutput("back", enginePower)
  46.     end
  47.    
  48.     if (angleX <= -triggerangle) then
  49.         redstone.setAnalogOutput("front", enginePower+1)
  50.     else
  51.         redstone.setAnalogOutput("front", enginePower)
  52.     end
  53.    
  54.     -- Y stabilization
  55.     if (angleZ >= triggerangle) then
  56.         redstone.setAnalogOutput("right", enginePower+1)
  57.     else
  58.         redstone.setAnalogOutput("right", enginePower)
  59.     end
  60.    
  61.     if (angleZ <= -triggerangle) then
  62.         redstone.setAnalogOutput("left", enginePower+1)
  63.     else
  64.         redstone.setAnalogOutput("left", enginePower)
  65.     end
  66. end
  67.  
  68. function DisplayInfo()
  69.     local location = data.position
  70.     local velocity = sublevel.getVelocity()
  71.    
  72.     local angleY = data.orientation.v.y * 180
  73.     local angleX = data.orientation.v.x * 180 * angleY/180
  74.     local angleZ = data.orientation.v.z * 180 * angleY/180
  75.    
  76.     term.setCursorPos(1,1)
  77.     print("X:" .. destination.x .. " Z:" .. destination.z)
  78.    
  79.     term.setCursorPos(2,4)
  80.     print("Direction / Angle / Speed")
  81.     term.setCursorPos(2,5)
  82.     print("North / " .. angleX .. " / " .. velocity.x)
  83.     term.setCursorPos(2,6)
  84.     print("South / " .. -angleX .. " / " .. -velocity.x)
  85.     term.setCursorPos(2,7)
  86.     print("East / " .. angleZ .. " / " .. velocity.z)
  87.     term.setCursorPos(2,8)
  88.     print("West / " .. -angleZ .. " / " .. -velocity.z)
  89. end
  90.  
  91. print("Write destination coordinates.")
  92. print("X:")
  93. destination.x = tonumber(read())
  94. print("Z:")
  95. destination.z = tonumber(read())
  96.  
  97. while (true) do
  98.     data = sublevel.getLogicalPose()
  99.     TiltTowards()
  100.     DisplayInfo()
  101.     sleep(0.1)
  102. end
Advertisement
Add Comment
Please, Sign In to add comment