MegiRicky

driveM144.lua

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