MegiRicky

Z-10Control

Jul 4th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. local modem = peripheral.wrap("right")
  2. local tailRotor = peripheral.find("Create_RotationSpeedController")
  3. local redrouter = peripheral.find("redrouter")
  4. local controls = { rotorRPM = 5}
  5.  
  6. print("Input the controlChannel, default = 1100")
  7. local controlChannel = io.read()
  8. if controlChannel == "" then
  9. controlChannel = 1100
  10. end
  11. controlChannel = tonumber(controlChannel)
  12. flightInfoChannel = controlChannel + 2
  13. modem.open(controlChannel) -- Open a channel to communicate
  14. modem.open(flightInfoChannel)
  15.  
  16.  
  17. redstone.setOutput("top",false)
  18. tailRotor.setTargetSpeed(0)
  19.  
  20. -- Normalize a vector
  21. local function normalizeVector(v)
  22. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  23. if length == 0 then
  24. return {0, 0, 0}
  25. end
  26. return {v[1] / length, v[2] / length, v[3] / length}
  27. end
  28.  
  29. -- Normalize the rotation matrix
  30. local function normalizeRotationMatrix(rotMatrix)
  31. local normalizedMatrix = {}
  32. for i = 1, #rotMatrix do
  33. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  34. end
  35. return normalizedMatrix
  36. end
  37.  
  38. -- Get the pitch of the ship
  39. local function getPitch()
  40. local rotMatrix = ship.getTransformationMatrix()
  41. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  42. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  43. end
  44. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  45. -- Get the yaw of the ship
  46. local function getYaw()
  47. local rotMatrix = ship.getTransformationMatrix()
  48. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  49. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  50. end
  51.  
  52. -- Get the roll of the ship
  53. local function getRoll()
  54. local rotMatrix = ship.getTransformationMatrix()
  55. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  56. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  57. end
  58.  
  59. local function modemMessage()
  60. while true do
  61. if modem then
  62. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  63. if channel == cannonHitPosChannel then
  64. cannonHitPos = message
  65. elseif channel == controlChannel then
  66. controls = message
  67. end
  68. else
  69. sleep()
  70. end
  71. end
  72. end
  73.  
  74. local function informationSending()
  75. local currentVel = ship.getVelocity()
  76. local currentPitch = getPitch()
  77. local currentYaw = math.deg(getYaw())
  78. currentYaw = math.rad((currentYaw + 180) % 360)
  79. local currentRoll = getRoll()
  80. local currentPosition = ship.getWorldspacePosition()
  81. flightInfo = {velocity = currentVel, pos = currentPosition, pitch = currentPitch, yaw = currentYaw, roll = currentRoll}
  82. modem.transmit(flightInfoChannel,flightInfoChannel,flightInfo)
  83. end
  84.  
  85. local function mainRotorControl()
  86. if controls and controls.rotorRPM then
  87. if controls.engine == "on" then
  88. redstone.setAnalogOutput("top",controls.rotorRPM)
  89. --print("setting rpm to "..cointrols.rotorRPM)
  90. elseif controls.engine == "off" then
  91. redstone.setAnalogOutput("top",0)
  92. end
  93. end
  94. end
  95.  
  96. local function tailRotorControl()
  97. if controls then
  98. if controls.yawLeft then
  99. tailRotor.setTargetSpeed(86)
  100. elseif controls.yawRight then
  101. tailRotor.setTargetSpeed(-86)
  102. else
  103. --if manualControl then
  104. tailRotor.setTargetSpeed(0)
  105. --end
  106. end
  107. end
  108. end
  109.  
  110. local function rocketControl()
  111. while true do
  112. local vel = ship.getVelocity()
  113. local speed = math.sqrt(vel.x^2 + vel.y^2 + vel.z^2)
  114.  
  115. if controls and controls.weaponChoosen == "rocket" then
  116. if controls.fire and speed <= 15 then
  117. redrouter.setOutput("top", true)
  118. else
  119. redrouter.setOutput("top", false)
  120. end
  121. else
  122. redrouter.setOutput("top", false)
  123. end
  124.  
  125. sleep()
  126. end
  127. end
  128.  
  129.  
  130. local lastTime = os.clock()
  131. local dt = 0.1 -- inistial time step
  132.  
  133. local function PIDController(Kp, Ki, Kd, error, integral, derivative, prevError, dt)
  134. -- Calculate the proportional, integral, and derivative components
  135. local proportional = Kp * error
  136. integral = integral + error * dt
  137. derivative = (error - prevError) / dt
  138.  
  139. -- Calculate output
  140. local output = proportional + (Ki * integral) + (Kd * derivative)
  141.  
  142. -- Return the PID output and updated integral and previous error
  143. return output, integral, error
  144. end
  145.  
  146. local Kp_yaw, Ki_yaw, Kd_yaw = 0.8, 0, 0.15
  147. local yawError = 0
  148. local yawIntegral = 0
  149. local yawPrevError = 0
  150. local yawControlCooldown = 0 -- last time of manual input
  151. local desiredYaw = nil
  152.  
  153. local function yawStablizer()
  154. while true do
  155. if flightInfo then
  156. local currentTime = os.clock()
  157. local currentYawDeg = math.deg(flightInfo.yaw)
  158.  
  159. -- Set desiredYaw if not initialized
  160. if not desiredYaw then
  161. desiredYaw = currentYawDeg
  162. end
  163.  
  164. -- Detect manual control and reset cooldown
  165. if controls.yawLeft or controls.yawRight or controls.rollLeft or controls.rollRight then
  166. yawControlCooldown = currentTime
  167. desiredYaw = currentYawDeg
  168. manualControl = true
  169. elseif (currentTime - yawControlCooldown) > 3 then
  170. -- If no manual input for over 1 second, engage PID yaw stabilization
  171. manualControl = false
  172. yawError = desiredYaw - currentYawDeg
  173. yawAdjustment, yawIntegral, yawPrevError = PIDController(
  174. Kp_yaw, Ki_yaw, Kd_yaw,
  175. yawError, yawIntegral,
  176. (yawError - yawPrevError), yawPrevError, dt
  177. )
  178. tailRotor.setTargetSpeed(-yawAdjustment)
  179. end
  180. end
  181. sleep()
  182. end
  183. end
  184.  
  185.  
  186. local function main()
  187. while true do
  188. local now = os.clock()
  189. dt = now - lastTime
  190. lastTime = now
  191. if dt <= 0 then dt = 0.001 end
  192.  
  193. informationSending()
  194. mainRotorControl()
  195. tailRotorControl()
  196. sleep()
  197. end
  198. end
  199.  
  200. parallel.waitForAny(
  201. main,
  202. modemMessage,
  203. rocketControl
  204. )
Advertisement
Add Comment
Please, Sign In to add comment