jackz revised this gist 9 months ago. Go to revision
1 file changed, 212 insertions
lib.lua(file created)
| @@ -0,0 +1,212 @@ | |||
| 1 | + | DRILL_RELAY_PERIPHERAL = "redstone_relay_5" | |
| 2 | + | CONTROLS_RELAY_PERIPHERAL = "redstone_relay_6" | |
| 3 | + | ENDSTOP_RELAY_PERIPHERAL = "redstone_relay_8" | |
| 4 | + | DRILL_RPM = 32 | |
| 5 | + | MOVE_RPM = 120 | |
| 6 | + | -- The dimensions of the quarry site { x/width, z/length } | |
| 7 | + | LENGTH = { 6, 6 } | |
| 8 | + | ||
| 9 | + | local phDrill = peripheral.wrap(DRILL_RELAY_PERIPHERAL) | |
| 10 | + | local phCtrl = peripheral.wrap(CONTROLS_RELAY_PERIPHERAL) | |
| 11 | + | local phEnd = peripheral.wrap(ENDSTOP_RELAY_PERIPHERAL) | |
| 12 | + | ||
| 13 | + | local DRILL_CLUTCH = "right" -- Activates drill if unpowered | |
| 14 | + | local DRILL_GEARSHIFT = "left" -- Moves drill up if powered | |
| 15 | + | ||
| 16 | + | local MOVE_CLUTCH = "left" -- Activates gantry if unpowered | |
| 17 | + | local MOVE_GEARSHIFT = "right" -- Moves gantry backwards if powered, forward if not powered | |
| 18 | + | local MOVE_INVERT = "front" -- Switches axis Z to X | |
| 19 | + | ||
| 20 | + | local ENDSTOP_X = "left" -- Detects end of X axis | |
| 21 | + | local ENDSTOP_Z = "right" -- Detects end of Z axis, home | |
| 22 | + | local HOME_TIMEOUT_ITERATIONS = 30 | |
| 23 | + | ||
| 24 | + | ||
| 25 | + | -- END CONFIG | |
| 26 | + | ||
| 27 | + | local function pick(value, a, b) | |
| 28 | + | if value then | |
| 29 | + | return a | |
| 30 | + | else | |
| 31 | + | return b | |
| 32 | + | end | |
| 33 | + | end | |
| 34 | + | local function clamp(val, min, max) | |
| 35 | + | if val < min then | |
| 36 | + | return min | |
| 37 | + | elseif val > max then | |
| 38 | + | return max | |
| 39 | + | else | |
| 40 | + | return val | |
| 41 | + | end | |
| 42 | + | end | |
| 43 | + | BLOCKS_PER_TICK = clamp(MOVE_RPM / 512, -0.49, 0.49) | |
| 44 | + | local function calculateSeconds(numBlocks) | |
| 45 | + | return ((math.abs(numBlocks)) * BLOCKS_PER_TICK) + BLOCKS_PER_TICK | |
| 46 | + | end | |
| 47 | + | ||
| 48 | + | local function moveGantry(speed, invert) | |
| 49 | + | if invert then | |
| 50 | + | phCtrl.setOutput(MOVE_GEARSHIFT, true) | |
| 51 | + | end | |
| 52 | + | phCtrl.setOutput(MOVE_CLUTCH, false) | |
| 53 | + | sleep(speed) | |
| 54 | + | phCtrl.setOutput(MOVE_CLUTCH, true) | |
| 55 | + | phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 56 | + | end | |
| 57 | + | ||
| 58 | + | function MoveX(blocks) | |
| 59 | + | local sec = calculateSeconds(blocks) | |
| 60 | + | -- Set inverse control to switch to X axis | |
| 61 | + | phCtrl.setOutput(MOVE_INVERT, true) | |
| 62 | + | moveGantry(sec, blocks < 0) | |
| 63 | + | phCtrl.setOutput(MOVE_INVERT, false) | |
| 64 | + | end | |
| 65 | + | function MoveZ(blocks) | |
| 66 | + | local sec = calculateSeconds(blocks) | |
| 67 | + | moveGantry(sec, blocks < 0) | |
| 68 | + | end | |
| 69 | + | --- Move the drill up or down N blocks | |
| 70 | + | function MoveDrill(blocks) | |
| 71 | + | local sec = calculateSeconds(blocks) | |
| 72 | + | if blocks > 0 then | |
| 73 | + | phDrill.setOutput(DRILL_GEARSHIFT, true) | |
| 74 | + | end | |
| 75 | + | sleep(sec) | |
| 76 | + | phDrill.setOutput(DRILL_CLUTCH, true) | |
| 77 | + | phDrill.setOutput(DRILL_GEARSHIFT, false) | |
| 78 | + | end | |
| 79 | + | ||
| 80 | + | function Home() | |
| 81 | + | -- Bring the drill up | |
| 82 | + | phDrill.setOutput(DRILL_CLUTCH, false) | |
| 83 | + | phDrill.setOutput(DRILL_GEARSHIFT, true) | |
| 84 | + | ||
| 85 | + | io.write("Homing Z-axis.. ") | |
| 86 | + | local i = 0 | |
| 87 | + | if phEnd.getInput(ENDSTOP_Z) then | |
| 88 | + | MoveZ(1) | |
| 89 | + | i = i + 1 | |
| 90 | + | if i > HOME_TIMEOUT_ITERATIONS then | |
| 91 | + | error("Home X Timed out") | |
| 92 | + | end | |
| 93 | + | -- if ENDSTOP still active: at the end | |
| 94 | + | -- if ENDSTOP not active: at the start | |
| 95 | + | end | |
| 96 | + | -- Move back | |
| 97 | + | repeat | |
| 98 | + | MoveZ(-1) | |
| 99 | + | until phEnd.getInput(ENDSTOP_Z) | |
| 100 | + | print("done") | |
| 101 | + | ||
| 102 | + | io.write("Homing X-axis.. ") | |
| 103 | + | i = 0 | |
| 104 | + | MoveX(-1) | |
| 105 | + | repeat | |
| 106 | + | MoveX(1) | |
| 107 | + | i = i + 1 | |
| 108 | + | if i > HOME_TIMEOUT_ITERATIONS then | |
| 109 | + | error("Home X Timed out") | |
| 110 | + | end | |
| 111 | + | until phEnd.getInput(ENDSTOP_X) | |
| 112 | + | print("done") | |
| 113 | + | Reset() | |
| 114 | + | end | |
| 115 | + | ||
| 116 | + | ||
| 117 | + | -- Tries to auto determine size, must be called after Home() | |
| 118 | + | function CalculateSize() | |
| 119 | + | local x = 2 -- timing issues mean its off by 2 | |
| 120 | + | local z = 2 | |
| 121 | + | io.write("X: ") | |
| 122 | + | repeat | |
| 123 | + | MoveX(-1) | |
| 124 | + | x = x + 1 | |
| 125 | + | until phEnd.getInput(ENDSTOP_X) | |
| 126 | + | print(x) | |
| 127 | + | ||
| 128 | + | sleep(1) | |
| 129 | + | ||
| 130 | + | io.write("Z: ") | |
| 131 | + | repeat | |
| 132 | + | MoveZ(1) | |
| 133 | + | z = z + 1 | |
| 134 | + | until phEnd.getInput(ENDSTOP_Z) | |
| 135 | + | print(z) | |
| 136 | + | ||
| 137 | + | return { x, z } | |
| 138 | + | end | |
| 139 | + | ||
| 140 | + | function IsAtEndstop(type) | |
| 141 | + | if type == "X" then | |
| 142 | + | return phEnd.getInput(ENDSTOP_X) | |
| 143 | + | elseif type == "Z" then | |
| 144 | + | return phEnd.getInput(ENDSTOP_Z) | |
| 145 | + | else | |
| 146 | + | error("invalid type") | |
| 147 | + | end | |
| 148 | + | end | |
| 149 | + | ||
| 150 | + | -- Mines a layer, assuming at home | |
| 151 | + | function MineLayer(width, length) | |
| 152 | + | local isRight = -1 | |
| 153 | + | local z = 1 | |
| 154 | + | phCtrl.setOutput(MOVE_CLUTCH, false) | |
| 155 | + | repeat | |
| 156 | + | print("Start Line", z) | |
| 157 | + | -- phCtrl.setOutput(MOVE_CLUTCH, false) | |
| 158 | + | phCtrl.setOutput(MOVE_INVERT, true) -- use x axis | |
| 159 | + | -- phCtrl.setOutput(MOVE_GEARSHIFT, true) | |
| 160 | + | -- phCtrl.setOutput(MOVE_GEARSHIFT, (not isLeft) and true or false) | |
| 161 | + | io.write("X: moving") | |
| 162 | + | io.write((width * isRight) .. " blocks..") | |
| 163 | + | ||
| 164 | + | MoveX(width * isRight) | |
| 165 | + | isRight = pick(isRight > 0, -1, 1) | |
| 166 | + | -- repeat until os.pullEvent("redstone") and phEnd.getInput(ENDSTOP_X) | |
| 167 | + | print("Done") | |
| 168 | + | ||
| 169 | + | -- io.write("X: moving right.. ") | |
| 170 | + | -- phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 171 | + | -- repeat until os.pullEvent("redstone") and phEnd.getInput(ENDSTOP_X) | |
| 172 | + | -- print("Done") | |
| 173 | + | -- isLeft = not isLeft | |
| 174 | + | -- repeat | |
| 175 | + | -- sleep(calculateSeconds(2)) | |
| 176 | + | -- -- phCtrl.setOutput(MOVE_CLUTCH, true) | |
| 177 | + | -- -- phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 178 | + | -- x = x + 1 | |
| 179 | + | -- until phEnd.getInput(ENDSTOP_X) | |
| 180 | + | ||
| 181 | + | -- -- reverse | |
| 182 | + | -- phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 183 | + | -- repeat | |
| 184 | + | -- sleep(0.2) | |
| 185 | + | -- -- phCtrl.setOutput(MOVE_CLUTCH, true) | |
| 186 | + | -- -- phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 187 | + | -- x = x + 1 | |
| 188 | + | -- until phEnd.getInput(ENDSTOP_X) | |
| 189 | + | ||
| 190 | + | -- advance Z | |
| 191 | + | -- z = z + 1 | |
| 192 | + | io.write("Z: advancing 1.. ") | |
| 193 | + | phCtrl.setOutput(MOVE_CLUTCH, false) | |
| 194 | + | phCtrl.setOutput(MOVE_INVERT, false) -- use z axis | |
| 195 | + | phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 196 | + | -- MoveZ(-4) | |
| 197 | + | sleep(calculateSeconds(1)) | |
| 198 | + | phCtrl.setOutput(MOVE_CLUTCH, true) | |
| 199 | + | z = z + 1 | |
| 200 | + | print("Done") | |
| 201 | + | -- sleep(0.4) | |
| 202 | + | until phEnd.getInput(ENDSTOP_Z) | |
| 203 | + | end | |
| 204 | + | ||
| 205 | + | function Reset() | |
| 206 | + | phDrill.setOutput(DRILL_CLUTCH, true) | |
| 207 | + | phDrill.setOutput(DRILL_GEARSHIFT, false) | |
| 208 | + | ||
| 209 | + | phCtrl.setOutput(MOVE_CLUTCH, true) | |
| 210 | + | phCtrl.setOutput(MOVE_GEARSHIFT, false) | |
| 211 | + | phCtrl.setOutput(MOVE_INVERT, false) | |
| 212 | + | end | |
Newer
Older