Go Down

Topic: Is there a simple quadrature decoder module? (Read 5111 times) previous topic - next topic

jremington

Quote
Unfortunately, the same program did not work on my first 100 attempts.
You are certainly patient!

Since you are ill-advisedly using someone else's encoder library, start with the minimal example that comes with the library and learn how it works.

A better approach would be to write your own code, using ISR for the external interrupts. With this approach, quadrature encoders can be read and fully decoded using roughly a dozen lines of code.

Here is one approach.

Tutorial on interrupts here.

DocStein99

Ok thanks for the information.  I should probably decode my own encoder.  After reading over the documents that the author of that library took their time to write, I felt confident that their skills and experience to design that function surpassed my own skills with interrupt handling and the processing of fast code counters.

Making my own closed-loop Arduino motor driver is a fairly involved project that needs me to master the encoder and P.I.D. (or whatever) motor pulse output loop.  Which is why I was searching for an open source, budget / beginner device / module to get me started and study, so that I can learn from that example and / or modify it so I can advance the actual project I'm trying to work on. 

For the time being, I found this "KANGAROO" module, that connects right up to my Sabertooth driver, handles the quad-decoding, positing, and motor speed regulation that I was looking for, for the steering and traction control drive output to begin with.

jremington

I took another look at your code, and in addition to all the unnecessary code, you are definitely misusing the encoder library.

For example, you should NOT be using the "double" type for encPosition.

Code: [Select]
//Define Variables we'll be connecting to
//int32_t encPosition;
double encPosition, encPositionOld;


There is a short example program on the page describing that library. Try it. There are also speed tests which should be very informative.

Incidentally, that library appears to be well documented and coded. You would do very well to study the material.

DocStein99

#18
Sep 21, 2017, 04:40 am Last Edit: Sep 21, 2017, 04:48 am by DocStein99
I took another look at your code, and in addition to all the unnecessary code, you are definitely misusing the encoder library.

For example, you should NOT be using the "double" type for encPosition.

Code: [Select]
//Define Variables we'll be connecting to
//int32_t encPosition;
double encPosition, encPositionOld;


Code: [Select]

ID myPID( &encPosition, &pidOutput, &target, Kp, Ki, Kd, DIRECT);


That is a stupid mistake, I have overlooked.  I could not figure out how to pass that to the PID object, requires a DOUBLE.  The program actually ran, and I was getting readings, I forgot about the issue, which probably presents problems.  Thanks for bringing that up, I overlooked this.

Code: [Select]

ID myPID( &encPosition, &pidOutput, &target, Kp, Ki, Kd, DIRECT);

DocStein99

I went back and re-modified my code.  I ran tests again.  I find the encoder counting to be working exceptionally good.

Ok, so now I just need to work on the PID or motor pulse output part of the program.

Coding Badly

I find the encoder counting to be working exceptionally good.
A reasonable thing to do at this point is post your working code so others have an opportunity to learn from what you have done.


DocStein99

I could, after I can be sure it's working, but is not.  I re-calculated since the hardware encoder is attached to the gearbox to deliver: 512ppm * 4=2048 quadrature signals.  Then * 27 = 55296 signals per revolution.  I put a sharpie-mark on a gear tooth aimed at the "HOME" position - now take notice to the mark back and fourth to find it's losing encoder counts again, when I increase the speed limit of the motor.

I'm using this PID library - that I do not understand yet what that is doing. 

polymorph

Simple hardware that decodes quadrature into up pulses on one pin, down pulses on the other pin. Just a 74HC74 dual D flip-flop.

https://sites.google.com/site/radiohamtechnology/49er-digital-vfo

Should be simpler to just count pulses than decode quadrature.

Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

MarkT

Should be simpler to just count pulses than decode quadrature.

Well maybe. but its not hard to do the quadrature with a little bit shuffling or table lookup,
and counting two sets of pulses needed two counters with hardware clock inputs.
 
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

DocStein99

I think I need to calculate the TIME the processor will take to count 1 detent move on this encoder.  There are 55296 counts per revolution.  This sketch DOES work - when I analog.write pulse output up to 64.  I use a laser aimed at one tooth to test that the wheel will spin forward one revolution and reverse one revolution.  Once I set the limit on the PID curve, above 108 - the forward and return becomes LOOSE, the tooth on the wheel passes the laser target.  The encoder reads 55296 counts and the wheel is out of position. 

I did not measure the speed of 1 revolution.  To ESTIMATE, it is less than 1 second.  Possibly 500 ms.

So, if I use the 74HC74 - all I need to do is 1 line of code on the two hardware interrupt pins to ++ or -- the encoder count.  This would lighten up the load of calculating quadrature position cycles for each detent cycle.  Ironically - I actually DO have 74HC74 in my box from 10 years ago, so I don't need to wait 3 weeks for the postman to fly from China for me.

I'm going to setup this hardware RPM / counter device I have to help me verify the number of pulses, and compare against the Arduino's count of captured signals. 

polymorph

Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

DocStein99

Ok, well tonight I hooked up an external counter - to compare it to the Arduino.  Unfortunately this dumb "DELTA CTA4" can ONLY count 5k cps in quadrature counting mode.  So that makes this device REALLY SLOW compared to the Arduino's counting, so I can't use this to compare the counts to.  I can only get an accurate reading AFTER I set the maximum speed to "5" which is something like 2 seconds per rpm super granny slow.




DocStein99

#27
Sep 22, 2017, 08:27 am Last Edit: Sep 22, 2017, 11:42 am by DocStein99
I used the 7474 ic, and it worked right away.  I set the maximum pulse-out @ 255, and I've got the full speed of this 12v motor accurately positioning, using that device.  Thank you - that worked great!  Took out all that overhead processing, just 2 lines of code in the interrupts and it works like a charm.

tropicdome

I used the 7474 ic, and it worked right away.  I set the maximum pulse-out @ 255, and I've got the full speed of this 12v motor accurately positioning, using that device.  Thank you - that worked great!  Took out all that overhead processing, just 2 lines of code in the interrupts and it works like a charm.
Hi @DocStein99
Could you share in more details what you did to get it to work, maybe share a schematic, code example and a picture?

/cheers

Smajdalf

Simple hardware that decodes quadrature into up pulses on one pin, down pulses on the other pin. Just a 74HC74 dual D flip-flop.

https://sites.google.com/site/radiohamtechnology/49er-digital-vfo
...
However this decoding may lose/gain pulses during direction change. Or when the motor is stopped and shaking slightly. It may be problem for some applications.
How to insert images: https://forum.arduino.cc/index.php?topic=519037.0

Go Up