ESP32 and PWM



  • I am actually using in C++ a raw ESP32 to drive a servomotor.
    It works well for me with its internal PWM.

    Is it possible to do the same thing with lowjs on ESP32 (not Neonious One) ?

    In the documentation gpio/signal it is documented as not possible with raw ESP32.

    If it is possible I will transfert the driving of one servomotor and suppress one MCU !!!



  • Two things are on my list right now:

    • Adding API for hibernation
    • Fixing the out of memory problem

    Regarding the second thing: Is it possible for you to give me an example script which is independent of your project which shows the memory problem? Makes it possible for me to fix it. Maybe you can remove code as much as possible so at the end the version is small and still does not work?

    Regarding the a better signal module to control the servo from the one ESP32: That is possible, but work on my side which is difficult for me to add to my schedule.

    Two options for the servo interface:

    • If somebody else builds the code (would be a C/C++ low native API module which uses the Remote Control peripheral of ESP32), that would be great! I could just add it to low.js

    • Or somebody would have to pay for 2 days of my time. But most probably too expensive for a hobby project..

    Thomas



  • @neoniousTR

    I wrote this small program on my Neonious One :

    //
    // Garbage collector test
    //
    
    "use strict";
    
    const os = require("os");
    
    const LOOP_COUNT = 80;
    
    var datas = Array(1000).fill({});
    datas[0].bogus = "Bogus !";
    
    // main loop
    run();
    setInterval(run, 60 * 1000);
    
    function run() {
      //console.log("begin os.freemem=" + os.freemem());
      var i;
      try {
          var arr = [];
          for(i = 0; i < LOOP_COUNT; i++) {
            //console.log(i + " os.freemem=" + os.freemem());
            arr = arr.concat(datas);
          }
      }
      catch(error) {
          console.error(error);
          console.log(i + " os.freemem=" + os.freemem());
      }
      finally {
          console.log(i + " os.freemem=" + os.freemem());
      }
    }
    

    The free memory seems to be sufficient ~2M but at loop count near 72,
    I have this trouble :

    [09:06:41] 72 os.freemem=2613272
    [09:07:40] Error: allocation of 650360 bytes failed
    [09:07:40]     at [anon] internal
    [09:07:40]     at concat () native strict preventsyield
    [09:07:40]     at run (/dist/TESTS/testgc.js:1) strict preventsyield
    [09:07:40] 72 os.freemem=2574984
    

    The IDE display a memory usage coherent with os.freemem.



  • Thank you! I will try asap, might be early next week because of other work however.



  • @neoniousTR

    Thomas, prefer this source, easier to analyze :

    //
    // Garbage collector test
    //
    
    "use strict";
    
    const os = require("os");
    const lowsys = require("lowsys");
    
    const LOOP_COUNT = 50;
    
    var datas = '0'.repeat(10000);
    
    // main loop
    run();
    setInterval(run, 30 * 1000);
    
    function run() {
      var i, arr = '';
      try {
          for(i = 0; i < LOOP_COUNT; i++) {
            arr = arr.concat(datas);
            console.log(i + " arr.length=" + arr.length + " os.freemem=" + os.freemem() + ' os.totalmem=' + os.totalmem());
          }
      }
      catch(error) {
          console.error(error);
    
      }
      finally {
            console.log(i + " arr.length=" + arr.length + " os.freemem=" + os.freemem() + ' os.totalmem=' + os.totalmem());
      }
    }
    
    [18:16:09] 42 arr.length=430000 os.freemem=2559720 os.totalmem=4194304
    [18:16:09] 43 arr.length=440000 os.freemem=2558136 os.totalmem=4194304
    [18:16:09] 44 arr.length=450000 os.freemem=2556552 os.totalmem=4194304
    [18:16:09] 45 arr.length=460000 os.freemem=2522200 os.totalmem=4194304
    [18:16:09] 46 arr.length=470000 os.freemem=2520616 os.totalmem=4194304
    [18:16:09] Error: allocation of 480029 bytes failed
    [18:16:09]     at [anon] internal
    [18:16:10]     at concat () native strict preventsyield
    [18:16:10]     at run (/dist/TESTS/testgc.js:1) strict preventsyield
    [18:16:10] 47 arr.length=470000 os.freemem=2593400 os.totalmem=4194304
    

    If 2 bytes per string character, a rapid calculus :
    At iteration 46, the string length is 470000=~1M.
    During iteration 47, we need to allocate a new string of 480000 elements ~1M without freeing the previous string, a total of 2M, but the free memory was 2.5M !!!

    But with the error, It seems the memory for 1 character is 1 byte, not 2 bytes. If it is true, more difficult to understand !

    Error: allocation of 480029 bytes failed
    


  • In my last source with Strings, the string content is always zeros.
    If the storage is UTF-8, it is is pure Ascii string, very simple.

    But if all the previous temporary strings are kept in a cache and not freed ....


Log in to reply