Author Topic: Lua and Sensors  (Read 6903 times)

0 Members and 1 Guest are viewing this topic.

Offline jonyjazy

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
Lua and Sensors
« on: February 05, 2015, 04:57:37 am »
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


Offline LDStudios

  • Coder Of Tomorrow
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 388
  • Rating: +41/-1
    • View Profile
    • LD Studios
Re: Lua and Sensors
« Reply #1 on: February 05, 2015, 06:27:48 am »
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?



Offline jonyjazy

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
Re: Lua and Sensors
« Reply #2 on: February 06, 2015, 07:24:21 am »
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

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua and Sensors
« Reply #3 on: February 06, 2015, 02:25:49 pm »
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
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline LDStudios

  • Coder Of Tomorrow
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 388
  • Rating: +41/-1
    • View Profile
    • LD Studios
Re: Lua and Sensors
« Reply #4 on: February 06, 2015, 05:16:45 pm »
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: [Select]
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
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!



Offline jonyjazy

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
Re: Lua and Sensors
« Reply #5 on: February 09, 2015, 04:52:13 am »
I liked the "Ow...." comment a lot! :)

First of all thanks for your help. I´m really new at this so all help is more than welcome.

Thank you

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua and Sensors
« Reply #6 on: February 09, 2015, 01:24:07 pm »
I liked the "Ow...." comment a lot! :)
We were all beginners once... ;)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation