Calculator Community > Lua

Lua and Sensors

(1/2) > >>

jonyjazy:
Hello

When I do a Lua program to display live readings from sensors via var.recall, I get a delay from the happening of an event to the display of that event. Why is that? How can I solve this?

For exemple, if I use a motion detector and move an object, only about 1s of moving it the Lua shows the information on screen.

Thanks

LDStudios:
I'd say part of the problem could be the delay of sensing, storing and recalling math variables. You also might be missing some platform.window:invalidate()s, which could definitely cause some lag or even complete lack of motion. Do you mind posting the lua script for the program you're using?

jonyjazy:
Hello again.

Here is the code

"
bridge1=image.new(_R.IMG.bs)
bridge2=image.new(_R.IMG.bola2)
timer.start(0.01)
function on.timer()
    local pc=var.recall("meter.posição")
    local vc=var.recall("meter.velocidade")
    local ac=var.recall("meter.aceleração")
    function on.paint(gc)
    gc:drawImage(bridge1,0,0)
    gc:setColorRGB(250,250,250)
    gc:drawString("Posição-> "..pc,22,22)
    gc:drawString("Velocidade-> "..vc,22,44)
    gc:drawString("Aceleração-> "..ac,22,66)
    gc:drawImage(bridge2,200,28+70*pc)
    platform.window:invalidate()
    gc:setColorRGB(183,119,103)
    gc:fillArc(195+18*pc,203,10/pc,8,0,360)
    end
end
"

What it does is to draw a ball (image on bridge2) up and down according to the position the sensor grabs. Only problem is that there is a delay on what we see (looking at the real object) and the output of the Lua.

Thanks

Adriweb:
Ow....

- Don't create a function within another function (that's only for very specific circumstances). Just separate the two functions you have.
- putting the invalidate() within on.paint(gc) is completely pointless, put it at the end of the on.timer() rather.

It should work better with that done, already. Well that's just from looking a few seconds at your code, I haven't tested.

Also, you could benefit from using var.monitor / on.varChange etc. : http://wiki.inspired-lua.org/var.monitor

LDStudios:
For future reference, you can use the handy little "code" tags  to insert code into a post, just click the "#" symbol when writing a post. It looks like this:


--- Code: ---bridge1=image.new(_R.IMG.bs)
bridge2=image.new(_R.IMG.bola2)
timer.start(0.01)

function on.timer()
    local pc = var.recall("meter.posição")
    local vc = var.recall("meter.velocidade")
    local ac = var.recall("meter.aceleração")
    platform.window:invalidate()
end

function on.paint(gc)
    gc:drawImage(bridge1, 0, 0)
    gc:setColorRGB(250, 250, 250)
    gc:drawString("Posição-> "..pc, 22, 22)
    gc:drawString("Velocidade-> "..vc, 22, 44)
    gc:drawString("Aceleração-> "..ac, 22, 66)
    gc:drawImage(bridge2, 200, 28+70*pc)
    gc:setColorRGB(183, 119, 103)
    gc:fillArc(195+18*pc, 203, 10/pc, 8, 0, 360)
end

--- End code ---
Also, just to clarify, "platform.window:invalidate()" essentially calls the on.paint routine, so putting it inside of on.paint won't do you much good ;)

Good luck!

Navigation

[0] Message Index

[#] Next page

Go to full version