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

LUA Reactor Monitoring issues.

Started by DennisVL, 20 October 2012 - 10:47 PM
DennisVL #1
Posted 21 October 2012 - 12:47 AM
Hey there,

I've been an active reader of this forum for quite a while now, and honestly I've learned a lot. But I'm still not an expert.
Recently I've come across an error to which I can not find any possible fix.

First of my basic setup.

Monitor is on top, controller for the reactor is at the back, controller for storage devices is at the bottom of the computer.

I've been getting the following error message after the first timer trigger: startup:33: attempt to compate __lt on nil and number.

Here's my full code as it is right now:

------------------------ API Loads ------------------------
os.unloadAPI("Sensors")
os.loadAPI("/rom/apis/sensors")
------------------------ Basic Locals ------------------------
local monitor = peripheral.wrap( "top" )
-- Full-Fill
local graphChar="#"
-- Half-Fill
local graphHalfChar="."
------------------------ GUI Functions ------------------------
local sizeX, sizeY = monitor.getSize()
function round(val, decimal)
  if (decimal) then
    return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  else
    return math.floor(val+0.5)
  end
end
function writeAt(x,y,str)
monitor.setCursorPos(x ,y);
monitor.write(str);
end
function hBar(x,y,size,label,val,maxval)
if val<0 then val=0 end;
local pct = round(val*100/maxval,1);
local scale=100/size;
local fill=pct/scale;
writeAt(x,y,label)
writeAt(15,y,"[");
local spos=math.floor(fill)
for i=1,spos do
  monitor.write(graphChar)
end

if fill % math.floor(fill) >0 then
  monitor.write(graphHalfChar);
  spos = spos+1;
end

for i=spos+1,size do
  monitor.write(" ")
end
monitor.write("]"..string.rep(" ",5-string.len(pct))..pct.."%  ");
end
function hBar2(x,y,size,label,val,maxval)
if val<0 then val=0 end
local pct = round(val*100/maxval,1);
local scale=100/size;
local fill=pct/scale;
writeAt(x,y,label)
writeAt(12,y,"[");
local spos=math.floor(fill)
for i=1,spos do
  monitor.write(graphChar)
end

if fill % math.floor(fill) >0 then
  monitor.write(graphHalfChar);
  spos = spos+1;
end

for i=spos+1,size do
  monitor.write(" ")
end
monitor.write("]"..string.rep("",5-string.len(pct))..pct.."%  ");
end
function vr(x,y,size)
if size>=sizeX then size=sizeX-1 end
for i=y,y+size do
  monitor.setCursorPos(x,i);
  monitor.write("|");
end
end
function hr(y)
for x=1,sizeX do
  monitor.setCursorPos(x,y);
  monitor.write("-");
end
end
------------------------ Main Code ------------------------
local sensor ="Sensor";
local side = "back";
local targets = sensors.getAvailableTargetsforProbe(side,sensor,"Reactor");
local e = sensorsData.getProbe("ic2","reactor");
local reactor = sensors.getSensorReadingAsDict(side,sensor,targets[1],"Reactor");
local content = sensors.getSensorReadingAsDict(side,sensor,targets[1],"ReactorContent");
local sensor ="Sensor";
local side = "bottom";
local targets = sensors.getAvailableTargetsforProbe(side,sensor,"EUStorage");
local targets_mfsu = sensors.getAvailableTargetsforProbe(side,sensor,"EUStorage");
local eus = sensorsData.getProbe("ic2","eustorage");
local mfsu={};
for ind,tg in ipairs(targets_mfsu) do
mfsu[ind] = sensors.getSensorReadingAsDict(side,sensor,targets_mfsu[ind],"EUStorage")
end
local refresh = 2
local timer1=os.startTimer(refresh)
local count=1;
local progress={"-","\\","|","/"};
local itemMap = sensors.getItemsInfo({"Coolant Cell","Uranium Cell","Integrated Heat Disperser","Integrated Reactor Plating","Ice","Water Bucket","Bucket", "Near-depleted Uranium Cells"},content);

while not done do
if reactor[e.size.val] == 1 then
  writeAt(2,4, "Chambers:    [1] 2 3 4 5 6")
elseif reactor[e.size.val] == 2 then
  writeAt(2,4, "Chambers:    1 [2] 3 4 5 6")
elseif reactor[e.size.val] == 3 then
  writeAt(2,4, "Chambers:    1 2 [3] 4 5 6")
elseif reactor[e.size.val] == 4 then
  writeAt(2,4, "Chambers:    1 2 3 [4] 5 6")
elseif reactor[e.size.val] == 5 then
  writeAt(2,4, "Chambers:    1 2 3 4 [5] 6")
else
  writeAt(2,4, "Chambers:    1 2 3 4 5 [6]")
end


writeAt(2,2,"----------Reactor Info-----------")
hBar (2,5,10,"CoreTemp: ",reactor[e.heat.val],reactor[e.size.val]*1000+10000)
writeAt(2,6, "(EU/t)  :    "..reactor[e.energy.val])

writeAt(2,8,"----------Cooling Info-----------")
hBar(2,10,10,"Coolant :",itemMap["Coolant Cell"].qty*10000-itemMap["Coolant Cell"].dmg,itemMap["Coolant Cell"].qty*10000);
hBar(2,11,10,"Dispersr:",itemMap["Integrated Heat Disperser"].qty*10000-itemMap["Integrated Heat Disperser"].dmg,itemMap["Integrated Heat Disperser"].qty*10000);

writeAt(2,13,"------------Fuel Info------------")
hBar(2,15,10,"Intact  :",itemMap["Uranium Cell"].qty*10000-itemMap["Uranium Cell"].dmg,itemMap["Uranium Cell"].qty*10000);
hBar(2,16,10,"Depleted:",itemMap["Uranium Cell"].qty*10000-itemMap["Uranium Cell"].dmg,itemMap["Uranium Cell"].qty*10000);

writeAt(37,2,"EU Storage")
for ind,tg in ipairs(targets_mfsu) do
  hBar2(37,2+ind,10,"MFSU-"..ind,mfsu[ind][eus.storage.val],mfsu[ind][eus.storage.maxval]);
end

hr(1)
hr(18)
vr(1,1,18)
vr(57,1,18)
vr(36,1,18)
vr(35,1,18)

evt,k = os.pullEvent()
if evt=="timer" and k == timer1 then
  timer1=os.startTimer(refresh)
  reactor = sensors.getSensorReadingAsDict(side,sensor,targets[1],"Reactor")
  content = sensors.getSensorReadingAsDict(side,sensor,targets[1],"ReactorContent")
  itemMap = sensors.getItemsInfo({"Coolant Cell","Uranium Cell","Integrated Heat Disperser","Integrated Reactor Plating","Ice","Water Bucket","Bucket", "Near-depleted Uranium Cells"},content);
  for ind,tg in ipairs(targets_mfsu) do
   mfsu[ind] = sensors.getSensorReadingAsDict(side,sensor,targets_mfsu[ind],"EUStorage")
  end
 
elseif evt=="key" and (k == 28 or k ==57) then
  done=true
end

writeAt(15,1,progress[count])
count = count+1;
if count>4 then count=1 end
end
term.restore();
term.setCursorPos(1,sizeY)

If it helped anybody, you're welcome.
And I hope somebody knows the answer to my question.
ChunLing #2
Posted 21 October 2012 - 05:00 AM
You must be missing one of the values that goes into calculating "fill" in "if fill % math.floor(fill) >0 then". Make sure that size,val,and maxval are all getting passed to hBar() reliably…like by adding "print(size,val,maxval)" right at the beginning of the function.
DennisVL #3
Posted 21 October 2012 - 03:05 PM
Fixed the issue, later on in the code it still reflects on the locals 'side' and 'sensor' which have been redetermined halfway to identify the local 'eus'.

Now I have a problem where the sensors won't detect the values of the sensor that's connected to the bottom part of the computer.
ChunLing #4
Posted 21 October 2012 - 08:48 PM
Can't help much with sensors, I'm afraid. No experience with them.