ecco7777

CC Create Aeronamics Car

Apr 26th, 2026
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. -- Robot Controller
  2. -- Peripheral: navigator (left), bundled cable (front)
  3.  
  4. local nav = peripheral.wrap("left")
  5. local CABLE_SIDE = "front"
  6.  
  7. -- Bundle color bitmasks
  8. local YELLOW = colors.yellow
  9. local BLACK = colors.black
  10. local BLUE = colors.blue
  11. local ORANGE = colors.orange
  12. local MAGENTA = colors.magenta
  13. local RED = colors.red
  14.  
  15. local doubleSpeed = false
  16. local shooting = false
  17. local running = true
  18.  
  19. -- ─── Bundle helpers ───────────────────────────────────────────────
  20.  
  21. local function getBundle()
  22. return redstone.getBundledOutput(CABLE_SIDE)
  23. end
  24.  
  25. local function setBundle(mask)
  26. redstone.setBundledOutput(CABLE_SIDE, mask)
  27. end
  28.  
  29. local function setBits(mask)
  30. setBundle(bit.bor(getBundle(), mask))
  31. end
  32.  
  33. local function clearBits(mask)
  34. setBundle(bit.band(getBundle(), bit.bnot(mask)))
  35. end
  36.  
  37. local function setColor(color, state)
  38. if state then setBits(color) else clearBits(color) end
  39. end
  40.  
  41. -- ─── Motor control ────────────────────────────────────────────────
  42.  
  43. local function stop()
  44. -- preserve red (shooting) and orange (speed), just kill movement
  45. local preserve = 0
  46. if shooting then preserve = bit.bor(preserve, RED) end
  47. if doubleSpeed then preserve = bit.bor(preserve, ORANGE) end
  48. setBundle(bit.bor(MAGENTA, preserve))
  49. end
  50.  
  51. local function drive(direction)
  52. local mask = 0
  53. -- Left/right are swapped per hardware observation, so invert them here
  54. if direction == "forward" then
  55. mask = bit.bor(YELLOW, BLACK)
  56. elseif direction == "backward" then
  57. mask = bit.bor(YELLOW, BLACK, BLUE)
  58. elseif direction == "right" then
  59. mask = BLACK -- was YELLOW, swapped
  60. elseif direction == "left" then
  61. mask = YELLOW -- was BLACK, swapped
  62. end
  63. if doubleSpeed then mask = bit.bor(mask, ORANGE) end
  64. if shooting then mask = bit.bor(mask, RED) end
  65. setBundle(mask)
  66. end
  67.  
  68. -- ─── Angle helpers ────────────────────────────────────────────────
  69.  
  70. local function getAngle()
  71. return nav.getRelativeAngle()
  72. end
  73.  
  74. -- Returns the shortest signed delta from `from` to `to`
  75. -- positive = clockwise, negative = counter-clockwise
  76. local function shortestDelta(from, to)
  77. local d = (to - from) % 360
  78. if d > 180 then d = d - 360 end
  79. return d
  80. end
  81.  
  82. local function turn90(direction)
  83. local startAngle = getAngle()
  84.  
  85. drive(direction)
  86.  
  87. while true do
  88. os.sleep(0.02)
  89. local traveled = math.abs(shortestDelta(startAngle, getAngle()))
  90. print(traveled)
  91. if traveled >= 87 then break end
  92. end
  93.  
  94. stop()
  95. end
  96.  
  97. -- ─── Timed burst ──────────────────────────────────────────────────
  98.  
  99. local function burst(direction, seconds)
  100. drive(direction)
  101. os.sleep(seconds)
  102. stop()
  103. end
  104.  
  105. -- ─── Instruction set runner ───────────────────────────────────────
  106.  
  107. local function runInstructions(str)
  108. for i = 1, #str do
  109. local c = str:sub(i, i)
  110. if c == "f" then
  111. burst("forward", 1)
  112. elseif c == "b" then
  113. burst("backward", 1)
  114. elseif c == "l" then
  115. turn90("left")
  116. elseif c == "r" then
  117. turn90("right")
  118. elseif c == "d" then
  119. doubleSpeed = not doubleSpeed
  120. print("Double speed: " .. tostring(doubleSpeed))
  121. end
  122. os.sleep(0.1)
  123. end
  124. print("Instruction set complete.")
  125. end
  126.  
  127. -- ─── Key state tracking ───────────────────────────────────────────
  128.  
  129. local heldKeys = {}
  130.  
  131. local function updateDrive()
  132. local w = heldKeys[keys.w]
  133. local s = heldKeys[keys.s]
  134. local a = heldKeys[keys.a]
  135. local d = heldKeys[keys.d]
  136.  
  137. if w and not s and not a and not d then
  138. drive("forward")
  139. elseif s and not w and not a and not d then
  140. drive("backward")
  141. elseif a and not d and not w and not s then
  142. drive("left")
  143. elseif d and not a and not w and not s then
  144. drive("right")
  145. elseif not w and not s and not a and not d then
  146. stop()
  147. end
  148. end
  149.  
  150. -- ─── Input loop ───────────────────────────────────────────────────
  151.  
  152. local function inputLoop()
  153. while running do
  154. local event, key = os.pullEvent()
  155.  
  156. if event == "key" then
  157. if key == keys.w or key == keys.s or
  158. key == keys.a or key == keys.d then
  159. heldKeys[key] = true
  160. updateDrive()
  161.  
  162. elseif key == keys.up then
  163. parallel.waitForAny(
  164. function() burst("forward", 1) end,
  165. function() os.pullEvent("key") end
  166. )
  167.  
  168. elseif key == keys.down then
  169. parallel.waitForAny(
  170. function() burst("backward", 1) end,
  171. function() os.pullEvent("key") end
  172. )
  173.  
  174. elseif key == keys.left then
  175. parallel.waitForAny(
  176. function() turn90("left") end,
  177. function() os.pullEvent("key") end
  178. )
  179.  
  180. elseif key == keys.right then
  181. parallel.waitForAny(
  182. function() turn90("right") end,
  183. function() os.pullEvent("key") end
  184. )
  185.  
  186. elseif key == keys.x then
  187. doubleSpeed = not doubleSpeed
  188. -- update live if motors are running
  189. local w = heldKeys[keys.w]; local s = heldKeys[keys.s]
  190. local a = heldKeys[keys.a]; local d = heldKeys[keys.d]
  191. if w or s or a or d then updateDrive() else stop() end
  192. print("Double speed: " .. tostring(doubleSpeed))
  193.  
  194. elseif key == keys.r then
  195. shooting = not shooting
  196. setColor(RED, shooting)
  197. print("Shooting: " .. tostring(shooting))
  198.  
  199. elseif key == keys.i then
  200. stop()
  201. io.write("Enter instruction set: ")
  202. local input = io.read()
  203. if input and #input > 0 then
  204. runInstructions(input:lower())
  205. end
  206.  
  207. elseif key == keys.q then
  208. running = false
  209. stop()
  210. print("Shutting down.")
  211. end
  212.  
  213. elseif event == "key_up" then
  214. if key == keys.w or key == keys.s or
  215. key == keys.a or key == keys.d then
  216. heldKeys[key] = nil
  217. updateDrive()
  218. end
  219. end
  220. end
  221. end
  222.  
  223. -- ─── Main ─────────────────────────────────────────────────────────
  224.  
  225. stop()
  226. print("Robot controller started.")
  227. print("WASD = drive | arrows = burst/turn90 | X = speed | R = shoot | I = instructions | Q = quit")
  228.  
  229. inputLoop()
Advertisement
Add Comment
Please, Sign In to add comment