Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Reactor Controller
- --Version 3
- --Dependencies
- --ReactorConfigReader V1
- --ReactorMonitor V1
- --Configuration
- local ConfigLocation = "disk/ReactorV3.settings";
- local ReactorConfigReader = require("ReactorConfigReader");
- local ReactorMonitor = require("ReactorMonitor");
- local config = nil;
- --Status types
- local Status = {
- IDLE = 0;
- CHARGING = 1;
- HIGH = 2;
- }
- --Status variables
- local State = {
- PrevEnergyLevel = nil;
- PrevSleepTime = nil;
- CurrentSystemStatus = Status.CHARGING;
- CurrentSystemStatusStart = nil;
- PrevSystemStatus = Status.IDLE;
- PrevSystemStatusStart = nil;
- PrevSystemStatusEnd = nil;
- }
- --functions
- local function ThrowFatalError(message)
- if(config ~= nil and config.monitor ~= nil) then
- config.monitor.setTextScale(3);
- config.monitor.clear();
- config.monitor.setTextColor(colors.purple);
- config.monitor.write("Error " .. message);
- end
- error(message);
- end
- local function Initialize()
- print("Reactor Control Initializing");
- config = ReactorConfigReader:LoadConfiguration(ConfigLocation);
- if config == nil then
- ThrowFatalError("Failed to Load Configuration");
- return;
- end
- State.PrevEnergyLevel = config.reactor.getEnergyStored() / config.maxPowerStore * 100;
- State.PrevSleepTime = 0;
- State.CurrentSystemStatusStart = os.clock();
- State.PrevSystemStatusStart = os.clock();
- State.PrevSystemStatusEnd = os.clock();
- config.reactor.setActive(true);
- config.reactor.setAllControlRodLevels(config.optimalInsertionPercent);
- sleep(1);
- end
- --increases rod length / decreases energy output
- local function IncreaseRodLength()
- local numRods = config.reactor.getNumberOfControlRods();
- local rodLevel = config.reactor.getControlRodLevel(0);
- if rodLevel >= config.optimalInsertionPercent then
- return;
- end
- config.reactor.setAllControlRodLevels(rodLevel + 1);
- end
- --decreases rod length / increases energy output
- local function DecreaseRodLength()
- local numRods = config.reactor.getNumberOfControlRods();
- local rodLevel = config.reactor.getControlRodLevel(0);
- if rodLevel == 0 then
- return;
- end
- config.reactor.setAllControlRodLevels(rodLevel - 1);
- end
- local function IsAllRodsOptimalLength()
- sleep(1);
- local numRods = config.reactor.getNumberOfControlRods();
- for i = 0, numRods - 1 do
- local rodLevel = config.reactor.getControlRodLevel(i)
- if rodLevel < config.optimalInsertionPercent then
- return false;
- end
- end
- return true;
- end
- local function SetNewState(newState, firstChange)
- --set archival vars
- if(firstChange) then
- State.PrevSystemStatus = State.CurrentSystemStatus;
- State.PrevSystemStatusStart = State.CurrentSystemStatusStart;
- State.PrevSystemStatusEnd = os.clock();
- end
- State.CurrentSystemStatus = newState;
- State.CurrentSystemStatusStart = os.clock();
- if State.CurrentSystemStatus == Status.IDLE then
- config.reactor.setAllControlRodLevels(100);
- elseif State.CurrentSystemStatus == Status.CHARGING then
- config.reactor.setAllControlRodLevels(config.optimalInsertionPercent);
- end
- end
- --returns the sleep time that should then be followed
- local function UpdateReactorState()
- State.PrevEnergyLevel = config.reactor.getEnergyStored() / config.maxPowerStore * 100;
- local firstChange = true;
- local change = true;
- while(change) do
- change = false;
- sleep(1);
- --if idle
- if State.CurrentSystemStatus == Status.IDLE then
- if State.PrevEnergyLevel <= config.powerLowerBounds then
- change = true;
- SetNewState(Status.CHARGING,firstChange);
- firstChange = false;
- else
- return config.normalSleepTimeSeconds;
- end
- elseif State.CurrentSystemStatus == Status.CHARGING then
- if State.PrevEnergyLevel >= config.powerUpperBounds then
- change = true;
- SetNewState(Status.IDLE,firstChange);
- firstChange = false;
- elseif State.PrevEnergyLevel <= config.powerHighLBounds then
- change = true;
- SetNewState(Status.HIGH,firstChange);
- firstChange = false;
- else
- return config.normalSleepTimeSeconds;
- end
- elseif State.CurrentSystemStatus == Status.HIGH then
- if State.PrevEnergyLevel >= config.powerUpperBounds then
- change = true;
- SetNewState(Status.IDLE, firstChange)
- firstChange = false;
- elseif State.PrevEnergyLevel >= config.powerHighUBounds then
- IncreaseRodLength();
- if IsAllRodsOptimalLength() then
- change = true;
- SetNewState(Status.CHARGING,firstChange);
- firstChange = false;
- else
- return config.frequentSleepTimeSeconds;
- end
- elseif State.PrevEnergyLevel <= config.powerHighLBounds then
- DecreaseRodLength();
- return config.frequentSleepTimeSeconds;
- else
- return config.frequentSleepTimeSeconds;
- end
- end
- end
- return config.normalSleepTimeSeconds;
- end
- --Main loop
- Initialize();
- print("Starting Reactor Monitoring");
- while true do
- local sleepTime = UpdateReactorState();
- sleep(1);
- State.PrevSleepTime = sleepTime;
- ReactorMonitor.updateMonitor(config, State);
- sleep(sleepTime);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement