Jump to content




How To Use ccSensors


  • You cannot reply to this topic
45 replies to this topic

#1 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 04:29 AM

How to Use ccSensors ( Not to be confused with openCCSensors )
— By TheOriginalBIT and remiX

Over the past few weeks there has been a large increase in the amount of people wanting to use ccSensors in Tekkit to do something. There is however no one that has a fantastic idea of how to fully use the peripheral due to a lack of documentation. So here is a compilation of everything we (TheOriginalBIT and remiX) have found out through our own experiments and helping people on “Ask a Pro”.

What is required:
  • A ComputerCraft computer
  • A Sensor Controller
  • A Sensor
  • A Sensor Card ( World, Inventory, BuildCraft, IndustrialCraft, Forestry, RedPower2, Equivalent Exchange, Advanced Power Systems, Thaumcraft2, Proximity )
  • Transmitter card
Setup:

Firstly, make sure you have what is needed in the above ‘What is required’. When you do, place the computer down anywhere with a Sensor Controller attached to it - like this. The Sensor Controller can be attached at any side to the computer.

Then place a sensor near what you want to be reading that is also within a reasonable distance to the SensorController you want it to be used with.
Once that is done, insert a Blank Transmitter Card into the SensorController at the bottom right and click ‘Encode’ and remove the Transmitter Card from the SensorController. You will notice the Transmitter Card will change colours, which is meant to happen.

Now open up the Sensor and place the Transmitter Card which you encoded from the SensorController into the bottom most left slot of the Sensor. You will notice there is another open spot next to the Transmitter Card slot - this slot is for the Module. There is also a spot where you can name the sensor to make it easier to identify in your code and/or on a UI.

In order for this all to work how you want it to, you will need the correct SensorModule. Search ‘Sensor’ and find the correct one, and place it into the slot next to the Transmitter Card in the Sensor. You should notice information come up on the sensor according to which SensorModule you placed inside the Sensor.
An example setup can be found here.

Do notice the colours of the SensorController and the Sensor are the same? That means they are connected.

Coding For The Sensors:

There is a very limited amount of documentation for ccSensors that can be found here.

Finding the Sensor Controller

The first piece of code that we can use is to find out the side of the computer that the sensor controller is on. There are two methods of doing this; Using the peripherals API or using a function that ccSensors gives us to use. The ccSensors way is much easier, but both are detailed below:

ccSensors method:
local controllerSide = sensors.getController()


peripheral API method:
local function findSensorController()
for _,side in pairs( rs.getSides() ) do
   if peripheral.getType( side ) == “SensorController” then
	 return side
   end
end
end

local controllerSide = findSensorController()


Using either of these methods to find the sensor controller is acceptable and comes down to personal choice ( although I don’t know why you wouldn’t use the one line, ccSensors, method over the longer, peripherals API method ).

Getting the Controller’s Sensors

Next we must find all the sensors that the Sensor Controller is able to access and use. To do this we use the following function.
local sensorsList = sensors.getSensors( controllerSide )

If you wish to see all the sensors that the controller can access you can use the following code.
for _, v in pairs( sensors.getSensors( controllerSide ) do
print( v )
end


When we use the getSensors function it will return us a table containing all the names of the sensors that the controller can access ( NOTE: It has been discovered that in some SMP worlds that the names do not work and all sensors will have the name “Sensor”)

Getting the Sensor’s Probes

We now have a list of the sensors, now we need to get the probes for the sensors. There are various different probes for each sensor.
sensors.getProbes( controllerSide, sensorName )

To see a list of all the probes available to the sensors you can use the following code
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


If there is a sensor in particular that you can extract it directly from the return of the getProbes function like so
local probes = sensors.getProbes( controllerSide, sensor )[3]


Getting the Probe’s Targets

Once we have the probe or probes we are after we must then get the targets for the sensor. These targets are the actual items or blocks ( or any data ) that is in the game that we are trying to display or gather information for. This information varies for each Sensor type. The data that you have access from the target can be found out by the following code
sensors.getSensorReadingAsDict( controllerSide, sensor, target, probe )


Getting Information About the Target

To get the information or data about a target we use the following code
sensors.getSensorReadingAsDict( controllerSide, sensor, target, probe )


This will return us a table containing all the data about a given target. We can then access this data and use it in the functioning of our program. An example to display the amount of energy, as power level and percentage, inside of an IC2 Power Storage device is as follows:
print( “Power level: “..data.energy..”/”..data.maxStorage )
print( “Percentage: “..( data.energy / data.maxStorage * 100 )..”%” )


As there are so many sensor types there is no way we are going to list all the data for each target. So that being said the following example will print out all the data available for all the available targets, in all the available sensors ( this is recommended for use when your trying to find that one particular piece of data )
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


Summary

Using the steps shown above a great deal can be learnt about ccSensors and in fact it is how I ( TheOriginalBIT ) actually first figured out how to use ccSensors and still use when helping people in “Ask a Pro” with their ccSensors issues. Actually this method is quite a good method to use to figure out how to use ANY peripheral, just go through step-by-step until you get to where you need

Making it Easier

If all of this so far still isn’t making much sense, or you’re just not sure what sensor to use, remiX has made a small script to make sensor selection MUCH easier, this can be found here, an image of it running here.

Further Examples:

IC2 Power Storage Levels — By TheOriginalBIT: http://pastebin.com/asSLPyqZ
IC2 Power Levels — By remiX: http://pastebin.com/1UqupYub
Proximity — By remiX: http://pastebin.com/jzwn2xPQ
Inventory Module — By remiX: http://pastebin.com/8QZzwre9
Player Detector — By TheOriginalBIT: http://pastebin.com/F9DwAFcn

Still Don’t Get It?

Feel free to leave a comment below asking us a question and we will try to answer it as best as we can :)

Edited by theoriginalbit, 30 April 2013 - 08:35 PM.


#2 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 20 January 2013 - 06:43 AM

+1. Good job.

#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 20 January 2013 - 11:53 AM

I hope people with problems with ccSensors come here first now :P

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 11:55 AM

View PostremiX, on 20 January 2013 - 11:53 AM, said:

I hope people with problems with ccSensors come here first now :P
Hope so... If not at least we can just post this link to them :P

#5 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 12:10 PM

Nice tutorial guys :) But I'm still sadly still not getting it, managed it this far:
Spoiler

With this code:
Spoiler

But yet, I cant get any code to work as it should work, So my question remains and I'm not asking to explain the whole thing bit by bit, I assume a tip wil be enough :)

[Question]
Spoiler


And, here what i've tried (apologies for the messy code, assume I just dont get it)

local controllerSide = Sensors.getController()
sensors.getSensorReadingAsDict("left","Sensor","World,-1123,30,2134","WorldProbe")

Also tried: ("left",/Sensor/World,-1123,30,2134/WorldProbe) And many other combinations
And, ofcourse tried it using shell.run("/ccSensors/console/ And didnt came much furder then that
And yes, even more ways but it just does not seems to work somehow and I dont want to copy codes (which will probaly work) because I want to learn it for myself.

Thanks in advance :)


Quote

Hope so... If not at least we can just post this link to them :P


And, here is the result :)

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 12:19 PM

View PostNLBlackEagle, on 20 January 2013 - 12:10 PM, said:

Quote

Hope so... If not at least we can just post this link to them :P
And, here is the result :)
Its ok... It means we can improve the post a little to help people more... plus we do say this

View PostTheOriginalBIT, on 20 January 2013 - 04:29 AM, said:

Still Don’t Get It?
Feel free to leave a comment below asking us a question and we will try to answer it as best as we can :)

It does make it very hard when ccSensors developer left little to no documentation...


Also very good job with what you have done... seems like you have a problem with the reactor components showing nan ( not a number ) though...

Ok so now to the problem... It seems to me that your trying to use the World sensors to detect players... This is actually done using the proximity sensor... unfortunately I am not aware of any module that can tell you how many people are connected to the world... only how many are within the range of the sensor... ( can you confirm remiX? )
Using the proximity sensor you could get the targets for Players and then just use #tableTargetsStoredIn to get the count of the players... and of course you can then use this in any way you wish for example
if #targets > 5 then
  print( "There are heaps of people in range!" )
else
  print( "There are "..#targets.." in range." )
end


#7 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 12:50 PM

@TheOriginalBIT

No problems with the reactor, wait Ill show you :)
Spoiler

I'm having trouble not explaining about all the functions this beast has, lets keep it short "this reactor is not nearly matched by any other found on youtube" Thats why I had to post that picture in the spoiler haha (Ill probaly make a tutorial/guide on this reactor, and I might appear in creative now but this was all survival made (took about a year to collect all the knowlegde and add it to the reactor) [Edit] tried something new, which caused my HV 4x ins cable vaporising, 2070eu/t how awesome is that XD

And, yes the problem.

Well, Its not needed to see all the players in the world I even dont want that to, only within the range of the sensor so some a warning sign will apear (for example a lamp with a sign above of the sensors location) This is so I know if anyone has been near my base.

So, for example: I have set up a circle of sensors around my base (far enough so noone in the base will trigger it) When a player comes along in his boat the sensor will notice this resulting in: PlayerCount:1
What i basicly want is a program which is connected to this sensor and emit a redstone current when he recieves "playercount:1"

A logical code would be: If shell.run("/ccSensors/console/ ???? /playercount:1") == true then
rs.setBundledOutput("bottom", 1)

Wich activates a lamp wich will stay on (using redpower) so when i come online I see a lamp and know someone has been within the radius of the sensor triggering the lamp


[Edit]

But yes, indeed "if #targets = > 1 then" is the correct example but I dont get the code which gets me to this point i can type the example :P

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 01:03 PM

View PostNLBlackEagle, on 20 January 2013 - 12:50 PM, said:

No problems with the reactor, wait Ill show you :)
I was just noticing that on the initial image that the % for coolant cells and such show "nan%"

View PostNLBlackEagle, on 20 January 2013 - 12:50 PM, said:

Well, Its not needed to see all the players in the world I even dont want that to, only within the range of the sensor so some a warning sign will apear (for example a lamp with a sign above of the sensors location) This is so I know if anyone has been near my base.
Ok I was just clarifying to make sure that its defs not what you wanted to do...

View PostNLBlackEagle, on 20 January 2013 - 12:50 PM, said:

But yes, indeed "if #targets = > 1 then" is the correct example but I dont get the code which gets me to this point i can type the example :P
Yes this is an example... give me a minute to throw something together as a better example...

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 01:27 PM

Ok so using the program that remiX made under the "Making It Easier" heading I was able to determine that there is a bug with the proximity sensor... but we can work around that... :)

First you need to place down the sensors and computers with the proximity sensor module in the sensors.

Next using the code outlined in the OP you need to do the following
  • Get the sensor
  • Get the "Players" probe from the sensor ( note this will return ALL living entities :@ damn developers and their bugs )
  • Get the targets that have the name "vq" so that we ONLY get the players ( this is the players class, I think its a bug in the ccSensors code where it doesn't say what the entity type is)
Thats it... if we find targets with the name "vq" then it means that we have found a player within the range of the sensors... all you do then is increment some variable to say that a player was detected... then have the computer turn on the redstone to turn on the light when that variable is not 0...

make sense?

#10 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 01:40 PM

View PostTheOriginalBIT, on 20 January 2013 - 01:27 PM, said:

Ok so using the program that remiX made under the "Making It Easier" heading I was able to determine that there is a bug with the proximity sensor... but we can work around that... :)

First you need to place down the sensors and computers with the proximity sensor module in the sensors.

Next using the code outlined in the OP you need to do the following
  • Get the sensor
  • Get the "Players" probe from the sensor ( note this will return ALL living entities :@ damn developers and their bugs )
  • Get the targets that have the name "vq" so that we ONLY get the players ( this is the players class, I think its a bug in the ccSensors code where it doesn't say what the entity type is)
Thats it... if we find targets with the name "vq" then it means that we have found a player within the range of the sensors... all you do then is increment some variable to say that a player was detected... then have the computer turn on the redstone to turn on the light when that variable is not 0...

make sense?


I will do some testing using shell.run("ccSensors/
Anyway, ima going to make a program now and come back with a complete code I assume there will be some errors in there and If not ill give some feedback because others might want to do this too, thanks for the help so far! (I will post another reply in about 10-15 minutes as I guess :) )

So, to answer your questions, yes it does make sense :)

#11 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 02:42 PM

Ok, I have the feeling im on the good course heading for the solution :)
This is the code I've writed and instead of blank fields after reboot i actualy get a error now.

Well, here the code:

local controllerSide = sensors.getController
print("2")
local sensorName = "A1"
local WorldProbe = sensors.getProbes ("controllerSide",sensorName")
print("1")
local targets = sensors.getAvailableTargetsforProbe("controllerSide","sensorName","WorldProbe")


Error:

CraftOS 1.3
2
sensors:141: Invalid Side.
>_

So, something is wrong with ""local controllerSide = sensors.getController"" But what?

[I have the controller on the right side of the computer]

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 03:12 PM

ok so a few issues here outlined below:

Firstly for the controllerSide variable you want to do this
local controllerSide = sensors.getController()
note the ( ) at the end... this is because sensors.getController is a function and we must call it... by not using the ( ) at the end you are in fact telling Lua that the controllerSide is a pointer to the sensors.getController in memory...

Second issue, when passing values to a function you do it like this, the first is a variable, the second is a string, 3rd is a number and 4th is a function pointer...
someFunction( someVariable, "some string", 3, someFunction )
Now in your case you are trying to pass variables, but you have surrounded them with " making them strings... remove the " from around the variables in the getProbes and getAvailableTargetsforProbe and it should be good :)

#13 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 03:26 PM

View PostTheOriginalBIT, on 20 January 2013 - 03:12 PM, said:

ok so a few issues here outlined below:

Firstly for the controllerSide variable you want to do this
local controllerSide = sensors.getController()
note the ( ) at the end... this is because sensors.getController is a function and we must call it... by not using the ( ) at the end you are in fact telling Lua that the controllerSide is a pointer to the sensors.getController in memory...

Second issue, when passing values to a function you do it like this, the first is a variable, the second is a string, 3rd is a number and 4th is a function pointer...
someFunction( someVariable, "some string", 3, someFunction )
Now in your case you are trying to pass variables, but you have surrounded them with " making them strings... remove the " from around the variables in the getProbes and getAvailableTargetsforProbe and it should be good :)

local controllerSide = sensors.getController()
local sensorName = "A1"
local WorldProbe = sensors.getProbes (controllerSide,"sensorName")
local targets = sensors.getAvailableTargetsforProbe(controllerSide,sensorName,WorldProbe)

This seems to work (No errors but I still have a blanc screen, guess nothing should be appearing, lets add in the uhm playercount thing


#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 03:30 PM

View PostNLBlackEagle, on 20 January 2013 - 03:26 PM, said:

local WorldProbe = sensors.getProbes (controllerSide,"sensorName")
Missed a set of quotes... should be
local WorldProbe = sensors.getProbes (controllerSide,sensorName)
Black screen was because the sensor with the name sensorName didn't exist and the peripheral is coded badly so crashes the computer instead of showing an error :/

#15 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 03:37 PM

View PostTheOriginalBIT, on 20 January 2013 - 03:30 PM, said:

View PostNLBlackEagle, on 20 January 2013 - 03:26 PM, said:

local WorldProbe = sensors.getProbes (controllerSide,"sensorName")
Missed a set of quotes... should be
local WorldProbe = sensors.getProbes (controllerSide,sensorName)
Black screen was because the sensor with the name sensorName didn't exist and the peripheral is coded badly so crashes the computer instead of showing an error :/

getting complicated haha, oh wait [Edit] seems computercraft.info has some bugs, instead of just local WorldProbe = sensors.getProbes (controllerSide,sensorName) I saw like ColourID1831728> [font=helvetica, arial etc in front of it

#16 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 03:42 PM

View PostNLBlackEagle, on 20 January 2013 - 03:37 PM, said:

View PostTheOriginalBIT, on 20 January 2013 - 03:30 PM, said:

View PostNLBlackEagle, on 20 January 2013 - 03:26 PM, said:

local WorldProbe = sensors.getProbes (controllerSide,"sensorName")
Missed a set of quotes... should be
local WorldProbe = sensors.getProbes (controllerSide,sensorName)
Black screen was because the sensor with the name sensorName didn't exist and the peripheral is coded badly so crashes the computer instead of showing an error :/

getting complicated haha, oh wait [Edit] seems computercraft.info has some bugs, instead of just local WorldProbe = sensors.getProbes (controllerSide,sensorName) I saw like ColourID1831728> [font=helvetica, arial etc in front of it
Yeh that was my fault I got lazy and copy pasted it from previous post, and when you do that inside code tags it removes all the formatting and changes them to code tags... sorry

#17 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 04:06 PM

Ohh, no need to say sorry ya re helping me out pretty good. Im starting to understand but its like knowing how a bottle of beer looks like but not knowing the tast of it. So still having trouble with understanding everything. Anyway thanks for the tip about the () that will be usefull. But, im stuck again now with this:

local controllerSide = sensors.getController()
local sensorName = "A1"
local WorldProbe = sensors.getProbes (controllerSide,sensorName)
local targets = sensors.getAvailableTargetsforProbe(controllerSide,sensorName,WorldProbe)
local playerCount = ???

So, lets get this straight... with all those links, "targets" are the controller>sensor>probe>targets> ???
Guess thats what this code is? (not used to code like this and kinda unpractical for sutch a short code XD but thats my opinion, would come in handy with uhm my elevator! with around 50 rs.setoutputblabla strings for 3 floors so 150 >.<
But, guess the code is nearly done.

Anyway, if im correct you may give me the ''complete'' code/solution, because when you where talking about "vq" I didnt had a clue on how to put that into a code :P
Guess a complete code will explain a lot of things, and if I still dont understand it I could always ask and hope for an answer :P

#18 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 04:49 PM

View PostNLBlackEagle, on 20 January 2013 - 04:06 PM, said:

Ohh, no need to say sorry ya re helping me out pretty good. Im starting to understand but its like knowing how a bottle of beer looks like but not knowing the tast of it. So still having trouble with understanding everything. Anyway thanks for the tip about the () that will be usefull. But, im stuck again now with this:

local controllerSide = sensors.getController()
local sensorName = "A1"
local WorldProbe = sensors.getProbes (controllerSide,sensorName)
local targets = sensors.getAvailableTargetsforProbe(controllerSide,sensorName,WorldProbe)
local playerCount = ???

So, lets get this straight... with all those links, "targets" are the controller>sensor>probe>targets> ???
Guess thats what this code is? (not used to code like this and kinda unpractical for sutch a short code XD but thats my opinion, would come in handy with uhm my elevator! with around 50 rs.setoutputblabla strings for 3 floors so 150 >.<
But, guess the code is nearly done.

Anyway, if im correct you may give me the ''complete'' code/solution, because when you where talking about "vq" I didnt had a clue on how to put that into a code :P
Guess a complete code will explain a lot of things, and if I still dont understand it I could always ask and hope for an answer :P

Without knowing exactly how your setup is I cant do a "complete" code to how you want it... but here is what I can do http://pastebin.com/S1wmCiw5 ... this will scan all the living entites in the world (this is mobs too... but we can easily check if the name is "vq" and if it is count the players...

I think for what you want to do you will need to have a loop around this to constantly check... then at the end just use
if #players > 0 then


#19 NLBlackEagle

  • Members
  • 25 posts
  • LocationNL

Posted 20 January 2013 - 05:22 PM

startup:10: bad argument: table expected, got nil

Getting this error, (with the code you posted) Tried various ways to solve it but didnt worked

And yes, that exacly what I was planning to make :)

#20 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 05:27 PM

change line 3 to the name of the sensor to read... and if that fails just manually change "controllerSide" on line 1 to be "right" or what ever side the controller is on... that code definitely works... I tested a few times before posting it...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users