Jump to content
  • 0

Quadrature encoder signals reading online with scripts and events?


Demmo

Question

Hello,

I have found this topic with script for counting edges of digital signals

and I wonder if it's possible to modify it somehow (or use completely different approach) to read quadrature encoder signals with Analog Discovery? Most important thing, I think, is that I have to compare signals state at current event (i.e. I have "1" on signal A and need to know what is the state of B signal in that particular moment) and be able to compare it with previous state (event) to decide if I should increment or decrement my counter. And it should be triggered with both edges but I assume that trigger options on DIO line decides when events are generated.

Is it possible to capture and count edges without sample count limit? I mean something like hardware triggering on digital inputs instead of starting on first trigger and sampling defined number of samples with defined frequency (Stream mode). At the moment it looks more like signals recording and post-processing (offline) and I wonder if it is possible online to execute encoder script live on each trigger? It's not a must but would be nice ;)

And one more thing - I have downgraded my WaveForms to version from mentioned topic because in newer version there is no Stream mode. Was it renamed or replaced by something better?

 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

Hi @Demmo

You could add a custom decoder to measure the distance between the encoder edges and use the Script tool to provide feedback for your system. Here the data is just printed to output window.

image.png.a7482154e602d8f0feb0a7aec4f5856f.png

Quad Decoder:

// rgData: input, raw digital sample array
// rgValue: output, decoded data array
// rgFlag: output, decoded flag array

var c = rgData.length // c = number of raw samples
var p0 = 0; // previous dio0 value
var p01 = 0; // previous xor value
var v = 0; // distance between encoder edges
var j = 0; // previous measurement position

for(var i = 0; i < c; i++){ // for each sample
    var s = rgData[i]; // digital sample
    var f0 = 1&(s>>0); // dio 0 value
    var f1 = 1&(s>>1); // dio 1 value
    var f01 = f0 ^ f1; // xor
    if(f01==1){ // measure edge distance
        v++;
    }else if(p01==1){
        for(; j < i; j++){ // store value/flag since last measurement point
            rgFlag[j] = f0==p0?1:2; // dio 0 leading ? 1 : 2
            rgValue[j] = v;
        }
        v=0;
    }
    p01 = f01;
    p0 = f0;
}

Value to text:

// value: value sample
// flag: flag sample

function Value2Text(flag, value){
  switch(flag){
    case 1: return value;
    case 2: return -value;
    default: return "X";
  }
}
Value2Text(1,1)

 

Link to comment
Share on other sites

Hello @attila,

thank you for your answer.

I am glad that my case is possible to solve by script.

Please correct me if I am wrong - I will try to understand what is going on in your example.

There is a script on the screenshot.Is it executed at every trigger? Does it automatically start and stop acquisition by Logic1.run() and Logic1.stop()? Or are that only entry end exit points for script?
At first I thought that while() loop will just execute its code to count samples between triggers, like "there is no trigger -> add 1 to distance", "logic triggered -> print distance / generate event" but it doesn't work like that. I suppose that such kind of logic is under the hood and we are processing its result stored in Logic.Channels.Quad.data[0]...

OK, I have just noticed, this is what is actually done in Quad decoder ;) And this part is clear, I think.

Mentioned new decoder should be added by "Add custom" on signal list, right? And where should be Value2Text() added? In script? And what is it mentioned for? It will generate events with -1 or +1 values that could be processed later or what? :)

Link to comment
Share on other sites

Hi @Demmo

Yes, Logic Analyzer / Add Custom.
The Decoder code should go to Decoder tab and "Value to text" to "Value to text" tab.

image.png.ae9059b8264a56b4f0a9a1e6ac55cec4.pngimage.png.afcd4855030a339aff10c4dfd2593db5.png

The decoding is performed by this custom interpreter.

The code in the Script tool is intended to start the Logic Analyzer and enter the while loop for each acquisition.
It depends on your system how you control it based on the decoded data...
image.png.e7f96bd646d2b3b1cbec8ebaaf03b122.png

Logic1.run() // start the Logic Analyzer
while(Logic1.wait()){ // for each acquisition while this Script tool is not stopped
    // provide feedback for your system based on data
    Logic1.Channels.Quad.data // is the decoded data array
}
Logic1.stop() // stop it

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...