The purpose of this project is to create a system that give precise control of speed, acceleration and motion to motors. Although stepper motors can provide high torque and are highly synchronized to the inputs (therefore the microcontroller controlling it can keep track of their current position), they are not efficient while running, it will severely affect the time a battery can last. Also, if for some reason the stepper motor stalls, there is no way for the controller to know that it didn't move but it will register as if it had, this will throw the whole system into chaos (imagine a 3D printer skipping a step because it got jam, the whole printing will be messed up from the event of occurrence). For this reason, I have implemented a system that can control DC motors with feedback to keep track of what has the motor done regardless of the amount of PWM cycles, stalls, etc. This requires the motors to have quadrature encoders (for this analog/power board the motors must be 12V-24V), the LM629 precision motor controller, and a microcontroller to setup the LM629 ICs. With this system, mobile robots, for example, can enjoy of symmetrical speeds on their motors.
In video above, a test drive of a prototype mobile robot shows that it can effectively spin on it center without having a lop-siding. It also goes through a simple obstacle course with predictable movements.
How the LM629 works internallyThe LM629 is a microcontroller peripheral that incorporates all the functions of a sample-data motion control system in a single device. Using the LM629 makes the complex task of designing a fast and precise motion control system much easier. The LM629 implements a position decoder, a summing junction, a digital PID loop compensation filter and a trajectory profile generator. The LM629 provides a 7-bit PWM signal plus a sign bit, providing a sign-magnitude output port. In operation, a host microcontroller downloads the acceleration, velocity and target position values to the trajectory generator. At each sample interval these values are used to calculate new demand or "set point" positions which are fed into the summing junction. Actual position of the motor is determined from the output signals of an optical incremental encoder. The internal decoder sends the actual position to the other input of the summing junction and subtracts from the "demand" position to form the error signal input for the control loop compensator which is a PID filter.
The trajectory profiles that are sent by the the host microcontroller to the LM629 are plotted in terms of velocity versus time. The reason these are considered velocity profiles is that the desired position is calculated every sample interval. For constant velocity these desired position increments will be the same every sample interval, for acceleration and deceleration the desired position increments will respectively increase and decrease per sample interval. Target position is the integral of the velocity profile.
The host microcontroller communicates to the LM629 through parallel communication. The LM629 has three internal registers: status, high, and low bytes. These are controlled by the RD, WR and PS lines (active low) and by use of the busy bit of the status byte. The status byte is read by bringing RD and PS low, bit 0 is the busy bit. Commands are written by bringing WR and PS low. When PS is high, WR brought low writes data into LM629 and similarly, RD is brought low to read data from LM629. Data transfer is a two-byte operation written in most to least significant byte order. The above description assumes that CS is low.
Types of user commands: Initialization, Filter control commands, Trajectory control commands, Interrupt control commands and Data reporting commands.
Communication flow between the LM629 and its host is controlled by using a busy bit, bit 0, in the Status Byte. The busy bit must be checked to be at logic 0 by the host before commands and data are issued or data is read. This includes between data byte pairs for command with multiple words of data.
In order to implement a system based on the LM629 motor controller, I have created two PCBs. One PCB house the digital components such as the PIC18F46K22 and the LM629s. All the gerber and drill files have been included to order the PCBs to be manufactured through JLCPCB. I have also include the bill of materials and the schematics that can be used to put this together. The digital side can be built on a breadboard and prefboard easily, but the analog/power board contains some challenges since the package for of the H-Bridge doesn't align with the 2.54mm pitch on the previously mention mediums. Also, the digital isolators come in a surface mount form.
This project is doable on a prefboard and breadboard with a few modifications, If you decide to recreate this project without isolators and different h-bridges.
The microcontroller on this board sends the commands, coefficients and trajectory to each of the LM629 individually. Only two are shown here since the current project that is using them only contains two motors, two more can be added to control a total of 4 motor LM629. The LM629 send PWM and sign signals are sent to the analog board (second board) through the 2x10 pin connector where h-bridges. Through this same connector, the quadrature encoder signals are received by the LM629s.
On the analog/power power board, the signals coming from the digital board are connected to the h-bridges through digital isolators. The use of digital isolators allow enables communication between two systems without the need of having a common ground. The electrical motors generate a lot of electrical noise, and these dips and spikes on the voltage can cause malfunction on the digital components such as microcontroller, microprocessors or other digital systems that have a low threshold for electrical noise, that's why this motor controller scheme was designed using digital isolators.
The analog/power board receives the sign-magnitude signals through the 2x10 connector as mentioned above, as well as 5V and digital ground to power the quadrature encoder and the digital side of the isolators.
To achieve full isolation, two different power source must be used, in my project I used two separate batteries.
Compiling the Code for the MicrocontrollerTo compile the code for the MCU, follow the next steps:
- Download the repository as mentioned above
- Extract the contents of the ZIP file somewhere where the path name doesn't contain too many character, e.g. root of the C: drive
- Open MPLABX (install it if you have to, make sure to also install the XC8 compiler)
- On the toolbar, click on File-> Open Project
- Navigate to the location where the contents of the zip file extracted
- Open the folder, then click on the project and click on "Open Project"
- Once the project is open, you may navigate the source and modify it to your needs.
- On the toolbar go to Production->Clean and Build Project
The compiler should have generated a ".hex" file. which can be used to program the microcontroller.
Programming the MicrocontrollerTo program the microcontroller, make sure that the microcontroller is powered on with 5V and the Pickit 3 Programmer is connected to the "Programmer" header pins.
- In the toolbar go to Production->Make and Program Device
- An MPLABX warning window will popup to remind you to make sure that the chip connected is the same as selected in the project and for the voltage to be the one recommended. click OK.
In the following video, I explain how to generate your own look up table to change the speed ranges in your motor controller board.
ConclusionThis project can be adapted to any project that needs high precision motor control and stepper motors are not an option. It also uses an off-the-shelf part to accomplish this purpose, so large part of testing and debugging has been done already. A lot of testing and debugging would still be necessary if the solution would be to implement the PID in software on another MCU or in hardware in an FPGA.
The library that I have written is easily portable to other architectures.
Comments