MegiRicky

EMBTDrive

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