Methods for Controlling Multiple Stepper Motors Using Embedded Design

Methods for Controlling Multiple Stepper Motors Using Embedded Design

Industrial Control Classroom

Industrial Control Classroom【www.gkket.com】Essential website for engineers

Add WeChat:gk-auto Join WeChat group

Electrical Control Automation – Free materials worth over 10,000 yuan

Click to download for free

(DownLoad)

Authors: Wang Yandong, Peng Mingsha, Li Wei

Abstract: This article designs and implements a three-way stepper motor control system, which is based on the RTThread embedded real-time system, improving the system’s real-time performance and later functionality expansion capabilities. The system control circuit uses the STM32F4 series microcontroller, combined with the low-power stepper motor driver A4988, completing the hardware circuit board design. The software utilizes the built-in finish mechanism of the operating system to achieve preliminary parsing of commands, allowing the control system to read G-code commands to control multiple motors to move along a specified trajectory, while also being able to adjust the acceleration and deceleration parameters of the motor’s motion. Practical project verification proves that the system has excellent stability.

Introduction

With the development of computer technology and microelectronics technology, stepper motors, as execution units for automation control, are increasingly used in various fields. Many control areas require synchronous coordination control of multiple stepper motors, such as military, aviation, and robotics control. Especially in recent years, with the development of embedded technology and integration, their application range has gradually expanded to ordinary civilian industries, such as small engraving machines and 3D printing. Stepper motors, as electromechanical components that convert electrical pulse signals into corresponding angular or linear displacements, have the greatest advantage of being easy to control in an open-loop manner, with no cumulative errors. In applications, precise control of speed and position is particularly important, so motor control must have real-time characteristics [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 internal coordination of the motor control module and synchronization with other modules, making the structure complex and implementation difficult.

With the enhancement of microcontroller performance itself, completing real-time coordinated control of multiple motors based on embedded systems has many advantages. The real-time characteristics of embedded systems enable them to handle coordinated control of multiple motors; their scalability makes it easy to expand other modules; and they support multitasking, making program development easier, facilitating maintenance, and improving system stability and reliability [2].

This study designs a multi-stepper motor control system based on embedded real-time systems, which coordinates the control of three stepper motors based on embedded technology to achieve precise control of speed and position in multi-axis systems, while also allowing real-time adjustment of motor motion speed. The focus of this design is on software aspects of coordinated control of motors, ensuring that the system meets expected positioning accuracy. The system itself can easily expand various functions based on different functional requirements, providing certain practical value.

Hardware Circuit Design

1.1 Overall Design The hardware structure of this design is shown in Figure 1.Methods for Controlling Multiple Stepper Motors Using Embedded Design

Figure 1 Hardware Structure Diagram

The main control chip uses an internal UART serial port to communicate with the host computer, receiving control commands sent by the host computer and sending the current system status back to the host. It reads G-code files from the SD card through the SPI interface, and the main control chip parses the G commands line by line, generating pulses to drive the motors based on the position offsets and speeds specified in the G commands, thereby controlling the three stepper motors to reach their target positions accurately at the recommended speeds. Three limit switches can provide feedback on the motor’s position, mainly used to initialize the stepper motor system and define the internal coordinate system of the motor system.

In this design, the main controller uses the STM32F4 series microcontroller from STMicroelectronics, while the motor driver chip uses the A4988, with the stepper motor being a two-phase four-wire 42 stepper motor.

1.2 Main Control Chip The main control chip in this system circuit is based on the ARM core STM32F40x microcontroller, which has 1 MB Flash and 256 KB RAM, allowing it to run a small embedded system completely. This microcontroller also includes a large number of on-chip peripheral resources, making it very suitable for control applications.

1.3 Driver Module The motor driver module in this system circuit uses the A4988 motor driver from Allegro, which is a complete micro motor driver with an integrated converter. It can operate bipolar stepper motors in full, half, 1/4, 1/8, and 1/16 step modes, with an output driving capability of up to 35 V and ±2 A, and is easy to control. Only a pulse needs to be input on the corresponding pins of the driver to drive the stepper motor to take a micro step, without needing to consider the sequence table or other low-level motor control. The driver module circuit is shown in Figure 2.Methods for Controlling Multiple Stepper Motors Using Embedded Design

Figure 2 Driver Module

This device uses 1/16 microstepping mode, with the MS1, MS2, and MS3 pins set to high. 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 rotation direction; STEP is the step control pin for the motor, inputting pulses to control the number of steps and speed of the motor.

Software System Design

2.1 Overall Software Design Based on the design goals, this article divides the system into different sub-functions, fully utilizing the advantages of multitasking in embedded systems, treating each sub-function as a different task process in the system, including: host command parsing task, user interface display, monitoring progress recording, motion planning management process, and motor driving task. Each task synchronizes through the semaphore of the embedded system. To ensure the real-time performance of the stepper motor system, the motor driving process is the core process, set with the highest task priority, and no other processes can preempt its CPU control except for interrupts.

The system is designed with a user interaction interface, using a serial port industrial control color screen for simple control, serving as the control method when the system is detached from the host computer.

This article will mainly introduce the implementation of the software design for motor motion planning and low-level driving: reading motion commands from the serial port or SD card and converting them into actual movements of the motor mechanism.

1. Using the finish shell mechanism to read commands from the serial port or directly from the SD card, recognize the commands, and then store them in the command buffer;

2. The motion planning process takes the commands and converts them into Block objects, which contain information such as speed, direction, and acceleration, and adds them to the circular buffer queue in the Planner;

3. Whenever there are Block objects in the Planner, the motor driving process will start, reading the block objects and initiating timer interrupts.

2.2 Selection of Embedded System In selecting a real-time embedded system, this design uses the RTThread embedded operating system, which is a Linux-like system and a domestic open-source fully preemptive real-time operating system kernel [3]. The RTThread real-time operating system kernel is an efficient hard real-time kernel, with excellent real-time performance, stability, and scalability, requiring only 3 KB ROM and 1 KB RAM at a minimum. Its kernel package provides most synchronization and communication mechanisms, and the task scheduling algorithm is based on priority full preemptive thread scheduling, supporting up to 256 thread priorities. This embedded system supports various types of processors 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 components. This design uses the system’s shell component to simplify the command parsing function.

The host command parsing task relies on the finish shell mechanism in the embedded system, which is a built-in user command line component of RT-Thread. Users can use the finish shell through serial devices; it is designed as an independent process in the system, with a default priority of 8. The process attempts to obtain user input from external devices and then parses and executes user commands. Relying on the shell mechanism provided by the embedded system, user commands can be easily customized, implemented through the following macro definition: FINSH_FUNCTION_EXPORT_ALIAS().Methods for Controlling Multiple Stepper Motors Using Embedded Design

Figure 3 Gcode_recev Flowchart

This article implements a Gcode_recev(char *movecmd) function, where the parameter movecmd is one or more lines of G commands. The flowchart of the function structure is shown in Figure 3, where the command buffer is a global custom structure. The structure contains a circular buffer and a semaphore, using this global variable to achieve data sharing between processes and using the semaphore to avoid competition for buffer data.

Through the macro definition “FINSH_FUNCTION_EXPORT_ALIAS(Gcode_recev,G,”Gcode_recev(“”)”)”, we can execute motion commands in the form of G(“cmd”) through the serial terminal for debugging purposes. In addition, the system also implements functions such as turning the motor on and off, clearing the motion buffer, and viewing parameters. 2.3 Motion Planning Management

Methods for Controlling Multiple Stepper Motors Using Embedded Design

Figure 4 Motion Planning Management Flowchart

The actual use state of the system typically involves one end continuously reading motion commands while the other end continuously drives multiple stepper motors to move. There is a structure queue in between to buffer the speed differences of execution at both ends, and the process structure flowchart is shown in Figure 4.

The system can recognize motion commands (usually G commands) from the serial port or SD card, which contain the target coordinates and speed parameters for motor movement. After reading a line of motion commands, the system parses the commands in conjunction with global information. To prevent the motor from starting too fast or stopping too quickly and failing to accurately position, the motion method of the stepper motor adopts a trapezoidal motion pattern.

In the program, a Planner_t structure is designed. The stage1 part is a circular buffer, and the size of the buffer can be defined according to actual usage. The code is as follows: typedef struct{//stage1stepBlock_t stepBlock[BUFFER_SIZE];int32_thead , tail , len;//stage2V……}Planner_t;

The stepBlock_t structure contains information such as the direction of movement, number of steps, maximum number of steps (i.e., the maximum number of steps for each motor), acceleration steps, deceleration steps, speed, etc., for the low-level motor driving function code as follows: typedef struct{//stage1int32_t steps[3];bool dir[STEPPER_NUM];int32_t steps_event_count;//stage2int32_t accelerate_until;int32_t decelerate_after;float acceleration;}stepBlock_t;

2.4 Motor Driving The above only describes the planning of stepper motor motion without involving the low-level motor driving. The actual motor driving generally uses two methods: timer PWM pulses or timer interrupts [4]. This article uses timer interrupt functions to directly drive the stepper motor at the lower level of the operating system, only needing to send a pulse to the CLK pin of a motor driver in the interrupt function to drive the motor a micro step.

Among multiple user task processes in the system, the motor driving process is the most important, as it completes the lowest level control of the stepper motor. To ensure smooth operation of the motor under the current motion commands, this process is very sensitive to timing, determining the upper limit of motor movement speed in the system. The process flowchart is shown in Figure 5, implemented with a continuously looping function that corresponds to the planning management process, which continuously fills block data into the Planner structure. The motor driving process continuously retrieves block data from the Planner, starting the Timer based on the parameters in the Block, initially setting the timer parameters, and ultimately driving the motor in the timer interrupt service function.Methods for Controlling Multiple Stepper Motors Using Embedded Design

Figure 5 Motor Driving Process

The STM32F40x series microcontroller provides multiple timer resources and can achieve clock frequencies of up to 84 MHz. In this system, the maximum speed of the motor is 1500 RPM, and when the driver uses 16 microstepping, to reach this speed, a timer frequency of about 72 kHz is required, which can fully meet the needs considering the complexity of the system’s multitasking.

The application of timer interrupts in the system ensures the accuracy of the intervals between two micro steps. To ensure the real-time nature of the embedded system, the execution time of the timer interrupt service function should be minimized [5]. Therefore, in the interrupt service function, only two necessary tasks are performed: sending pulses to the stepper motor and updating the timer. The flowchart of the interrupt service function is shown in Figure 6.Methods for Controlling Multiple Stepper Motors Using Embedded Design

Conclusion

This article implements control of multiple stepper motors based on the RTThread embedded real-time system. Based on embedded systems, the system functions are designed and implemented in modules, greatly reducing the overall design difficulty and facilitating future functional expansion, achieving control of multiple stepper motors at a lower cost. The 3D printing project based on this design is already in use, and the system operates stably.

Sharing is the greatest encouragement! Thank you for your support!

Important Notice

Want to join the Electrical Control Automation Technology Exchange Group?

Please add the class leader as a friend

and note: region-industry-name nickname to obtain group entry qualification.

Methods for Controlling Multiple Stepper Motors Using Embedded Design

Electrical Control Automation
Specialized focus sharing







Share to Moments, share with friends
——————————————————————

▣ Source: Applications of Microcontrollers and Embedded Systems, infringement deleted!
▣ Statement: The materials in this article are collected from the internet, edited and organized by Electrical Control Automation. The copyrights of the videos, images, and text used in the article belong to the original authors. However, due to the numerous reprints, it is impossible to confirm the true original authors, so only the source of reprint is indicated. If there are any copyright issues, please contact 17621634088 (same WeChat number) in time, and we will immediately confirm the copyright and pay remuneration according to national regulations!

Methods for Controlling Multiple Stepper Motors Using Embedded Design

They all followed, what are you waiting for?

Methods for Controlling Multiple Stepper Motors Using Embedded Design Methods for Controlling Multiple Stepper Motors Using Embedded Design Methods for Controlling Multiple Stepper Motors Using Embedded Design Methods for Controlling Multiple Stepper Motors Using Embedded Design Methods for Controlling Multiple Stepper Motors Using Embedded Design Methods for Controlling Multiple Stepper Motors Using Embedded Design Methods for Controlling Multiple Stepper Motors Using Embedded Design

Methods for Controlling Multiple Stepper Motors Using Embedded Design

Methods for Controlling Multiple Stepper Motors Using Embedded Design
ClickRead the original text to start your journey in electrical control automation

Leave a Comment

×