I am working on a long distance robot control program. I have a Geolyzer and Linked Card installed inside of it for getting an image of the robot's surroundings and then sends the data to my control station where the mod OpenGlasses draws blocks for an illustration. I got it working when I simply registered a event.listen() at the start of the program, but it is kind of an inconvenience if I want other kinds of signals from the robot. So my idea was to only have one registered when it's waiting for the scan data and when it's done it should unregister it. But this new approach doesn't seem to work for some reason. I made it print out its return value when I registered/unregistered it, and it returned 1 and false.
I have no idea how to proceed to the next step. Help is appreciated!
station.lua
local c = require("component")local lights = require("lights")local gButtons = require("gButtonAPI4")local gKeyboard = require("gKeyboard")local event = require("event")local ser = require("serialization")local thread = require("thread")local d = require("computer")local GPU = c.proxy("332a93bc-f0a6-4c71-9460-d7a7d6ee0d2c")local APU = c.proxy("4ba6d276-efab-474c-ba48-bcbd7fd62353")--local glasses = c.glasseslocal terrain ={}local localPos ={0,0,0}local facing ="W"-- S W N E (+Z -X -Z +X)local digitDisplays ={{"a87d9238-f46e-4b17-b605-6d8771fba4c4","28dfb050-4f70-4bf8-b221-cc84ab43c660","fef15435-a434-4e33-a3ca-3f725e02dd7f","c0941fad-d5fb-4dce-b356-724431e6b66c"},{"d5a80dc6-963c-4fb5-98ea-711ebb2ff1eb","c868c412-22f5-4605-8e4a-b4bce3e1320c","19b54b1e-8970-4305-9294-d3582d85d06d","c06effa6-bada-4ddc-8578-c46d155b4250"},{"6191d4e0-715f-4c8c-a98e-8960c79c26ab","54ab4452-b63f-4b43-a1e1-13208eac1628","84948db9-81dd-4646-932f-a047ecdef4d7","f226bacf-7af6-47ad-be01-6bb31fcfda9b"}}localfunction compress(tab)
tab = ser.serialize(tab)
tab = c.data.deflate(tab)return tab
endlocalfunction decompress(val)
val = c.data.inflate(val)
val = ser.unserialize(val)return val
endlocalfunction addBlock(pos,color)local x = c.glasses.addCube3D()
x.set3DPos(table.unpack(pos))
x.setColor(table.unpack(color))
x.setAlpha(0.7)
x.setScale(0.8)return x
endlocalfunction HSL(hue, saturation, lightness, alpha)if hue <0or hue >360thenreturn0,0,0, alpha
endif saturation <0or saturation >1thenreturn0,0,0, alpha
endif lightness <0or lightness >1thenreturn0,0,0, alpha
endlocal chroma =(1- math.abs(2* lightness -1))* saturation
local h = hue/60local x =(1- math.abs(h %2-1))* chroma
local r, g, b =0,0,0if h <1then
r,g,b=chroma,x,0elseif h <2then
r,b,g=x,chroma,0elseif h <3then
r,g,b=0,chroma,x
elseif h <4then
r,g,b=0,x,chroma
elseif h <5then
r,g,b=x,0,chroma
else
r,g,b=chroma,0,x
endlocal m = lightness - chroma/2return(r+m)*255,(g+m)*255,(b+m)*255end
lights.digit(3,digitDisplays[1],0xff033d)
lights.digit(5,digitDisplays[2],0xff033d)
lights.digit(9,digitDisplays[3],0xff033d)
GPU.set(25,5,"Le GPU")
APU.set(25,5,"La APU")for i=1,5do d.beep()endlocalfunction receiveScanData(_,_,_,_,_,msg1,msg2,msg3,msg4)
print(msg1)if msg1 =="scanData"thenelse--[[addBlock(table.unpack(decompress(msg1)),{0,255,0})
addBlock(table.unpack(decompress(msg2)),{0,255,0})
addBlock(table.unpack(decompress(msg3)),{0,255,0})
addBlock(table.unpack(decompress(msg4)),{0,255,0})--]]local coord ={decompress(msg1),decompress(msg2),decompress(msg3),decompress(msg4)}for i in pairs(coord)do
thisHue =360-((coord[i][1]+5))/9*360
addBlock({coord[i][1],coord[i][2],coord[i][3]},{HSL(thisHue,1,0.5,1)})if coord[i][2]then print(coord[i][1],coord[i][2],coord[i][3],thisHue)endendendend--event,_,linkingCard,_,_,msg1,msg2,msg3,msg4--event.listen("modem_message",receiveScanData)
gButtons.initialize()--name,group,label,x,y,w,h,color,alpha,callback
gButtons.createNewButton("scan",nil,"Scan",5,5,35,14,{255,0,0},0.4,function(x)
gButtons.Color(x,{0,255,0})--c.glasses.removeAll()
c.tunnel.send("scan")
os.sleep(0.4)
gButtons.Color(x)end)localfunction update()--print(event.pull(1/20,"modem_message"))
os.sleep(1/20)
thread.create(function()
gButtons.update()end)
thread.create(function()local _,_,_,_,_,o = event.pull(1/20,"modem_message")--print(o)if o =="scanData"then
print(event.listen("modem_message",receiveScanData))
print("Enabled")endif o =="scanDataComplete"then
print(event.ignore("modem_message",receiveScanData))
print("Disabled")endend)endwhiletruedo update()end
remote.lua (Robot)
local computer = require("computer")local c = require("component")local event = require("event")local ser = require("serialization")local tty = require("tty")local thread = require("thread")local hardValues ={}local terrain ={}--c.tunnel.send()localfunction compress(val)
val = ser.serialize(val)
val = c.data.deflate(val)return val
endlocalfunction decompress(val)
val = c.data.inflate(val)
val = ser.unserialize(val)return val
endlocalfunction scan()local x ={}
print("Analyzing ..")for i=-4,4dofor d=-4,4do
table.insert(x,c.geolyzer.scan(i,d))endend
print("Done Analyzing.")return x
end
thread.create(function()
os.sleep(5)
tty.clear()
print("Initilizing ..")--os.execute("/bin/components.lua")--c.gpu.bind("125bb046-a2c9-4182-8459-785265229229")end):detach()localfunction receive(_,_,_,_,_,msg1,msg2,msg3,msg4)
computer.beep()
terrain ={}if msg1 =="scan"then
print("Starting Sequence ..")--[[for x in pairs(hardValues) do
for z in pairs(hardValues[x]) do
for y in pairs(hardValues[x][z]) do
c.gpu.set(1,6,x .. " " .. y .. " " .. z)
if hardValues[x][z][y] > 0 then
table.insert(terrain,{x,y,z})
--print(x .. " " .. y .. " " .. z)
break
end
end
end
end--]]--local x,z = -4,-4for x=-4,4do--x = x + 1for z=-4,4dolocal tile = c.geolyzer.scan(x,z)--z = z + 1--os.sleep(2)--print(tile)for y=#tile,1,-1do--print(y)if tile[y]~=0then
table.insert(terrain,{x,y-33,z})--print("Found: ",x,y,z)--breakendif y <31thenbreakendendendend
print("Calculation Complete.")
print(#terrain .." entries.")
print("Sending Data ..")
c.tunnel.send("scanData")
os.sleep(1)for i=1,#terrain,4do
c.tunnel.send(
compress(terrain[i]),
compress(terrain[i+1]),
compress(terrain[i+2]),
compress(terrain[i+3]))end
print("Data Sent.")
os.sleep(1)
c.tunnel.send("scanDataComplete")endend
event.listen("modem_message",receive)localfunction update()
os.sleep(0)endwhiletruedo update()end
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
Hi!
I am working on a long distance robot control program. I have a Geolyzer and Linked Card installed inside of it for getting an image of the robot's surroundings and then sends the data to my control station where the mod OpenGlasses draws blocks for an illustration. I got it working when I simply registered a event.listen() at the start of the program, but it is kind of an inconvenience if I want other kinds of signals from the robot. So my idea was to only have one registered when it's waiting for the scan data and when it's done it should unregister it. But this new approach doesn't seem to work for some reason. I made it print out its return value when I registered/unregistered it, and it returned 1 and false.
I have no idea how to proceed to the next step. Help is appreciated!
station.lua
remote.lua (Robot)
Link to post
Share on other sites