# Motor position control

#### bia

Joined Sep 26, 2013
11
Has anyone been able to successfully position a brushed DC motor using a motor controller and quadrature encoder with a microcontroller? By sucessfully position, I mean have the motor stop within 4 or less pulses of the desired encoder position and be able to repeat the process hundreds of time with approximately a 4-pulse (1 cycle) precision. Not interested in what a microcontroller can theoretically do. Practical experience doing this only, please. Thanks.

#### Sensacell

Joined Jun 19, 2012
2,908
Done it with a PIC18F chip- PID and PWM- two motors at once.

#### bia

Joined Sep 26, 2013
11
Nice. Thanks, Sensacell. Can you tell me what speed the motors were turning and what encoder resolution. I don't know much about PIC MCUs but I'm guessing this was in assembly language? Also, what clock speed? I seem to be getting conflicting infornation on that with google.

#### Sensacell

Joined Jun 19, 2012
2,908
I used an external quadrature counter chip:

http://www.lsicsi.com/encoders.htm

Encoder counts and RPM are almost irrelevant when you have a hardware counter for the encoder. The important factor is the time constant of the motor and mechanical system as it relates to the PID loop update rate.

PIC running at 10 MIPS (40 Mhz clock)
Code in assembly.

#### Experimentonomen

Joined Feb 16, 2011
331
Except sensors encoders included are becoming obsolete. Its all done sensorless nowdays.

#### Sensacell

Joined Jun 19, 2012
2,908
Except sensors encoders included are becoming obsolete. Its all done sensorless nowdays.

Hmm? Not for precision motion control applications.

Sensorless motors are great for prime movers, but how do you position a rotor +- 0.5 degrees without a precision feedback mechanism i.e an encoder or resolver?

#### bia

Joined Sep 26, 2013
11
A quadrature decoder chip is a definite possibility. At this point I am trying to ascertain whether anyone has successfully done this using a microcontroller to count quad encoder pulses.

Sensacell, I echo your question about sensorless positioning. Care to elaborate on that, Experimentonomen?

#### Experimentonomen

Joined Feb 16, 2011
331
I can swear i was once told that someone had made some 100% sensorless motor positioning using nothing but the motors bemf signals, and was atleast as accurate as encoder based systems.

And if im not mistaken it was on one of those robots used in like car production lines for welding and such.

#### Sensacell

Joined Jun 19, 2012
2,908
Counting encoder pulses in SW is a bandwidth eater- not recommended.

Using the peripheral counters is an option, but not without it's limitations.

#### THE_RB

Joined Feb 11, 2008
5,438
A quadrature decoder chip is a definite possibility. At this point I am trying to ascertain whether anyone has successfully done this using a microcontroller to count quad encoder pulses.
...
Yes, I have done it on a number of projects.

Motors are relatively slow, so it does not take too much timeslice to read the encoder. I do it in a timer interrupt, on a PIC 18F 40Mhz (10MHz ticks) you can run an interrupt at 256 ticks; (10M / 256 = 39kHz).

As long as your encoder will not change faster than 39kHz that will work fine. A 500 count encoder at 39kHz maxes out at 78 RPS which is 4680 RPM. Encoders with less counts per rev would obviously have a higher max RPM.

Here is the interrupt code for the full quadrature recoder to an absolute position (variable mpos), it is very fast;
Rich (BB code):
void interrupt()
{
//-------------------------------------------------------
// This is TMR0 int at approx 39kHz (40MHz PIC 18F)
//-------------------------------------------------------
// clear int flag straight away to give max safe time
INTCON.T0IF = 0;

//  update encoder to see if motor has moved, records it position
enc_new = (PORTA & 0b00000011);   // get the 2 encoder bits
if(enc_new != enc_last)
{
if(enc_new.F1 != enc_last.F0) mpos++;   // record new motor position
else                          mpos--;
enc_last = enc_new;
}
}
There is a simple project below that might give you some ideas, it is a "novelty" project to turn a DC motor at very slow precise average speed but might give you some ideas for encoder decoding etc;
http://www.romanblack.com/onesec/DCmotor_xtal.htm

Last edited by a moderator:

#### THE_RB

Joined Feb 11, 2008
5,438

Code tags and html links seem to have both stopped working? They worked for me yesterday.

#### bia

Joined Sep 26, 2013
11
Thanks, THE_RB. Besides counting pulses, were you able to properly control deceleration, overshoot, etc.?

The end goal is to do PID control on two motors concurrently, as Sensacell has previously stated that he has done.

Joined Jul 18, 2013
23,789
I can swear i was once told that someone had made some 100% sensorless motor positioning using nothing but the motors bemf signals, and was atleast as accurate as encoder based systems.

And if im not mistaken it was on one of those robots used in like car production lines for welding and such.
In the High speed servo world, some BLDC or AC sinusoidal motors will do a brief exercise of the motor and calculate rotor - stator position and from then on use the encoder to provide the right commutation for the 3 winding stator. instead of using hall effect devices or equivalent tracks on the encoder disk.
Automotive welding typically uses robots and these without almost no exception use absolute encoders, not quadrature.
Max.

#### strantor

Joined Oct 3, 2010
5,671
Except sensors encoders included are becoming obsolete. Its all done sensorless nowdays.
I disagree.

#### THE_RB

Joined Feb 11, 2008
5,438
Thanks, THE_RB. Besides counting pulses, were you able to properly control deceleration, overshoot, etc.?

The end goal is to do PID control on two motors concurrently, as Sensacell has previously stated that he has done.
I was. But good position AND power control is quite a difficult task if you don't have much experience. You are basically talking about building a complete servo driver using one chip, actually worse as now you are talking about making TWO complete servo drivers on the one chip?

The short answer is that if the motor is slow and things change slowly etc it's not too hard. If you need fast response and high stability, and/or if you have wild load behaviour etc than everything needs to perform very well in real time and it is not a job for a beginner.

So if this is for a little robot with two slow DC gearmotors, and is for robot locating/positioning, then yes it should not be too hard. If it is for a high performance servo positioning application then you should just buy proper servo drivers.

#### bia

Joined Sep 26, 2013
11
Thanks, THE_RB. I suspected it would be very difficult. But, having read how easy it is by all the armchair programmers on the web who have never done it but can tell you exactly how, I though I would pose the question.

The problem with servos is I haven't found one that will produce 80 in-lb torque in a NEMA-17 frame. If you know of one, I would sure like to hear about it. If you have used a closed-loop brushed DC motor controller that will handle continuous current of 15+amps, I would love to hear your experience.

BTW, not exactly a beginner. I've been at this 30 years with a CS degree. Just never ran into a situation where I needed to make my own wheel (at least not this wheel) before.

Last edited:

Joined Jul 18, 2013
23,789
The problem with servos is I haven't found one that will produce 80 in-lb torque in a NEMA-17 frame. If you know of one, I would sure like to hear about it.
As a rule the only way to get a higher than average torque size in a given NEMA frame is to extend the motor length, Baldor used to make DC servo's in this manner, but they always seemed inordinately long!
Max.

#### bia

Joined Sep 26, 2013
11
As a rule the only way to get a higher than average torque size in a given NEMA frame is to extend the motor length, Baldor used to make DC servo's in this manner, but they always seemed inordinately long!
Max.
I don't think anyone is making those anymore which is why I'm stuck with brushed DC.

#### KaguraHime

Joined Sep 25, 2013
7
Novice here.

If you need a motor to stop at precisely a position, it sounds like an electromagnetic brake is needed.

If you need the motor to stop at the same places every time, it sounds like limit switches will do.

If you're using a stepper motor, then encoders usually aren't required. Though this type of motor doesn't have much torque and it'd miss steps if the load's too much.