MegiRicky

Z-18Main.lua

Sep 26th, 2024 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 KB | None | 0 0
  1. local engine = peripheral.wrap("left")
  2. local rotor = peripheral.find("Create_RotationSpeedController")
  3. local tailRotor = peripheral.find("Tail_RotorController") -- Assuming tail rotor is controlled separately
  4. local modem = peripheral.wrap("right")
  5.  
  6. redstone.setOutput("left",false)
  7. redstone.setOutput("front",false)
  8. redstone.setOutput("right",false)
  9.  
  10. print("Input the controlChannel, default = 1100")
  11. local controlChannel = io.read()
  12. if controlChannel == "" then
  13. controlChannel = 1100
  14. end
  15. controlChannel = tonumber(controlChannel)
  16. modem.open(controlChannel) -- Open a channel to communicate
  17. AuxControlChannel = controlChannel + 1
  18. modem.open(AuxControlChannel)
  19.  
  20. local controls = {}
  21. local AuxControls = {}
  22. local desiredYVel,desiredPitch
  23. local currentRotorSpeed = 0
  24. local currentTailRotorSpeed = 0
  25. local newRotorSpeed = 0
  26. local speed = 0
  27.  
  28. local Kp_rotor, Ki_rotor, Kd_rotor = 0.4, 1, 0.5
  29. local Kp_yaw, Ki_yaw, Kd_yaw = 0.5, 0.01, 0.1 -- Yaw PID values
  30. local yVelError = 0
  31. local yVelIntegral = 0
  32. local yVelPrevError = 0
  33. local yawError = 0
  34. local yawIntegral = 0
  35. local yawPrevError = 0
  36. local prevYaw = 0 -- To store the previous yaw value
  37. local currentYaw = 0
  38. local Kp_pitch, Ki_pitch, Kd_pitch = 0.1, 0.2, 0.01 -- Pitch PID values
  39. local pitchError = 0
  40. local pitchIntegral = 0
  41. local pitchPrevError = 0
  42. local currentPitch = 0
  43. local dt = 0.1 -- Time step for PID
  44.  
  45. -- Normalize a vector
  46. local function normalizeVector(v)
  47. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  48. if length == 0 then
  49. return {0, 0, 0}
  50. end
  51. return {v[1] / length, v[2] / length, v[3] / length}
  52. end
  53.  
  54. -- Normalize the rotation matrix
  55. local function normalizeRotationMatrix(rotMatrix)
  56. local normalizedMatrix = {}
  57. for i = 1, #rotMatrix do
  58. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  59. end
  60. return normalizedMatrix
  61. end
  62.  
  63. -- Get the pitch of the ship
  64. local function getPitch()
  65. local rotMatrix = ship.getTransformationMatrix()
  66. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  67. return -math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  68. end
  69. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  70. -- Get the yaw of the ship
  71. local function getYaw()
  72. local rotMatrix = ship.getTransformationMatrix()
  73. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  74. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) + math.pi -- Extract yaw from the matrix
  75. end
  76.  
  77. -- Get the roll of the ship
  78. local function getRoll()
  79. local rotMatrix = ship.getTransformationMatrix()
  80. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  81. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  82. end
  83.  
  84. local function PIDController(Kp, Ki, Kd, error, integral, derivative, prevError, dt)
  85. -- Calculate the proportional, integral, and derivative components
  86. local proportional = Kp * error
  87. integral = integral + error * dt
  88. derivative = (error - prevError) / dt
  89.  
  90. -- Calculate output
  91. local output = proportional + (Ki * integral) + (Kd * derivative)
  92.  
  93. -- Return the PID output and updated integral and previous error
  94. return output, integral, error
  95. end
  96.  
  97. local function pullFuel()
  98. while true do
  99. engine.pullFluid("back", 20, "createdieselgenerators:biodiesel")
  100. print("fuelPulled")
  101. sleep(1)
  102. end
  103. end
  104.  
  105. local function modemMessage()
  106. while true do
  107. if modem then
  108. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  109. if channel == cannonHitPosChannel then
  110. cannonHitPos = message
  111. elseif channel == controlChannel then
  112. controls = message
  113. end
  114. else
  115. sleep()
  116. end
  117. end
  118. end
  119.  
  120. -- Y Velocity stabilization using the main rotor
  121. local function yVelStabilize(desiredYVel)
  122. local currentVel = ship.getVelocity() -- Get current velocity of the helicopter
  123.  
  124. -- Calculate the error between desired and current Y velocity
  125. local yVelError = desiredYVel - currentVel.y
  126.  
  127. -- Use PID controller to calculate rotor speed adjustment
  128. local rotorAdjustment, yVelIntegral, yVelPrevError = PIDController(Kp_rotor, Ki_rotor, Kd_rotor, yVelError, yVelIntegral, (yVelError - yVelPrevError), yVelPrevError, dt)
  129.  
  130. -- Get the current rotor speed and adjust it based on PID output
  131. local getRotorSpeed = rotor.getTargetSpeed()
  132. if getRotorSpeed then
  133. currentRotorSpeed = getRotorSpeed
  134. end
  135.  
  136. -- Adjust rotor speed based on whether we're going too fast upward or downward
  137. if currentVel.y > desiredYVel then
  138. -- If we're going up too fast, reduce rotor speed
  139. newRotorSpeed = math.abs(currentRotorSpeed) - math.abs(rotorAdjustment) -- Reduce speed
  140. if newRotorSpeed < 0 then
  141. newRotorSpeed = 0
  142. end
  143. elseif currentVel.y < desiredYVel then
  144. -- If we're going down too fast, increase rotor speed
  145. newRotorSpeed = math.abs(currentRotorSpeed) + math.abs(rotorAdjustment) -- Increase speed
  146. else
  147. -- If close to desired Y velocity, maintain the speed or apply minor adjustments
  148. newRotorSpeed = currentRotorSpeed
  149. end
  150.  
  151. -- Set the new rotor speed to stabilize Y velocity
  152. rotor.setTargetSpeed(newRotorSpeed)
  153.  
  154. -- Sleep for dt (time step)
  155. sleep(dt)
  156. end
  157.  
  158. local function pitchStabilizer(desiredPitch)
  159. -- Get the current pitch from the helicopter
  160. local currentPitch = -math.deg(getPitch())
  161.  
  162. -- Calculate the error between the desired pitch and the current pitch
  163. local pitchError = desiredPitch - currentPitch
  164.  
  165. local pitchAdjustment, pitchIntegral, pitchPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchError, pitchIntegral, (pitchError - pitchPrevError), pitchPrevError, dt)
  166. pitchAdjustment = pitchAdjustment
  167. -- Adjust the rotor speed or control surfaces based on the PID output
  168. AuxControls.pitch = pitchAdjustment
  169.  
  170. -- Sleep for dt (time step)
  171. sleep(dt)
  172. end
  173.  
  174. local function main()
  175. while true do
  176. local desiredYVel = 0
  177. if controls.rotorUp then
  178. desiredYVel = 20
  179. elseif controls.rotorDown then
  180. desiredYVel = -20
  181. end
  182. yVelStabilize(desiredYVel)
  183.  
  184. if math.abs(newRotorSpeed) < 128 then
  185. AuxControls.tailSpeed = newRotorSpeed * 1
  186. else
  187. AuxControls.tailSpeed = newRotorSpeed * 0.085
  188. end
  189.  
  190. if controls.yawLeft then
  191. AuxControls.tailSpeed = AuxControls.tailSpeed + 256
  192. elseif controls.yawRight then
  193. AuxControls.tailSpeed = AuxControls.tailSpeed - 256
  194. end
  195.  
  196. if AuxControls.tailSpeed < -256 then AuxControls.tailSpeed = -256 end
  197. if AuxControls.tailSpeed > 256 then AuxControls.tailSpeed = 256 end
  198.  
  199. local velocity = ship.getVelocity()
  200. local speed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2)
  201. if controls.pitchUp then
  202. desiredPitch = 30
  203. elseif controls.pitchDown then
  204. desiredPitch = -30
  205. if speed < 30 then
  206. AuxControls.throttle = 15
  207. else
  208. AuxControls.throttle = 0
  209. end
  210. else
  211. desiredPitch = 0
  212. AuxControls.throttle = 0
  213. end
  214. pitchStabilizer(desiredPitch)
  215. print("Throttle: "..AuxControls.throttle)
  216.  
  217. if controls.rollLeft then
  218. redstone.setOutput("right",true)
  219. redstone.setOutput("front",false)
  220. elseif controls.rollRight then
  221. redstone.setOutput("front",true)
  222. redstone.setOutput("right",false)
  223. else
  224. redstone.setOutput("front",false)
  225. redstone.setOutput("right",false)
  226. end
  227.  
  228. modem.transmit(AuxControlChannel,0,AuxControls)
  229. end
  230. end
  231.  
  232. parallel.waitForAny(
  233. pullFuel,
  234. main,
  235. modemMessage
  236. )
Advertisement
Add Comment
Please, Sign In to add comment