MegiRicky

drivePL-01

Aug 12th, 2024 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. -- drive PL-01.lua
  2. local modem = peripheral.find("modem") or error("No modem attached", 0)
  3. local left = peripheral.wrap("left")
  4. local right = peripheral.wrap("right")
  5. local suspensionMotor = peripheral.find("electric_motor")
  6.  
  7. print("Input the controlChannel, default = 500")
  8. local controlChannel = io.read()
  9. if controlChannel == "" then
  10. controlChannel = 500
  11. end
  12. controlChannel = tonumber(controlChannel)
  13. modem.open(controlChannel)
  14.  
  15. local RPM = 0
  16. local turnLevel = 0
  17.  
  18. -- Redstone sides for cannon and autocannon
  19. local cannonSide = "top"
  20. local autocannonSide = "bottom"
  21.  
  22. print("Tank controller started.")
  23.  
  24. local controls = {
  25. accelerate = false,
  26. decelerate = false,
  27. turnLeft = false,
  28. turnRight = false,
  29. suspensionUp = false,
  30. suspensionDown = false,
  31. fireCannon = false,
  32. fireAutocannon = false
  33. }
  34.  
  35. -- Function to set the speed based on current RPM and turn level
  36. local function setSpeed()
  37. while true do
  38. local currentLeft = left.getTargetSpeed()
  39. local currentRight = right.getTargetSpeed()
  40.  
  41. if turnLevel == 0 then
  42. if RPM ~= currentLeft then
  43. left.setTargetSpeed(-RPM)
  44. end
  45. if RPM ~= currentRight then
  46. right.setTargetSpeed(-RPM)
  47. end
  48. elseif turnLevel == 1 then
  49. if true then
  50. left.setTargetSpeed(-RPM / 1)
  51. end
  52. if true then
  53. right.setTargetSpeed(-RPM / 2)
  54. end
  55. elseif turnLevel == -1 then
  56. if true then
  57. left.setTargetSpeed(-RPM / 2)
  58. end
  59. if true then
  60. right.setTargetSpeed(-RPM / 1)
  61. end
  62. elseif turnLevel == 2 then
  63. if currentLeft ~= 32 then
  64. left.setTargetSpeed(-64)
  65. end
  66. if currentRight ~= -32 then
  67. right.setTargetSpeed(64)
  68. end
  69. elseif turnLevel == -2 then
  70. if currentLeft ~= -32 then
  71. left.setTargetSpeed(64)
  72. end
  73. if currentRight ~= 32 then
  74. right.setTargetSpeed(-64)
  75. end
  76. end
  77.  
  78. sleep()
  79. end
  80. end
  81.  
  82. local function executeControls()
  83. while true do
  84. if controls.accelerate then
  85. RPM = RPM + 64
  86. if controls.turnLeft then
  87. turnLevel = -1
  88. elseif controls.turnRight then
  89. turnLevel = 1
  90. else
  91. turnLevel = 0
  92. end
  93. elseif controls.decelerate then
  94. RPM = RPM - 64
  95. if controls.turnLeft then
  96. turnLevel = -1
  97. elseif controls.turnRight then
  98. turnLevel = 1
  99. else
  100. turnLevel = 0
  101. end
  102. else
  103. if controls.turnLeft then
  104. turnLevel = -2
  105. elseif controls.turnRight then
  106. turnLevel = 2
  107. else
  108. turnLevel = 0
  109. end
  110. end
  111.  
  112. -- Gradually slow down if neither accelerate nor decelerate is pressed
  113. if not(controls.accelerate or controls.decelerate) then
  114. if RPM > 0 then
  115. RPM = RPM - 32
  116. elseif RPM < 0 then
  117. RPM = RPM + 32
  118. end
  119. end
  120.  
  121. -- Clamp RPM to a maximum and minimum value
  122. if RPM > 256 then
  123. RPM = 256
  124. elseif RPM < -256 then
  125. RPM = -256
  126. end
  127.  
  128. -- Suspension control
  129. if controls.suspensionUp then
  130. suspensionMotor.setSpeed(128)
  131. elseif controls.suspensionDown then
  132. suspensionMotor.setSpeed(-128)
  133. elseif not(controls.suspensionDown or controls.suspensionUp) then
  134. suspensionMotor.setSpeed(0)
  135. end
  136.  
  137. -- Fire controls
  138. if controls.fireCannon then
  139. print("Firing Cannon")
  140. redstone.setOutput("bottom", true)
  141. else
  142. redstone.setOutput("bottom", false)
  143. end
  144.  
  145. if controls.fireAutocannon then
  146. print("Firing Autocannon")
  147. redstone.setOutput("front", true)
  148. else
  149. redstone.setOutput("front", false)
  150. end
  151.  
  152. sleep() -- Adjust the delay for control responsiveness
  153. end
  154. end
  155.  
  156. -- Function to listen for modem messages and update controls
  157. local function modem_message()
  158. while true do
  159. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  160. if channel == controlChannel then
  161. controls = message
  162. end
  163. end
  164. end
  165.  
  166. -- Run the functions in parallel
  167. parallel.waitForAll(
  168. modem_message,
  169. executeControls,
  170. setSpeed
  171. )
  172.  
Advertisement
Add Comment
Please, Sign In to add comment