lib.lua
· 5.7 KiB · Lua
Raw
DRILL_RELAY_PERIPHERAL = "redstone_relay_5"
CONTROLS_RELAY_PERIPHERAL = "redstone_relay_6"
ENDSTOP_RELAY_PERIPHERAL = "redstone_relay_8"
DRILL_RPM = 32
MOVE_RPM = 120
-- The dimensions of the quarry site { x/width, z/length }
LENGTH = { 6, 6 }
local phDrill = peripheral.wrap(DRILL_RELAY_PERIPHERAL)
local phCtrl = peripheral.wrap(CONTROLS_RELAY_PERIPHERAL)
local phEnd = peripheral.wrap(ENDSTOP_RELAY_PERIPHERAL)
local DRILL_CLUTCH = "right" -- Activates drill if unpowered
local DRILL_GEARSHIFT = "left" -- Moves drill up if powered
local MOVE_CLUTCH = "left" -- Activates gantry if unpowered
local MOVE_GEARSHIFT = "right" -- Moves gantry backwards if powered, forward if not powered
local MOVE_INVERT = "front" -- Switches axis Z to X
local ENDSTOP_X = "left" -- Detects end of X axis
local ENDSTOP_Z = "right" -- Detects end of Z axis, home
local HOME_TIMEOUT_ITERATIONS = 30
-- END CONFIG
local function pick(value, a, b)
if value then
return a
else
return b
end
end
local function clamp(val, min, max)
if val < min then
return min
elseif val > max then
return max
else
return val
end
end
BLOCKS_PER_TICK = clamp(MOVE_RPM / 512, -0.49, 0.49)
local function calculateSeconds(numBlocks)
return ((math.abs(numBlocks)) * BLOCKS_PER_TICK) + BLOCKS_PER_TICK
end
local function moveGantry(speed, invert)
if invert then
phCtrl.setOutput(MOVE_GEARSHIFT, true)
end
phCtrl.setOutput(MOVE_CLUTCH, false)
sleep(speed)
phCtrl.setOutput(MOVE_CLUTCH, true)
phCtrl.setOutput(MOVE_GEARSHIFT, false)
end
function MoveX(blocks)
local sec = calculateSeconds(blocks)
-- Set inverse control to switch to X axis
phCtrl.setOutput(MOVE_INVERT, true)
moveGantry(sec, blocks < 0)
phCtrl.setOutput(MOVE_INVERT, false)
end
function MoveZ(blocks)
local sec = calculateSeconds(blocks)
moveGantry(sec, blocks < 0)
end
--- Move the drill up or down N blocks
function MoveDrill(blocks)
local sec = calculateSeconds(blocks)
if blocks > 0 then
phDrill.setOutput(DRILL_GEARSHIFT, true)
end
sleep(sec)
phDrill.setOutput(DRILL_CLUTCH, true)
phDrill.setOutput(DRILL_GEARSHIFT, false)
end
function Home()
-- Bring the drill up
phDrill.setOutput(DRILL_CLUTCH, false)
phDrill.setOutput(DRILL_GEARSHIFT, true)
io.write("Homing Z-axis.. ")
local i = 0
if phEnd.getInput(ENDSTOP_Z) then
MoveZ(1)
i = i + 1
if i > HOME_TIMEOUT_ITERATIONS then
error("Home X Timed out")
end
-- if ENDSTOP still active: at the end
-- if ENDSTOP not active: at the start
end
-- Move back
repeat
MoveZ(-1)
until phEnd.getInput(ENDSTOP_Z)
print("done")
io.write("Homing X-axis.. ")
i = 0
MoveX(-1)
repeat
MoveX(1)
i = i + 1
if i > HOME_TIMEOUT_ITERATIONS then
error("Home X Timed out")
end
until phEnd.getInput(ENDSTOP_X)
print("done")
Reset()
end
-- Tries to auto determine size, must be called after Home()
function CalculateSize()
local x = 2 -- timing issues mean its off by 2
local z = 2
io.write("X: ")
repeat
MoveX(-1)
x = x + 1
until phEnd.getInput(ENDSTOP_X)
print(x)
sleep(1)
io.write("Z: ")
repeat
MoveZ(1)
z = z + 1
until phEnd.getInput(ENDSTOP_Z)
print(z)
return { x, z }
end
function IsAtEndstop(type)
if type == "X" then
return phEnd.getInput(ENDSTOP_X)
elseif type == "Z" then
return phEnd.getInput(ENDSTOP_Z)
else
error("invalid type")
end
end
-- Mines a layer, assuming at home
function MineLayer(width, length)
local isRight = -1
local z = 1
phCtrl.setOutput(MOVE_CLUTCH, false)
repeat
print("Start Line", z)
-- phCtrl.setOutput(MOVE_CLUTCH, false)
phCtrl.setOutput(MOVE_INVERT, true) -- use x axis
-- phCtrl.setOutput(MOVE_GEARSHIFT, true)
-- phCtrl.setOutput(MOVE_GEARSHIFT, (not isLeft) and true or false)
io.write("X: moving")
io.write((width * isRight) .. " blocks..")
MoveX(width * isRight)
isRight = pick(isRight > 0, -1, 1)
-- repeat until os.pullEvent("redstone") and phEnd.getInput(ENDSTOP_X)
print("Done")
-- io.write("X: moving right.. ")
-- phCtrl.setOutput(MOVE_GEARSHIFT, false)
-- repeat until os.pullEvent("redstone") and phEnd.getInput(ENDSTOP_X)
-- print("Done")
-- isLeft = not isLeft
-- repeat
-- sleep(calculateSeconds(2))
-- -- phCtrl.setOutput(MOVE_CLUTCH, true)
-- -- phCtrl.setOutput(MOVE_GEARSHIFT, false)
-- x = x + 1
-- until phEnd.getInput(ENDSTOP_X)
-- -- reverse
-- phCtrl.setOutput(MOVE_GEARSHIFT, false)
-- repeat
-- sleep(0.2)
-- -- phCtrl.setOutput(MOVE_CLUTCH, true)
-- -- phCtrl.setOutput(MOVE_GEARSHIFT, false)
-- x = x + 1
-- until phEnd.getInput(ENDSTOP_X)
-- advance Z
-- z = z + 1
io.write("Z: advancing 1.. ")
phCtrl.setOutput(MOVE_CLUTCH, false)
phCtrl.setOutput(MOVE_INVERT, false) -- use z axis
phCtrl.setOutput(MOVE_GEARSHIFT, false)
-- MoveZ(-4)
sleep(calculateSeconds(1))
phCtrl.setOutput(MOVE_CLUTCH, true)
z = z + 1
print("Done")
-- sleep(0.4)
until phEnd.getInput(ENDSTOP_Z)
end
function Reset()
phDrill.setOutput(DRILL_CLUTCH, true)
phDrill.setOutput(DRILL_GEARSHIFT, false)
phCtrl.setOutput(MOVE_CLUTCH, true)
phCtrl.setOutput(MOVE_GEARSHIFT, false)
phCtrl.setOutput(MOVE_INVERT, false)
end
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 |