Guest User

startup.lua

a guest
May 16th, 2026
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. local altitude = peripheral.find("altitude_sensor")
  2.  
  3. local relay = peripheral.find("redstone_relay")
  4.  
  5. local speedRotation = peripheral.find("Create_RotationSpeedController")
  6.  
  7. local swivel = peripheral.find("swivel_bearing")
  8.  
  9. local PID = require ".pid"
  10.  
  11.  
  12.  
  13. term.clear()
  14.  
  15. term.setCursorPos(1, 1)
  16.  
  17.  
  18.  
  19. local TARGET_HEIGHT = 100
  20.  
  21. local TARGET_ANGLE  = 0
  22.  
  23.  
  24.  
  25. parallel.waitForAny(
  26.  
  27.     -- Module: AutoHeight
  28.  
  29.     function ()
  30.  
  31.         local burnerSide = "top"
  32.  
  33.  
  34.  
  35.         local lastHeight = altitude.getHeight()
  36.  
  37.         local lastTime = os.clock()
  38.  
  39.  
  40.  
  41.         local velocity = 0
  42.  
  43.  
  44.  
  45.         local DEADZONE = 2           -- зона покоя
  46.  
  47.         local STRONG_ZONE = 40       -- сильный недолёт
  48.  
  49.         local PREDICT_TIME = 6       -- прогноз вперёд
  50.  
  51.         local MAX_BURN = 4           -- максимум работы горелки
  52.  
  53.         local MIN_BURN = 0.15        -- минимальный импульс
  54.  
  55.  
  56.  
  57.         local function burner(state)
  58.  
  59.             redstone.setOutput(burnerSide, state)
  60.  
  61.         end
  62.  
  63.  
  64.  
  65.         while true do
  66.  
  67.             local now = os.clock()
  68.  
  69.             local dt = now - lastTime
  70.  
  71.             lastTime = now
  72.  
  73.             local height = altitude.getHeight()
  74.  
  75.             velocity = (height - lastHeight) / dt
  76.  
  77.             lastHeight = height
  78.  
  79.             local predictedHeight = height + velocity * PREDICT_TIME
  80.  
  81.             local error = TARGET_HEIGHT - predictedHeight
  82.  
  83.             if velocity > 3 and height > TARGET_HEIGHT then
  84.  
  85.                 burner(false)
  86.  
  87.             elseif math.abs(error) <= DEADZONE then
  88.  
  89.                 burner(false)
  90.  
  91.             elseif error > 0 then
  92.  
  93.  
  94.  
  95.                 local burnTime
  96.  
  97.                 if error > STRONG_ZONE then
  98.  
  99.                     burnTime = MAX_BURN
  100.  
  101.                 else
  102.  
  103.                     burnTime = math.max(
  104.  
  105.                         MIN_BURN,
  106.  
  107.                         math.min(MAX_BURN, error * 0.5)
  108.  
  109.                     )
  110.  
  111.                 end
  112.  
  113.  
  114.  
  115.                 burner(true)
  116.  
  117.                 sleep(burnTime)
  118.  
  119.                 burner(false)
  120.  
  121.             else
  122.  
  123.                 burner(false)
  124.  
  125.             end
  126.  
  127.             sleep(0.05)
  128.  
  129.         end
  130.  
  131.     end,
  132.  
  133.  
  134.  
  135.     -- Module: AutoAngle
  136.  
  137.     function ()
  138.  
  139.         local pid = PID:new(0.5, 0.001, 0.5, -5, 5)
  140.  
  141.         local block = false
  142.  
  143.         while true do
  144.  
  145.             local angle = swivel.getTargetAngle()
  146.  
  147.             local raw = pid:compute(TARGET_ANGLE, angle, 1)
  148.  
  149.             local limited = -math.max(-20, math.min(raw, 20))
  150.  
  151.             if math.abs(limited) > 0.02 and math.abs(limited) < 1 then
  152.  
  153.                 if block == false then
  154.  
  155.                     if (limited > 0) then
  156.  
  157.                         speedRotation.setTargetSpeed(math.ceil(limited))
  158.  
  159.                     else
  160.  
  161.                         speedRotation.setTargetSpeed(math.floor(limited))
  162.  
  163.                     end
  164.  
  165.                     block = true
  166.  
  167.                 else
  168.  
  169.                     speedRotation.setTargetSpeed(0)
  170.  
  171.                 end
  172.  
  173.             else
  174.  
  175.                 if block then block = false end
  176.  
  177.                 speedRotation.setTargetSpeed(limited)
  178.  
  179.             end
  180.  
  181.             sleep(0.1)
  182.  
  183.         end
  184.  
  185.     end,
  186.  
  187.  
  188.  
  189.     -- Module: Command handler + auto‑навигация
  190.  
  191.     function ()
  192.  
  193.         local lastCommand = ""
  194.  
  195.         while true do
  196.  
  197.             local cmd = read()
  198.  
  199.             if cmd and cmd ~= "" and cmd ~= lastCommand then
  200.  
  201.                 lastCommand = cmd
  202.  
  203.                 local colonPos = string.find(cmd, ":")
  204.  
  205.                 if colonPos then
  206.  
  207.                     local param = string.sub(cmd, 1, colonPos-1):lower()
  208.  
  209.                     local valueStr = string.sub(cmd, colonPos+1)
  210.  
  211.  
  212.  
  213.                     -- Обработка auto:x,z
  214.  
  215.                     if param == "auto" then
  216.  
  217.                         local commaPos = string.find(valueStr, ",")
  218.  
  219.                         if commaPos then
  220.  
  221.                             local targetX = tonumber(string.sub(valueStr, 1, commaPos-1))
  222.  
  223.                             local targetZ = tonumber(string.sub(valueStr, commaPos+1))
  224.  
  225.                             if targetX and targetZ and sublevel then
  226.  
  227.                                 while true do
  228.  
  229.                                     local pose = sublevel.getLogicalPose()
  230.  
  231.                                     local curX = pose.position.x
  232.  
  233.                                     local curZ = pose.position.z
  234.  
  235.                                     local w = pose.orientation.a
  236.  
  237.                                     local x = pose.orientation.v.x
  238.  
  239.                                     local y = pose.orientation.v.y
  240.  
  241.                                     local z = pose.orientation.v.z
  242.  
  243.  
  244.  
  245.                                     local siny_cosp = 2 * (w*y + x*z)
  246.  
  247.                                     local cosy_cosp = 1 - 2 * (y*y + z*z)
  248.  
  249.                                     local yaw = math.deg(math.atan(siny_cosp, cosy_cosp))
  250.  
  251.                                    
  252.  
  253.                                     local direction = { x = targetX - curX, z = targetZ - curZ }
  254.  
  255.                                     local length = math.sqrt(direction.x^2+direction.z^2)
  256.  
  257.                                     local normilized = { x = direction.x/length, z = direction.z/length }
  258.  
  259.                                     local art = math.atan(normilized.x, normilized.z) * (180 / math.pi)
  260.  
  261.                                     local error = yaw - art
  262.  
  263.  
  264.  
  265.                                     TARGET_ANGLE = error * 2
  266.  
  267.                                     print(string.format("art=%.1f, yaw=%.1f, TARGET_ANGLE=%.1f", art, yaw, TARGET_ANGLE))
  268.  
  269.                                     sleep(0.05)
  270.  
  271.                                 end
  272.  
  273.                             else
  274.  
  275.                                 print("Error")
  276.  
  277.                             end
  278.  
  279.                         end
  280.  
  281.                     else
  282.  
  283.                         local value = tonumber(valueStr)
  284.  
  285.                         if value then
  286.  
  287.                             if param == "a" or param == "angle" then
  288.  
  289.                                 TARGET_ANGLE = value
  290.  
  291.                             elseif param == "h" or param == "height" then
  292.  
  293.                                 TARGET_HEIGHT = value
  294.  
  295.                             end
  296.  
  297.                         end
  298.  
  299.                     end
  300.  
  301.                 end
  302.  
  303.             end
  304.  
  305.         end
  306.  
  307.     end
  308.  
  309. )
Add Comment
Please, Sign In to add comment