Design of Rolling Ball Control System Based on PID Algorithm

The ball and plate system is a medium for researching automation control. As a multivariable, non-linear control object, the sphere studies how its fixed-point control and trajectory motion have great significance for automatic control. The software part of a ball and plate system designed in this paper adopts PID control. The hardware part uses STM32F103RCT6 as the main control chip. The position of the ball is obtained through the four-wire resistive screen and the HC05 Bluetooth module is used to communicate with the mobile phone. The corresponding state of the system is displayed by the TFT liquid crystal module. The fixed point control of the small ball and the specified trajectory movement are realized. The fixed point control can change the fixed point of the small ball at any time by clicking the mobile phone screen, and the specified trajectory movement can be controlled by the cricket system button or the mobile APP control.


Preface
Cricket system is an ideal classical controlled object in the field of control, which is equivalent to the two-dimensional extension of a cue system. Its control purpose is to make the ball on the onedimensional space or two-dimensional space stable at fixed points and track the specified trajectory by controlling the movement of a two-dimensional space. Because of its classical nonlinearity, it is used as a practical tool for classical control theory and modern control theory. In order to achieve the robust tracking of the expected trajectory of the ball, it is necessary to improve the dynamic and static performance of the cricket system by combining the classical control theory with modern control theory. As a control system with variable, strong coupling and nonlinear control objects, cricket system is actually similar to the control theory research tool of inverted pendulum and other systems [1]. It can not only be used to study classical control theory, modern control theory, but also can be extended to all aspects of automatic control field. Studying cricket system needs mechanics, mathematics, automatic control theory and other fields, which plays a great significance for verifying the theoretical cricket system in these fields. This paper demonstrates that it is a low-cost cricket system, which uses the resistance screen, steering gear, single-chip microcomputer and other modules to verify PID algorithm to achieve the specified trajectory tracking and fixed-point stability of the sphere on the resistance screen.

Positional PID control calculation
PID control is often used in practical project. The so-called PID controller is actually composed of three control units: proportional control unit (P), integral control unit (I) and differential control unit (D). Proportional control, integral control and differential control have their respective advantages [2]. Generally speaking, the proportional control unit (P) is necessary in the actual engineering, according to requirements about the actual engineering requirements, so there are various of PID control methods, including P control, PI control, PD control, PID control.
In this paper, the positional PID control calculation is adopted. According to the comparation of realtime coordinates fed back by the four-wire resistance screen and the target coordinates, the control deviation is obtained, and then the control is carried out by proportional, integral and differential actions, so that the deviation tends to zero. The PID (1) In the formula, , and are the three parameters of PID controller, ( ) is the current deviation, ( − 1) is the last deviation, ∑ ( ) is ( ) and the accumulated sum of the previous deviations, where t is 1,2,3..., t.

System hardware design
In terms of the hardware of cricket system, the following modules and chips are used: STM32F103RCT6 main control chip, steering gear, TFT LCD screen, four-wire resistance screen, 2S model airplane battery, bluetooth, mobile APP. The main control chip is featured by high performance, low cost and low power consumption [3].
The following three types of circuits are designed based on each module: STM32F103RCT6 minimum system circuit, 3.3 and 5V voltage regulating power circuit, and four-wire resistance screen AD sampling circuit. Figure 1 shows the hardware framework of the cricket system.

circuit design of voltage stabilizing power supply
A key point of stable and reliable operation of cricket system is that it has stable and reliable input voltage, which mainly includes two parts of voltage regulator circuit: 5V voltage regulator circuit and 3.3v voltage regulator circuit. Figure 2 shows the 5V voltage regulator circuit, which is mainly used to provide voltage for the steering gear and hc-05 bluetooth module. Lm2596s-5 is used as the voltage regulator circuit of 5V, which is produced by Texas instruments. Its maximum output current can reach 3A, and a circuit with a stable output of 5V can be completed with this chip in a very few external periods. VBAT is connected to the positive pole of model aircraft battery 2S and input to LM2596S.   Figure 3 shows the 3.3v voltage regulator circuit, whose main function is to provide 3.3v voltage to STM32F103RCT6, TFT LCD screen and four-wire resistance screen. The input of this circuit can be provided by the 5V voltage output in figure 2 or by connecting USB to the computer to provide the input voltage. The voltage regulator chip adopts asm1117-3.3.

AD sampling circuit design
The key of cricket control system is to obtain the real-time coordinates of the sphere, and the key to obtain the real-time coordinates of the sphere is to calculate the coordinates of the established coordinate system, so the AD sampling circuit is a key and difficult point.  Figure 4 shows the AD sampling circuit, which controls the on and off of four transistors through four IO ports of STM32F103 to determine the 3.3v voltage of Y electrode or X electrode, and then determines which ADC to adopt according to which electrode is given the 3.3v voltage.

PWM output, bluetooth, TFT LCD interface circuit design
PWM output interface, bluetooth interface and TFT LCD interface are relatively simple, only need to STM32 corresponding I/O port can be out.
The interface circuit of the steering gear, through PB6 and PB7 output two PWM control the Angle of the two steering gear.
The bluetooth module interface circuit provides the power voltage to HC05 through 5V voltage, communicates with HC05 through STM32F103 serial port 3, and then completes the communication between APP and STM32 by connecting the bluetooth module.
The interface circuit of TFT LCD provides power to TFF through 3.3v voltage, and then communicates with TFT through PB13 and PB15. Hardware SPI communication is adopted to complete the display of corresponding parameters including PID parameter, target coordinate value and real-time coordinate value [4].

System software design
The main flow chart of the cricket system program is shown in Figure 6. The program first initializes the MCU peripherals and then enters the interface operation function, mainly by judging the current button state to determine whether to enter the PID adjustment sphere link to avoid the cricket system. The horizontal output is not installed or the ball is not placed on the four-wire resistive screen, resulting in a PWM output angle. If the steering angle changes too much, the four-wire resistive screen will fall to the ground and damage the resistive screen. PID regulation sphere position through the timer 1 provides 10 ms interrupt time base to realize, once every 10 ms into the timer interrupt service function, total into four times to complete the PWM duty cycle of output at a time, for the first time into the ADC sampling Y + voltage to determine the Y coordinates in the coordinate system, through a serial port for the second time in three read APP data, for the third time in ADC acquisition system to determine the X axis coordinates X + voltage, the fourth time to enter through the front access to real-time coordinates and given coordinates through the PID algorithm corresponding PWM output control steering Angle, the fourth time back after the completion of the first cycle.

PID algorithm programming
The PID algorithm is controlled by the timer 1 interrupt service function. The corresponding code is as follows: First, the corresponding PWM duty cycle outputted by the PID is calculated by calling balanceX() and balanceY(), then the output clipping process is performed by four if, and finally the corresponding PWM is output to the PWM and the real-time coordinates of the sphere are sent to the mobile APP.
Balanced PID control function balanceX() in the X direction: • Calculate the deviation of the X coordinate of the target coordinate from the X axis of the real time coordinate: Bias=(Angle-Zero_X) • Calculate the rate of change of the deviation: Differential=Bias-Last_Bias • Integral control of the deviation and integral limit The balance PID control function balanceY() in the Y direction is the same as the X direction principle, and will not be described here for the sake of brevity.

Mobile APP Design
The mobile app is based on the Android SDK API 27 and uses Android Studio for java programming. Bluetooth is used between the App and the MCU. The MCU sends the coordinates of the cricket to the mobile phone. The analog interface on the mobile phone changes the position of the ball according to the coordinates. When the ball on the mobile phone is dragged, the coordinates are sent to the MCU. , thus changing the position of the ball in reality [5]. The establishment of the mobile APP coordinate system requires customizing a view. The custom View package has three parts: the InnerCircleView is the custom view of the ball, the OuterCircleView is the custom view of the outer board, and the JoyStickView is the package of the entire custom view. After the custom View is completed, the view coordinates on Android are transformed by a series of coordinates, so that it is consistent with the 2.1.2 coordinate system, and finally the view is written in the layout xml corresponding to the MainActivity activity.

PID parameter tuning
The adjustment of the PID parameters is adjusted by the computer host computer, and the four parameters of the real-time coordinates acquired by the STM32 and the PWM duty ratio of the PID output are sent to the computer for adjustment by the serial port 1.
Through continuous debugging, is 55, of X-axis coordinates is 0.3, of Y-axis is 0.25, and is 58. As a proportional control, P functions to amplify the deviation between the real-time coordinates and the set coordinates, so that the system speeds up the corresponding speed. I, as integral control, is used to integrate the deviation so that the cricket system finds the PWM duty cycle that is most suitable for the current set coordinates. As a differential control, D can predict the deviation in advance, which can make the cricket system reach equilibrium and stability quickly.
First, adjust the parameter, adjust it from small to large, and find an optimal parameter that can achieve the fastest response without causing a large overshoot. In practice, it is found that unless the set PWM is just right, the resistance screen is balanced and the cricket system placement requirements are very high, so in practice, I need to add I to integrate the cricket system to find a PWM duty cycle that can make the resistance screen level.
So the next step is to adjust adjust from big to small. Due to the characteristics of integral action, the integral action will be small to large. The cricket system can not only find a PWM duty cycle that makes the resistance screen level and can quickly eliminate P. The static error caused by the adjustment. The consequence of the previous I parameter is that the system cannot predict the deviation in time. Only when the proportional adjustment takes a relatively large value can the cricket system be barely balanced, so the next step is to adjust the differential D, from small to large. When a suitable value is obtained, the system can predict the deviation in advance, reduce the overshoot caused by P adjustment, and accelerate the system response speed. P parameters, I parameters, and D parameters are three essential in the cricket system, so when these three parameters have already obtained better values, it is necessary to combine the characteristics of the three parameters to continue to adjust them to find the best one. Parameters.

Conclusions
This paper first builds a hardware circuit of a cricket system, and then uses the PID algorithm to control it, so that the sphere on the resistive screen can achieve fixed point stability and track the specified trajectory. In addition, a special mobile phone APP was written to communicate with the cricket system through Bluetooth, and finally a complete cricket control system based on PID algorithm was formed. The innovation of this paper is: the code is simple and easy to implement, the mobile APP displays the actual position of the current sphere in real time, and the target coordinates can be changed at any time.