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

Need help with some Big Reactor Code

Started by amboyscout, 02 January 2017 - 01:42 AM
amboyscout #1
Posted 02 January 2017 - 02:42 AM
I have 16 reactors set up like this: http://i.imgur.com/xcUn8JH.png http://i.imgur.com/xcUn8JH.png http://i.imgur.com/BMFAutH.png
I am trying to write some code to control them in sequence, increasing power production from the bottom up. The current code that I have will follow

Code for the bottom (1st) reactor

local reactorNum = 1
local reactor = peripheral.wrap('left')
local computer = peripheral.wrap('top')
local active = 0
local energy = 5000000
local allRods = 1600
local increment = 5
local rodMax = 1600 - ((reactorNum-1)*100)
while active == 0 do
	active = reactor.getConnected
	if active == 0 then
		print('Reactor not connected')
	end
	os.sleep(1)
end
print('Reactor connected')
if reactor.getActive == 0 then
	reactor.setActive(1)
end
while true do
	energy = reactor.getEnergyStored
	if energy < 1000 and allRods > 0 then
		allRods = allRods - increment
	elseif energy > 9500000 and allRods < 1600 then
		allRods = allrods + increment
	end
	if rodMax - allRods > 100 then
		reactor.setAllControlRodLevels(0)
		print('Control rod level = 0')
		computer.transmit(1,1,allrods-100)
	else
		reactor.setAllControlRodLevels(100-(rodMax-allRods))
		print('Control rod level = '..(rodMax-allRods))
		computer.transmit(1,1,rodMax-100)
	end
	os.sleep(1)
end

Code for reactors 2-15

local reactorNum = CHANGE REACTOR NUMBER
local reactor = peripheral.wrap('left')
local computer = peripheral.wrap('top')
local modem = peripheral.wrap('bottom')
local active = 0
local rodMax = 1600 - ((reactorNum-1)*100)
local allRods = rodMax
modem.open(REACTOR NUMBER MINUS 1 HERE)
while active == 0 do
	active = reactor.getConnected
	if active == 0 then
		print('Reactor not connected')
	end
	os.sleep(1)
end
print('Reactor connected')
if reactor.getActive == 0 then
	reactor.setActive(1)
end
while true do
	local message = os.pullEvent('modem_message')
	allRods = message
	if rodMax - allRods > 100 then
		reactor.setAllControlRodLevels(0)
		print('Control rod level = 0')
		computer.transmit(reactorNum,reactorNum,allrods-100)
	else
		reactor.setAllControlRodLevels(100-(rodMax-allRods))
		print('Control rod level = '..(rodMax-allRods))
		computer.transmit(reactorNum,reactorNum,rodMax-100)
	end
end

Code for the top (16th) reactor

local reactorNum = 16
local reactor = peripheral.wrap('left')
local modem = peripheral.wrap('bottom')
local active = 0
local rodMax = 1600 - ((reactorNum-1)*100)
local allRods = rodMax
modem.open(15)
while active == 0 do
	active = reactor.getConnected
	if active == 0 then
		print('Reactor not connected')
	end
	os.sleep(1)
end
print('Reactor connected')
if reactor.getActive == 0 then
	reactor.setActive(1)
end
while true do
	local message = os.pullEvent('modem_message')
	allRods = message
	if rodMax - allRods > 100 then
		reactor.setAllControlRodLevels(0)
		print('Control rod level = 0')
	else
		reactor.setAllControlRodLevels(100-(rodMax-allRods))
		print('Control rod level = '..(rodMax-allRods))
	end
end

Currently, I am trying to debug the 1st reactor's code. All other code seems to run fine. This is the error I get: http://i.imgur.com/1MmwSQs.png Any Ideas on what Im doing wrong? Also, is there anything else messed up with my code?
Bomb Bloke #2
Posted 02 January 2017 - 04:20 AM
Remember, if you wish to call a function and get a result back from it, you need to stick brackets behind its name.
Dog #3
Posted 02 January 2017 - 04:23 AM
Also, when you capture results from os.pullEvent() it provides the event first, then the data, even when you're filtering for specific events.
amboyscout #4
Posted 02 January 2017 - 04:33 AM
Remember, if you wish to call a function and get a result back from it, you need to stick brackets behind its name.
What do you mean by that? Sorry for being incompetent, not great at lua, and its my first program for it and CC.

Also, when you capture results from os.pullEvent() it provides the event first, then the data, even when you're filtering for specific events.
So how should it look? ditto for my apology above
Dog #5
Posted 02 January 2017 - 05:41 AM
Remember, if you wish to call a function and get a result back from it, you need to stick brackets behind its name.
What do you mean by that? Sorry for being incompetent, not great at lua, and its my first program for it and CC.

I can see at least two places where you've referenced reactor.someFunction instead of calling reactor.someFunction() (by putting the brackets at the end)

Also, when you capture results from os.pullEvent() it provides the event first, then the data, even when you're filtering for specific events.
So how should it look? ditto for my apology above

In your case, with a modem message, it should look like this…

local event, modemSide, senderChannel, replyChannel, message = os.pullEvent("modem_message")
Take a look at the wiki entry for more information.
amboyscout #6
Posted 02 January 2017 - 07:03 AM
I can see at least two places where you've referenced reactor.someFunction instead of calling reactor.someFunction() (by putting the brackets at the end)


reactor.getConnected()
and

reactor.getActive()
Return errors instead of working as expected.


energy = reactor.getEnergyStored()
After implementing that it gives a new error: http://i.imgur.com/ZCU1lZh.png

I implemented the modem message change, however I will not be able to test it until the first reactors pc works right.

EDIT: BTW thanks for the help :)/>

EDIT2: Ran a basic program and noticed a similar issue: http://i.imgur.com/pkvX23b.png http://i.imgur.com/vRQu9yf.png Pack is FTB Infinity Evolved 2.6.0 curse launcher on a server.
Edited on 02 January 2017 - 06:32 AM
Bomb Bloke #7
Posted 02 January 2017 - 07:43 AM
… Return errors instead of working as expected.

Given that you have bothered to tell us what they are, I assume you intend to figure those ones out for yourself, yes?


energy = reactor.getEnergyStored()
After implementing that it gives a new error: http://i.imgur.com/ZCU1lZh.png

You're being told that no function called "getEnergyStored" exists within your "reactor" table. As far as I'm aware all versions of Big Reactors should offer it, but it may be helpful to tell us which version of Big Reactors your pack uses. Also double-check that you haven't got some other peripheral sitting to the left of your computer…
blunty666 #8
Posted 02 January 2017 - 09:13 AM
If your setup is still as it is in the pictures then you are wrapping the reactor peripherals incorrectly. By doing 'peripheral.wrap("left")' you are wrapping the modem not the reactor. Instead you will need to do 'peripheral.wrap("BigReactors-Reactor_xx")' where 'xx' is the number given when you connect the Reactor by right clicking the modem attached to it.
amboyscout #9
Posted 02 January 2017 - 04:16 PM
… Return errors instead of working as expected.

Given that you have bothered to tell us what they are, I assume you intend to figure those ones out for yourself, yes?


energy = reactor.getEnergyStored()
After implementing that it gives a new error: http://i.imgur.com/ZCU1lZh.png

You're being told that no function called "getEnergyStored" exists within your "reactor" table. As far as I'm aware all versions of Big Reactors should offer it, but it may be helpful to tell us which version of Big Reactors your pack uses. Also double-check that you haven't got some other peripheral sitting to the left of your computer…
Yeah, not placing the () after them seems to make them work, though I don't think they actually work… They might work like you said if blunty666 was right about my problem. The big reactors version is 0.4.3A. The reactor is the only peripheral.

If your setup is still as it is in the pictures then you are wrapping the reactor peripherals incorrectly. By doing 'peripheral.wrap("left")' you are wrapping the modem not the reactor. Instead you will need to do 'peripheral.wrap("BigReactors-Reactor_xx")' where 'xx' is the number given when you connect the Reactor by right clicking the modem attached to it.
That could be it, let me look into whether or not that fixes it.
amboyscout #10
Posted 02 January 2017 - 05:46 PM
So yeah, I needed to implement the () after
reactor.getActive
and
reactor.getConnected
when I implemented the new code for reactor 1. However, I am still having some odd issues. The control rod level goes up and up and up in increments of 5, hits 100, drops to 0, and then I error out: http://i.imgur.com/3W6Ntu4.png The reactor also never turns on. I have not tested any new code for the other reactors, but here it is in its updated form anyway.

Reactor 1
Spoiler

local reactorNum = 1
local reactor = peripheral.wrap('left')
local computer = peripheral.wrap('top')
local active = 0
local energy = nil
local allRods = 1600
local increment = 5
local rodMax = 1600 - ((reactorNum-1)*100)
while active == 0 do
	active = reactor.getConnected()
	if active == 0 then
		print('Reactor not connected')
	end
	os.sleep(1)
end
print('Reactor connected')
if reactor.getActive() == 0 then
	reactor.setActive(1)
end
while true do
	energy = reactor.getEnergyStored()
	if energy < 1000 and allRods > 0 then
		allRods = allRods - increment
	elseif energy > 9500000 and allRods < 1600 then
		allRods = allrods + increment
	end
	if rodMax - allRods > 100 then
		reactor.setAllControlRodLevels(0)
		print('Control rod level = 0')
		computer.transmit(1,1,allrods-100)
	else
		reactor.setAllControlRodLevels(100-(rodMax-allRods))
		print('Control rod level = '..(rodMax-allRods))
		computer.transmit(1,1,rodMax-100)
	end
	os.sleep(1)
end

Reactors 2-15
Spoiler

local reactorNum = CHANGE REACTOR NUMBER
local reactor = peripheral.wrap('SET REACTOR PERIPHERAL NAME')
local computer = peripheral.wrap('top')
local modem = peripheral.wrap('bottom')
local active = 0
local rodMax = 1600 - ((reactorNum-1)*100)
local allRods = rodMax
modem.open(REACTO NUMBER MINUS 1)
while active == 0 do
	active = reactor.getConnected()
	if active == 0 then
		print('Reactor not connected')
	end
	os.sleep(1)
end
print('Reactor connected')
if reactor.getActive() == 0 then
	reactor.setActive(1)
end
while true do
	local event, modemSide, senderChannel, replyChannel, message = os.pullEvent("modem_message")
	allRods = message
	if rodMax - allRods > 100 then
		reactor.setAllControlRodLevels(0)
		print('Control rod level = 0')
		computer.transmit(reactorNum,reactorNum,allrods-100)
	else
		reactor.setAllControlRodLevels(100-(rodMax-allRods))
		print('Control rod level = '..(rodMax-allRods))
		computer.transmit(reactorNum,reactorNum,rodMax-100)
	end
end

Reactor 16
Spoiler

local reactorNum = 16
local reactor = peripheral.wrap('left')
local modem = peripheral.wrap('bottom')
local active = 0
local rodMax = 1600 - ((reactorNum-1)*100)
local allRods = rodMax
modem.open(15)
while active == 0 do
	active = reactor.getConnected()
	if active == 0 then
		print('Reactor not connected')
	end
	os.sleep(1)
end
print('Reactor connected')
if reactor.getActive() == 0 then
	reactor.setActive(1)
end
while true do
	local event, modemSide, senderChannel, replyChannel, message = os.pullEvent("modem_message")
	allRods = message
	if rodMax - allRods > 100 then
		reactor.setAllControlRodLevels(0)
		print('Control rod level = 0')
	else
		reactor.setAllControlRodLevels(100-(rodMax-allRods))
		print('Control rod level = '..(rodMax-allRods))
	end
end
Edited on 02 January 2017 - 04:47 PM
Bomb Bloke #11
Posted 02 January 2017 - 11:54 PM
"allrods" is not the same thing as "allRods".