This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xBlizzDevious's profile picture

Improved, Modular Control System

Started by xBlizzDevious, 11 March 2014 - 01:59 AM
xBlizzDevious #1
Posted 11 March 2014 - 02:59 AM
Hey guys,

Firstly: I'm playing on FTB Monster and focusing on BigReactors and Thermal Expansion for the following code.

I've been using a rather primitive and useless reactor control system that I built for a single reactor and energy cell that would start up the reactor when the EC got below 30% full and would then cut off once the reactor got to 90% full. I then added in another energy cell and ignored it in my program and then finally, I added a second reactor. When I did that, I modified the program a small amount to allow control of the secondary reactor. The program still only monitors one, though.

Here's the code for the program I've got:
Spoiler

hec = peripheral.wrap("cofh_thermalexpansion_energycell_1")
br1 = peripheral.wrap("BigReactors-Reactor_0")
br2 = peripheral.wrap("BigReactors-Reactor_1")

local maxStore = 0
local curStore = 0
local thirty = 0
local ninety = 0
local rTemp = 0
local rCurStore = 0
local rFuelAmount = 0
local rFuelMax = 0
local rWasteAmount = 0
local rEProduced = 0
local rCRodL = 0

maxStore = hec.getMaxEnergyStored("")
thirty = (maxStore/100)*30

while true do
	curStore = hec.getEnergyStored("")
	
	rTemp = br1.getTemperature()
	rCurStore = br1.getEnergyStored()
	rFuelAmount = br1.getFuelAmount()
	rFuelMax = br1.getFuelAmountMax()
	rWasteAmount = br1.getWasteAmount()
	rEProduced = math.floor(br1.getEnergyProducedLastTick() + br2.getEnergyProducedLastTick())
	
	term.clear()
	term.setCursorPos(1,1)
	
	print("			Energy Cell: "..curStore.." / "..maxStore.." RF")
	print("")
	print("			Temperature: "..rTemp.." C")
	print("	   Energy (Reactor): "..rCurStore.." RF")
	print("				   Fuel: "..rFuelAmount.." / "..rFuelMax.." mB")
	print("				  Waste: "..rWasteAmount.." mB")
	print("	   Energy Last Tick: "..rEProduced.." RF")
	print("")

	if curStore <= thirty then
		br1.setActive(true)
  br2.setActive(true)
	elseif rCurStore >= 8000000 then
		br1.setActive(false)
  br2.setActive(false)
	end

if br1.getActive then
  print("			 Reactor is: ON")
else
  print("			 Reactor is: OFF")
end

	sleep(0.5)
end

Now, I'm trying to create a program that will allow me to add reactors in as and when I want to, along with improved energy cell handling and also display to an external monitor.

For the final product on the monitor, I want something that will display something like this (though preferably a little tidier than this version):


Reactors:			1		2		3
Temperature:		X		X		X
Energy Stored:		X		X		X
Fuel Amount:		X		X		X
Waste Amount:		X		X		X
Energy Last Tick:	X		X		X
Status:				ON		ON		OFF


Energy Cells:		1		2
Stored:				X		X
Max Storage			X		X

I've modified some code that someone helped me with for another program to iterate through attached peripherals and so I've got this:

Spoiler

local m = {}
local br = {}
local ec = {}

term.clear()
term.setCursorPos(1,1)

for a,b in pairs(peripheral.getNames()) do
	if peripheral.getType(B)/>/> == "cofh_thermalexpansion_energycell" then
		ec[#ec+1] = peripheral.wrap(B)/>/>
	end
	
	if peripheral.getType(B)/>/> == "BigReactors-Reactor" then
		br[#br+1] = peripheral.wrap(B)/>/>
	end
	
	if peripheral.getType(B)/>/> == "monitor" then
		m[#m+1] = peripheral.wrap(B)/>/>
	end
end

My aim is to have something that will then run a "while true do" loop iterating through all attached peripherals and running all relevant functions on all of the energy cells and reactors and then displaying it to all attached monitors (for easy monitoring of reactors all around my base). Each reactor should be able to switch on and off independently, however, they should really only turn on when the attached energy cells get below a certain threshold.

My main question is, basically, how would I program the loop to iterate through each "peripheral" and control it and how do I access each peripheral from the tables I've created?

Thanks for any help!
xBlizzDevious #2
Posted 14 March 2014 - 12:14 AM
I was searching for some more information, specifically on BigReactors and I happened to stumble on this program. It's really good! It controls a reactor really well and changes the control rods to suit how much power is in the system and needs to be generated to keep it relatively stable. I've currently got 4 separate computers set up - one on each of my reactors - and they work quite well, but I want to consolidate that all to a single computer running on a network and displaying the information from all reactors to a single monitor. I also want it to still be modular so that I can add more reactors as and when I need them.

Here's the pastebin link: http://pastebin.com/V0eueruY

And here's the Reddit post that I got it from (top response): http://www.reddit.com/r/feedthebeast/comments/1vzds0/big_reactors_efficiency_speculation_and_questions/

Any help would be appreciated, thanks!
theoriginalbit #3
Posted 16 March 2014 - 02:08 AM
So I think you're going to have to expand just a little bit more for us, as I know that I'm definitely not familiar with BigReactors (from now on I'll reference as BR), as I'm sure many others aren't as well.

When you say you want to add more reactors and monitors, do you want them automatically detected? do you want them manually added? do you want them automatically detected but then have a screen on the computer pop up and ask what you'd like it to display? what happens when theres more BR than can fit on a monitor? what happens when a monitor is too small to display information? How do these energy cells (EC) get put into the BR, does a computer have to do it? how? you say that you've got 4 computers setup and working, can you post the code, we could advise you how to merge them? when a BR is added, do you also need to supply the network name of the accompanying EC? does it bring up a list of EC's and you state which one it is for? these are just a few questions that you could expand on, obviously there is lots more to consider so that we can advise you on the best ways.

It's easy enough to get a program to iterate through all the peripherals in a network with peripheral.getNames however its not efficient to use that every loop of you're program, its far more efficient to use it only in the initialisation stage and make a table of all the peripherals, then use the peripheral and peripheral_detach events to add and remove a monitor, BR, and EC to and from your program.
xBlizzDevious #4
Posted 16 March 2014 - 02:50 AM
So I think you're going to have to expand just a little bit more for us, as I know that I'm definitely not familiar with BigReactors (from now on I'll reference as BR), as I'm sure many others aren't as well.

When you say you want to add more reactors and monitors, do you want them automatically detected? do you want them manually added? do you want them automatically detected but then have a screen on the computer pop up and ask what you'd like it to display? what happens when theres more BR than can fit on a monitor? what happens when a monitor is too small to display information? How do these energy cells (EC) get put into the BR, does a computer have to do it? how? you say that you've got 4 computers setup and working, can you post the code, we could advise you how to merge them? when a BR is added, do you also need to supply the network name of the accompanying EC? does it bring up a list of EC's and you state which one it is for? these are just a few questions that you could expand on, obviously there is lots more to consider so that we can advise you on the best ways.

It's easy enough to get a program to iterate through all the peripherals in a network with peripheral.getNames however its not efficient to use that every loop of you're program, its far more efficient to use it only in the initialisation stage and make a table of all the peripherals, then use the peripheral and peripheral_detach events to add and remove a monitor, BR, and EC to and from your program.

BR just acts as a peripheral with OpenPeripherals. I have a computer running the program in the second post there attached directly to each of the reactors.

What I want to be able to do, is simply attach a modem to each of the reactors and have a single computer be able to control all of them at once. I also want it to allow you to add another reactor in, then just restart the program and it'll control that one too.

I don't really know how to do (visual) tables on a monitor, but I'd like to have something like that for displaying the reactors' variables, all acquirable with the respective getters.

Thanks for the response! If you need any more info, just ask.
theoriginalbit #5
Posted 16 March 2014 - 03:03 AM
Okay so there's still a lot of unanswered questions as to the specifics of implementation, but that's fine :)/> we can cross those bridges when you come to them, if you get stuck on them :)/>

BR just acts as a peripheral with OpenPeripherals.
I figured that much, but I need to know what Mod adds it too, so I can lookup in our code what methods OpenP provides you with :)/>

I also want it to allow you to add another reactor in, then just restart the program and it'll control that one too.
Using the method I suggested you could actually have the program/computer not need to restart in order to detect the new BR.

I don't really know how to do (visual) tables on a monitor, but I'd like to have something like that for displaying the reactors' variables, all acquirable with the respective getters.
Basically you're going to have to print the required data to the screen, I suggest starting out with trying to print it to the Computer's terminal 'cause the easiest/best method of writing data to a monitor works in the same way, you could actually use the same code if you use term.redirect to point all your terminal calls to a monitor. Example:

print("I print on the terminal")
term.redirect(peripheral.wrap("monitor_0"))
print("I print on the monitor")
term.restore()
print("I print back on the terminal again")
xBlizzDevious #6
Posted 16 March 2014 - 03:21 AM
Okay so there's still a lot of unanswered questions as to the specifics of implementation, but that's fine :)/> we can cross those bridges when you come to them, if you get stuck on them :)/>

BR just acts as a peripheral with OpenPeripherals.
I figured that much, but I need to know what Mod adds it too, so I can lookup in our code what methods OpenP provides you with :)/>

I also want it to allow you to add another reactor in, then just restart the program and it'll control that one too.
Using the method I suggested you could actually have the program/computer not need to restart in order to detect the new BR.

I don't really know how to do (visual) tables on a monitor, but I'd like to have something like that for displaying the reactors' variables, all acquirable with the respective getters.
Basically you're going to have to print the required data to the screen, I suggest starting out with trying to print it to the Computer's terminal 'cause the easiest/best method of writing data to a monitor works in the same way, you could actually use the same code if you use term.redirect to point all your terminal calls to a monitor. Example:

print("I print on the terminal")
term.redirect(peripheral.wrap("monitor_0"))
print("I print on the monitor")
term.restore()
print("I print back on the terminal again")

Here's the wiki page for the computer port: http://wiki.technicpack.net/Reactor_Computer_Port

I could use either initialise on startup or re-run it each loop, I guess. Startup would suffice, though.

The energy cells being part of the system is kinda obsolete with the program that I linked in the 2nd post as that monitors the reactors and controls them extremely well. I could actually just set up another system and network for the energy cells should I desire it. I've already created an MFSU Storage Monitor in an old world, so I could modify that to work with Energy Cells instead of BatBoxes, in theory.

Printing the data on the screen is not an issue, though that redirect thing is something I was unaware of and its super handy! Adapting a table dependant on how many reactors are attached would be more difficult. Even spacing won't be easy.
theoriginalbit #7
Posted 16 March 2014 - 03:47 AM
Here's the wiki page for the computer port: http://wiki.technicp...r_Computer_Port
Ah so its not OpenPeripheral then, no need for me to look into the code for the functions, thanks for the link.

I could use either initialise on startup or re-run it each loop, I guess. Startup would suffice, though.
using the detection method I suggested would just mean there'd be no need to restart the program every time you added something new.

I've already created an MFSU Storage Monitor in an old world, so I could modify that to work with Energy Cells instead of BatBoxes, in theory.
yes, you'd need to make some changes, for example getEUStored is getEnergyStored (or is it getStoredEnergy… no i think its the first one) and other small changes like that, but you'd follow the same principle.

Adapting a table dependant on how many reactors are attached would be more difficult. Even spacing won't be easy.
would it be viable to perhaps use one monitor group (say a 2x2 monitor) for say one or two BR's?