Pulse counter support

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Mrwhy@arcor.de
Posts: 1
Joined: Sun Feb 24, 2019 5:19 pm

Pulse counter support

Post by Mrwhy@arcor.de » Sun Feb 24, 2019 5:34 pm

Hi Forum :)

I'm new to microcontroller programming and micropython... I tinkered some days with an Esp32 without psram and achieved what I was up to, thanks to all this great docs and tutorial!

But now I think I have to go deeper in the esp idf and micropython interfaces. I want to use the integrated pulse counter(pcnt) of the esp32 microcontroller and as far as I have seen there is no implementation yet, or am I wrong?

I think this pulse counters are a nice way of counting the pulses on an input. As I understand this, they are relatively efficient, and one might be able to query the pulses and reset the counter. Maybe it is interesting in one or another scenario.

What do you think?

Best

Bjoern

Iceman
Posts: 3
Joined: Thu Jul 25, 2019 6:47 pm

Re: Pulse counter support

Post by Iceman » Thu Jul 25, 2019 6:49 pm

I have the same question regarding implementing pulse counting. Anyone have suggestions?

Thanks!

pidou46
Posts: 101
Joined: Sat May 28, 2016 7:01 pm

Re: Pulse counter support

Post by pidou46 » Fri Jul 26, 2019 2:12 pm

Hi,

Yes I think it would be usefull for keeping track of an for encoder for instance.

I think it would be a nice feature to add at the V2.0 roadmap https://github.com/micropython/micropython/issues/4821, still I have no idea of what coding effort it would involve...
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pulse counter support

Post by jimmo » Mon Jul 29, 2019 1:23 am

Yup this would be a good feature! I see you added it to the 2.0 feature suggestion list.

But out of curiosity, how fast are the pulses that you want to count?

Iceman
Posts: 3
Joined: Thu Jul 25, 2019 6:47 pm

Re: Pulse counter support

Post by Iceman » Mon Jul 29, 2019 11:15 pm

Thanks all. Pidou46 I saw you're post on the Github v2.0 roadmap thread and gave a thumbs-up if that helps!

For the application of reading a motor encoder, something up to like 10-20 khz pulse counting ability would work for a motor spinning at a few thousand RPM...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pulse counter support

Post by pythoncoder » Tue Jul 30, 2019 11:23 am

I would be wary of using this to read an encoder. Vibration can cause jitter around an edge. To read an incremental encoder properly you need to take account of this, otherwise you'll see a gradual drift away from the correct value. There is some code in this repo. The algorithm is based on hardware I developed for an NC machine, so it's been proven in an industrial environment.
Peter Hinch
Index to my micropython libraries.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Pulse counter support

Post by OutoftheBOTS_ » Tue Jul 30, 2019 8:52 pm

I have played with encoders a bit and found software encoder counting has limits. Many of the DC motors that I have used with encoders and gear boxes have multi pole quadrature encoders, this means signals from the encoder easily come in the KHz range (even faster with higher performance brush-less motors). If you start to connect multiple motors with encoders to the 1 MCU then it tends to use up a crazy amount of CPU just for counting the encoders.

I found best solution is to use a hardware encoder counter. I have found a couple of options for this either an external counter like a lsr7366r or what I use these days is a STM32 MCU as they have hardware encoder counters as part of their timers.

Iceman
Posts: 3
Joined: Thu Jul 25, 2019 6:47 pm

Re: Pulse counter support

Post by Iceman » Tue Jul 30, 2019 11:36 pm

Hi OutoftheBOTS - In this instance though we're actually trying to use the hardware encoder of the ESP32, which is very capable of high rates and has nice integrated interrupts. This peripheral is very capable and useful beyond just a motor or motor(s).
OutoftheBOTS_ wrote:
Tue Jul 30, 2019 8:52 pm
I have played with encoders a bit and found software encoder counting has limits. Many of the DC motors that I have used with encoders and gear boxes have multi pole quadrature encoders, this means signals from the encoder easily come in the KHz range (even faster with higher performance brush-less motors). If you start to connect multiple motors with encoders to the 1 MCU then it tends to use up a crazy amount of CPU just for counting the encoders.

I found best solution is to use a hardware encoder counter. I have found a couple of options for this either an external counter like a lsr7366r or what I use these days is a STM32 MCU as they have hardware encoder counters as part of their timers.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Pulse counter support

Post by OutoftheBOTS_ » Wed Jul 31, 2019 1:28 am

Iceman wrote:
Tue Jul 30, 2019 11:36 pm
Hi OutoftheBOTS - In this instance though we're actually trying to use the hardware encoder of the ESP32, which is very capable of high rates and has nice integrated interrupts. This peripheral is very capable and useful beyond just a motor or motor(s).
OutoftheBOTS_ wrote:
Tue Jul 30, 2019 8:52 pm
I have played with encoders a bit and found software encoder counting has limits. Many of the DC motors that I have used with encoders and gear boxes have multi pole quadrature encoders, this means signals from the encoder easily come in the KHz range (even faster with higher performance brush-less motors). If you start to connect multiple motors with encoders to the 1 MCU then it tends to use up a crazy amount of CPU just for counting the encoders.

I found best solution is to use a hardware encoder counter. I have found a couple of options for this either an external counter like a lsr7366r or what I use these days is a STM32 MCU as they have hardware encoder counters as part of their timers.
Yes correct your wanting to add support for using a hardware peripheral, it would be good to have this support on the ESP MCUs :)

I haven't looked at what this pulse counter is capable of. To count a quadrature encoder you have to be able to detect both rising and falling on 2 pins to both count up and down depending upon the direction the encoder is turning. Also it is usual that on many projects you want to have multiple motor all with encoders so you need multiple hardware peripherals to count them. The STM32F407VGT6 that I have been plying with has 14 separate timers which each timer can be used as a quadrature encoder counter.

pidou46
Posts: 101
Joined: Sat May 28, 2016 7:01 pm

Re: Pulse counter support

Post by pidou46 » Thu Aug 01, 2019 5:53 am

It seems able to handel encoders: https://docs.espressif.com/projects/esp ... /pcnt.html
- it count rising and/or falling edge
- a filter can be set to avoid glinches
- up to 8 counter are availables, not as much as STM32, but it enable quite sophisticated projects.

But now the hardest part is how to bring it to esp32 micropython port ?
My C skills dates from ages, and I have no idea where to start with ? I guess setting up a toolchain (GCC, ESP-IDF, ect...)
Is there somewhere a tutorial or even a sample of ported fonctionality ?
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016

Post Reply