Nucleo F411RE How read encoder with 0.005mm step?

Posted on
  • Hi. I am new in Espruino and JS.
    I have linear encoder with 0.005mm step.
    When I move encoder slow. It is about 1Khz for channel.
    All is fine. Program make count.
    But I need more faster works. It is about 200Hhz for channel. It will 1000mm/sec.
    Program don`t see steps when move with 1000mm/sec.
    Can you give advice?

  • Could you share your current code?

  • //example #1
    var step = 0;
    function encFun(){
    var a = digitalRead(C6);
    var b = digitalRead(C8);
    //  print('a', a);
    //  print('b', b);
       if (a == HIGH && b == LOW){
         step ++;
         print(step);
    return;
    }
    else if (a == HIGH && b == HIGH){
           step --;
           print(step);
    return;
    }
    }
    setWatch(encFun, C6, true); 
    
  • If you take out the print it would probably keep up- you could add 1 sec timeout and print the value of step there rather than every time it changes.

    The uart output is your bottleneck here.

  • as well as maybe the allocation of variables a and b, maybe you could try to allocate them globally

  • I'm not sure that allocating variables globally is of help since locals are searched first when interpreting the code... if of course depends on how many other locals and globals are around. I understand where the though is coming from: allocation and deallocation work can may be skipped... on the other hand for instance garbage collection, any reference in a scope does something when 'entering and leaving' the scope... may be it is less of housekeeping.

  • Yes, the allocation won't be a big issue - in fact the current solution is probably best.

    Since you're only actually doing stuff if a is high, why not do:

    var step = 0;
    function encFun(){
       if (!digitalRead(C8)){
         step ++;
         print(step);
      } else {
         step --;
         print(step);
      }
    }
    setWatch(encFun, C6, {edge:"rising",repeat:true}); 
    

    or for superfast:

    function setup() {
      var s = 0;
      var r = C8.read.bind(C8);  
      setWatch(function () {s+=r()?1:-1;}, C6, {edge:"rising",repeat:true}); 
      return function(){ return s; };
    }
    
    var getStep = setup();
    

    As has been said, at anything over 100/sec the prints will probably cause problems and personally I probably wouldn't trust even the above much above 4000/sec. For 200kHz you're better off seeing if you can set up the STM32's built in hardware timers to do it for you - I'm pretty sure I've read that they can be set to work with encoders.

  • Thank you guys.
    And Thank you Gordon. You are Best!
    Hello from Ukraine!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Nucleo F411RE How read encoder with 0.005mm step?

Posted by Avatar for user78121 @user78121

Actions