zamoth

Untitled

Jul 25th, 2025 (edited)
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. -- wrap your peripherals
  2. local ctrl   = peripheral.wrap("left")
  3. local stress = peripheral.wrap("top")
  4.  
  5. -- parameters
  6. local maxSpeed  = 256     -- absolute RPM limit
  7. local minSpeed  = 1       -- keep it turning
  8. local loopDelay = 0.5     -- seconds between adjustments
  9.  
  10. -- initialize targetSpeed
  11. local targetSpeed = math.max(minSpeed, math.min(maxSpeed, ctrl.getTargetSpeed() or minSpeed))
  12. ctrl.setTargetSpeed(targetSpeed)
  13.  
  14. -- control loop
  15. while true do
  16.   -- read current values
  17.   local currentStress = stress.getStress()
  18.   local cap           = stress.getStressCapacity()
  19.   -- get what the controller currently has set
  20.   local currentSpeed  = ctrl.getTargetSpeed() or targetSpeed
  21.  
  22.   -- compute stress per RPM directly
  23.   local stressPerRPM = currentStress / currentSpeed
  24.  
  25.   -- compute the RPM that would put us right at capacity
  26.   local idealSpeed = math.floor(cap / stressPerRPM)
  27.  
  28.   -- clamp to min/max
  29.   idealSpeed = math.max(minSpeed, math.min(maxSpeed, idealSpeed))
  30.  
  31.   -- only update if it actually changes
  32.   if idealSpeed ~= currentSpeed then
  33.     targetSpeed = idealSpeed
  34.     ctrl.setTargetSpeed(targetSpeed)
  35.        
  36.         -- print status
  37.     print(string.format(
  38.       "Stress: %d/%d (%.1f%%) | %.4f stress/RPM | Speed: %d RPM",
  39.       currentStress, cap, (currentStress/cap)*100, stressPerRPM, targetSpeed
  40.     ))
  41.   end
  42.  
  43.   os.sleep(loopDelay)
  44. end
  45.  
Advertisement
Add Comment
Please, Sign In to add comment