Post general discussions on using our drivers to write your own software here
Anders_Eriksson
Newbie
Posts: 0
Joined: Fri Nov 15, 2019 10:09 am

Hello,

I have asked for this encoding functionality in Picoscope6 and also searched on internet, but without any luck.

I am new to python, numpy and Jupyter notebook, but I am really impressed of how well it works!

Below is the code, see also the attached 2 figures of an example on how I use it in Jupyter notebook!

Kind regards,
Anders

Matlab/Octave; Encoder2pos.m
function position = Encoder2pos (chA, chB)
chA_plus_minus = chA * 2 - 1;
chB_diff = [0; diff(chB)];
chA_prod = chA_plus_minus .* chB_diff;

chB_plus_minus = chB * 2 - 1;
chA_diff = [0; diff(chA)];
chB_prod = -chB_plus_minus .* chA_diff;

position = cumsum(chA_prod + chB_prod);
endfunction

In python using numpy;
def Encoder2pos(chA, chB):
chA_plus_minus = (chA * 2) - 1
chB_diff = np.concatenate(([0], np.diff(chB)))
chA_prod = chA_plus_minus * chB_diff

chB_plus_minus = (chB * 2) - 1
chA_diff = np.concatenate(([0], np.diff(chA)))
chB_prod = -chB_plus_minus * chA_diff

position = np.cumsum(chA_prod + chB_prod)
return position
Attachments

Anders_Eriksson
Newbie
Posts: 0
Joined: Fri Nov 15, 2019 10:09 am

### Re: Converting quadrature encoder signal

Hi again,

Here is a small example!

import numpy as np

%matplotlib notebook
import matplotlib.pyplot as plt

chA = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0])
chB = np.array([0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0])

def Encoder2pos(chA, chB):
chA = (chA > 0.5) * 1
chB = (chB > 0.5) * 1
chA_plus_minus = (chA * 2) - 1
chB_diff = np.concatenate(([0], np.diff(chB)))
chA_prod = chA_plus_minus * chB_diff

chB_plus_minus = (chB * 2) - 1
chA_diff = np.concatenate(([0], np.diff(chA)))
chB_prod = -chB_plus_minus * chA_diff

position = np.cumsum(chA_prod + chB_prod)
return position

pos = Encoder2pos(chA, chB)
pos
array([ 0, 1, 2, 3, 4, 3, 2, 1, 0, -1, -2, -3, -4], dtype=int32)
Attachments