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

Newbie - First program - controlling nuclear reactors

Started by ward9250, 21 March 2014 - 05:08 PM
ward9250 #1
Posted 21 March 2014 - 06:08 PM
Hi everyone, I'm used to programming in several languages so in general I'm comfortable coding, but not completely knowledgeable in lua. Anyway I figured a good way to learn some computer-craft was to replace my complicated redstone setup controlling multiple nuclear reactors, with a computer-craft system. I figured a good way to do this would be each reactor would have a computer, which would do two things, it would monitor for messages from a master computer and perform actions based on those messages, and also monitor the state of the reactor, and notify the master controller if something occurs, like a temperature spike. Likewise, the master will listen to messages from the individual reactor computers - say it get's sent an "I'm too hot" message, and then depending on the settings of the master controller it could send a response instruction, like shut down the reactor permanently, or shut down for 5 mins and start up again and so on. So I got coding on the side of the individual reactor computers, a program I'm calling "reactor_slave".

My code is below. We define the monitoring function that sends a message to the master first, then the function that istens and responds to the master's messages, then define some variables for the reactor, and then I figured the parallel.waitForAny function would allow me to run these two functions, responding to master messages, and sending messages to master, at the same time:

--Continually checks the reactor's temperature.
function checkReactor()
while true do
  if rs.getInput(tempSide) and rs.getOutput(reactorSide) then
		 print("The reactor has a severely high temperature.")
   print("Alerting the control computer...")
   rednet.send(masterID, "TooHot")
  elseif rs.getInput(tempSide) and rs.getOutput(reactorSide) == false then
   print("The reactor is very hot, however it has already been turned off.")
   end
end
end

--Checks for messages from the controller & responds.
function checkMessages()
while true do
  senderID, message = rednet.recieve()
  if senderID == masterID then  
   if message == indshutdown or message == "ALLSHUTDOWN" then
  
							   print("Recieved an order from controller to shutdown, shutting down")
	  
	rs.setOutput(reactorSide, false)
	  
	print("Shutdown the reactor.")
	
		  elseif message == indstartup or message == "ALLSTARTUP" then
				  print("Recieved an order from controller to start up.")
	rs.setOutput(reactorSide, true)
	print("Started up the reactor.")
   end
  end
end
end
--Variables
for individual reactor.
reactorSide = "back"
tempSide = "left"
masterID = 8
indstartup = "startupbreed"
indshutdown = "shutdownbreed"
--Script Body.
rednet.open("top")
rs.setOutput(reactorOutput, false)
parallel.waitForAny(checkReactor, checkMessages)


However if I run this code I get an error to do with the waitForAny function:

parallel:22:reactor_server:6: Too long without yielding.

What am I doing wrong? And how can I simultaneously check reactor status and send messages of status, and also check and do reactions based on messages recieved from the master computer?

Thanks,
Ben.
Lyqyd #2
Posted 21 March 2014 - 06:40 PM
Your checkReactor function doesn't yield at all. Throw an os.pullEvent("redstone") at the end to wait for redstone changes for each iteration.