MegiRicky

CC:VS FBW redstone hub

Jul 21st, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. local modem = peripheral.wrap("bottom")
  2. local engine = peripheral.find("Create_RotationSpeedController")
  3. local routerLeft = peripheral.wrap("left")
  4. local routerRight = peripheral.wrap("right")
  5. local gear = peripheral.find("Create_SequencedGearshift")
  6.  
  7. local throttleChannel = 400
  8. local rudderChannel = 401
  9. local elevatorChannel = 402
  10. local aileronChannel = 403
  11. local landingGearChannel = 404
  12. local flapChannel = 405
  13.  
  14. modem.open(throttleChannel)
  15. modem.open(rudderChannel)
  16. modem.open(elevatorChannel)
  17. modem.open(aileronChannel)
  18. modem.open(landingGearChannel)
  19. modem.open(flapChannel)
  20.  
  21.  
  22. local engineSpeed = 0
  23. local rudderControl = "stop"
  24. local aileronControl = 0
  25. local elevatorControl = 0
  26. local currentGear = "down"
  27. local targetGear = "down"
  28. local flap = 0
  29.  
  30. local function listenForCoordinates()
  31. while true do
  32. local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  33. if senderChannel == throttleChannel then
  34. engineSpeed = message
  35. elseif senderChannel == rudderChannel then
  36. rudderControl = message
  37. elseif senderChannel == elevatorChannel then
  38. elevatorControl = message
  39. elseif senderChannel == aileronChannel then
  40. aileronControl = message
  41. elseif senderChannel == landingGearChannel then
  42. targetGear = message
  43. elseif senderChannel == flapChannel then
  44. flap = message
  45. end
  46. end
  47. end
  48.  
  49. routerLeft.setOutput("left", false)
  50. routerLeft.setOutput("top", false)
  51. routerLeft.setOutput("back", false)
  52. routerLeft.setOutput("bottom", false)
  53. routerRight.setOutput("left", false)
  54. routerRight.setOutput("top", false)
  55. routerRight.setOutput("back", false)
  56. routerRight.setOutput("bottom", false)
  57.  
  58. parallel.waitForAny(
  59. listenForCoordinates,
  60. function()
  61. while true do
  62. -- Engine control
  63. print("throttle: " .. engineSpeed)
  64. print("rudderControl: " .. rudderControl)
  65. print("elevatorControl: " .. elevatorControl)
  66. print("aileronControl: " .. aileronControl)
  67. print("Current Gear: "..currentGear)
  68. print("Target Gear: "..targetGear)
  69. print("Flap: "..flap)
  70. engine.setTargetSpeed(engineSpeed)
  71. if engineSpeed < -128 then
  72. redstone.setOutput("back",true)
  73. else
  74. redstone.setOutput("back",false)
  75. end
  76. --[[mouse and keyboard
  77. if aileronControl == "left" then
  78. print("rolling left")
  79. routerLeft.setOutput("front", true)
  80. routerRight.setOutput("front", false)
  81. elseif aileronControl == "right" then
  82. print("rolling right")
  83. routerRight.setOutput("front", true)
  84. routerLeft.setOutput("front", false)
  85. else
  86. routerRight.setOutput("front", false)
  87. routerLeft.setOutput("front", false)
  88. end
  89.  
  90. -- Rudder control (yaw)
  91. if rudderControl == "left" then
  92. print("yaw turning left")
  93. routerLeft.setOutput("top", true)
  94. routerRight.setOutput("top", false)
  95. elseif rudderControl == "right" then
  96. print("yaw turning right")
  97. routerRight.setOutput("top", true)
  98. routerLeft.setOutput("top", false)
  99. else
  100. routerRight.setOutput("top", false)
  101. routerLeft.setOutput("top", false)
  102. end
  103.  
  104. -- Elevator control (pitch)
  105. if elevatorControl == "up" then
  106. print("pitching up")
  107. redstone.setOutput("top", true)
  108. redstone.setOutput("front",false)
  109. elseif elevatorControl == "down" then
  110. print("pitching down")
  111. redstone.setOutput("top", false)
  112. redstone.setOutput("front",true)
  113. else
  114. redstone.setOutput("top", false)
  115. redstone.setOutput("front",false)
  116. end]]
  117. if aileronControl < 0 then
  118. routerRight.setAnalogOutput("front",math.abs(aileronControl))
  119. routerLeft.setOutput("front",false)
  120. elseif aileronControl > 0 then
  121. routerLeft.setAnalogOutput("front",math.abs(aileronControl))
  122. routerRight.setOutput("front",false)
  123. else
  124. routerLeft.setOutput("front",false)
  125. routerRight.setOutput("front",false)
  126. end
  127.  
  128. if rudderControl == "left" then
  129. print("yaw turning left")
  130. routerLeft.setOutput("top", true)
  131. routerRight.setOutput("top", false)
  132. elseif rudderControl == "right" then
  133. print("yaw turning right")
  134. routerRight.setOutput("top", true)
  135. routerLeft.setOutput("top", false)
  136. else
  137. routerRight.setOutput("top", false)
  138. routerLeft.setOutput("top", false)
  139. end
  140.  
  141. if elevatorControl > 0 then
  142. print("pitching up")
  143. redstone.setAnalogOutput("top", math.abs(elevatorControl))
  144. redstone.setOutput("front",false)
  145. elseif elevatorControl < 0 then
  146. print("pitching down")
  147. redstone.setOutput("top", false)
  148. redstone.setAnalogOutput("front",math.abs(elevatorControl))
  149. else
  150. redstone.setOutput("top", false)
  151. redstone.setOutput("front",false)
  152. end
  153.  
  154. if not(currentGear == targetGear) then
  155. routerRight.setOutput("bottom",true)
  156. sleep(0.1)
  157. routerRight.setOutput("bottom",false)
  158. if currentGear == "down" then
  159. currentGear = "up"
  160. elseif currentGear == "up" then
  161. currentGear = "down"
  162. end
  163. end
  164.  
  165. routerLeft.setAnalogOutput("back",flap)
  166.  
  167. sleep(0.2)
  168. end
  169. end
  170. )
Advertisement
Add Comment
Please, Sign In to add comment