Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- wrap your peripherals
- local ctrl = peripheral.wrap("left")
- local stress = peripheral.wrap("top")
- -- parameters
- local maxSpeed = 256 -- absolute RPM limit
- local minSpeed = 1 -- keep it turning
- local loopDelay = 0.5 -- seconds between adjustments
- -- initialize targetSpeed
- local targetSpeed = math.max(minSpeed, math.min(maxSpeed, ctrl.getTargetSpeed() or minSpeed))
- ctrl.setTargetSpeed(targetSpeed)
- -- control loop
- while true do
- -- read current values
- local currentStress = stress.getStress()
- local cap = stress.getStressCapacity()
- -- get what the controller currently has set
- local currentSpeed = ctrl.getTargetSpeed() or targetSpeed
- -- compute stress per RPM directly
- local stressPerRPM = currentStress / currentSpeed
- -- compute the RPM that would put us right at capacity
- local idealSpeed = math.floor(cap / stressPerRPM)
- -- clamp to min/max
- idealSpeed = math.max(minSpeed, math.min(maxSpeed, idealSpeed))
- -- only update if it actually changes
- if idealSpeed ~= currentSpeed then
- targetSpeed = idealSpeed
- ctrl.setTargetSpeed(targetSpeed)
- -- print status
- print(string.format(
- "Stress: %d/%d (%.1f%%) | %.4f stress/RPM | Speed: %d RPM",
- currentStress, cap, (currentStress/cap)*100, stressPerRPM, targetSpeed
- ))
- end
- os.sleep(loopDelay)
- end
Advertisement
Add Comment
Please, Sign In to add comment