#include <AccelStepper.h>
#include <Bounce.h>
AccelStepper motor(4, 7, 8, 9, 10);
Bounce button1(4, 10);
Bounce button2(5, 10);
void setup() {
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
delay(10);
motor.setMaxSpeed(400);
motor.setAcceleration(1500);
}
unsigned long target=0;
void loop() {
motor.run();
button1.update();
if (button1.fallingEdge()) {
target += 100;
motor.moveTo(target);
}
motor.run();
button2.update();
if (button2.fallingEdge()) {
target += 1000;
motor.moveTo(target);
}
motor.run();
if (motor.distanceToGo() == 0) {
int in = analogRead(A0);
motor.setMaxSpeed(map(in, 0, 1023, 10, 400));
}
}
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
Just because it has a quadrature decoder chip doesn't mean it will properly count encoder pulses directly. You would still have to have a second IC between the encoder and the Teensy.the chip on the Teensy 3 has a dedicated quadrature decoder, capable of counting pulse rates into the MHz, with no impact on the CPU - unlike the encoder library that can eat most or all of the CPU servicing the interrupts for a modest DC servo.
If you're going to do that, you might as well use a LS7166 or LS7366 which would do everything for you and store the count in a register.Another approach to cut down on the CPU usage is to use some discrete logic, eg. flip-flops or an LS7183 to convert the quadrature signal into up/down pulse trains, and use two timer inputs to count them, and subtract them to find the change in position each time through your servo loop.
I'm not going to debate technical details any further. Obviously, the hobby market holds a different view of things from the industrial market.Oh and by the way, Paul is 100% correct in saying that an A/B quadrature signal is Gray code, aka "reflected binary": 00 01 11 10 00However... generally people in the industry only talk about Gray code when they mean absolute encoders, that put out eg. 9-bit or 12-bit Gray code based on a code wheel, giving the absolute position of the wheel at any moment. With incremental encoders, where you have to count the pulses, the term is always "quadrature" (unless it's eg. up/down pulses), even though mathematically speaking it's also Gray code. So everyone is right, depending how you look at it...
Just because it has a quadrature decoder chip doesn't mean it will properly count encoder pulses directly. You would still have to have a second IC between the encoder and the Teensy.
...even if you jump through that hoop, you'll find that it's not as easy controlling position of a brushed motor as you might think.
Anyhow, in the interest of saving myself a lot of grief, I posed the question on all the major microcontroller forums: "Has anyone been able to successfully control the position a brushed DC motor using a motor controller and quadrature encoder with a microcontroller?"
There were two camps: The first was comprised solely of armchair programmers who have never actually done it but are quite sure it would be no problem. And they have the pseudocode to prove it. The second camp consisted of those who had actually tried it, most of whom were unable to successfully do it. Only one person from Bangkok said he had actually done it with a PIC 40mHz controller in assembly language and even then, he had used external quadrature counter ICs. Here is his observation on his successful attempt to do this, verbatim: "It's a massive project to get all this stuff to work correctly, this project took MONTHS to finish, a gnarly beast."
So, I'll leave it to you to decide which camp knows what they're talking about. Good luck. If you get it working, please let us know.
After you've tried it, come back and let me know how it worked for you. Otherwise, I'm not interested in your opinion how easy it is.If you need a 5V to 3.3V level conversion, there are many ways to deal with that very common requirement, not all of them needing an IC. Also if the encoder is too far away from the chip, you'll need line drivers - for example the CUI-10XE-10 includes that. I mean come on, this is very basic stuff that everyone has to deal with, what is the big complaint?
The problem I had with Teensy 3 was that I could never get the pulse current from the encoders right. The closest I have come so far is with a 619ohm resistor on both channels. Even that is subject to source voltage changes. Paul said he used 4.7k ohm resistors in his demo video.
you'll find that it's not as easy controlling position of a brushed motor as you might think. If you're using a servo motor, you should already have what you need built into the motor drive circuitry.