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

Tekkit Classic + CCsensors help for Nuclear Reactor

Started by HighMans, 02 September 2014 - 08:32 PM
HighMans #1
Posted 02 September 2014 - 10:32 PM
I'm pretty much a newb to coding, and I need a bit of help here. This is my goal, I have a nuclear reactor that is running, and when it's done with it's cycle I need the computer to
  1. Shut off the timer (redstone signal) to my ice generator.
  2. Turn off the Nuclear Reactor (another redstone signal)
  3. Turn on the timer (another redstone signal) to extract Depleted Isotope Cells
  4. Turn on the timer to input Uranium cells. (3 and 4 can be combined in to one signal.)
I looked a bit in to ccsensors, and it can detect the inventory of the reactor and displays the name for the Depleted Isotope Cell. Anyone with a bit of insight to this, please help. Thanks again!


Thinking about it, it probably can be all incorporated in to one signal.
Edited on 02 September 2014 - 09:52 PM
Bomb Bloke #2
Posted 03 September 2014 - 02:54 AM
ccSensors is going back a bit…

I guess you just need to use Redstone API function calls in response to the reactor's status. Wrap them up in a few while loops and you'd get something along these lines:

while true do
	print("Reactor is running")
	
	rs.setOutput("left", false) -- Disables a redstone signal out the left side of the computer
	
	while <reactor is running> do
		sleep(5)
	end
	
	print("Cycle complete")
	
	rs.setOutput("left", true) -- Enables a redstone signal out the left side of the computer
	
	while <new cells aren't in the reactor> do
		sleep(5)
	end
end

Which Redstone signals you'd enable/disable depends on your exact setup - you can hopefully see that it's trivial to handle multiple signals at once simply by adding more lines specifying more sides.

Beats me how you'd detect what the reactor is doing, but it sounds like you might already have a handle on that. Post your code if you get stuck.
HighMans #3
Posted 03 September 2014 - 02:58 AM
Can you post a guide to the basics of creating a program (I can't code for the life of me) and how to use an api?
Bomb Bloke #4
Posted 03 September 2014 - 04:17 AM
For the "basics", I guess start reading the tutorials here - you'd want to go at least as far as functions. Although it doesn't deal with ComputerCraft-specific commands, you might find repl.it handy for experimentation.

ComputerCraft-specific APIs are documented here. You might also want to read this tutorial as well.
HighMans #5
Posted 03 September 2014 - 03:59 PM
Woot, thanks. So looking at your code, is my assumption right?

The first while loop, loops the whole program. Then on the 3rd line, if the reactor is running (checking a conditional) it will set the redstone output = to 0 and then sleep for 5s and loop until the reactor isn't running. Then when the reactor isn't running, it'll say at the cycle is complete, an on line 9 it'll turn on the redstone signal. Then on the next line, the redstone light will stay on until there are no more new cells in the reactor.
HighMans #6
Posted 03 September 2014 - 08:07 PM
Okay, I'm stuck now… I'm following this guide.

This is my code:


os.unloadAPI("/rom/apis/sensors")
os.loadAPI("/rom/apis/sensors")

local controllerSide = sensors.getController()
local sensorsList = sensors.getSensors( controllerSide )

for _, sensor in pairs( sensors.getSensors( controllerSide ) ) do
print( “Probes for the sensor: “..sensor )
for _, probes in pairs( sensors.getProbes( controllerSide, sensor ) ) do
   print( probes )
end
end

This is the error message: "bios:206: [string "2"]:8: unexpected symbol"

I fixed it sort of by replacing that line with print( sensor )
Edited on 03 September 2014 - 06:09 PM
Dog #7
Posted 03 September 2014 - 08:15 PM
If I had to guess (and this is a probably a stretch), I'd say that your quotes are not 'standard' and could be triggering the error. Notice how yours 'lean back' and the ones below do not…

print( "Probes for the sensor: " .. sensor )
HighMans #8
Posted 03 September 2014 - 08:24 PM
If I had to guess (and this is a probably a stretch), I'd say that your quotes are not 'standard' and could be triggering the error. Notice how yours 'lean back' and the ones below do not…

print( "Probes for the sensor: " .. sensor )

You are correct, I don't even know how that happened. XD
HighMans #9
Posted 03 September 2014 - 09:02 PM
I'm having a real hard time with understand ccsensors. This is the code from the aforementioned website,


os.unloadAPI("/rom/apis/sensors")
os.loadAPI("/rom/apis/sensors")

local controllerSide = sensors.getController()
local sensorsList = sensors.getSensors( controllerSide )

for _,sensor in pairs( sensors.getSensors( controllerSide ) ) do
for _,probe in pairs( sensors.getProbes( controllerSide, sensor ) ) do
   for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probe ) ) do
		 for k, data in pairs( sensors.getSensorReadingAsDict( controllerSide, sensor, target, probe ) ) do
		   print( k.." : "..tostring( data ) )
		 end
   end
end
end

This is the output.



I don't understand how to output just the data for the inventory as it's giving me a whole slew of other information.

edit: I've narrowed down the code a bit and put in solid variables for the first two for loops. It's now only giving me inventory information.


os.unloadAPI("/rom/apis/sensors")
os.loadAPI("/rom/apis/sensors")

local controllerSide = sensors.getController()
local sensor = "Sensor"
local probes = "InventoryContent"


for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
	 for k, data in pairs( sensors.getSensorReadingAsDict( controllerSide, sensor, target, probes ) ) do
		print( k.." : "..tostring( data ) )
	 end
end


The last issue to tackle is I don't understand the for loops. I tried setting a variable equal to
sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes )
and then replacing that variable with "target" in
sensors.getSensorReadingAsDict( controllerSide, sensor, **INSERT VARIABLE**, probes )
and it outputs nothing.'

Edit:

I tried just outputting the value of the target function to see what it outputs and it just gives me "table: *insert random alphanumeric stuff, 7-8 characters long.*"



os.unloadAPI("/rom/apis/sensors")
os.loadAPI("/rom/apis/sensors")

local controllerSide = sensors.getController()
local sensor = "Sensor"
local probes = "InventoryContent"
local target = sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes )

print( target )
Edited on 03 September 2014 - 11:27 PM
Bomb Bloke #10
Posted 04 September 2014 - 04:29 AM
The idea is that when you assign a table to a variable, you don't assign the table itself so much as you assign a pointer to it (think "shortcut files"). When you try to print "target", you end up printing the pointer to the table "target" leads to.

You could use another "for" loop to print what's in that table:

for key, value in pairs(target) do
  print(key..": "..value)
end
HighMans #11
Posted 04 September 2014 - 05:04 AM
Okay. So, I managed to figure out what target was. Is there a way to set a variable inside the for loop I can use outside of the for loop? Or can I just ignore the for loop somehow, and just set up the variable directly to the information in the table itself?

Or… Is there some way for me to use a for loop with that function alongside a conditional to see if the output of the function matches a string and then output a redstone signal.
Bomb Bloke #12
Posted 04 September 2014 - 05:32 AM
Okay. So, I managed to figure out what target was. Is there a way to set a variable inside the for loop I can use outside of the for loop? Or can I just ignore the for loop somehow, and just set up the variable directly to the information in the table itself?

Depends on which variable you're talking about.

The "counters" used in the loop are always local to that loop. If that's not what you want, you could not use a "for" loop:

local i=1
while i < 10 do
  --do stuff
  i = i + 1
end

You could also copy your counter variable's contents into other variables while your "for" loop is running. This latter approach would be needed when using a "pairs"-based "for" loop.

Make sure to read the tables and scope tutorials from that directory I linked to earlier. They're quite relevant here.

Or… Is there some way for me to use a for loop with that function alongside a conditional to see if the output of the function matches a string and then output a redstone signal.

Um, which function? What string?
Edited on 04 September 2014 - 03:32 AM
HighMans #13
Posted 04 September 2014 - 12:06 PM
This is my end goal. Here is the code:

os.unloadAPI("/rom/apis/sensors")
os.loadAPI("/rom/apis/sensors")

local controllerSide = sensors.getController()
local sensor = "Sensor"
local probes = "InventoryContent"


for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
for k, data in pairs( sensors.getSensorReadingAsDict( controllerSide, sensor, target, probes ) ) do
print( k.." : "..tostring( data ) )
end
end


My end goal is to search through that table in the for loop, find a value that matches "1xitem.itemCellUranEmpty@0" and output that in to a variable so I can check later to see if it's true. If it doesn't find the value, then it outputs "NIL" or "False". I want this so I can check to see if my reactor has any of the aforementioned item in it, and if it does output a redstone signal.
Bomb Bloke #14
Posted 04 September 2014 - 03:08 PM
Ok.

Going off the screenshot you posted earlier, it looks like the inventory slots of the thingamy are shoved into numeric indexes within the output of "getSensorReadingAsDict". table.maxn() gives us the highest numeric key in a table, so we can use that to concentrate on just those inventory slots.

Something like:

os.loadAPI("/rom/apis/sensors")

local controllerSide, sensor, probes = sensors.getController(), "Sensor", "InventoryContent"

local found = false

for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
	local data = sensors.getSensorReadingAsDict( controllerSide, sensor, target, probes )
	
	for i = 1, table.maxn(data) do
		if data[i] == "1*item.itemCellUranEmpty@0" then
			found = true
			break    -- Escape from the currently running loop, no need to search further.
		end
	end
	
	if found then break end  -- If we escaped the above loop, we'll want to escape this one too.
end

if found then print("Looks like we got a match.") end

Or! We could use a function, and return from it when we get our match:

os.loadAPI("/rom/apis/sensors")

local function hasEmptyCell()
	local controllerSide, sensor, probes = sensors.getController(), "Sensor", "InventoryContent"

	for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
		local data = sensors.getSensorReadingAsDict( controllerSide, sensor, target, probes )

		for i=1, table.maxn(data) do
			if data[i] == "1*item.itemCellUranEmpty@0" then
				return true
			end
		end
	end

	return false
end

if hasEmptyCell() then print("Looks like we got a match.") end
Edited on 04 September 2014 - 01:11 PM
HighMans #15
Posted 04 September 2014 - 07:47 PM
Hallelujah! It works! Your code it works! Thanks!!!
There's only one little thing left. How do I loop the code to run every 5 seconds? I'm trying to check if the reactor has mentioned item every 5 seconds. Lastly I tried tacking this at the end but it didn't turn the redstone on even when "found" prints "true"

if found == "true" then
rs.setOutput("front", true) else
rs.setOutput("front", false)
end

Full code



os.loadAPI("/rom/apis/sensors")

local controllerSide, sensor, probes = sensors.getController(), "Sensor", "InventoryContent"

local found = false

for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
		local data = sensors.getSensorReadingAsDict( controllerSide, sensor, target, probes )

		for i = 1, table.maxn(data) do
				if data[i] == "1*item.itemCellUranEmpty@0" then
						found = true
						break	-- Escape from the currently running loop, no need to search further.
				end
		end

		if found then break end  -- If we escaped the above loop, we'll want to escape this one too.
end

if found == "true" then
rs.setOutput("front", true) else
rs.setOutput("front", false)
end
Edited on 04 September 2014 - 06:35 PM
Lyqyd #16
Posted 04 September 2014 - 08:35 PM
There's a difference between true and "true". Take the quotes away and try again, as it appears that you're using the boolean value true elsewhere, not the string "true".
HighMans #17
Posted 04 September 2014 - 08:41 PM
There's a difference between true and "true". Take the quotes away and try again, as it appears that you're using the boolean value true elsewhere, not the string "true".

OH! I didn't notice it was checking a boolean value, I thought it was just a string!

I got it to loop! Finally! I think this is the final code.


os.loadAPI("/rom/apis/sensors")

while true do
local function hasEmptyCell()
        local controllerSide, sensor, probes = sensors.getController(), "Sensor", "InventoryContent"

        for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
                local data = sensors.getSensorReadingAsDict( controllerSide, sensor, target, probes )

                for i=1, table.maxn(data) do
                        if data[i] == "1*item.itemCellUranEmpty@0" then
                                return true
                        end
                end
        end

        return false
end

if hasEmptyCell() then found = true else found = false end

print( found )
if found == true then 
rs.setOutput("front", true) else 
rs.setOutput("front", false)
end
sleep(1)
end

One last small question, if I omit the "sleep(1)" I get a "Too long without yielding error". What's the fastest this computer can go before crashing?
Edited on 04 September 2014 - 10:27 PM
Bomb Bloke #18
Posted 05 September 2014 - 01:15 AM
You can take it down to sleep(0) and it won't crash. It'll slow all the other computers in the world down without good reason, though - frankly I'd recommend something like sleep(5) or sleep(10).
HighMans #19
Posted 05 September 2014 - 03:19 AM
You can take it down to sleep(0) and it won't crash. It'll slow all the other computers in the world down without good reason, though - frankly I'd recommend something like sleep(5) or sleep(10).

Ah okay. Thank you so much for helping me code this stupid little thing xD. (Is it possible for me to donate to you? :P/>)
Edited on 05 September 2014 - 01:37 AM
Bomb Bloke #20
Posted 05 September 2014 - 10:18 AM
Heh, thanks, but that's not necessary. I'm not affiliated with Dan or his ComputerCraft project, I'm just a guy posting on a forum. ;)/>

Though if you are interested in donating to ComputerCraft development, check here or here.
Edited on 05 September 2014 - 08:22 AM