Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local motor = peripheral.wrap("electric_motor_1")
- local storage = peripheral.wrap("inventory_1")
- local modem = peripheral.find("modem") or error("No modem attached", 0)
- modem.open(43)
- modem.open(45)
- local totalLogs = 0
- local previousLogs = 0
- local previousTime = os.clock()
- local logConsumptionRates = {}
- local maxSamples = 10 -- Number of samples for moving average
- local isShutdown = false
- print("Thinking of Woo. Just Woo. Just Woo..")
- -- Function to calculate motor speed based on log consumption rate
- local function adjustMotorSpeed()
- local currentTime = os.clock()
- local timeElapsed = currentTime - previousTime
- -- Calculate the current total logs
- totalLogs = 0
- for slot, item in pairs(storage.list()) do
- if (item.name == "minecraft:birch_log") then
- totalLogs = totalLogs + item.count
- end
- end
- -- Calculate the rate of log consumption
- local logConsumptionRate = (previousLogs - totalLogs) / timeElapsed
- -- Add the current rate to the list of rates
- table.insert(logConsumptionRates, logConsumptionRate)
- if #logConsumptionRates > maxSamples then
- table.remove(logConsumptionRates, 1)
- end
- -- Calculate the moving average of the log consumption rates
- local sum = 0
- for _, rate in ipairs(logConsumptionRates) do
- sum = sum + rate
- end
- local averageLogConsumptionRate = sum / #logConsumptionRates
- -- Adjust motor speed based on the average log consumption rate
- local motorSpeed = math.min(math.max(averageLogConsumptionRate * 10, 0), 256) -- Scale and clamp the speed between 0 and 256
- -- Ensure motor speed increases if logs are below 3000
- if totalLogs < 3000 then
- motorSpeed = math.min(motorSpeed + 10, 256)
- end
- modem.transmit(43, 43, "BL1 " .. motor.getEnergyConsumption())
- -- Print debug information
- print("Current Time: " .. currentTime)
- print("Time Elapsed: " .. timeElapsed)
- print("Total Logs: " .. totalLogs)
- print("Previous Logs: " .. previousLogs)
- print("Log Consumption Rate: " .. logConsumptionRate)
- print("Average Log Consumption Rate: " .. averageLogConsumptionRate)
- print("Motor Speed: " .. motorSpeed)
- motor.setSpeed(motorSpeed)
- -- Update previous logs and time
- previousLogs = totalLogs
- previousTime = currentTime
- end
- -- Initial log count
- for slot, item in pairs(storage.list()) do
- if (item.name == "minecraft:birch_log") then
- totalLogs = totalLogs + item.count
- end
- end
- previousLogs = totalLogs
- -- Function to handle modem messages
- local function handleModemMessages()
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if channel == 45 then
- if message == "SHUTDOWN" then
- isShutdown = true
- elseif message == "OPERATE" then
- isShutdown = false
- end
- end
- end
- end
- -- Main loop to adjust motor speed
- local function adjustMotorSpeedLoop()
- while true do
- if not isShutdown then
- adjustMotorSpeed()
- end
- -- Wait for a short period before the next update
- sleep(0.1)
- end
- end
- -- Run both functions concurrently
- parallel.waitForAny(handleModemMessages, adjustMotorSpeedLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement