Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 15 of 15

Thread: Teensy 4.x H/W Quadrature Encoder Library

  1. #1
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,335

    Teensy 4.x H/W Quadrature Encoder Library

    I have a library based on NXP Quad Encoder SDK driver for the Encoder module but modified to support the Teensy 4.x infrastructure.

    NOTE: you will need to incorporate the Update imxrt.h for encoder structure #402 to make use of the library.

    There are 4 hardware quadrature encoders available the Teensy 4.0. These are currently supported on FlexIO pins: 0, 1, 2, 3, 4, 5, 7, 30, 31 and 33. The library is available in my GitHub repository: https://github.com/mjs513/Teensy-4.x...ncoder-Library.


    Basic usuage for the T4 is similar to the current Teensy Encoder library.
    Code:
    QuadEncoder myEnc(enc, pin1, pin2); 
       enc - encoder object (specify 1, 2, 3 or 4).
       pin1 - phase A
       pin2 - phase b
    myEnc.read();
       Returns the accumulated position. This number can be positive or negative. 
    myEnc.write(newPosition);
       Set the accumulated position to a new number.
    A similar to example to that on https://www.pjrc.com/teensy/td_libs_Encoder.html is shown below. See SimpleEncoder.ino in the examples folder.
    Code:
    //
    * http://www.pjrc.com/teensy/td_libs_Encoder.html
    *
    * This example code is in the public domain.
    */
    
    #include "Quadencoder.h"
    
    // Change these pin numbers to the pins connected to your encoder.
    // Allowable encoder pins:
    // 0, 1, 2, 3, 4, 5, 7, 30, 31 and 33
    // Encoder on channel 1 of 4 available
    // Phase A (pin0), PhaseB(pin1), 
    QuadEncoder knobLeft(1, 0, 1);
    // Encoder on channel 2 of 4 available
    //Phase A (pin2), PhaseB(pin3), Pullups Req(0)
    QuadEncoder knobRight(2, 2, 3);
    //   avoid using pins with LEDs attached
    
    void setup() {
     Serial.begin(9600);
     Serial.println("TwoKnobs Encoder Test:");
     /* Initialize Encoder/knobLeft. */
     knobLeft.setInitConfig();
     //Optional filter setup
     //knobLeft.EncConfig.filterCount = 5;
     //knobLeft.EncConfig.filterSamplePeriod = 255;
     knobLeft.init();
     /* Initialize Encoder/knobRight. */
     knobRight.setInitConfig();
     //knobRight.EncConfig.filterCount = 5;
     //knobRight.EncConfig.filterSamplePeriod = 255;
     knobRight.init();
    }
    
    long positionLeft  = -999;
    long positionRight = -999;
    
    void loop() {
     long newLeft, newRight;
     newLeft = knobLeft.read();
     newRight = knobRight.read();
     if (newLeft != positionLeft || newRight != positionRight) {
       Serial.print("Left = ");
       Serial.print(newLeft);
       Serial.print(", Right = ");
       Serial.print(newRight);
       Serial.println();
       positionLeft = newLeft;
       positionRight = newRight;
     }
     // if a character is sent from the serial monitor,
     // reset both back to zero.
     if (Serial.available()) {
       Serial.read();
       Serial.println("Reset both knobs to zero");
       knobLeft.write(0);
       knobRight.write(0);
     }
    }
    Using two KY-040 Rotary Encoder Module was tested:
    Click image for larger version. 

Name:	IMG-0034.jpg 
Views:	5 
Size:	62.4 KB 
ID:	18251
    with the following output on the serial monitor:
    Click image for larger version. 

Name:	Capture.JPG 
Views:	4 
Size:	56.1 KB 
ID:	18252

    In addition a Rotary Encoder with Different Resolutions (200PPR, Line driver with 5V was used to test the index pin functionality:
    Click image for larger version. 

Name:	IMG-0028.jpg 
Views:	3 
Size:	134.6 KB 
ID:	18253

    A few examples are included in the library showing its capabilities.

    Enjoy. Let me know if you run into any problems.
    Last edited by mjs513; 11-23-2019 at 03:13 PM.

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    578
    That's very useful indeed. I will certainly test it with high resolution encoders as soon as I find some time.
    If you want to do some performance testing I have a Teensy quadrature tester on gitHub https://github.com/luni64/EncSim

    Click image for larger version. 

Name:	interface.png 
Views:	10 
Size:	61.1 KB 
ID:	18254

    It generates encoder signals up to some 1.4MHz and can simulate random bouncing and non 90 signals. Details in the GitHub readme

  3. #3
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,335
    @luni

    Thanks for the link to the encoder sim. Just took a quick look and impressive. Just downloaded it and will give it a try later today.

  4. #4
    Senior Member
    Join Date
    May 2016
    Posts
    230
    Excellent! However, I have been using the PJRC Encoder library successfully (no missing steps) with the T4 in an application with two encoders for several weeks now. Is there any reason to switch?

  5. #5
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,335
    @DerekR

    If the PRJC Encoder Library is working I really don't see any reason to switch. It does have the advantage of being able to use any pair of digital pins while the Hardware Encoder library is limited to the available pins. There are a bunch more bells and whistles if you need them though. Plus it only has 4 hw encoders that you can use. For most of my applications - robotics will probably continue using the PRJC Encoder Library. Guess it goes back to the old saying "the right tool for the right job"

  6. #6
    Junior Member
    Join Date
    Dec 2019
    Posts
    3

    Error compiling Encoder.h

    I have been working with PJRC Encoder library with Arduino UNO earlier but now I'm moving to Teensy4.0. the encoder is hall effect encoder but I don't think that's the problem( yet)

    Code:
    #include <Encoder.h>
    
    Encoder pitch(1,2);
    long pitch_count ;
    
    void setup() {
      // put your setup code here, to run once:
    pinMode(1, INPUT);    //Pitch Encoder signal1
    pinMode(2, INPUT);    //Pitch Encoder signal2
    pinMode(3, OUTPUT);    //AIN1
    pinMode(4, OUTPUT);    //AIN2
    pinMode(5, OUTPUT);    //PWMA
    pinMode(6, OUTPUT);    //MotorEnable
    
    Serial.begin(9600);
    pitch.write(0);// reset encoder counter to 0
    
    }
    
    void loop() {
          digitalWrite(3, LOW);   //AIN1
          digitalWrite(4, HIGH);  //AIN2
          analogWrite(5, 80);     //PWMA
          digitalWrite(6, HIGH);  //MotorEnable
          pitch_count = pitch.read(); //get pitch count reading
          Serial.println(pitch_count);
    }
    with this simple code I've got error message

    Code:
    Arduino: 1.8.10 (Windows 10), TD: 1.48, Board: "Teensy 4.0, Serial, 600 MHz, Faster, US English"
    
    In file included from C:\Users\USER\Documents\Arduino\Pipe\Teensy\MotorEncoder2\MotorEncoder2.ino:1:0:
    
    C:\Users\USER\Documents\Arduino\Pipe\libraries\Encoder/Encoder.h: In constructor 'Encoder::Encoder(uint8_t, uint8_t)':
    
    C:\Users\USER\Documents\Arduino\Pipe\libraries\Encoder/Encoder.h:82:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
    
       encoder.pin1_register = PIN_TO_BASEREG(pin1);
    
                             ^
    
    C:\Users\USER\Documents\Arduino\Pipe\libraries\Encoder/Encoder.h:84:25: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
    
       encoder.pin2_register = PIN_TO_BASEREG(pin2);
    
                             ^
    
    Multiple libraries were found for "Encoder.h"
     Used: C:\Users\USER\Documents\Arduino\Pipe\libraries\Encoder
     Not used: C:\Program
    Error compiling for board Teensy 4.0.
    
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    any pointer of how to solve this?

    P.S. if I change board setting to Teensy 3.6 or Arduino Uno it will compile fine.
    It's running on window10, Arduino 1.8.10, Teensyduino 1.48
    Last edited by iamtawon; 12-01-2019 at 05:14 AM. Reason: add info

  7. #7
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    I've also try Teensy4.0 Quadencoder library by running the example twoknob code I've got Error compling code for board teensy4.0


    Arduino: 1.8.10 (Windows 10), TD: 1.48, Board: "Teensy 4.0, Serial, 600 MHz, Faster, US English"

    C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\USER\Documents\Arduino\Pipe\libraries -fqbn=teensy:avr:teensy40:usb=serial,speed=600,opt= o2std,keys=en-us -ide-version=10810 -build-path C:\Users\USER\AppData\Local\Temp\arduino_build_833 932 -warnings=default -build-cache C:\Users\USER\AppData\Local\Temp\arduino_cache_832 200 -verbose C:\Users\USER\Documents\Arduino\Pipe\Teensy\MotorE ncoder2\MotorEncoder2.ino
    C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\USER\Documents\Arduino\Pipe\libraries -fqbn=teensy:avr:teensy40:usb=serial,speed=600,opt= o2std,keys=en-us -ide-version=10810 -build-path C:\Users\USER\AppData\Local\Temp\arduino_build_833 932 -warnings=default -build-cache C:\Users\USER\AppData\Local\Temp\arduino_cache_832 200 -verbose C:\Users\USER\Documents\Arduino\Pipe\Teensy\MotorE ncoder2\MotorEncoder2.ino
    Using board 'teensy40' from platform in folder: C:\Program
    Using core 'teensy4' from platform in folder: C:\Program
    Detecting libraries used...
    "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=148 -DARDUINO=10810 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teen sy4" "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\sketch\\MotorEncoder2.ino.cpp" -o nul
    Alternatives for Quadencoder.h: [Teensy-4.x-Quad-Encoder-Library-master]
    ResolveLibrary(Quadencoder.h)
    -> candidates: [Teensy-4.x-Quad-Encoder-Library-master]
    "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=148 -DARDUINO=10810 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teen sy4" "-IC:\\Users\\USER\\Documents\\Arduino\\Pipe\\librar ies\\Teensy-4.x-Quad-Encoder-Library-master" "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\sketch\\MotorEncoder2.ino.cpp" -o nul
    "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=148 -DARDUINO=10810 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teen sy4" "-IC:\\Users\\USER\\Documents\\Arduino\\Pipe\\librar ies\\Teensy-4.x-Quad-Encoder-Library-master" "C:\\Users\\USER\\Documents\\Arduino\\Pipe\\librar ies\\Teensy-4.x-Quad-Encoder-Library-master\\Quadencoder.cpp" -o nul
    Generating function prototypes...
    "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=148 -DARDUINO=10810 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teen sy4" "-IC:\\Users\\USER\\Documents\\Arduino\\Pipe\\librar ies\\Teensy-4.x-Quad-Encoder-Library-master" "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\sketch\\MotorEncoder2.ino.cpp" -o "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\preproc\\ctags_target_for_gcc_minus_e. cpp"
    "C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\preproc\\ctags_target_for_gcc_minus_e. cpp"
    Compiling sketch...
    "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/precompile_helper" "C:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr/cores/teensy4" "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932" "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -x c++-header -O2 -g -Wall -ffunction-sections -fdata-sections -nostdlib -MMD -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=148 -DARDUINO=10810 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr/cores/teensy4" "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932/pch/Arduino.h" -o "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932/pch/Arduino.h.gch"
    "C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -c -O2 -g -Wall -ffunction-sections -fdata-sections -nostdlib -MMD -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=148 -DARDUINO=10810 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932/pch" "-IC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teen sy4" "-IC:\\Users\\USER\\Documents\\Arduino\\Pipe\\librar ies\\Teensy-4.x-Quad-Encoder-Library-master" "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\sketch\\MotorEncoder2.ino.cpp" -o "C:\\Users\\USER\\AppData\\Local\\Temp\\arduino_bu ild_833932\\sketch\\MotorEncoder2.ino.cpp.o"
    In file included from C:\Users\USER\Documents\Arduino\Pipe\Teensy\MotorE ncoder2\MotorEncoder2.ino:1:0:

    C:\Users\USER\Documents\Arduino\Pipe\libraries\Tee nsy-4.x-Quad-Encoder-Library-master/Quadencoder.h:146:12: error: 'IMXRT_ENC_t' does not name a type

    volatile IMXRT_ENC_t* ENC;

    ^

    Multiple libraries were found for "Quadencoder.h"
    Used: C:\Users\USER\Documents\Arduino\Pipe\libraries\Tee nsy-4.x-Quad-Encoder-Library-master
    Using library Teensy-4.x-Quad-Encoder-Library-master in folder: C:\Users\USER\Documents\Arduino\Pipe\libraries\Tee nsy-4.x-Quad-Encoder-Library-master (legacy)
    Error compiling for board Teensy 4.0.
    I wonder if my setting is wrong? since so many people using both encoder libray without such problem, how can I check?

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,125
    Quote Originally Posted by iamtawon View Post
    I've also try Teensy4.0 Quadencoder library by running the example twoknob code I've got Error compling code for board teensy4.0


    C:\Users\USER\Documents\Arduino\Pipe\libraries\Tee nsy-4.x-Quad-Encoder-Library-master/Quadencoder.h:146:12: error: 'IMXRT_ENC_t' does not name a type

    volatile IMXRT_ENC_t* ENC;
    ...
    I wonder if my setting is wrong? since so many people using both encoder libray without such problem, how can I check?
    See post #1 :: "NOTE: you will need to incorporate the Update imxrt.h for encoder structure #402 to make use of the library."
    That type define was added to that file.

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,850
    This message is trying to tell you something...

    Multiple libraries were found for "Encoder.h"
    Used: C:\Users\USER\Documents\Arduino\Pipe\libraries\Enc oder
    Not used: C:\Program
    Arduino is using a copy you installed in Documents\Arduino\Pipe\libraries (which is probably an old version from before Teensy 4.0 existed), and it's ignoring the copy Teensyduino installed (in a folder that didn't copy completely from the window to your message here on the forum).

    Try moving or deleting the copy of Encoder from Documents\Arduino\Pipe\libraries, so Arduino will use the one from Teensyduino.

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,125
    Quote Originally Posted by PaulStoffregen View Post
    This message is trying to tell you something...

    Arduino is using a copy you installed in Documents\Arduino\Pipe\libraries (which is probably an old version from before Teensy 4.0 existed), and it's ignoring the copy Teensyduino installed (in a folder that didn't copy completely from the window to your message here on the forum).

    Try moving or deleting the copy of Encoder from Documents\Arduino\Pipe\libraries, so Arduino will use the one from Teensyduino.
    Paul - he did that before I posted the same about 'private lib' - from this thread he is using the newest by @mjs513 in post #7 :: C:\Users\USER\Documents\Arduino\Pipe\libraries\Tee nsy-4.x-Quad-Encoder-Library-master

    What was missed is the change as noted in post #1 - ref in post #8 - needed PULL request update for imxrt.h

  11. #11
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Quote Originally Posted by PaulStoffregen View Post
    This message is trying to tell you something...



    Arduino is using a copy you installed in Documents\Arduino\Pipe\libraries (which is probably an old version from before Teensy 4.0 existed), and it's ignoring the copy Teensyduino installed (in a folder that didn't copy completely from the window to your message here on the forum).

    Try moving or deleting the copy of Encoder from Documents\Arduino\Pipe\libraries, so Arduino will use the one from Teensyduino.
    this solve the problem! now I can run the "classic" Teensy encoder on Teensy 4.0 many thanks!

    and for Quadencoder library for teensy4.X
    "See post #1 :: "NOTE: you will need to incorporate the Update imxrt.h for encoder structure #402 to make use of the library."That type define was added to that file."
    I notice that but I have no idea what it was and how to it since I did not have imrt.h in the library folder. any guidance would be appreciate here.

  12. #12
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,335
    The "imxrt.h" file is part of the teensy cores files which is installed when you install Teensyduino.

    For me the path looks like "F:\arduino-1.8.10\hardware\teensy\avr\cores\teensy4". So essentially navigate to your Arduino IDE folder go to the hardware directory - then teensy directory - then avr directory - then cores directory then the teensy4 directory and you will see the "imxrt.h" file

  13. #13
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    578
    I finally found time to try your lib.
    I tested it with count rates up to 1 MHz and randomly switching direction. I also tried phase angles as small as 10 and bounced signal transitions.
    So far I didn't observe any lost count so it seems to work pretty perfectly.

    Just one small improvement suggestion: The read function should return an int not an unsigned. Casting of course is no problem, but the inexperienced user might get confused by seeing strange numbers doing something like Serial.println(enc.read())

  14. #14
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,335
    @luni
    Thank for testing and glad its working. I forget to post that I did play around with your test sketch and seemed to be working. Got side tracked on other diversions which is way too common lately.

    I did make the change as you suggested and updated the GitHub repository with the change.

    Thanks again for testing.

  15. #15
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    578
    Thanks for the quick fix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •