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

ccSensors Tank Monitor

Started by Tbone, 23 September 2012 - 02:37 PM
Tbone #1
Posted 23 September 2012 - 04:37 PM
Hi.

I´m trying to create a tank monitor program using ccSensors, but i have at few questions.

I have these 3 tanks:

[attachment=476:Tanks.png]

And I get this on my monitor:

[attachment=477:Monitor.png]

My code looks like this:

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

mon = peripheral.wrap("top")

for i=1,1000 do

mon.setCursorPos(1,1)
mon.write("Fuel Level")

os.sleep(5)

mon.clear()

function printDict(data)
for i,v in pairs(data) do
print(tostring(i).." - "..tostring(v))
end
end

ctrl = sensors.getController()
print(ctrl)

data = sensors.getSensors(ctrl)
printDict(data)

Fuel = data[1]
–Oil = data[2]

data = sensors.getSensorInfo(ctrl,Fuel)
–printDict(data)

–data = sensors.getSensorInfo(ctrl,Oil)
–printDict(data)

sensors.setSensorRange(ctrl,Fuel,"3")
–print(sensors.getSensorInfo(ctrl,Fuel).SensorRange)

data = sensors.getProbes(ctrl,Fuel)
–printDict(data)

liquidTank = data[8]

sensors.setTarget(ctrl,Fuel,liquidTank)

data = sensors.getAvailableTargetsforProbe(ctrl,Fuel,liquidTank)
printDict(data)

–[[Tank1]]–

fuelLevel1 = data[9]
fuelLevel2 = data[6]
fuelLevel3 = data[3]

–[[Tank2]]–

fuelLevel4 = data[8]
fuelLevel5 = data[5]
fuelLevel6 = data[2]

–[[Tank3]]–

fuelLevel7 = data[7]
fuelLevel8 = data[4]
fuelLevel9 = data[1]

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel1,liquidTank)
–print(data.stored)
mon.setCursorPos(1,4)
mon.write("Tank 1")
mon.setCursorPos(21,3)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel2,liquidTank)
–print(data.stored)
mon.setCursorPos(21,4)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel3,liquidTank)
–print(data.stored)
mon.setCursorPos(21,5)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel4,liquidTank)
–print(data.stored)
mon.setCursorPos(1,8)
mon.write("Tank 2")
mon.setCursorPos(21,7)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel5,liquidTank)
–print(data.stored)
mon.setCursorPos(21,8)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel6,liquidTank)
–print(data.stored)
mon.setCursorPos(21,9)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel7,liquidTank)
–print(data.stored)
mon.setCursorPos(1,12)
mon.write("Tank 3")
mon.setCursorPos(21,11)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel8,liquidTank)
–print(data.stored)
mon.setCursorPos(21,12)
mon.write(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel9,liquidTank)
–print(data.stored)
–mon.write(data.stored)
mon.setCursorPos(21,13)
mon.write(data.stored)
end

os.reboot()

I know it is pretty messy, and if any of you got at easier solution let me know.

My question is:

How do i combine "fuelLevel1, fuelLevel2 and fuelLevel3" in to one total value?

Thanks. Thomas.
sjele #2
Posted 23 September 2012 - 04:39 PM
try var1 + var2 + var3

Example:

local var1 = 4
local var2 = 6
local var3 = 20

local total = var1 + var2 + var3
Tbone #3
Posted 23 September 2012 - 04:53 PM
Hmm. Sorry I dont get it..

Can you put the values from my code in to your example?
sjele #4
Posted 23 September 2012 - 05:04 PM
I am not 200% sure if this is the right vars but i will give it a shot.

--Tank 1
totalFuel1 = fuelLevel1 + fuelLevel2 + fuelLevel3


--Tank 2
totalFuel2 = fuelLevel4 + fuelLevel5 + fuelLevel6

--Tank 3
totalFuel3 = fuelLevel7 + fuelLevel8 + fuelLevel9
Tbone #5
Posted 23 September 2012 - 05:15 PM
It should look like this right?

it is crashing the code if i do it like this..


--[[Tank1]]--
fuelLevel1 = data[9]
fuelLevel2 = data[6]
fuelLevel3 = data[3]
totalFuel1 = fuelLevel1 + fuelLevel2 + fuelLevel3


Fatal_Exception #6
Posted 24 September 2012 - 12:29 AM
What's the error message?
Tbone #7
Posted 24 September 2012 - 02:13 PM
Hi.

I get this error:

"startup:58: attempt to perform arithmetic __add on string and string"

Using this code:

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

mon = peripheral.wrap("top")

for i=1,1000 do

mon.setCursorPos(1,1)
mon.write("Fuel Level")

os.sleep(5)

mon.clear()

function printDict(data)
for i,v in pairs(data) do
print(tostring(i).." - "..tostring(v))
end
end

ctrl = sensors.getController()
–print(ctrl)

data = sensors.getSensors(ctrl)
–printDict(data)

Fuel = data[1]
–Oil = data[2]

data = sensors.getSensorInfo(ctrl,Fuel)
printDict(data)

–data = sensors.getSensorInfo(ctrl,Oil)
–printDict(data)

sensors.setSensorRange(ctrl,Fuel,"3")
print(sensors.getSensorInfo(ctrl,Fuel).SensorRange)

–sensors.setSensorRange(ctrl,Oil,"3")
–print(sensors.getSensorInfo(ctrl,Oil).SensorRange)

data = sensors.getProbes(ctrl,Fuel)
–printDict(data)

liquidTank = data[8]

sensors.setTarget(ctrl,Fuel,liquidTank)

data = sensors.getAvailableTargetsforProbe(ctrl,Fuel,liquidTank)
–printDict(data)

–[[Tank1]]–

fuelLevel1 = data[9]
fuelLevel2 = data[6]
fuelLevel3 = data[3]
local totalFuel1
totalFuel1 = fuelLevel1 + fuelLevel2 + fuelLevel3
–printDict(totalFuel1)

–[[Tank2]]–

fuelLevel4 = data[8]
fuelLevel5 = data[5]
fuelLevel6 = data[2]

–[[Tank3]]–

fuelLevel7 = data[7]
fuelLevel8 = data[4]
fuelLevel9 = data[1]

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel1,liquidTank)
–print(data.stored)
mon.setCursorPos(1,4)
mon.write("Tank 1")
mon.setCursorPos(21,3)
mon.write(data.stored/16000*100)
–print(string.format("%.1f", data.stored/16000*100))

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel2,liquidTank)
–print(data.stored)
mon.setCursorPos(21,4)
mon.write(data.stored/16000*100)
–print(data.stored)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel3,liquidTank)
–print(data.stored)
mon.setCursorPos(21,5)
mon.write(data.stored/16000*100)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel4,liquidTank)
–print(data.stored)
mon.setCursorPos(1,8)
mon.write("Tank 2")
mon.setCursorPos(21,7)
mon.write(data.stored/16000*100)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel5,liquidTank)
–print(data.stored)
mon.setCursorPos(21,8)
mon.write(data.stored/16000*100)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel6,liquidTank)
–print(data.stored)
mon.setCursorPos(21,9)
mon.write(data.stored/16000*100)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel7,liquidTank)
–print(data.stored)
mon.setCursorPos(1,12)
mon.write("Tank 3")
mon.setCursorPos(21,11)
mon.write(data.stored/16000*100)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel8,liquidTank)
–print(data.stored)
mon.setCursorPos(21,12)
mon.write(data.stored/16000*100)

data = sensors.getSensorReadingAsDict(ctrl,Fuel,fuelLevel9,liquidTank)
–print(data.stored)
–mon.write(data.stored)
mon.setCursorPos(21,13)
mon.write(data.stored/16000*100)
end

os.reboot()
Fatal_Exception #8
Posted 24 September 2012 - 05:46 PM
Take a look at this and adjust it to suit:
Spoiler
os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")

local sensorName = "Sensor"  -- Sensor channel name
local tanks = {[1] = {9,6,3},   -- IDs for the tanks
			   [2] = {8,5,2},
			   [3] = {7,4,1},
			  }

local monitorSide = "top"
local sleepTime = 2 -- How often to refresh

local sensorType = "liquidTank"
local levels = {}

-- Get the total amount of liquid in a tank
-- by summing the individual blocks
local function getTankTotal(tank)
  local tankTotal = 0
  for k, v in pairs(tanks[tank]) do
	tankTotal = tankTotal + levels[v]
  end
  return tankTotal
end

-- Get the amount of liquid in an individual tank block
function getIndividualTankLevel(tank, sub)
  -- This seems kinda clumsy, but meh
  local _tank = tanks[tank]
  local _sub = _tank[sub]
  return levels[_sub]
end

-- Query CCSensors for the tank levels
function getLevelsFromSensor()
  levels = {}
  local data = sensors.getAvailableTargetsforProbe(ctrl, sensorName, sensorType)
  for k, v in pairs(data) do
	table.insert(levels, sensors.getSensorReadingAsDict(ctrl, sensorName, v, sensorType).stored)
  end
end

local ctrl = sensors.getController()
local mon = peripheral.wrap(monitorSide)

while true do
  getLevelsFromSensor()

  -- Format this stuff however you want

  mon.clear()
  mon.setCursorPos(1,1)
  mon.write("Fuel Levels")
  mon.setCursorPos(1,2)
  mon.write("Tank 1: " .. getTankTotal(1))
  mon.setCursorPos(1,3)
  mon.write("Tank 2: " .. getTankTotal(2))
  mon.setCursorPos(1,4)
  mon.write("Tank 3: " .. getTankTotal(3))
  os.sleep(sleepTime)
end

Oh, and the reason you were gettings errors in your code was, the values you were trying to add together weren't numbers. They were identifiers to tell CCsensors which block you wanted to look at.
Tbone #9
Posted 24 September 2012 - 09:50 PM
Wow;-)

Thanks a lot…..

I will give it a try tomorrow..
Tbone #10
Posted 25 September 2012 - 05:56 PM
Its me again;-)

I did this, but i get a error at line 21.

bios:206: [string "test"]:21: ´=´ expected

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

local sensorName = "Oil" – Sensor channel name
local tanks = {[1] = {10,7,4,1}, – IDs for the tanks
[2] = {11,8,5,2},
[3] = {12,9,6,3},}

local monitorSide = "top"
local sleepTime = 2 – How often to refresh

local sensorType = "liquidTank"
local levels = {}

–Get the total amount of liquid in a tank
–by summing the individual blocks
local function getTankTotal(tank)
mon.write

local tankTotal = 0 –[[THIS IS LINE 21]–
for k, v in pairs(tanks[tank]) do
tankTotal = tankTotal + levels[v]
end
return tankTotal
end

– Get the amount of liquid in an individual tank block
function getIndividualTankLevel(tank, sub)
– This seems kinda clumsy, but meh
local _tank = tanks[tank]
local _sub = _tank
return levels[_sub]
end

– Query CCSensors for the tank levels
function getLevelsFromSensor()
levels = {}
local data = sensors.getAvailableTargetsforProbe(ctrl, sensorName, sensorType)
for k, v in pairs(data) do
table.insert(levels, sensors.getSensorReadingAsDict(ctrl, sensorName, v, sensorType).stored)
end
end

local ctrl = sensors.getController()
local mon = peripheral.wrap(monitorSide)

while true do
getLevelsFromSensor()

– Format this stuff however you want

mon.clear()
mon.setCursorPos(1,1)
mon.write("Oil Levels")
mon.setCursorPos(1,2)
mon.write("Tank 1: " .. getTankTotal(1))
mon.setCursorPos(1,3)
mon.write("Tank 2: " .. getTankTotal(2))
mon.setCursorPos(1,4)
mon.write("Tank 3: " .. getTankTotal(3))
os.sleep(sleepTime)
end
sjele #11
Posted 25 September 2012 - 05:58 PM
On line 21 you have mon.write

mon.write("text") is how it is supposed to be used
Tbone #12
Posted 25 September 2012 - 06:00 PM
On line 21 you have mon.write

mon.write("text") is how it is supposed to be used

woops. Sorry should have noticed that….
Tbone #13
Posted 25 September 2012 - 06:11 PM

test:22: attempt to perform arithmetic __add on nil and nil

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

local sensorName = "Oil" – Sensor channel name
local tanks = {[1] = {10,7,4,1}, – IDs for the tanks
[2] = {11,8,5,2},
[3] = {12,9,6,3},}

local monitorSide = "top"
local sleepTime = 2 – How often to refresh

local sensorType = "liquidTank"
local levels = {}

–Get the total amount of liquid in a tank
–by summing the individual blocks
local function getTankTotal(tank)

local tankTotal = 0
for k, v in pairs(tanks[tank]) do
tankTotal = tankTotal + levels[v]
print(tankTotal)
end
return tankTotal
end

– Get the amount of liquid in an individual tank block
function getIndividualTankLevel(tank, sub)
– This seems kinda clumsy, but meh
local _tank = tanks[tank]
local _sub = _tank
return levels[_sub]
end

– Query CCSensors for the tank levels
function getLevelsFromSensor()
levels = {}
local data = sensors.getAvailableTargetsforProbe(ctrl, sensorName, sensorType)
for k, v in pairs(data) do
table.insert(levels, sensors.getSensorReadingAsDict(ctrl, sensorName, v, sensorType).stored)
end
end

local ctrl = sensors.getController()
local mon = peripheral.wrap(monitorSide)

while true do
getLevelsFromSensor()

– Format this stuff however you want

mon.clear()
mon.setCursorPos(1,1)
mon.write("Oil Levels")
mon.setCursorPos(1,2)
mon.write("Tank 1: " .. getTankTotal(1))
mon.setCursorPos(1,3)
mon.write("Tank 2: " .. getTankTotal(2))
mon.setCursorPos(1,4)
mon.write("Tank 3: " .. getTankTotal(3))
os.sleep(sleepTime)
end
Tbone #14
Posted 26 September 2012 - 04:07 PM

test:22: attempt to perform arithmetic __add on nil and nil

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

local sensorName = "Oil" – Sensor channel name
local tanks = {[1] = {10,7,4,1}, – IDs for the tanks
[2] = {11,8,5,2},
[3] = {12,9,6,3},}

local monitorSide = "top"
local sleepTime = 2 – How often to refresh

local sensorType = "liquidTank"
local levels = {}

–Get the total amount of liquid in a tank
–by summing the individual blocks
local function getTankTotal(tank)

local tankTotal = 0
for k, v in pairs(tanks[tank]) do
tankTotal = tankTotal + levels[v]
print(tankTotal)
end
return tankTotal
end

– Get the amount of liquid in an individual tank block
function getIndividualTankLevel(tank, sub)
– This seems kinda clumsy, but meh
local _tank = tanks[tank]
local _sub = _tank
return levels[_sub]
end

– Query CCSensors for the tank levels
function getLevelsFromSensor()
levels = {}
local data = sensors.getAvailableTargetsforProbe(ctrl, sensorName, sensorType)
for k, v in pairs(data) do
table.insert(levels, sensors.getSensorReadingAsDict(ctrl, sensorName, v, sensorType).stored)
end
end

local ctrl = sensors.getController()
local mon = peripheral.wrap(monitorSide)

while true do
getLevelsFromSensor()

– Format this stuff however you want

mon.clear()
mon.setCursorPos(1,1)
mon.write("Oil Levels")
mon.setCursorPos(1,2)
mon.write("Tank 1: " .. getTankTotal(1))
mon.setCursorPos(1,3)
mon.write("Tank 2: " .. getTankTotal(2))
mon.setCursorPos(1,4)
mon.write("Tank 3: " .. getTankTotal(3))
os.sleep(sleepTime)
end

Nobody knows? Or is it so obvious that I should be able to see it?