Left for archival purposes.

User avatar
By milikiller
#7888 Hi this is working code for relative acurate servo pulse generator


this is new function in tmr.c
Code: Select all// Lua: servo_pulse( us , pin )
static int tmr_servo_pulse( lua_State* L )
{
  s32 us;
  unsigned pin;

  us = luaL_checkinteger( L, 1 );
  pin = luaL_checkinteger( L, 2 );

  MOD_CHECK_ID( gpio, pin );

  if ( us <= 0 )
    return luaL_error( L, "wrong arg range" );

  if ( us >= 1000000 )
      return luaL_error( L, "too long delay" );

  //noInterrupts();
  DIRECT_WRITE_HIGH(pin);   // drive output high
  delayMicroseconds(us);
  DIRECT_WRITE_LOW(pin);
  //interrupts();

  return 0;
}


you must write it to tmr_map[]

and this is code in lua to generate pulse

Code: Select allpin_servo = 3
servo_pulse_lenght = 1500

tmr.alarm(0, 20, 1, function()
     tmr.servo_pulse(servo_pulse_lenght,pin_servo)             
end)
User avatar
By Fr4gg0r
#7899 Your code calls a lua function every 20ms, which is then interpreted to call your custom c code; clearly a waste of cpu cycles..
Why don't you create the timer in c, and then expose start() and stop() functions to lua?

Apart from that, have you tried recompiling nodemcu with a higher resolution?
The duty is currently a 16bit variable, so theoretically it can hold 2^6*1024 . You just need to change one define in pwm.h .
User avatar
By milikiller
#7901 My skills in C is not good enough to write this. You can write this, if you can. I can try it, but I am new in ESP8266 and timers and OS calls is new for me.

One problem is acuracy of timers.. When heavy use TCP communication, the timer is not acurate, pwm works bad and everything with lower priority not working propertly.

https://github.com/nodemcu/nodemcu-firmware/issues/124
User avatar
By milikiller
#7903
Fr4gg0r wrote:Apart from that, have you tried recompiling nodemcu with a higher resolution?
The duty is currently a 16bit variable, so theoretically it can hold 2^6*1024 . You just need to change one define in pwm.h .


This is solution for pit, but not for me.

I need to use PWM at higher frequency than 50Hz and separated use servos at 50Hz