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

[Request] ccSensors - Detect MFSU energy

Started by HendRoox, 16 July 2014 - 10:49 AM
HendRoox #1
Posted 16 July 2014 - 12:49 PM
Hi. I have 6 MFSU and I'm need to detect MFSU energy. :/ Anyone help me?
Cranium #2
Posted 16 July 2014 - 08:32 PM
Moved to Ask A Pro.
hilburn #3
Posted 16 July 2014 - 11:15 PM
I'm assuming you mean OpenCCSensors as CCSensors is super out of date

Now as a disclaimer, I don't have OpenCCSensors to play around with so this is based on a look at the wiki and is very rough, you will almost definitely have to change some of the values I use


os.loadAPI("ocs/apis/sensor")
sensor = peripheral.wrap("left") --#Power Sensor card in sensor on left
function mfsuPower()
    local power = 0
    for i, j in pairs(sensor.getTargets())
	    if j.name == "MFSU" then power = power + j.power end --#I don't know what the name or power is stored as, do a textutils.serialize() to find out, will also tell you what the MFSU name is which I am also guessing at
    end
    return power
end
print(mfsuPower())
Cranium #4
Posted 16 July 2014 - 11:33 PM
I'd assume that they're using OpenPeripheral.
hilburn #5
Posted 16 July 2014 - 11:49 PM
I was going off the title…
HendRoox #6
Posted 17 July 2014 - 08:55 PM
I'm assuming you mean OpenCCSensors as CCSensors is super out of date

Now as a disclaimer, I don't have OpenCCSensors to play around with so this is based on a look at the wiki and is very rough, you will almost definitely have to change some of the values I use


os.loadAPI("ocs/apis/sensor")
sensor = peripheral.wrap("left") --#Power Sensor card in sensor on left
function mfsuPower()
	local power = 0
	for i, j in pairs(sensor.getTargets())
		if j.name == "MFSU" then power = power + j.power end --#I don't know what the name or power is stored as, do a textutils.serialize() to find out, will also tell you what the MFSU name is which I am also guessing at
	end
	return power
end
print(mfsuPower())


API Sensors is already being loaded
startup:6: attempt to call nil

://
Bubba #7
Posted 17 July 2014 - 08:58 PM
HendRoox, due to the fact that you seem unwilling to put any effort into this, I'm putting this into General as a program request. If anyone would like to write this program, please feel free. If HendRoox changes his mind and decides he would like to write the program himself and still needs help, he can report the post and have it moved back to AaP.
Edited on 17 July 2014 - 07:01 PM
Cranium #8
Posted 17 July 2014 - 09:09 PM
Changed the title to reflect a request for a program.
HendRoox #9
Posted 18 July 2014 - 08:43 AM

while true do
sleep(1)
mon = peripheral.wrap("right")
mon.clear()
mon.setTextScale(2)
ctrl = sensors.getController()
data = sensors.getSensors(ctrl)
reactorSensor = data[1]
data = sensors.getSensorInfo(ctrl,reactorSensor)
sensors.setSensorRange(ctrl,reactorSensor,"2")
data = sensors.getProbes(ctrl,reactorSensor)
eustorageProbe = data[3]
data = sensors.getAvailableTargetsforProbe(ctrl,reactorSensor,eustorageProbe)
mfsuTarget = data[3]
sensors.setTarget(ctrl,reactorSensor,mfsuTarget)
data = sensors.getSensorReadingAsDict(ctrl,reactorSensor,mfsuTarget,eustorageProbe)
mon.setCursorPos(1, 1)
mon.write("MFSU EU:"..data.energy)
mon.setCursorPos(1, 2)
mon.write("MFSU Out:"..data.output)
end

:// It's write the one MFSU. How to convert to write all MFSU? pls help
LBPHacker #10
Posted 18 July 2014 - 12:32 PM
Exchanged ideas with the guy on Skype and we worked out a solution. All the code above misses is a for loop that iterates through the table returned by sensors.getAvailableTargetsforProbe instead of just getting the 3rd element.
Clean, indented, working code and a screenshot
local mfsuMaxEnergy = 10000000
local energybarWidth = 10
local monitorSide = "top"

local monitor = peripheral.wrap(monitorSide)
monitor.setTextScale(2)
local controller = sensors.getController()
local sensor = sensors.getSensors(controller)[1]
sensors.setSensorRange(controller, sensor, 10)
local probes = sensors.getProbes(controller, sensor)

while true do
    monitor.clear()
    local data = sensors.getAvailableTargetsforProbe(controller, sensor, "EUStorage")
    for ix = 1, #data do
        local energy = math.min(mfsuMaxEnergy, math.max(0, sensors.getSensorReadingAsDict(controller, sensor, data[ix], "EUStorage").energy))
        local barprogress = math.ceil(energy / (mfsuMaxEnergy / energybarWidth))
        monitor.setCursorPos(1, ix)
        monitor.write(string.format("[%s%s] (%s%i Eu)", string.rep("#", barprogress), string.rep(" ", energybarWidth - barprogress), string.rep(" ", 8 - #tostring(energy)), energy))
    end
    sleep(1)
end
[attachment=1819:finally.png]
He's playing on a Tekkit Classic server (CC 1.33) so no color magic.

EDIT: Holy crap, did I really just post it without using code tags? Fixed.
Edited on 18 July 2014 - 11:06 AM