Abstract: This paper designs and implements a three-channel stepper motor control system based on the RTThread embedded real-time system , which improves the system's real-time performance and future functional expansion capabilities. The system control circuit uses an STM32F4 series microcontroller combined with a low-power stepper motor driver, A4988, to complete the hardware circuit board design. The software utilizes the operating system's built-in finsh mechanism to perform preliminary instruction parsing. The control system can read G-instructions to control multiple motors to move along a specific trajectory, and can also adjust the acceleration and deceleration parameters of the motors. Practical project verification demonstrates that the system has excellent stability.
introduction
With the development of computer technology and microelectronics technology, stepper motors, as the execution unit of automation control, are increasingly used in various fields. Many control fields require synchronous and coordinated control of multiple stepper motors, such as military, aviation, and robot control. In particular, in recent years, with the development of embedded technology and integration, its application scope has gradually expanded to ordinary civilian industries, such as small engraving machines and 3D printing. As an electromechanical component that converts electrical pulse signals into corresponding angular or linear displacements, the biggest advantage of stepper motors is that they are easy to control in open loop and have no accumulated error. In applications, the precision control of speed and position is particularly important, so the control of the motor must be real-time [1].
Traditionally, microcontroller control is suitable for relatively simple motor control systems. For complex systems, if motor control is only one module, it requires both coordinated movement within the motor control module and synchronization with other modules, resulting in a complex structure and high implementation difficulty.
With the enhancement of the performance of the microcontroller itself, it has many advantages to complete the real-time coordinated control of multiple motors on the basis of the embedded system. The real-time characteristics of the embedded system enable it to be competent in the coordinated control of multiple motors; the customizability characteristics make it easy to expand other modules of the system; it can support multiple tasks, making program development easier and maintenance more convenient, while improving the stability and reliability of the system [2].
This study designs a multi-stepper motor control system based on an embedded real-time system. This system uses embedded technology to collaboratively control three stepper motors, achieving precise control of the speed and position of the multi-axis system and allowing real-time adjustment of motor speed. The focus of this design is the software-based collaborative control of the motors, ensuring the system achieves the desired positioning accuracy. The system itself can be easily expanded to include various functions to meet different requirements, demonstrating its practical value.
Hardware circuit design
1.1 Overall Design
The main control chip communicates with the host computer via its built-in UART serial port, receiving control commands from the host computer and sending the current system status to the host computer. It reads the G-code file from the SD card via the SPI interface, parsing the G-instructions line by line. Based on the position offset and speed in the G-instructions, the main control chip obtains the pulses to drive the motors, thereby controlling the three stepper motors to accurately reach the target position at the recommended speed. Three limit switches provide feedback on the motor's position information, primarily used to initialize the stepper motor system and define its internal coordinate system.
In this circuit design, the main controller uses ST's STM32F4 series microcontroller, the motor driver chip uses A4988, and the stepper motor used as the actuator is a two-phase four-wire 42 stepper motor.
1.2 Main Control Chip
The main control chip in this system is an STM32F40x microcontroller based on the ARM core. The chip contains 1MB of Flash and 256KB of RAM, making it perfectly capable of running small embedded systems. This microcontroller also includes a large number of on-chip peripheral resources, making it ideal for control applications.
1.3 Driver Module
The motor drive module in this system uses the Allegro A4988 motor driver, which is a complete micro motor driver with a built-in converter. It can operate bipolar stepper motors in full, half, 1/4, 1/8 and 1/16 step modes, and has an output drive capability of up to 35V and ±2A. It is also simple to control; a single pulse input to the corresponding pin of the driver is enough to drive the stepper motor to produce a microstep. The program does not need to consider the phase sequence table or other low-level motor control.
This device uses a 1/16 microstepping mode. The MS1, MS2, and MS3 pins must be set to a high level. The main controller only needs to control the ENABLE, DIR, and STEP pins on the motor driver chip to fully control this stepper motor. ENABLE is the enable pin for the stepper motor controller; DIR is the direction control pin for the motor, used to control the direction of rotation; STEP is the step count control pin, inputting pulses to control the number of steps and the speed of the motor.
Software system design
2.1 Overall Software Design
Based on the design objectives, this paper divides the system into different sub-functions, fully utilizing the multi-tasking advantages of embedded systems. Each sub-function is implemented as a different task process within the system, including: host computer instruction parsing task, user interface display, recording and monitoring process, motion planning and management process, and motor drive task. These tasks are synchronized using semaphores within the embedded system. To ensure the real-time performance of the stepper motor system, the motor drive process is designated as the core process, with the highest priority. Except for interrupts, no other process can preempt its CPU control.
The system is designed with a user interface and uses a serial port industrial control color screen as the terminal. It is simple to control and serves as the control method when the system is disconnected from the host computer.
This article will mainly introduce the software design of motor motion planning and the implementation of the underlying driver: reading motion commands from the serial port or SD card and converting them into the actual movement of the motor mechanism.
① Use the finshshell mechanism to read commands from the serial port or directly from the SD card, identify the commands, and then store them in the command buffer;
② The motion planning process obtains instructions, converts them into Block objects, which contain information such as velocity, direction, and acceleration, and adds them to the circular buffer queue in the Planner;
③ Whenever a Block object exists in the Planner, the motor drive process will start, read the Block object, and start a timer interrupt.
2.2 Embedded System Selection
In the selection of real-time embedded systems, this design uses RTThread embedded operating system, which is a Linux-like system and a domestic open-source preemptive real-time operating system kernel [3]. RTThread real-time operating system kernel is a high-efficiency hard real-time kernel with excellent real-time performance, stability and scalability, and can be as small as 3KB ROM and 1KB RAM. Its kernel package provides most of the synchronization and communication mechanisms, and the task scheduling algorithm is based on priority-based preemptive thread scheduling, which can support up to 256 thread priorities. This embedded system supports processors with various core types from ARM7 to CortexM3.
In addition to the kernel, this embedded system also includes a file system, TCP/IP protocol stack, graphical user interface, and user shell component. This design utilizes the system's shell component to simplify command parsing.
The host computer instruction parsing task relies on the finshshell mechanism in the embedded system. finshshell is a built-in user command-line component of RT-Thread, accessible via a serial port. It is designed as an independent process with a default priority of 8. The process attempts to obtain user input from external devices and then parses and executes the user commands. User instructions can be easily customized using the shell mechanism provided by the embedded system, implemented through the following macro definition: FINSH_FUNCTION_EXPORT_ALIAS().
This article implements a `Gcode_recev(char* movecmd)` function, where `movecmd` is one or more lines of G instructions. The function's structure and flow are described, with the instruction buffer being a globally defined, custom structure. This structure contains a circular buffer and a semaphore. This global variable enables data sharing between processes, and the semaphore prevents buffer contention.
By defining the macro "FINSH_FUNCTION_EXPORT_ALIAS(Gcode_recev,G,"Gcode_recev("")")", we can execute motion commands in the form of G ("cmd") via the serial terminal to achieve debugging purposes. In addition, the system also implements functions for starting and stopping the motor, clearing the motion buffer, and viewing parameters.
2.3 Exercise Planning and Management
In actual use, the system typically operates by continuously reading motion commands from one end and continuously driving multiple stepper motors to move from the other. A structure queue exists between the two ends to buffer the difference in execution speed.
The system can recognize motion commands (usually G commands) via serial port or SD card. These commands contain the target coordinates and speed parameters of the motor movement. After reading a line of motion commands, the system parses the commands in conjunction with global information. To prevent step loss due to excessively fast starting speed or inaccurate positioning due to excessively fast stopping speed, the stepper motor uses a trapezoidal motion method.
The program uses a Planner_t structure. The stage1 part is a circular buffer, the size of which can be defined according to actual usage. The code is as follows:
typedef struct {
//stage1
stepBlock_tstepBlock[BUFFER_SIZE];
int32_thead, tail, len;
//stage2V
...
Planner_t;
The `stepBlock_t` structure contains information such as the direction of movement of each stepper motor, the number of steps, the main step number (i.e., the maximum number of steps for each motor), the number of steps before acceleration, the number of steps before deceleration, and the speed. The underlying motor drive function code is as follows:
typedef struct {
//stage1
int32_tsteps[3];
booldir[STEPPER_NUM];
int32_tsteps_event_count;
//stage2
int32_taccelerate_until;
int32_tdecelerate_after;
floatacceleration;
}stepBlock_t;
2.4 Motor Drive
The above is just the planning of the stepper motor movement, without involving the underlying motor drive. The actual motor drive generally adopts two methods: timer PWM pulse or timer interrupt [4]. In this paper, the timer interrupt function is used to directly drive the stepper motor at the lower layer of the operating system. It is only necessary to send a pulse to a certain motor driver CLK pin in the interrupt function to drive the motor one microstep.
Among the multiple user task processes in the system, the motor drive process is the most important. It actually completes the lowest-level control of the stepper motor. To ensure smooth motor operation under the current motion command, this process is very sensitive to time requirements, and it determines the upper limit of the motor's speed in the system. The process flowchart is shown in Figure 5. This process is implemented using a continuously looping function. Corresponding to the planning management process continuously filling block data into the Planner structure, the motor drive process continuously retrieves block data from the Planner, starts the Timer according to the parameters in the Block, sets the timer parameters initially, and finally drives the motor to move in the timer interrupt service function.
The STM32F40x series microcontrollers utilize multiple timer resources, with clock frequencies up to 84MHz. In this system, the motor's maximum speed is 1500rpm, and the driver operates at 16 microseconds. To achieve this speed, a timer frequency of approximately 72kHz is required, which is more than sufficient considering the complexity of the system's multitasking.
The application of timer interrupt in the system only ensures the accuracy of the interval between two microsteps. In order to ensure the real-time performance of the embedded system, the execution time of the timer interrupt service function should be shortened as much as possible [5]. Therefore, only two necessary tasks are performed in the interrupt service function: to give pulses to the stepper motor and to update the timer.
Conclusion
This paper presents the control of multiple stepper motors based on the RTThread embedded real-time system. By using an embedded system, the system functions are designed and implemented in modules, significantly reducing the overall design complexity and facilitating future functional expansion. This approach achieves control of multiple stepper motors at a relatively low cost. A 3D printer project based on this design is already in use and the system is running stably.