Closed-Loop Motors
Interested in 3D printing? In this article Miguel explains how you can use closed-loop motors as a replacement for stepper motors.
A few years ago, I was bitten by the RepRap bug, and I’ve been compulsively building 3-D printers ever since (see Photo 1). While looking for old printer motors to repurpose for my projects, it occurred to me that no one was using steppers anymore. Most people, including inkjet printer manufacturers, seem to believe that DC motors are just better devices.
This article series consists of two parts. In the first part, I’ll introduce the idea of using closed-loop motors as a replacement for stepper motors. In the second part, I’ll present the details of how that can be achieved.
I scavenged stepper motors from old printers for my very first 3-D printer (a Prusa Mendel model). It’s still in operation five years later.
STEPPERS 101
Stepper motors are one type of motor that you can stop at precise angular positions. They’re well suited for position control applications because a microcontroller can operate them without the need of position feedback other than a home switch. But operating a stepper can become a noisy business when the simplest drive, known as full step, is used, given the level of vibration induced to the shaft. That’s why the microstepping technique is used most of the time. Instead of the shaft jumping from one stop angle to the next, a smooth transition can be achieved thanks to microstepping. Moreover, microstepping increases the angular resolution of the steppers adding a finer grain (up to a point) on the stop angle selection. Quieter and more precise machines are a good reward for a small change in the motor drive electronics.
STEPPERS & 3-D PRINTERS
A Cartesian 3-D printer is a contraption comprising four axes, each of which needs precise speed and position control. Three of the axes correspond to the X, Y, and Z axes of motion on the print head (also known as hot end). The fourth one controls the filament extrusion. (A solid plastic filament is pushed to a heated fusion chamber and exits through the hot end nozzle as a melted filament.)
For those of us interested in embedded control, there are a couple more details besides the control of these four axes, as the hot end temperature needs to be precisely controlled so the plastic filament is melted but not burned. It is common to use a PID control to achieve a stable hot end temperature regardless of cooling due to hot end motion or more cold plastic being fed into the hot end. And in some machines, a heated bed is used to prevent the parts being printed from warping. Such a heated bed adds a second temperature control loop to the set of tasks the 3-D printer firmware needs to take care of.
DISASTER STRIKES
The life of a 3-D printer is not always peaceful. During the long hours required for printing a complex part, the axis move thousands of feet as plastic is deposited layer after layer. On a good day, the process ends with a perfectly printed product. But, on some occasions, the hot end (print head) hits a small plastic blob, one of the axes misses one or more steps, and the motion ends at an unexpected angle. Since the motor control is completely open loop, the printer controller assumes every time motion is completed as ordered. Thus, if a motor misses steps, the control software still assumes the motor stops at the commanded angle and the error will propagate to the rest of the print (see Photo 2). This shows up as a layer shift on a printed part. It is not a frequent problem when a printer is well tuned, but it’s unwelcome when it happens.
— ADVERTISMENT—
—Advertise Here—
So, after a while, I started wondering why everyone was just working open-loop. The bottom line is that it mostly works, it’s simple, it’s well understood, and it’s affordable. All of them are good reasons, but they could not prevent me from wondering.
CLOSED-LOOP TO THE RESCUE
The obvious question was why isn’t closed-loop control used? And the obvious answer was that open-loop is simpler and cheaper, and these are the main reason steppers are used in the first place.
Why were stepper motors abandoned for DC motors? There are a number of reasons for this, but output quality is one of the most common. The higher the desired print resolution in dots per inch, the more important the linearity of the distance measurement. Steppers provide decent but not perfect linearity, and using some mechanical feedback, like these transparent bands with vertical stripes, would result in more accurate information about the print head’s real position.
Once the feedback is put in place, the case for using stepper motors for the print heads starts to have less traction. After all, a brushed DC motor with similar power can be cheaper, smaller and even operate quieter than a stepper. Besides, a single H-bridge is needed to drive it. But inkjet printers—even though they use now brushed DC motors for carriage and print-head movement—are not a case of position control, as the print head moves back and forth from one side of the page to the other without stopping anywhere in between. It is more a swipe action than a position control. However, a fused filament 3-D printer print head has a more complex motion pattern when printing a part.
DO TO 3-D WHAT THEY DID TO 2-D
After thinking about the possible advantages a closed-loop position control could bring to 3-D printing, I considered that the best course of action was to create a solution that could be an easy replacement of existing motors. And to make the transition smoother, I had to avoid the need for changing the 3-D printer firmware or control electronics.
In order for such a solution to have some interest for my fellow reprappers, it needs to be affordable too. So I had a look at the cost of the current setup using steppers. A stepper motor like the one I use costs around $15 and the drive electronics go for another $5 or so. That left me around $20 for my alternative solution. Of course, there are turnkey solutions from different vendors that some people have tested and reported on, but at hundreds of dollars per axis, they aren’t widely used.
The answer is always on the Internet, you just need to look for it, so I did. I browsed away for motors with built-in incremental encoders that were powerful enough to move a printer’s carriage. I determined experimentally I would need at least 0.5 inch-pounds of stall torque. And I decided I would avoid geared motors and I would use direct drive instead for the drive to be quieter. After some days of searching the Internet, I settled with some brushed motors with built-in optical incremental encoders for less than $6 each (see Photo 3). But buying from an unknown vendor in China, I got no technical details on the motor performance, and the data I got about the encoder, which claimed to be an 888 pulses per revolution (PPR) encoder, turned out to be wrong, as the encoder disc was only 448 PPR.
While waiting from the motors from China, the next step was to prepare the driving electronics. But to make a solution compatible with existing printers, I decided I would emulate the drive logic of a stepper motor that used a couple of signals to receive commands from the main controller: step and direction. When the printer controller wants an axis to move, it selects the desired direction of that motor’s driver direction signal and next it sends carefully timed pulses to the step signal of the driver so the motor moves the exact desired distance with a certain speed pattern.
MOTION CONTROL DETAILS
Driving a stepper motor on a linear axis is a bit like driving a car between two traffic lights. The car does not move at a constant speed all the time, nor does the motor. Cartesian 3-D printers use a so-called trapezoidal speed motion pattern when doing any movement which closely resembles to the car analogy. From a standstill, the movement starts with a given acceleration and that is sustained until the desired cruise speed is reached (e.g., the city speed limit). From then on, the speed remains the same until you approach a red light and then start to reduce speed.
— ADVERTISMENT—
—Advertise Here—
If you look at the way speed changed over time, you will see a trapezoidal pattern, a rising slope at the beginning, a constant speed phase, and finally a falling slope until the motion stops completely. Both acceleration and deceleration phases can more easily be calculated if constant acceleration is used, although values for acceleration and deceleration may be different (see Figure 1).
Acceleration and deceleration phases are easily calculated if constant acceleration is used.
For a given motor and load, the acceleration is limited by the motor’s power. Consider the car analogy again. You need a better horsepower versus weight figure for higher acceleration to be possible.
The Arduino-based brain of most 3D printers uses a careful timing on the pulses sent to the step signal of the motor driver to represent the desired speed at any given moment. Creating these pulses accurately becomes a real-time task that the microcontroller needs to perform precisely for the motion to take place as planned. Because of this, there is an upper limit of the maximum frequency the controller can deliver, which in the case of the popular Marlin firmware is of 40 kHz. Maximum frequency translates into a limit of the maximum speed of an axis, which also depends on the type of gearing or relationship of teeth per revolution of the mechanical system (how many pulses you need to complete an inch of travel).
ANOTHER CONTROLLER
When a stepper motor is replaced by a brushed DC motor, I need a new component to take care of the same step and direction pulses that are used to control a stepper motor. But because DC motors are designed for permanent rotation, a control-loop is needed for the motor to stop at the desired angle every time. This control-loop is performed by a microcontroller that senses the shaft’s current location and compares it with the target location. The difference of the two is fed to a PID controller that calculates the output signal to drive the motor. That means a processor is needed besides the motor with a built-in encoder and the motor driver. That can only increase costs.
Let’s review what the microcontroller needs to do. On one hand, it needs to make sure the motor is always in the correct position (i.e., the position control task, see Figure 2). On the other hand, the microcontroller has to receive the direction and step signals from the 3-D printer’s main controller and update the target location accordingly. In addition, certain calibration and maintenance features would come in handy when setting up the controller. These features could use a communications port for the exchange of commands and setup parameters. There are some parameters, like the values for the Proportional, Integrative, or Derivative gains of the PID that will need to be tuned specifically for each axis. After all, even if motors are similar, the weight and friction of each axis might be different, therefore requiring a different adjustment for each motor. These values could be stored with the firmware, but if the user can change them without recompiling the firmware and store them in EEPROM, usability would be better.
So we need an affordable microcontroller that can do the job and handle serial communications. Such a microcontroller will handle several time-sensitive signals like the step input signal, whose pulses are to be accurately accounted for, plus the two quadrature signals coming from the incremental optical encoder. Each one of these three signals must be precisely monitored if we want our system to perform accurately. That means that an interrupt input for each one will be the best choice and our candidate microcontroller has to have at least three external interrupt pins.
As a long time Arduino user, I looked for cheap versions that could do the job. The Arduino Pro Mini is a small board featuring an Atmel ATmega328 and not much more that can be had for less than $2. However, it has only two external interrupts. It isn’t a deal breaker, but I need to consider other options.
PROPER OPERATION
You now know how a closed-loop DC motor can achieve an accurate position control that can mimic the behavior of a stepper motor. In Part 2, I’ll explain how to get feedback from a motor shaft and how the control loop should be tuned for proper operation.
RESOURCES
B. Beauregard, Arduino-PID-Library, Ver. 1.1.1, https://github.com/br3ttb/Arduino-PID-Library/.
———, “Improving the Beginner’s PID—Introduction,” 2011, http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/.
ESP8266 Community Forum, “ESP8266 Arduino,” www.esp8266.com/viewforum.php?f=25.
HomoFaciens, “Rotary Encoder: How to Build a Digital Servo Using an Arduino and Photo Sensors,” www.youtube.com/watch?v=XIUrnR8bLAI.
Texas Instruments, “Stepper Motor Basics and Control—How It Works,” www.youtube.com/watch?v=bngx2dKl5jU.
Youmagine, “DC Motor Closed-Loop Control Software,” www.youmagine.com/designs/dc-motor-closed-loop-control-software.
SOURCES
AS50xx Magnetic Position Sensors
AMS | www.ams.com
Arduino Pro Mini
Arduino | www.arduino.cc
ESP8266 Wi-Fi Chip
Espressif Systems | www.espressif.com
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JULY 2016 #312 – Get a PDF of the issue
Sponsor this ArticleMiguel Sánchez (PhD, Computer Science) is an associate professor at the Polytechnic University of Valencia, Spain. He has worked in the Department of Computer Engineering since 1988.