Multi encoder sketch with acceleration

Post your cool example code here.
madias
Posts: 812
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Multi encoder sketch with acceleration

Postby madias » Mon Apr 27, 2015 3:51 pm

easy to use sketch for setting up multiple encoders with acceleration (faster movements, more data increase). IRQ driven, HW timer and direct pin access (instead of digitalRead).
Attachments
multi_encoder_acc.zip
(1.29 KiB) Downloaded 439 times

mausi_mick
Posts: 129
Joined: Fri Aug 12, 2016 1:40 pm

Re: Multi encoder sketch with acceleration

Postby mausi_mick » Sat Nov 12, 2016 3:08 am

I think the code in the zip-file is not ok,
I think there should be one more { ... } at this lines: <<#######


...

if ( (gpio_read_bit(PIN_MAP[encoderpinB[counter]].gpio_device, PIN_MAP[encoderpinB[counter]].gpio_bit) ? HIGH : LOW) != B_set[counter] )
{
B_set[counter] = !B_set[counter];
if ( B_set[counter] && !A_set[counter] )
// <###################################### {
if (millis() - encodertimer > 3)
encoderpos[counter] -= 1;
else
encoderpos[counter] -= 5;
// <######################################## }
encodertimer = millis();
}


mausi_mick

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Multi encoder sketch with acceleration

Postby RogerClark » Sat Nov 12, 2016 6:18 am

The code compiles OK, so those brackets aren't essential, but I agree it makes it easier to read and maintain the code if it did have them.

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Multi encoder sketch with acceleration

Postby RogerClark » Sat Dec 03, 2016 7:56 am

Matthias

BTW.

I tried you code and it worked fine, but I found I needed to use the hardware timer for another purpose, so I found it doesnt work too bad if you simply turn on the internal pullup resistor and use a 100nF capacitor between the output of the rotary encoder and GND.

I know this isnt as good as the 2 resistors and one capacitor debounce method, but it worked OK for me.

The only drawback with this simple method (using attachinterrupt on input A), is that if you turn really quickly, it looses some steps and you don't get the 5x acceleration. But I could add acceleration by getting the millis in the interrupt handler and if it was less than some small value, since the last interrupt, then I could add or subtract 5 instead of 1.

But I do like the external analog debounce method.

User avatar
Pito
Posts: 849
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Multi encoder sketch with acceleration

Postby Pito » Mon Dec 05, 2016 9:40 am

FYI - in very past I worked with http://www.bourns.com/docs/Product-Data ... f?sfvrsn=0 encoders (DDS stuff) and there are types with detents and without detents, and also with 1/4 quadrature cycle per detent or a full cycle per detent. So I had to modify the sw based on the encoder I got.. Still a few in my junk box, I will try..
Pukao Hats Cleaning Services Ltd.

max_sl
Posts: 4
Joined: Thu Dec 15, 2016 2:35 pm
Location: Russia, Saint-Petersburg

Re: Multi encoder sketch with acceleration

Postby max_sl » Thu Dec 15, 2016 2:51 pm

Hello,
I used your "sketch" with my rotary encoder that named "ROTARY ENCODER TYPE: LPD3806-600BM-G5-24C" and it works fine on the first sight.
But after couple of turns it starts to miss impulses (app. 10 impulses at one turn) When I delete a part of code for the second encoder, it starts work better and it misses only 5 impulses at one turn.
When I used quadrature encoder at the Arduino Due, all was fine. Could you explain, how I can get a valid number of impulses on the STM32duino?
(I have STM32f103c8t6)
Sorry for bad English. I am from Russia)

User avatar
Pito
Posts: 849
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Multi encoder sketch with acceleration

Postby Pito » Thu Dec 15, 2016 7:02 pm

Your encoder gives
.. 600 pulses/revolution for a single phase..
- when using it as an manual input you can easily do 1-2rev per second. That is 600-1200 pulses/sec single phase (and it gives probably more in the quadrature mode). The routine uses millis() for the measurements - so to overload the sw could be easy then.. Try to use micros() instead. Also you may try to decrease the

Code: Select all

#define ENCODER_RATE 1000    // in microseconds;

a bit..
Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Multi encoder sketch with acceleration

Postby RogerClark » Thu Dec 15, 2016 7:23 pm

I rewrote to just use interrupt on one of the encoder output, and read the value of the other.

As long as you use hardware debounce, e.g an internal pullup resistor and a small capacitor from the encoder output to gnd, this worked well.

But if you rotate quickly, it still misses steps because of the debounce time.
I tuned the capacitor value for my particular encoder, as it only seemed to need 50nF with the internal 22k pullup.
But even then, I could make it loose steps, so one approach to hiding this, is to increase the amount that you change the position variable if you detect the encoder is being rotated quickly.

User avatar
Pito
Posts: 849
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Multi encoder sketch with acceleration

Postby Pito » Thu Dec 15, 2016 7:35 pm

With 50nF and 22k you get ~1-2msec tau, so you actually limit the "real pulse rate" to this tau..
The typical bounce of a microswitch takes <1ms, so the RC value seems to be ok.
I think a processing of every pulse should be possible with a 72MHz arm :) provided the encoder's interrupt priority would be the highest..
Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Multi encoder sketch with acceleration

Postby RogerClark » Thu Dec 15, 2016 10:05 pm

Pito

I tried a few capacitors starting much higher but I didn't have anything less than 50nF in my box of spares (well I had some 22pf etc but they were much too low)

I agree perhaps 10nF would be OK.


Return to “Code snipplets”

Who is online

Users browsing this forum: No registered users and 1 guest