PID Control Learning System Part 3
As part of my efforts to learn about motor control and PID conrollers I needed to figure out how to read Quadrature Encoders so that I can measure and adjust motor speed. Using encoders I should be able to measure motor speed with greater precision and accuracy than would be possible from counting each output shaft rotation. And with the right encoders I should be able to measure the motor speed on a regular basis (say every 0.1 second) which makes the PID control simplier and more predictiable.
Rotary encoders are devices that count rotations either of the output shaft or if a geared motor, the primary motor shaft. You will find optical encodes that trigger when a light beam is interupted and magnetic encoders that trigger when a magnet passes close to a hall effect sensor.
Quadrature encoders are when there are two sensors on the same ring. Quadrature encoders provide several benefits:
- Greater trigger resolution over single sensors, hence faster
- Higher counts per rotation and this leads to greater precision and accuracy
- The ability to know what the rotational direction is at any point in time
With the addition of a third sensor you can also measure the position of the wheel (or whichever part is rotating). Three sensors is a bit advanced and is not part of this blog post.
There are a lot of resources on the web talking about using encoders. For my purpose I just need some sample code that runs in an mbed device. The code below is designed for a mBuino platform. However it also runs on an NXP LPC1768 (with a few minor changes) and should be adaptable to all mbed platforms if they have enough pins available.
Geared Motors and Encoders
When the encoder is attached to the primary motor shaft of a reduction geared motor you can get very high output shaft resoultion from the encoders. Consider the motor I used from pololu which is stated to use a 30:1 reduction gear, with a maximum output shaft speed of 350 RPM. When using both sensors for the qaudrature encoder the motor triggers 64 times for each primary shart rotation.
Lets do the math for one output shaft rotation:
1 output rotation * 30 primary motor rotations / ourpur rotations = 30 primary rotations / output shaft rotations 30 primary rotations * 64 triggers / rotation = 1920 triggers / output shaft rotation
The documentation on the motor I used states:
The quadrature encoder provides a resolution of 64 counts per revolution of the motor shaft when counting both edges of both channels.
This is important because most encoder libraries have a configuration setting for which encoding scheme to use.
This demonstration program used the mbed QEI library provides for X2 and X4 encoding. We need X4 since we are measuring both the rise and fall of the output traces for both sensors. The code sample shows how this all works.
I’m pretty new to using mbed and encoders so I validated the calculated motor speed (output shaft rotations per minute) using a hall effect sensor with a disc with a magnet on the output shaft. The good new is the values are identical. I guess that means I’m doing something right! You can see the setup in the pictures and video at the end of the post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66