-->
Page 1 of 4

tmr as pwm not working or working with huge memory leak

PostPosted: Tue May 26, 2015 7:20 pm
by LeprA
I try to use the tmr to do a pwm function but either i get a memory is leak or the tmr do not work.
I need a faster pwm than the one that is included that is why i am doing this.


This one is not working when i try to use the "tmr" as local variable, the tmr.delay just don't do anything in this case.
----------------------------------------------------------------------------------------------
Code: Select allfunction forwardandrewind()
      local tmr = tmr
      if(forward == 1) then
         forward = 0
         gpio.write(3,gpio.HIGH)
      else
         forward = 1
         gpio.write(3,gpio.LOW)
      end
         
      for x=1,16000 do
         gpio.write(4,gpio.HIGH)
         tmr.delay(1)
         gpio.write(4,gpio.LOW)
         tmr.delay(1)
         tmr.wdclr()
      end
      tmr = nil

      print(node.heap())
   end


This one is working when i try to use the "tmr" but leak memory.
----------------------------------------------------------------------------------------------

Code: Select allfunction forwardandrewind()
      if(forward == 1) then
         forward = 0
         gpio.write(3,gpio.HIGH)
      else
         forward = 1
         gpio.write(3,gpio.LOW)
      end
         
      for x=1,16000 do
         gpio.write(4,gpio.HIGH)
         tmr.delay(1)
         gpio.write(4,gpio.LOW)
         tmr.delay(1)
         tmr.wdclr()
      end
      tmr = nil

      print(node.heap())
   end



Any suggestions?

Re: tmr as pwm not working or working with huge memory leak

PostPosted: Wed May 27, 2015 4:42 am
by TerryE
The tmr.delay() argument is in µSec, so a delay of 1 µSec is going to look like its not working.

Setting _G.tmr = nil is a bad idea. This will cause tmr to be re-resolved on each reference with the sort of leak that you see.

Re: tmr as pwm not working or working with huge memory leak

PostPosted: Wed May 27, 2015 6:09 am
by cwilt
You will gain far more speed if you convert gpio.write, gpio.HIGH, and gpio.LOW to local variables. Using a tight loop and setting CPU speed to max I recorded a gpio toggle of 124khz.

Re: tmr as pwm not working or working with huge memory leak

PostPosted: Wed May 27, 2015 12:21 pm
by cal
Moin,

IMHO lua is mostly a glue or prototype language.
If you identify proper functions that need speed, encode them in c and call them from lua.

My € 0,02 cents,
Cal