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

OpenCCSensors and TE energy cell problem

Started by georgekate, 30 January 2016 - 02:32 PM
georgekate #1
Posted 30 January 2016 - 03:32 PM
Hello, I am trying to read the stored energy in a thermal expansion energy cell using openccsensors but for some reason I cant hook the stored energy. More specifically when I run the sensorview program everything seems fine: http://i.imgur.com/shSBlLa.png
However, when I run my program to read those values it seems as if the sensor is not even reading some of the elements. My code:
os.loadAPI("ocs/apis/sensor")
device = sensor.wrap("left")
while true do
local elements = device.getTargets()
for k,v in pairs(elements) do
  if v.Name == "Resonant Energy Cell" then
   print("a " .. v.StoredPercentage)
  end
end

os.sleep(0.5)
end
When I run the program, it says that v.StoredPercentage is nil (http://i.imgur.com/ko7RAmt.png). If I replace line 7(print) with:
for i,n in pairs(v) do print(i) end
It is supposed to print everything it has about the Energy cell(?). Instead I only get some of its properties http://i.imgur.com/UYdJCwS.png
What am I doing wrong here? How do I hook those values?
My setup: http://i.imgur.com/3uGcFuc.png. I am using Computercraft 1.75, openCCsensors 1.7.5 and thermal expansion 4.
Lyqyd #2
Posted 30 January 2016 - 06:07 PM
The right pane is showing the contents of the table returned by the getTargetDetails method, when called with the target name selected in the left pane as the first argument. Inside your if statement in the loop, remove the print call and add:


local details = device.getTargetDetails(k)
print(details.StoredPercentage)
georgekate #3
Posted 30 January 2016 - 06:46 PM
I tried it and device.getTargetDetails(k) returns nil..
Lyqyd #4
Posted 30 January 2016 - 07:13 PM
Please post the updated code.
georgekate #5
Posted 30 January 2016 - 07:32 PM
Hello, I am not sure what I did wrong last time, but now it worked fine. Thanks for the assistance!
georgekate #6
Posted 31 January 2016 - 12:38 AM
Hello, it would appear that it works under some strange circumstances. When I run a simple program that prints the stored percentage it works fine. When I run a more full-fledged program I get an error. The weird part is that if I run the small program first, terminate it, and then the big one it works fine. As a result everytime the server restarts I have to run the small program before the big one. What exactly am I doing wrong here? Image:
http://i.imgur.com/QPXD3R3.png
Line 27:
local details = device.getTargetDetails(k)

Small program:
os.loadAPI("ocs/apis/sensor")
device = sensor.wrap("left")
while true do
local elements = device.getTargets()
for k,v in pairs(elements) do
  if v.Name == "Resonant Energy Cell" then
   local details = device.getTargetDetails(k)
print(details.StoredPercentage)
  end
end
os.sleep(0.5)
end
Full program:
os.loadAPI("ocs/apis/sensor")
monitor = peripheral.wrap("monitor_2")
sensorE = peripheral.wrap("left")
sensorL = peripheral.wrap("right")
function pf(a)
if math.floor(a*100) % 100 < 10 then return "0"
else return "" end
end
while true do
local count = {}
local ammount = {}
local energy
count.oil = 0
ammount.oil = 0
count.fuel = 0
ammount.fuel = 0

local elementsE = sensorE.getTargets()
local elementsL = sensorL.getTargets()

for k,v in pairs(elementsE) do
  if v.Name == "Resonant Energy Cell" then
   local details = device.getTargetDetails(k)
   energy = details.StoredPercentage
  end
end

for k,v in pairs(elementsL) do
  if v.Tanks[1].Name == "oil" then
   if v.Position.Z == -7 and v.Position.X == 0 then
	count.oil = count.oil + 1
	ammount.oil = ammount.oil + v.Tanks[1].Amount
   end
  end

  if v.Tanks[1].Name == "fuel" then
   if v.Position.Z == 0 and v.Position.X == 2 then
	count.fuel = count.fuel + 1
	ammount.fuel = ammount.fuel + v.Tanks[1].Amount
   end
  end

end

local oil = ammount.oil/(count.oil*16000)*100
local oilP = math.floor(oil) .. "," .. pf(oil) .. (math.floor(oil*100) % 100)

local fuel = ammount.fuel/(count.fuel*16000)*100
local fuelP = math.floor(fuel) .. "," .. pf(fuel) .. (math.floor(fuel*100) % 100)

local energyP = math.floor(energy) .. "," .. pf(energy) .. (math.floor(energy*100) % 100)

monitor.clear()
monitor.setCursorPos(1, 1)
monitor.write("Energy Level: " .. energyP .. "%")
monitor.setCursorPos(1, 2)
monitor.write("Fuel Level: " .. fuelP .. "%")
monitor.setCursorPos(1, 3)
monitor.write("Oil Level: " .. oilP .. "%")

if energy < 65 then
  rs.setBundledOutput("front", colors.combine(rs.getBundledOutput("front"), colors.white))
end
if energy > 99 then
  rs.setBundledOutput("front", colors.subtract(rs.getBundledOutput("front"), colors.white))
end

if fuel < 90 then
  rs.setBundledOutput("front", colors.combine(rs.getBundledOutput("front"), colors.red))
end
if fuel > 99 then
  rs.setBundledOutput("front", colors.subtract(rs.getBundledOutput("front"), colors.red))
end
os.sleep(0.5)
end
valithor #7
Posted 31 January 2016 - 12:44 AM
Is the full program your startup program? Line 27 (if I counted correctly) is a blank line in the full program you posted, and that wouldn't throw a error.

edit:

Noticed the pastebin code was in the image, and no it is not the exact same.

The problem is that in the full program you never define the variable device. In your small program, you define it as a global variable, so the full program can use it when you run it after the small one.
Edited on 30 January 2016 - 11:45 PM
georgekate #8
Posted 31 January 2016 - 12:45 AM
The forum is messing up with my spacing. It is the line I posted
Edited on 30 January 2016 - 11:45 PM
valithor #9
Posted 31 January 2016 - 12:46 AM
The forum is messing up with my spacing. It is the lien I posted

Ahh sorry, didn't see that part of the post. :P/> The edit to my original post should show the problem.
georgekate #10
Posted 31 January 2016 - 12:50 AM
Ok I see it now. Silly problem. Thanks for the help