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

ComputerCraft and ccSensors

Started by wolf2k12, 28 August 2012 - 10:40 PM
wolf2k12 #1
Posted 29 August 2012 - 12:40 AM
For those of you that don't want to sit through a 45 minute video (although funshinex's video is DEFINITELY worth watching to get a start), here is the simplest way to work with sensors. I actually use this "framework" for all my sensors in every map I've used sensors in.

here goes:

-- First we get where the controller is on the computer
-- and all the sensors on that controller (and save the total number of sensors)
ctrl = sensors.getController()
sensorList = sensors.getSensors(ctrl)
sensorTotal = table.maxn(sensorList)
sensorTargetMap = {}  -- empty table declaration.
sensorProbe = "" -- This is set to whatever type you are looking for.
				 -- (IE): RedstoneEngine, LiquidTank, etc.
probeType =""	-- This is set to whatever information you are trying
				 -- to pull from the sensor.
	   -- Each probeType is dependent on whatever sensorProbe
	   -- is set to.  For example, with RedstoneEngine you can
	   -- find out if it is running or not by setting probeType
	   -- to "engine.energy" .  If this value is less than 1 then
	   -- the engine is not running.

-- Now select the sensors in the order WE want to query them.
-- These names are whatever you called your sensors.
sensorMap = { "SensorA", "SensorB", "SensorC", "SensorD" }
-- Here comes the real work.  This is several nested 'for' loops.
-- Basically, for every sensor, get every target, and from every target
-- find the sensors we want and get it's information.
-- NOTE:  This for loop evaluates every sensor ONE AT A TIME.
for a = 1, table.maxn(sensorList) do
   curSensor = sensorMap[a]
sensorTarg = sensors.getAvailableTargetsforProbe(ctrl,
  curSensor, sensorProbe)
-- Now we get targets for the current sensor and store them in our map.
for b = 1, table.maxn(sensorTarg) do
  table.insert(sensorTargetMap, sensorTarg[b])
end
-- and finally we get the sensor data we're looking for.
for c = 1, table.maxn(sensorTargetMap) do
  sensorResult = sensors.getSensorReadingAsTable(ctr, curSensor,
   sensorTargetMap[c], sensorProbe)
  for d = 1, table.maxn(sensorResult) do
   if tostring(sensorResult[d]) == probeType then
	probeState = tonumber(sensorResult[d+1])
  
	-- do whatever you want with the information.
   end
  end
end
sensorTargetMap = {}  -- empty the target table so we can move to the
					   -- next sensor.
end

Also, you can edit your lua programs without using the in-game editor. Just figure out which computer you are on (run id) then go look in your %appdata%/roaming/.minecraft/saves/WORLDNAME/computer/computer's ID.

If you want syntax highlighting get gVim. Makes it MUCH easier to code in color.

Hope this has been helpful.

-W
Nomotion #2
Posted 12 September 2012 - 10:34 AM
Surprised no one has thanked you for this yet. There is very little documentation im glad to see some people helping others out. This is probably the most helpful document i've found on how to grab the data from sensors. Well done and thanks.

Edit:I hit an end. How does one find out the probeTypes for different sensors? I am trying to detect the inventory of a chest to detect said item.
wolf2k12 #3
Posted 14 September 2012 - 11:39 PM
Surprised no one has thanked you for this yet. There is very little documentation im glad to see some people helping others out. This is probably the most helpful document i've found on how to grab the data from sensors. Well done and thanks.

Edit:I hit an end. How does one find out the probeTypes for different sensors? I am trying to detect the inventory of a chest to detect said item.

The way I did it was to go and right click on the sensor probe and look under Targets (I think) and it should show a list of object types within it's range.

Also, Probably nobody else has taken the time to do this but, in the process of making a sensor set up to monitor a 32 engine double-bank, I have also found that the 'order' in which the sensor picks out the targets are first South, then East..

To give you an example, I'll try to explain what I did to address each of my engines. Well, actually to adress 'a pair' of engines that were straight across from each other.

Anyway here goes.

X = engine, S = sensor

X S X
X X
X X
X X
X X
X S X
X X
X X
X X
X X
X S X
X X
X X
X X
X X
X S X

Leaving the sensors default range of '5', this allows me to monitor 16, or 16 pairs of engines with 3, 5, 5, and 3 (or 6, 10, 10, and 6, technically). Now we gotta figure out which sensors we're reading so we know which engine is being monitored. This is with a South to North engine placement.

1 S 4
2 5
3 6

This is with an East to West placement.

1 3 5

S

2 4 6

Knowing this, we can use that information to pull sensor data on any engine we choose. Since what I was working on only needed to monitor 1 set, I discarded the right hand numbers by using math to get rid of what I didn't need. So for the top, I took the total number of targets on that sensor and divided it by 2. On the bottom one, (since it starts with 1) after the first engine, I added 1 to my table numerator to skip one. Those can also be reversed if you're going from North to South and West to East, just by changing the math, or the beginning of the for() statement.

If you'd like some code examples let me know and I can most more in-depth information on how I have my engines set up and what I did to address them.

And thank you for thanking me :)/>/> It's nice to know that several hours of playing and learning didn't go unnoticed. Plus, I highly recommend watching FunshineX's video on youtube which explains in great detail some of the uses of sensors and how they work. About 20 minutes or so in is where he gets into the actual programming.

Also, if you understand the code, you can also open the API's in a text editor and see how the functions are declared and what information is being passed back and forth.

Have fun with it.

Oh yeah, I also discovered what I think is a bug although nobody seems to have answered me on it. But if you have multiple sensor controllers, and use the function setSensorRange(), it will disregard the controller and sensor information passed to it and will reset the range on _ALL_ sensors in the world. This will break any constant running programs, like the one I use for monitoring engines.
Sariaz #4
Posted 23 September 2012 - 08:24 AM
great code and lots of help sadly on some other forum i discovered that all but one of the proximity sensors probes is broken at least using this and every other code for ccsensors I've seen ;(
Mr. Fang #5
Posted 24 September 2012 - 01:56 PM
Did you know FunshineX has an account here on the forums as well? Go support him!
Sariaz #6
Posted 14 April 2013 - 08:01 PM
I have personly found ccsensors to be unreliable and overly complicated in the past this may have changed. For those who are being infuriated as I was by random glitches I would suggest switching to openccsensors which is less buggy, has sensor turtles, and is more versatile in what it can sense. The main thing that it can't do that ccsensors can is sense mod related things as openccsensors has no mod block sensors at the time of this post.
rhysjack7 #7
Posted 15 April 2013 - 06:00 AM
Can It Be Coded In Notepad++
LordIkol #8
Posted 18 April 2013 - 12:35 AM
I have personly found ccsensors to be unreliable and overly complicated in the past this may have changed. For those who are being infuriated as I was by random glitches I would suggest switching to openccsensors which is less buggy, has sensor turtles, and is more versatile in what it can sense. The main thing that it can't do that ccsensors can is sense mod related things as openccsensors has no mod block sensors at the time of this post.

Why you necro threads that are Dead since 7 Month already just to write sth nobody asked for??

Can It Be Coded In Notepad++

Yes, it can