MegiRicky

F-35FlightControl

Feb 14th, 2025 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.88 KB | None | 0 0
  1. local backRouter = peripheral.wrap("back")
  2. local frontRouter = peripheral.wrap("front")
  3. local topRouter = peripheral.find("redrouter")
  4. local monitor = peripheral.find("monitor")
  5. local modem = peripheral.wrap("right")
  6.  
  7. topRouter.setOutput("top",true)
  8. local isVTOLMode = false
  9. local Kp_pitch, Ki_pitch, Kd_pitch = 0.0055, 0, 0.0085 -- Pitch PID values
  10. local pitchError = 0
  11. local pitchIntegral = 0
  12. local pitchPrevError = 0
  13. local currentPitch = 0
  14. local Kp_roll, Ki_roll, Kd_roll = 0.1, 0.2, 0.02 -- roll PID values
  15. local rollError = 0
  16. local rollIntegral = 0
  17. local rollPrevError = 0
  18. local currentRoll = 0
  19. local dt = 0.1
  20.  
  21. local rearThrust,leftThrust,rightThrust =0,0,0
  22.  
  23. local controls = {}
  24. local weaponChoosen = "AIM-220"
  25.  
  26. backRouter.setOutput("front", false)
  27. backRouter.setOutput("back", false)
  28. backRouter.setOutput("left", false)
  29. backRouter.setOutput("right", false)
  30. backRouter.setOutput("top", false)
  31. backRouter.setOutput("bottom", false)
  32.  
  33. frontRouter.setOutput("front", false)
  34. frontRouter.setOutput("back", false)
  35. frontRouter.setOutput("left", false)
  36. frontRouter.setOutput("right", false)
  37. frontRouter.setOutput("top", false)
  38. frontRouter.setOutput("bottom", false)
  39.  
  40. topRouter.setOutput("front", false)
  41. topRouter.setOutput("back", false)
  42. topRouter.setOutput("left", false)
  43. topRouter.setOutput("right", false)
  44. topRouter.setOutput("top", false)
  45. topRouter.setOutput("bottom", false)
  46.  
  47. print("Input the controlChannel, default = 1420")
  48. local controlChannel = io.read()
  49. if controlChannel == "" then
  50. controlChannel = 1420
  51. end
  52. controlChannel = tonumber(controlChannel)
  53. modem.open(controlChannel) -- Open a channel to communicate
  54.  
  55. print("Input the pitchCompensation, default = 48.24")
  56. local pitchCompensation = io.read()
  57. if pitchCompensation == "" then
  58. pitchCompensation = 48.24
  59. end
  60. pitchCompensation = tonumber(pitchCompensation)
  61.  
  62. -- Normalize a vector
  63. local function normalizeVector(v)
  64. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  65. if length == 0 then
  66. return {0, 0, 0}
  67. end
  68. return {v[1] / length, v[2] / length, v[3] / length}
  69. end
  70.  
  71. -- Normalize the rotation matrix
  72. local function normalizeRotationMatrix(rotMatrix)
  73. local normalizedMatrix = {}
  74. for i = 1, #rotMatrix do
  75. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  76. end
  77. return normalizedMatrix
  78. end
  79.  
  80. -- Get the pitch of the ship
  81. local function getPitch()
  82. local rotMatrix = ship.getTransformationMatrix()
  83. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  84. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  85. end
  86. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  87. -- Get the yaw of the ship
  88. local function getYaw()
  89. local rotMatrix = ship.getTransformationMatrix()
  90. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  91. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) + math.pi -- Extract yaw from the matrix
  92. end
  93.  
  94. -- Get the roll of the ship
  95. local function getRoll()
  96. local rotMatrix = ship.getTransformationMatrix()
  97. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  98. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  99. end
  100.  
  101. local function PIDController(Kp, Ki, Kd, error, integral, derivative, prevError, dt)
  102. -- Calculate the proportional, integral, and derivative components
  103. local proportional = Kp * error
  104. integral = integral + error * dt
  105. derivative = (error - prevError) / dt
  106.  
  107. -- Calculate output
  108. local output = proportional + (Ki * integral) + (Kd * derivative)
  109.  
  110. -- Return the PID output and updated integral and previous error
  111. return output, integral, error
  112. end
  113.  
  114. -- Function to handle user click on the button
  115. local function handleClick()
  116. -- Get touch event from the monitor
  117. while true do
  118. local event, side, x, y = os.pullEvent("monitor_touch")
  119.  
  120. print("x: "..x.." y: "..y)
  121. end
  122. end
  123.  
  124. -- **Function to control pitch in VTOL mode**
  125. local function VTOLPitchControl(desiredPitch,baseThrust)
  126. local currentPitch = math.deg(getPitch()) + pitchCompensation -- Get the current pitch
  127. local pitchError = desiredPitch - currentPitch
  128. local pitchOutput, pitchIntegral, pitchPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchError, pitchIntegral, (pitchError - pitchPrevError), pitchPrevError, dt)
  129.  
  130. local compensationFactor = (15 - baseThrust) * 0.4
  131. local adjustedFrontThrust = (baseThrust + pitchOutput) * 1.3 + compensationFactor
  132. local rearThrust = math.min(math.max(baseThrust - pitchOutput, 0), 15)
  133. local frontThrust = math.min(math.max(adjustedFrontThrust, 0), 15)
  134. print("currentPitch: "..currentPitch)
  135. return frontThrust,rearThrust
  136. end
  137.  
  138. -- **Function to control roll in VTOL mode**
  139. local function VTOLRollControl(desiredRoll, baseThrust)
  140. local currentRoll = math.deg(getRoll()) -- Get the current roll
  141. local rollError = desiredRoll - currentRoll
  142. local rollOutput, rollIntegral, rollPrevError = PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral, (rollError - rollPrevError), rollPrevError, dt)
  143.  
  144. -- Calculate thrust values
  145. local leftThrust = baseThrust + rollOutput
  146. local rightThrust = baseThrust - rollOutput
  147.  
  148. -- Clamp the thrust values between 0 and 15
  149. leftThrust = math.max(0, math.min(15, leftThrust))
  150. rightThrust = math.max(0, math.min(15, rightThrust))
  151.  
  152. return leftThrust, rightThrust
  153. end
  154.  
  155. local function VTOLMode()
  156. while true do
  157. if controls and controls.hoverMode then
  158. local baseThrust = controls.throttle
  159.  
  160. if controls.pitchDown then
  161. frontThrust,rearThrust = VTOLPitchControl(-15,baseThrust)
  162. elseif controls.pitchUp then
  163. frontThrust,rearThrust = VTOLPitchControl(15,baseThrust)
  164. else
  165. frontThrust,rearThrust = VTOLPitchControl(0,baseThrust)
  166. end
  167. if controls.rollLeft then
  168. leftThrust, rightThrust = VTOLRollControl(-15,baseThrust)
  169. elseif controls.rollRight then
  170. leftThrust, rightThrust = VTOLRollControl(15,baseThrust)
  171. else
  172. leftThrust, rightThrust = VTOLRollControl(0,baseThrust)
  173. end
  174.  
  175. print("frontThrust: "..frontThrust.." rearThrust: "..rearThrust.." leftThrust: "..leftThrust.." rightThrust: "..rightThrust)
  176.  
  177. frontRouter.setAnalogOutput("right",rightThrust)
  178. frontRouter.setAnalogOutput("top",leftThrust)
  179. frontRouter.setAnalogOutput("front",rearThrust)
  180. frontRouter.setAnalogOutput("left",frontThrust)
  181. else
  182. frontRouter.setAnalogOutput("right",0)
  183. frontRouter.setAnalogOutput("top",0)
  184. frontRouter.setAnalogOutput("front",0)
  185. frontRouter.setAnalogOutput("left",0)
  186. frontRouter.setOutput("bottom",false)
  187. end
  188. sleep()
  189. end
  190. end
  191.  
  192. local function handleThrust()
  193. while true do
  194. if controls and not controls.hoverMode and controls.throttle then
  195. topRouter.setAnalogOutput("front", controls.throttle) -- Full thrust
  196. else
  197. topRouter.setAnalogOutput("front", 0)
  198. end
  199. sleep() -- Reduce processing load
  200. end
  201. end
  202.  
  203. local function handleRoll()
  204. while true do
  205. if controls and not controls.hoverMode and not controls.autoPilot then
  206. local rollLeft = controls.rollLeft and 15 or 0
  207. local rollRight = controls.rollRight and 15 or 0
  208.  
  209. topRouter.setAnalogOutput("left", rollLeft) -- Aileron left down
  210. topRouter.setAnalogOutput("right", rollRight) -- Aileron right down
  211. else
  212. if not controls.autoPilot then
  213. topRouter.setAnalogOutput("left", 0)
  214. topRouter.setAnalogOutput("right", 0)
  215. end
  216. end
  217. sleep()
  218. end
  219. end
  220.  
  221. local function handlePitch()
  222. while true do
  223. if controls and not controls.hoverMode and not controls.autoPilot then
  224. local pitchUp = controls.pitchUp and 15 or 0
  225. local pitchDown = controls.pitchDown and 15 or 0
  226.  
  227. backRouter.setAnalogOutput("top", pitchUp) -- Elevator up
  228. backRouter.setAnalogOutput("front", pitchDown) -- Elevator down
  229. else
  230. if not controls.autoPilot then
  231. backRouter.setAnalogOutput("top", 0)
  232. backRouter.setAnalogOutput("front", 0)
  233. end
  234. end
  235. sleep()
  236. end
  237. end
  238.  
  239. local function handleYaw()
  240. while true do
  241. if controls then
  242. local yawLeft = controls.yawLeft and 15 or 0
  243. local yawRight = controls.yawRight and 15 or 0
  244.  
  245. backRouter.setAnalogOutput("left", yawLeft)
  246. backRouter.setAnalogOutput("right", yawRight)
  247. else
  248. backRouter.setAnalogOutput("right", 0)
  249. backRouter.setAnalogOutput("right", 0)
  250. end
  251. sleep()
  252. end
  253. end
  254.  
  255. -- Runs all key handlers in parallel for instant response!
  256. local function normalMode()
  257. parallel.waitForAny(
  258. handleThrust,
  259. handleRoll,
  260. handlePitch,
  261. handleYaw
  262. )
  263. end
  264.  
  265. local function autoPilot()
  266. local pitchError, pitchIntegral, pitchPrevError = 0, 0, 0
  267. local rollError, rollIntegral, rollPrevError = 0, 0, 0
  268. local altitudeError, altitudeIntegral, altitudePrevError = 0, 0, 0
  269.  
  270. -- **PID Constants**
  271. local Kp_pitch, Ki_pitch, Kd_pitch = 0.25, 0.02, 0.015 -- Pitch PID values
  272. local Kp_roll, Ki_roll, Kd_roll = 0.05, 0.01, 0.01 -- Roll PID values
  273. local Kp_alt, Ki_alt, Kd_alt = 0.3, 0.02, 0.02 -- Altitude PID values (used for pitch control)
  274.  
  275. local dt = 0.1
  276. local targetAltitude = nil -- Stores altitude when autopilot is enabled
  277.  
  278. while true do
  279. if controls and controls.autoPilot then
  280. if not targetAltitude then
  281. targetAltitude = ship.getWorldspacePosition().y -- Lock initial altitude
  282. end
  283.  
  284. local currentAltitude = ship.getWorldspacePosition().y
  285. local currentPitch = math.deg(getPitch()) + pitchCompensation -- Get current pitch angle
  286. local currentRoll = math.deg(getRoll()) -- Get current roll angle
  287.  
  288. -- **Altitude Control (Using Pitch)**
  289.  
  290. altitudeError = targetAltitude - currentAltitude
  291. local altitudePitchCorrection, altitudeIntegral, altitudePrevError =
  292. PIDController(Kp_alt, Ki_alt, Kd_alt, altitudeError, altitudeIntegral,
  293. (altitudeError - altitudePrevError), altitudePrevError, dt)
  294.  
  295. -- **Total Pitch Correction (Altitude + Level Flight)**
  296. pitchError = altitudePitchCorrection - currentPitch
  297. local pitchOutput, pitchIntegral, pitchPrevError =
  298. PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchError, pitchIntegral,
  299. (pitchError - pitchPrevError), pitchPrevError, dt)
  300.  
  301. -- Adjust elevators to control altitude & stabilize pitch
  302. local pitchUp = math.max(0, math.min(15, pitchOutput))
  303. local pitchDown = math.max(0, math.min(15, -pitchOutput))
  304. backRouter.setAnalogOutput("top", pitchUp) -- Elevator up (nose down)
  305. backRouter.setAnalogOutput("front", pitchDown) -- Elevator down (nose up)
  306.  
  307. -- **Roll Control (Keep Level Wings)**
  308. rollError = currentRoll -- We want roll = 0
  309. local rollOutput, rollIntegral, rollPrevError =
  310. PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral,
  311. (rollError - rollPrevError), rollPrevError, dt)
  312.  
  313. -- Adjust ailerons to correct roll
  314. local rollLeft = math.max(0, math.min(15, rollOutput))
  315. local rollRight = math.max(0, math.min(15, -rollOutput))
  316. topRouter.setAnalogOutput("left", rollLeft) -- Aileron left
  317. topRouter.setAnalogOutput("right", rollRight) -- Aileron right
  318.  
  319. print("AutoPilot: Altitude Error:", altitudeError, "Pitch Correction:", altitudePitchCorrection)
  320. print("AutoPilot: Pitch Error:", pitchError, "Elevator Up:", pitchUp, "Elevator Down:", pitchDown)
  321. print("AutoPilot: Roll Error:", rollError, "Left Aileron:", rollLeft, "Right Aileron:", rollRight)
  322.  
  323. else
  324. -- **Reset Values When Autopilot is Off**
  325. targetAltitude = nil -- Reset altitude lock
  326. end
  327. sleep(dt)
  328. end
  329. end
  330.  
  331. local function miscControl()
  332. while true do
  333. if controls then
  334. if controls.switchToAIM220 then
  335. weaponChoosen = "AIM-220"
  336. elseif controls.switchToGBU then
  337. weaponChoosen = "GBU-42"
  338. elseif controls.switchToThunderbolt then
  339. weaponChoosen = "thunderBolt"
  340. elseif controls.switchToAIM9 then
  341. weaponChoosen = "AIM-9"
  342. elseif controls.switchToGun then
  343. weaponChoosen = "Gun"
  344. end
  345. if weaponChoosen == "AIM-9" and controls.fire then
  346. topRouter.setOutput("top",true)
  347. else
  348. topRouter.setOutput("top",false)
  349. end
  350. if weaponChoosen == "Gun" and controls.fire then
  351. redstone.setOutput("left",true)
  352. else
  353. redstone.setOutput("left",false)
  354. end
  355. end
  356. sleep()
  357. end
  358. end
  359.  
  360.  
  361. local function modemMessage()
  362. while true do
  363. if modem then
  364. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  365. if channel == controlChannel then
  366. controls = message
  367. end
  368. else
  369. sleep()
  370. end
  371. end
  372. end
  373.  
  374. parallel.waitForAny(
  375. handleClick,
  376. modemMessage,
  377. VTOLMode,
  378. normalMode,
  379. autoPilot,
  380. miscControl
  381. )
  382.  
Advertisement
Add Comment
Please, Sign In to add comment