Example program for the Teensy 3.2 micro-controller board using the QEI quadrature encoder library, the USBSerial.h library for serial port interface, and MotCon.h for motor control

Dependencies:   MotCon2 QEI USBDevice mbed

Teensy 3.2 Quadrature Encoder Motor Control with USB Serial Connection

/media/uploads/jebradshaw/teensymotexample.jpg

MC33926 Motor control board Pinout (Pololu board https://www.pololu.com/product/1212)

MC BoardTeensy 3.2
INVD5 (Direction Pin)
SLEWVcc (3.3V)
ENVcc (3.3V)
FB220 ohm to ground, 1K in series with 4.7uF low pass to A0 on Teensy (current read)
!SFNot Connected
!PWM / D1GND
PWM/!D2D4 (PWM Pin)
IN1GND
IN2Vcc (3.3V)
VDDVcc (3.3V)
GNDGND
VinNot Connected
Encoder ChannelsTeensy 3.2
PhaseAD2
PhaseBD3

// Tensy 3.2 QEI, USBSerial, MotCon example
// J. Bradshaw - 20180111

#include "mbed.h"
#include "USBSerial.h"
#include "MotCon.h"
#include "QEI.h"

QEI enc1(D2,D3,NC,1024, QEI::X2_ENCODING);
MotCon mot(D4, D5); //pwm, dir
USBSerial pc;
Serial ser1(D1,D0); // tx, rx
DigitalOut led1(LED1);
AnalogIn current(A0);   

int main() {
    wait(2.0);
    //pc.baud(9600);  //NO BAUD RATE FOR USBSerial object    
    pc.printf("\r\n%s\r\n", __FILE__); //display the filename (this source file)
    //pc.printf("%s %s\n",__TIME__,__DATE__);
    pc.printf("Version 1.0 - rev %s J. Bradshaw\r\n", __DATE__);
    
    enc1.reset();
    
    while(1) {
         for(float p=0; p<2.0*3.14159; p += 0.01) {
            float pwm_dc = sin(p);
            mot.mot_control(pwm_dc);
            float encoder1 = (float)enc1.getPulses();                                                  
            //pc.printf("%7.2f %7.2f %5.3f\r\n", pwm_dc, encoder1, current.read());
            led1 = !led1;
            
            wait(0.005);            
        }
    }
}

Files at this revision

API Documentation at this revision

Comitter:
jebradshaw
Date:
Thu Jan 11 19:39:34 2018 +0000
Parent:
0:f429a7e2e92c
Commit message:
Teensy 3.2 motor, encoder, USB serial port test

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f429a7e2e92c -r 9ffb6825058f main.cpp
--- a/main.cpp	Thu Jan 11 18:34:37 2018 +0000
+++ b/main.cpp	Thu Jan 11 19:39:34 2018 +0000
@@ -1,14 +1,17 @@
+// Tensy 3.2 QEI, USBSerial, MotCon example
+// J. Bradshaw - 20180111
+
 #include "mbed.h"
 #include "USBSerial.h"
 #include "MotCon.h"
 #include "QEI.h"
 
 QEI enc1(D2,D3,NC,1024, QEI::X2_ENCODING);
-MotCon mot(D4, D5);
+MotCon mot(D4, D5); //pwm, dir
 USBSerial pc;
 Serial ser1(D1,D0); // tx, rx
 DigitalOut led1(LED1);
-AnalogIn current(A0);
+AnalogIn current(A0);   
 
 int main() {
     wait(2.0);