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

Solar Control System help...

Started by Angry_Dragonoid, 23 January 2016 - 01:47 PM
Angry_Dragonoid #1
Posted 23 January 2016 - 02:47 PM
I am trying to setup a solar generator control system for Extra Utilities Solar Generators. I am using Openperipherals to connect them and the apis from the mod. Everything works fine in my program except one thing…the variables…

For some reason, the program creates the variable of the energy amount in the generator when the program starts, and works properly. But the variable does not change until the program restarts…how do I get it to refresh the variable while the program is running to update the energy amount and continue the program.

Please let me know if this is possible at all or not. I'm pretty sure I've seen it before in other people's programs, but not sure how they did it. I would say I know a fair bit of LUA, as I have created fairly complicated programs in my past. If it's not possible, I guess that's fine, but I want some way to automatically control the generators. Thanks in advance!

The pastebin…

http://pastebin.com/qPLshg40

The code…


generator1 = peripheral.wrap("extrautils_generatorsolar_6")
generator2 = peripheral.wrap("extrautils_generatorsolar_7")
generator3 = peripheral.wrap("extrautils_generatorsolar_8")
 
local energy1 = generator1.getEnergyStored()
local energy2 = generator2.getEnergyStored()
local energy3 = generator3.getEnergyStored()
function display()
    print("")
    term.clearLine(1,3)
    term.setCursorPos(1,3)
    print("Solar 1: " .. energy1 .. " / 100000 RF")
    print("")
    term.clearLine(1,5)
    term.setCursorPos(1,5)
    print("Solar 2: " .. energy2 .. " / 100000 RF")
    print("")
    term.clearLine(1,7)
    term.setCursorPos(1,7)
    print("Solar 3: " .. energy3 .. " / 100000 RF")
end
function scan()
  term.clear()
  term.setCursorPos(1,1)
  print("Scanning solar.")
  sleep(.5)
  term.clear()
  term.setCursorPos(1,1)
  print("Scanning solar..")
  sleep(.5)
  term.clear()
  term.setCursorPos(1,1)
  print("Scenning solar...")
  sleep(.5)
  term.clear()
  term.setCursorPos(1,1)
  print("Solar scanned...")
end
function main()
  scan()
  monitorGenerator1()
  while true do
    display()
    os.sleep(1)
  end
end

function generatorMonitor()
  while true do
    monitorGenerator1()
    sleep(1)
  end
end
function monitorGenerator1()
  if energy1 == 100000 then
    term.clearLine(1,9)
    term.setCursorPos(1,9)
    print("Solar 1 is full...")
    redstone.setOutput("top", true)
  end
  if energy1 == 0 then
    term.clearLine(1,9)
    term.setCursorPos(1,9)
    print("Solar 1 is filling...")
    redstone.setOutput("top", false)
  end
end
main()
TYKUHN2 #2
Posted 23 January 2016 - 04:18 PM
Variables don't automatically refresh. When you set the variables you set them to the value of the function at the time the variables were being processed. To update them you have to set them a new value (or the same function at a different time)
Lupus590 #3
Posted 23 January 2016 - 05:23 PM
you could do

local energy1 = generator1.getEnergyStored
local energy2 = generator2.getEnergyStored
local energy3 = generator3.getEnergyStored
--#note that all i did was remove the ()'s this makes energy1 a 'copy' of the function which is called generator1.getEnergyStored

but you would have to change where you use the variables to call them as functions.

also why are your generator variables global when you know how to make local variables?
Edited on 23 January 2016 - 04:24 PM
HPWebcamAble #4
Posted 23 January 2016 - 05:23 PM
First, term.clearLine doesn't accept arguments. You have to put the cursor on the line you want to clear, then call term.clearLine:


term.setCursorPos(1, 1)
term.clearLine() --# Ends up clearing the top line (y = 1)



But besides that, your code is structured incorrectly. I completely understand why, but unfortunately Lua will not.

Your best bet is to do everything in one loop:

local generator1 = peripheral.wrap("extrautils_generatorsolar_6")
local generator2 = peripheral.wrap("extrautils_generatorsolar_7")
local generator3 = peripheral.wrap("extrautils_generatorsolar_8")

while true do

  term.clear() --# Clear the whole screen

  --# Check generator1
  term.setCursorPos(1,3)
  print("Solar 1:" .. generator1.getEnergyStored() .. " / 100000 RF")
  term.setCursorPos(1,9)
  if generator1.getEnergyStored >= 100000 then
	print("Solar 1 is full!")
	rs.setOutput("top",true)
  else
	print("Solar 1 filling...")
	rs.setOutput("top",false)
  end

  --# Check generator2
  term.setCursorPos(1,5)
  print("Solar 2:" .. generator2.getEnergyStored() .. " / 100000 RF")

  --# Check generator3
  term.setCursorPos(1,7)
  print("Solar 3:" .. generator3.getEnergyStored() .. " / 100000 RF")

  --# Sleep for a second
  sleep(1)

end

Also wanted to mention, each solar generator SHOULD fill at the same rate. So just monitor one, and when it fills, you know its time to empty all of them.
There shouldn't be a need to check them all individually.
Edited on 23 January 2016 - 04:35 PM