MegiRicky

Y-20Control

Oct 23rd, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. local leftRouter = peripheral.wrap("left")
  2. local rightRouter = peripheral.wrap("right")
  3. local modem = peripheral.find("modem")
  4.  
  5. local throttle = 0
  6. local maintainHeight = 200
  7.  
  8.  
  9. local Kp_hover, Ki_hover, Kd_hover = 0.6, 1, 0.5
  10. local yVelError = 0
  11. local yVelIntegral = 0
  12. local yVelPrevError = 0
  13.  
  14. print("Input the controlChannel, default = 1200")
  15. local controlChannel = io.read()
  16. if controlChannel == "" then
  17. controlChannel = 1200
  18. end
  19. controlChannel = tonumber(controlChannel)
  20. modem.open(controlChannel)
  21.  
  22. redstone.setOutput("top", false)
  23. redstone.setOutput("front",false)
  24. leftRouter.setOutput("top", false)
  25. leftRouter.setOutput("front",false)
  26. rightRouter.setOutput("top", false)
  27. rightRouter.setOutput("front",false)
  28. redstone.setAnalogOutput("back",0)
  29.  
  30. local function modemMessage()
  31. while true do
  32. if modem then
  33. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  34. if channel == cannonHitPosChannel then
  35. cannonHitPos = message
  36. elseif channel == controlChannel then
  37. controls = message
  38. end
  39. else
  40. sleep()
  41. end
  42. end
  43. end
  44.  
  45. local function manualControlPitch()
  46. while true do
  47. if controls and not (controls.hoverMode) then
  48. if controls.pitchUp then
  49. redstone.setOutput("top", true)
  50. elseif controls.pitchDown then
  51. redstone.setOutput("front", true)
  52. else
  53. redstone.setOutput("top", false)
  54. redstone.setOutput("front", false)
  55. end
  56. end
  57. sleep(0.05) -- Reduce sleep time for higher responsiveness
  58. end
  59. end
  60.  
  61. local function manualControlYaw()
  62. while true do
  63. if controls and not (controls.hoverMode) then
  64. if controls.yawLeft then
  65. leftRouter.setOutput("top", true)
  66. elseif controls.yawRight then
  67. leftRouter.setOutput("front", true)
  68. else
  69. leftRouter.setOutput("top", false)
  70. leftRouter.setOutput("front", false)
  71. end
  72. end
  73. sleep(0.05) -- Reduce sleep time for higher responsiveness
  74. end
  75. end
  76.  
  77. local function manualControlRoll()
  78. while true do
  79. if controls and not (controls.hoverMode) then
  80. if controls.rollLeft then
  81. rightRouter.setOutput("top", true)
  82. elseif controls.rollRight then
  83. rightRouter.setOutput("front", true)
  84. else
  85. rightRouter.setOutput("top", false)
  86. rightRouter.setOutput("front", false)
  87. end
  88. end
  89. sleep(0.05) -- Reduce sleep time for higher responsiveness
  90. end
  91. end
  92.  
  93. local function manualThrottle()
  94. while true do
  95. if controls and not (controls.hoverMode) then
  96. if controls.throttleUp then
  97. throttle = throttle + 1
  98. elseif controls.throttleDown then
  99. throttle = throttle - 1
  100. end
  101. if throttle < 0 then throttle = 0 end
  102. if throttle > 15 then throttle = 15 end
  103. redstone.setAnalogOutput("back", math.floor(throttle))
  104. end
  105. sleep(0.05) -- Reduce sleep time for higher responsiveness
  106. end
  107. end
  108.  
  109.  
  110. local function PIDController(Kp, Ki, Kd, error, integral, derivative, prevError, dt)
  111. -- Calculate the proportional, integral, and derivative components
  112. local proportional = Kp * error
  113. integral = integral + error * dt
  114. derivative = (error - prevError) / dt
  115.  
  116. -- Calculate output
  117. local output = proportional + (Ki * integral) + (Kd * derivative)
  118.  
  119. -- Return the PID output and updated integral and previous error
  120. return output, integral, error
  121. end
  122.  
  123. local function yVelStabilize(desiredYVel)
  124. local currentVel = ship.getVelocity()
  125.  
  126. local yVelError = desiredYVel - currentVel.y
  127.  
  128. local rotorAdjustment, yVelIntegral, yVelPrevError = PIDController(Kp_rotor, Ki_rotor, Kd_rotor, yVelError, yVelIntegral, (yVelError - yVelPrevError), yVelPrevError, dt)
  129.  
  130. local getRotorSpeed = rotor.getTargetSpeed()
  131. if getRotorSpeed then
  132. currentRotorSpeed = getRotorSpeed
  133. end
  134.  
  135. if currentVel.y > desiredYVel then
  136. -- If we're going up too fast, reduce rotor speed
  137. newRotorSpeed = math.abs(currentRotorSpeed) - math.abs(rotorAdjustment) -- Reduce speed
  138. if newRotorSpeed < 0 then
  139. newRotorSpeed = 0
  140. end
  141. elseif currentVel.y < desiredYVel then
  142. newRotorSpeed = math.abs(currentRotorSpeed) + math.abs(rotorAdjustment) -- Increase speed
  143. else
  144. newRotorSpeed = currentRotorSpeed
  145. end
  146.  
  147. rotor.setTargetSpeed(newRotorSpeed)
  148.  
  149. sleep(dt)
  150. end
  151.  
  152. local function hoverMode()
  153. if controls then
  154. if controls.hoverMode then
  155. velocity = ship.getVelocity()
  156. position = ship.getWorldspacePosition()
  157. local roll = math.deg(ship.getRoll())
  158. redstone.setAnalogOutput("back",3)
  159.  
  160. if roll > -10 then
  161. leftRouter.setAnalogOutput("top",15)
  162. if velocity.y > 0 then
  163. redstone.setAnalogOutput("top", 0)
  164. rightRouter.setAnalogOutput("top",15)
  165. else
  166. redstone.setAnalogOutput("top", 10)
  167. redstone.setAnalogOutput("front",0)
  168. end
  169. else
  170. leftRouter.setOutput("top",true)
  171. if position.y > maintainHeight and velocity.y > -5 then
  172. redstone.setAnalogOutput("top",7)
  173. heightError = position.y - maintainHeight
  174. local rollPropotional = math.abs(heightError) * Kp_hover
  175. redstone.setAnalogOutput("back",2)
  176. if roll > -35 then
  177. rightRouter.setAnalogOutput("top",math.min(math.abs(rollPropotional),15))
  178. rightRouter.setAnalogOutput("front",0)
  179. else
  180. rightRouter.setAnalogOutput("top",0)
  181. rightRouter.setAnalogOutput("front",5)
  182. end
  183. else
  184. redstone.setAnalogOutput("top",13)
  185. redstone.setAnalogOutput("back",5)
  186. if roll < -25 then
  187. rightRouter.setAnalogOutput("top",0)
  188. rightRouter.setAnalogOutput("front",2)
  189. else
  190. rightRouter.setAnalogOutput("front",0)
  191. end
  192. end
  193. end
  194. end
  195. end
  196. sleep()
  197. end
  198.  
  199. local function main()
  200. while true do
  201. if controls and controls.hoverMode then
  202. hoverMode()
  203. else
  204. maintainHeight = ship.getWorldspacePosition().y -- Update maintainHeight based on the ship's current position
  205. end
  206. sleep()
  207. end
  208. end
  209.  
  210. -- Use parallel to run modem messages and all controls in their own threads
  211. parallel.waitForAny(
  212. modemMessage,
  213. manualControlPitch,
  214. manualControlRoll,
  215. manualControlYaw,
  216. manualThrottle,
  217. main
  218. )
Advertisement
Add Comment
Please, Sign In to add comment