Monday, November 21, 2016

Motors are for Moving

Now that we can move information around, let's talk about moving stuff!  I'll discuss three motor types and my strategy for using them.

First, servo motors.  Servo motors can operate in at least two modes (continuous or positional) but I'll focus on the positional mode here.  A servo motor in positional mode rotates to a set position based on an input signal.  This signal is typically a 50Hz square wave (20 millisecond period) where the time spent at Vcc varies between 1 and 2 milliseconds.  If that last sentence didn't make a lot of sense, there's no need to worry.  The Arduino platform makes this very easy.  Just include servo.h in your code and write a value between 0 and 180 to rotate the motor to a position between 0 and 180 degrees.  Follow this link or this link for more details.

Next are DC motors (not the brushless kind, that's a different post).  DC motors are either on or off and run either forward or backward.  However, most motors should not be connected directly to a microcontroller for a variety of reasons including insufficient power to drive the motor and possbile damage to the controller.  Instead, the microcontroller should send signals to some sort of motor driver.  There are a variety of ways to do this.  For my purposes, I chose to write a new library that takes advantage of the L293 quadruple half-H driver chip.  Once the library is included in the code, just call methods to run the motor forward, backward, or off.

Finally Stepper motors. If you want to know more about stepper motors, check out this wikipedia article.  I use bipolar stepper motors with 4 wires and therefore, a microcontroller would need 4 outputs to control it (through some sort of driver of course) if you used the stepper.h library included with the Arduino platform.  However, there are drivers that only need 2 inputs to turn the motor and I chose the A4988 driver for my purposes.  However, I was unable to find a good control library that met all my needs so I created one with the following features:

1)  Configurable velocity and acceleration/deceleration.
2)  Interrupt driven motor steps (so the microcontroller can do other things.
3)  Methods for executing some number of steps.  These are either blocking or non-blocking (done in the background)
4)  Methods to wait for the motor to finish its current task.
5)  Continuous mode.  This allows the microcontroller to start the motor and then do other things.  During this time, the motor will not stop regardless of the number of steps it makes.  The microcontroller can stop or decelerate the motor later but the number of steps executed will likely be unknown
6)  Configurable capacity for multiple motors
7)  RPM mode.  Instead of doing some number of steps, do some number of rotations.

I used the timer 1 interrupt to control the motor (since Arduino uses timer 0 and node communications uses timer 2).

This took a fair bit to develop (particularly getting the timing for deceleration right.  I must have tried at least 3 different ways of doing that).  And, the interrupt that manages the motor takes a fairly long time unfortunately (most interrupts are short).  However, most things seem to work well in spite of the time consuming interrupt.  Here is the library.


No comments:

Post a Comment