MegiRicky

ShimakazeEngineControl

Sep 10th, 2024 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. --engineControl.lua
  2. local defaultBaseChannel = 200
  3.  
  4. print("Enter base channel (press Enter to use default: " .. defaultBaseChannel .. "):")
  5. local inputChannel = io.read()
  6. if inputChannel == "" then
  7. inputChannel = 200
  8. end
  9. inputChannel = tonumber(inputChannel)
  10.  
  11. local statusChannel = inputChannel
  12. local controlChannel = inputChannel + 1
  13.  
  14. local engineLeft = peripheral.wrap("left")
  15. local engineRight = peripheral.wrap("right")
  16. local fuel = peripheral.find("create_connected:item_silo")
  17. local stress = peripheral.find("Create_Stressometer")
  18. local acc = peripheral.find("modular_accumulator")
  19. local modem = peripheral.wrap("front")
  20. local router = peripheral.find("redrouter")
  21. --local arController = peripheral.find("arController")
  22.  
  23. --arController.clear()
  24.  
  25. if not engineLeft then
  26. error("Left rotation controller not found")
  27. elseif not engineRight then
  28. error("Right rotation controller not found")
  29. elseif not modem then
  30. error("modem not found")
  31. end
  32.  
  33. if stress then
  34. modem.open(statusChannel)
  35. else
  36. print("Warning: Stressometer not found. Skipping stress monitoring.")
  37. end
  38.  
  39. if fuel then
  40. modem.open(statusChannel)
  41. else
  42. print("Warning: Fuel silo not found. Skipping fuel monitoring.")
  43. end
  44.  
  45. if acc then
  46. modem.open(statusChannel)
  47. else
  48. print("Warning: Accumulator not found. Skipping accumulator monitoring.")
  49. end
  50.  
  51. modem.open(statusChannel)
  52. modem.open(controlChannel)
  53.  
  54. local leftSpeed = 0
  55. local rightSpeed = 0
  56. local messageTable = nil
  57. local turnLevel = 0
  58. local yaw = 0
  59.  
  60. -- Normalize a vector
  61. local function normalizeVector(v)
  62. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  63. if length == 0 then
  64. return {0, 0, 0}
  65. end
  66. return {v[1] / length, v[2] / length, v[3] / length}
  67. end
  68.  
  69. -- Normalize the rotation matrix
  70. local function normalizeRotationMatrix(rotMatrix)
  71. local normalizedMatrix = {}
  72. for i = 1, #rotMatrix do
  73. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  74. end
  75. return normalizedMatrix
  76. end
  77.  
  78. -- Get the pitch of the ship
  79. local function getPitch()
  80. local rotMatrix = ship.getTransformationMatrix()
  81. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  82. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  83. end
  84. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  85. -- Get the yaw of the ship
  86. local function getYaw()
  87. local rotMatrix = ship.getTransformationMatrix()
  88. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  89. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  90. end
  91.  
  92. -- Get the roll of the ship
  93. local function getRoll()
  94. local rotMatrix = ship.getTransformationMatrix()
  95. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  96. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  97. end
  98.  
  99. local function listenForCoordinates()
  100. while true do
  101. local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  102. if senderChannel == controlChannel then
  103. messageTable = message
  104. leftSpeed = message.leftSpeed
  105. rightSpeed = message.rightSpeed
  106. turnLevel = message.turnLevel
  107. end
  108. end
  109. end
  110.  
  111. local function updateSpeed()
  112. while true do
  113. if arController and ship then
  114. velocity = ship.getVelocity()
  115. speed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2) * 1.94384449
  116. arController.clearElement("speed")
  117. arController.drawStringWithId("speed", "Speed: " .. (math.floor(speed * 100) / 100) .. " knots", 10, 210, 0xFFFFFF)
  118. end
  119. sleep(0.1)
  120. end
  121. end
  122.  
  123. local function updateYaw()
  124. while true do
  125. if arController and ship then
  126. local yaw = math.deg(getYaw) + 90
  127. if yaw < 0 then yaw = yaw + 360 end
  128. arController.clearElement("yaw")
  129. arController.drawStringWithId("yaw", "Heading: " .. (math.floor(yaw * 100) / 100) .. " degree", 10, 220, 0xFFFFFF)
  130. end
  131. sleep(0.1)
  132. end
  133. end
  134.  
  135. local function updateLeftSpeed()
  136. while true do
  137. if arController then
  138. arController.clearElement("leftSpeed")
  139. arController.drawStringWithId("leftSpeed", "Left RPM: " .. leftSpeed, 10, 230, 0xFFFFFF)
  140. end
  141. sleep(0.1)
  142. end
  143. end
  144.  
  145. local function updateRightSpeed()
  146. while true do
  147. if arController then
  148. arController.clearElement("rightSpeed")
  149. arController.drawStringWithId("rightSpeed", "Right RPM: " .. rightSpeed, 10, 240, 0xFFFFFF)
  150. end
  151. sleep(0.1)
  152. end
  153. end
  154.  
  155. local function updateTurnLevel()
  156. while true do
  157. if arController then
  158. arController.clearElement("turnLevel")
  159. arController.drawStringWithId("turnLevel", "Turn Level: " .. turnLevel, 10, 250, 0xFFFFFF)
  160. end
  161. sleep(0.1)
  162. end
  163. end
  164.  
  165.  
  166. parallel.waitForAny(
  167. listenForCoordinates,updateSpeed, updateYaw, updateLeftSpeed, updateRightSpeed, updateTurnLevel,
  168. function()
  169. while true do
  170. local statusData = {}
  171.  
  172. -- Fuel monitoring
  173. if fuel then
  174. local totalBlazeCakes = 0
  175. local total = fuel.size() * 64
  176. local items = fuel.list()
  177. for slot, itemDetail in pairs(items) do
  178. if itemDetail.name == "create:blaze_cake" then
  179. totalBlazeCakes = totalBlazeCakes + itemDetail.count
  180. end
  181. end
  182. statusData.fuel = { totalBlazeCakes = totalBlazeCakes, total = total }
  183. print("Total number of blaze cakes: ", totalBlazeCakes.." / "..total)
  184. end
  185.  
  186. -- Stress monitoring
  187. if stress then
  188. local usedSU = stress.getStress()
  189. local SUCapacity = stress.getStressCapacity()
  190. statusData.stress = { usedSU = usedSU, SUCapacity = SUCapacity }
  191. print("Used SU: ", usedSU)
  192. print("SU Capacity: ", SUCapacity)
  193. end
  194.  
  195. -- Accumulator monitoring
  196. if acc then
  197. local accCapacity = acc.getCapacity()
  198. local accEnergy = acc.getEnergy()
  199. statusData.accumulator = { accEnergy = accEnergy, accCapacity = accCapacity }
  200. print("Accumulator Capacity: ", accCapacity)
  201. print("Accumulator Energy: ", accEnergy)
  202. end
  203.  
  204. -- Yaw monitoring
  205. local yaw = math.deg(getYaw())
  206. if yaw < 0 then yaw = yaw + 360 end
  207. statusData.yaw = yaw
  208. print("Yaw: "..yaw)
  209. velocity = ship.getVelocity()
  210. statusData.speed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2) * 1.94384449
  211. print(statusData.speed)
  212.  
  213. -- Engine speeds
  214. statusData.leftSpeed = leftSpeed
  215. statusData.rightSpeed = rightSpeed
  216. print("Left speed: ", leftSpeed)
  217. print("Right speed: ", rightSpeed)
  218. print("turnLevel: "..turnLevel)
  219. print("statusChannel: "..statusChannel)
  220. print("controlChannel"..controlChannel)
  221.  
  222. if turnLevel then
  223. if turnLevel < 0 then
  224. redstone.setOutput("back",true)
  225. redstone.setOutput("top",false)
  226. elseif turnLevel > 0 then
  227. redstone.setOutput("back",false)
  228. redstone.setOutput("top",true)
  229. else
  230. redstone.setOutput("back",false)
  231. redstone.setOutput("top",false)
  232. end
  233. end
  234.  
  235.  
  236. local serializedData = textutils.serialize(statusData)
  237. modem.transmit(statusChannel, 0, serializedData)
  238.  
  239. -- Engine control
  240. engineLeft.setTargetSpeed(leftSpeed)
  241. engineRight.setTargetSpeed(rightSpeed)
  242.  
  243. sleep(0.2)
  244. end
  245. end
  246. )
  247.  
Advertisement
Add Comment
Please, Sign In to add comment