MegiRicky

55aAIM.lua

Dec 2nd, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. local modem = peripheral.wrap("right")
  2. local speaker = peripheral.find("speaker")
  3. local pitchMotor = peripheral.wrap("back")
  4. local yawMotor = peripheral.wrap("front")
  5.  
  6. pitchMotor.setTargetSpeed(0)
  7. yawMotor.setTargetSpeed(0)
  8.  
  9. local cannons = {}
  10. local targetInfo = {}
  11. local controlMode = "auto"
  12. local turnSpeed = 60 -- degrees per second (16 RPM)
  13. local directTurnThreshold = 16
  14.  
  15. local k = 1
  16. local nilCount = 0
  17. local i = 0
  18. local yawErrorSum = 0
  19. local lastYawError = 0
  20. local pitchErrorSum = 0
  21. local lastPitchError = 0
  22. local yawError = 0
  23. local yawIntegral = 0
  24. local yawPrevError = 0
  25. local pitchError = 0
  26. local pitchIntegral = 0
  27. local pitchPrevError = 0
  28. local lastTime = os.clock()
  29.  
  30. local Kp_yaw = 1
  31. local Ki_yaw = 0.1
  32. local Kd_yaw = -0.06
  33. local Kp_pitch = 0.8
  34. local Ki_pitch = 0.1
  35. local Kd_pitch = -0.06
  36. local dt = 0.1
  37.  
  38. print("Input the controlChannel number, default: 500")
  39. local controlChannel = io.read()
  40. if controlChannel == "" then
  41. controlChannel = 500
  42. end
  43. controlChannel = tonumber(controlChannel)
  44.  
  45. print("Input the cannon channel number, default: 1900")
  46. local cannonChannel = io.read()
  47. if cannonChannel == "" then
  48. cannonChannel = 1900
  49. end
  50. cannonChannel = tonumber(cannonChannel)
  51.  
  52. print("Enable pitch yaw limiter, yes/no, default: yes")
  53. local limitor = io.read()
  54. if limitor == "no" then
  55. limitor = false
  56. else
  57. limitor = true
  58. end
  59. local pitchUpperLimit = -6
  60.  
  61. print("Enter the yaw adjustment, default: 180(for 55a), 90 for HX3")
  62. local yawAdjustment = io.read()
  63. if yawAdjustment == "" then
  64. yawAdjustment = 180
  65. end
  66. local yawAdjustment = tonumber(yawAdjustment)
  67.  
  68. print("Do you want to use pitch, default: yes(for 55a), no for HX3")
  69. local usePitch = io.read()
  70. if usePitch == "no" then
  71. usePitch = false
  72. else
  73. usePitch = true
  74. end
  75.  
  76. if modem then
  77. modem.open(cannonChannel)
  78. modem.open(controlChannel)
  79. end
  80.  
  81. -- Normalize a vector
  82. local function normalizeVector(v)
  83. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  84. if length == 0 then
  85. return {0, 0, 0}
  86. end
  87. return {v[1] / length, v[2] / length, v[3] / length}
  88. end
  89.  
  90. -- Normalize the rotation matrix
  91. local function normalizeRotationMatrix(rotMatrix)
  92. local normalizedMatrix = {}
  93. for i = 1, #rotMatrix do
  94. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  95. end
  96. return normalizedMatrix
  97. end
  98.  
  99. -- Get the pitch of the ship
  100. local function getPitch()
  101. local rotMatrix = ship.getTransformationMatrix()
  102. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  103. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  104. end
  105. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  106. -- Get the yaw of the ship
  107. local function getYaw()
  108. local rotMatrix = ship.getTransformationMatrix()
  109. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  110. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  111. end
  112.  
  113. -- Get the roll of the ship
  114. local function getRoll()
  115. local rotMatrix = ship.getTransformationMatrix()
  116. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  117. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  118. end
  119.  
  120. local function PIDController(Kp, Ki, Kd, error, integral, prevError, dt)
  121. -- Calculate the proportional, integral, and derivative components
  122. local proportional = Kp * error
  123. integral = integral + error * dt -- Accumulate the error for the integral term
  124. local derivative = (error - prevError) / dt
  125.  
  126. -- Calculate the PID output
  127. local output = proportional + (Ki * integral) + (Kd * derivative)
  128.  
  129. -- Return the PID output, updated integral, and current error
  130. return output, integral, error
  131. end
  132.  
  133. -- Yaw control function using PID
  134. local function yawControl(deltaYaw, currentYaw)
  135. if math.abs(deltaYaw) then -- Only adjust if yaw error is significant
  136. -- PID controller for yaw
  137. local yawSpeed, yawIntegral, yawPrevError = PIDController(Kp_yaw, Ki_yaw, Kd_yaw, deltaYaw, yawIntegral, yawPrevError, dt)
  138.  
  139. -- Constrain the yaw speed to prevent runaway spinning
  140. yawSpeed = math.max(-128, math.min(128, yawSpeed))
  141.  
  142. print("deltaYaw: " .. deltaYaw)
  143. print("yawSpeed: " .. yawSpeed)
  144. print("currentYaw: "..currentYaw)
  145.  
  146. -- Set motor speed based on PID output
  147. yawMotor.setTargetSpeed(-yawSpeed)
  148. else
  149. yawMotor.setTargetSpeed(0) -- Stop motor if error is too small
  150. end
  151. end
  152.  
  153. local function pitchControl(deltaPitch, currentYaw)
  154. if math.abs(deltaPitch) then -- Only adjust if yaw error is significant
  155. -- PID controller for yaw
  156. local pitchSpeed, pitchIntegral, pitchPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, deltaPitch, pitchIntegral, pitchPrevError, dt)
  157.  
  158. -- Constrain the yaw speed to prevent runaway spinning
  159. pitchSpeed = math.max(-128, math.min(128, pitchSpeed))
  160.  
  161. print("deltaPitch: " .. deltaPitch)
  162. print("pitchSpeed: " .. pitchSpeed)
  163.  
  164. -- Set motor speed based on PID output
  165. pitchMotor.setTargetSpeed(pitchSpeed)
  166. else
  167. pitchMotor.setTargetSpeed(0) -- Stop motor if error is too small
  168. end
  169. end
  170.  
  171. local function aimCannon(targetPos, targetVel, sourceX, sourceY, sourceZ)
  172. if sourceX and sourceY and sourceZ then
  173. local currentTime = os.clock()
  174. local dt = currentTime - lastTime
  175. if dt == 0 then return end -- Prevent division by zero
  176. lastTime = currentTime
  177.  
  178. local dx = targetPos.x - sourceX
  179. local dy = targetPos.y - sourceY
  180. local dz = targetPos.z - sourceZ
  181. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  182.  
  183. local estimateTime = 0.8 -- for combating PID latency
  184. local estimateX = targetPos.x + targetVel.x * estimateTime
  185. local estimateY = targetPos.y + targetVel.y * estimateTime
  186. local estimateZ = targetPos.z + targetVel.z * estimateTime
  187.  
  188. local dx = estimateX - sourceX
  189. local dy = estimateY - sourceY
  190. local dz = estimateZ - sourceZ
  191.  
  192. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  193. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  194.  
  195. local yaw = math.deg(math.atan2(-dx, dz))
  196. yaw = (yaw + 180) % 360
  197.  
  198. local currentYaw = math.deg(getYaw())+yawAdjustment
  199. if usePitch then
  200. currentPitch = math.deg(-getPitch())
  201. else
  202. currentPitch = math.deg(getRoll())
  203. end
  204.  
  205. -- Adjust pitch based on distance adjustments
  206. local deltaYaw = (yaw - currentYaw + 180) % 360 - 180
  207. local deltaPitch = pitch - currentPitch
  208.  
  209.  
  210. -- Turning and pitch adjustment logic
  211. parallel.waitForAll(
  212. function() yawControl(deltaYaw, currentYaw) end,
  213. function() pitchControl(deltaPitch, currentPitch) end
  214. )
  215.  
  216. lastCurrentYaw = currentYaw
  217. lastCurrentPitch = currentPitch
  218. end
  219. end
  220.  
  221. local function modemMessage()
  222. while true do
  223. if modem then
  224. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  225. if channel == cannonChannel then
  226. targetInfo = message
  227. elseif channel == controlChannel then
  228. controls = message
  229. end
  230. else
  231. sleep()
  232. end
  233. end
  234. end
  235.  
  236. local function main()
  237. while true do
  238. if not(hideCannon) then
  239. local source = ship.getWorldspacePosition()
  240. local sourceX = source.x
  241. local sourceY = source.y
  242. local sourceZ = source.z
  243.  
  244. if targetInfo and targetInfo.targetPos and targetInfo.targetVel and controlMode == "auto" then
  245. aimCannon(targetInfo.targetPos, targetInfo.targetVel, sourceX, sourceY, sourceZ)
  246. end
  247. end
  248. sleep()
  249. end
  250. end
  251.  
  252. local function modeSwitch()
  253. while true do
  254. if redstone.getInput("back") then
  255. if controlMode == "auto" then
  256. controlMode = "manual"
  257. redstone.setOutput("top",false)
  258. redstone.setOutput("bottom",false)
  259. redstone.setOutput("left",false)
  260. redstone.setOutput("right",false)
  261. else
  262. controlMode = "auto"
  263. end
  264. end
  265. sleep()
  266. end
  267. end
  268.  
  269. parallel.waitForAny(
  270. modemMessage,
  271. main,
  272. modeSwitch
  273. )
  274.  
Advertisement
Add Comment
Please, Sign In to add comment