Avatar for Jofo

Jofo

Member since Aug 2019 • Last active Sep 2019
  • 1 conversations
  • 4 comments

Researcher/engineer consultant from Sweden, working mainly on 3D haptics

Most recent activity

    • 7 comments
    • 444 views
  • in Puck.js, Pixl.js and MDBT42
    Avatar for Jofo

    To follow up, here is the inlineC version, which actually is fast enough for my purpose! I am still interested in hardware support, maybe more power-efficient or leaving cpu for other tasks, but this could be good enough anyway. Super-cool that we can combine js and c this way, so thanks for that.

    var c = E.compiledC(`
    // void encA(bool)
    // void encB(bool)
    // int getEnc()
    const int stable[] = {0,-1,1,0,1,0,0,-1,-1,0, 0,1, 0,1, -1, 0};
    volatile bool encoder_raw[] = {false, false};
    volatile int counter = 0;
    volatile int prev_state = -1;
    void encoder_callback(int AB, bool value){
        encoder_raw[AB]=value;
        int cur_state = encoder_raw[0] << 1 | encoder_raw[1];
        if(prev_state < 0) prev_state = cur_state;
        counter += stable[prev_state << 2 | cur_state];
        prev_state=cur_state;
    };
    void encA(bool state){ encoder_callback(0,state); }
    void encB(bool state){ encoder_callback(1,state); }
    int getEnc() {
     int r = counter;
     return r;
    }
    `);
    setWatch(c.encA, D25, {repeat:true, edge:"both", irq:true});
    setWatch(c.encB, D26, {repeat:true, edge:"both", irq:true});
    
    // Every .1 seconds, report back the value
    setInterval(function() {
      print(c.getEnc());
    }, 100);
    
  • in Puck.js, Pixl.js and MDBT42
    Avatar for Jofo

    Thanks Robin,

    The specification in question is http://infocenter.nordicsemi.com/pdf/nRF­52832_PS_v1.1.pdf section 36 starting on page 346, also linked from the low-level library page http://www.espruino.com/NRF52LL
    It is actually from there I found out that the chip indeed had hardware support for quadrature decoding. The encoder I use is RLS RM08 https://www.rls.si/en/fileuploader/downl­oad/download/?d=1&file=custom%2Fupload%2­FRM08D01_12.pdf, but I think it would work just as well with more common optical encoders like HEDL5540 etc.

    connecting it to a counter

    Has a specific model and make been selected? What is the doc link for that also?

    I meant counter in the nRF52, like how it is done for counting button clicks in the example of low level library. Maybe not necessary, I am a bit unused to the low-level specification documentation (linked above).

    What access speed is expected? How many pulses must read and over what duration?

    We have 1024 counts per revolution and in our case we use it to detect angle of a manual axis rotation so it is perhaps max 10.000 counts per second. My software solution was just intended as a proof of concept but it works if I rotate slowly (~4s per turn), but skips counts if i turn faster.

    Here is the js code. Thanks for the inlineC suggestion, I will try and see if I can use it too.

    // D25 Green A channel
    // D26 Grey  B channel
    const stable = [0,-1,1,0,1,0,0,-1,-1,0, 0,1, 0,1, -1, 0];
    var encoder_raw = [false, false];
    var counter = 0;
    var prev_state = -1;
    
    var  on = false;
    setInterval(function() {
      on = !on;
      LED1.write(on);
      console.log("Counter " + counter);
    }, 100);
    
    const encoder_callback = function(AB, value)
    {
        encoder_raw[AB]=value;
        var cur_state = encoder_raw[0] << 1 | encoder_raw[1];
        if(prev_state < 0) prev_state = cur_state;
        counter += stable[prev_state << 2 | cur_state];
        prev_state=cur_state;
    };
    
    setWatch(function(e) {encoder_callback(0,true); }, D25, { repeat: true, edge: 'rising' });
    setWatch(function(e) {encoder_callback(0,false);}, D25, { repeat: true, edge: 'falling' });
    setWatch(function(e) {encoder_callback(1,true); }, D26, { repeat: true, edge: 'rising' });
    setWatch(function(e) {encoder_callback(1,false);}, D26, { repeat: true, edge: 'falling' });
    
  • in Puck.js, Pixl.js and MDBT42
    Avatar for Jofo

    Hello,
    First I want to say that the espruino and the low-level api to nRF52 is super-cool. I could try some of the examples you have provided, and it is so quick to work with.

    I would like to request or figure out how to use the QDEC - Quadrature decoder feature that is described in the nRF52832 Product Specification v1.1. I have a quadrature encoder with A and B pins connected to the MDBT42Q breakout board already (working in software, but too slow) and if I could use the hardware feature connecting it to a counter it would be awesome. Any suggestion how it could be done?

    Best regards
    Jonas

Actions