Design of two wheel self balancing car

This paper proposes a design scheme of the two-wheel self-balancing dolly, the integration of the gyroscope and accelerometer MPU6050 constitutes the car position detection device.System selects 32-bit MCU stmicroelectronics company as the control core, completed the processing of sensor signals, the realization of the filtering algorithm, motion control and human-computer interaction. Produced and debugging in the whole system is completed, the car can realize the independent balance under the condition of no intervention. The introduction of a suitable amount of interference, the car can adjust quickly to recover and steady state. Through remote control car bluetooth module complete forward, backward, turn left and other basic action..


Introduction
Into 21st century, the combination of micro-controller and automatic control of the intelligent robot has been an unprecedented development, two-wheeled self-balancing robot is brought into the society of the various industries. Two rounds of robots in the security, service, production and other aspects of the application more and more, need to adapt to the environment and the implementation of the task is more complex, which requires the robot must have the ability to perform tasks in some complex environments. Such as manned two-wheeled bales in the potholes on the road to walk to maintain stability; in the food side of the two rounds of robots should be stable enough to prevent liquid food spills. It is the main direction of the current self-balancing robot to improve the stability, fastness and accuracy of the two-wheeled self-balancing robot.
Today's two-wheeled self-balancing robots are commonly used in two rounds of co-axial and independent of the way, and in the structural design of the body center of gravity to be designed in the wheel center line is directly above, by controlling the wheel forward rotation to maintain body balance , by controlling the speed difference to achieve the car turning action. Therefore, two-wheeled vehicle body movement is more flexible, and can work in a rugged, multi-obstacle complex environment.

Balance control principle analysis
In the human life everywhere can see the phenomenon of balance control, such as people walking, the perception of the organ body and environmental information feedback to the brain, the brain and then adjust the waist and leg muscles, keep the body center of gravity in the foot of the support point above, so To achieve upright. Another example is to keep a straight stick on the fingertips to keep upright, people through the eyes to observe the tilt angle of the stick and tilt the trend, feedback to the brain to control the movement of the fingers, so keep the stick upright. In fact, this is through the eye feedback after the manual adjustment of the location of the negative feedback mechanism. The control principle of the two-wheel self-balancing car is the same as moving the fingertip to keep the stick upright. As the car's wheels and the ground will be relatively rolling, in the open-loop state of the car can not be balanced. In the closed loop, the car on the microcontroller to obtain MPU6050 attitude information, and then through the PID regulator to control the wheel rotation to keep the car balance.

PID regulator design
In engineering design, the proportionality, differential and integral units are often used to correct the setpoint of the demand in series, and these three components make up the PID regulator. PID development from the last century, analog signal PID slowly into digital PID, especially with the rapid development of electronic technology and micro-computer technology, digital PID with its easy to use features in the production, life has been an unprecedented application. In the PID control, the proportional control unit embodies the strength of the control effect. In order to meet the different requirements of the control performance in the engineering design, we can add integral or differential or introduce integral and derivative, and can be called PI regulator, PD regulator and PID regulator.
Whether there is a feedback channel is open-loop control system and closed-loop control system is the most essential difference. Closed-loop control includes four links: comparison links, the implementation of links, feedback links and adjustment links. Which set the value and the measurement of the feedback signal characteristics must be consistent, such as the encoder output pulse signal, through the microcontroller conversion is close to the actual speed, so you can directly set the speed of the software. PID controller by the P (proportional control unit), I (integral control unit) and D (differential control unit) three components. Formula 1 for the PID adjustment equation, including the proportion of links, integral links and differential links. Where e (t) is the input signal, u (t) is the output signal, is the scale factor, is the integral time constant, is the differential time constant.
The input and output transfer function of the car is: The solution has two poles:. Which has a pole located in the s plane of the right half of the plane, by the Nyquist stability criterion that the system is unstable, so the car can not maintain a balance in the stationary state [1]. It can be seen from the formula 2 that the magnitude of the acceleration provided by the wheel is derived from the feedback of the angle and angular velocity. Therefore, it is necessary to introduce the angle and angular velocity proportion (P) differential (D) feedback in the control system, as shown in Fig.1.

Figure 1 Control System Structure
The system transfer function after adding proportional differential feedback is: At this point, the system's two poles are.
, the Nyquist stability criterion shows that the two poles of the system are located in the left half of the s plane, to meet the stability of the system conditions, so the car to achieve self-balancing.
In the PID regulator in the role of each link in the system: P (proportional link): to speed up the response speed, will produce steady-state error. Increasing the scale factor can reduce the steady-state error, but at the same time increase the overshoot, the number of shocks increases, the adjustment time becomes longer. Excessive scale factor will make the system's steady-state deterioration, and even response to divergence. In this design, P in the vertical closed-loop main role is to enlarge the vertical deviation, to speed up the response speed, so that the car has a large enough resistance; P in the speed of the closed loop is the role of amplification speed deviation, the speed is fed back to the motor, Raise the upright ring.
I (integral link): through the accuracy of system control, clear the accumulation of errors in the past, thereby eliminating the static difference, improve the system's steady-state performance. In this design, I in the speed of the closed loop inside the role is to eliminate the static difference, can guarantee the stability of the upright closed loop. D (differential link): with the role of advanced regulation, so that overshoot reduction, stability, enhance the dynamic performance of the system. In this design, D in the vertical ring in the role is to adjust the tilt angle, reduce the overshoot, so that the car into the steady state faster.

Attitude detection system
Complementary filtering is the angle obtained by sampling gyroscope in the sampling period of each microcontroller, and the angle obtained by sampling acceleration conversion is averaged to correct the angle of the gyroscope, and finally an accurate and stable angle can be obtained value.
PID control algorithm to achieve the premise is to have a precise and reliable feedback signal, so the attitude detection system in two rounds of self-balancing car research is essential. The MPU6050 integrates a 3-axis MEMS gyroscope, a 3-axis MEMS accelerometer, a digital motion processor (DMP), and the MPU6050 utilizes three 16-bit analog-to-digital converters for gyroscopes and accelerometers sampling. MPU6050 not only to meet the functional requirements of self-balancing car, and AD sampling accuracy, can accurately obtain the car's gesture information.
But the gyroscope is inertial sensor, when the outside temperature changes or shock, the sensor will produce uncontrolled drift and noise, resulting in detection error, then we must add a complementary filter algorithm, the gyroscope and accelerometer collection The data are complementary to the fusion, making the measured angle more accurate and stable The system consists of the following circuits: STM32F103RCT6 minimum system circuit, power conversion circuit, tilt sensor circuit, motor drive circuit and encoder detection circuit. The hardware design of the system is shown in Fig.2.

power conversion circuit design
The reliable operation of the system is to have a stable and reliable power supply design. The system power supply module consists of 12V filter circuit, driver switch circuit, single-chip switch circuit, 5V regulator circuit and 3.3V regulator circuit module. PCB routing in the various modules of the ground only in the output point of a single point of communication, reducing the power conversion module between the mutual interference.
Power conversion circuit provides a total of three kinds of power supply: (1) 12V battery power: to drive the chip motor power supply interface power supply.
(2) 5V step-down power supply: the encoder circuit, the motor driver chip logic power supply interface.
(3) 3.3V step-down power supply: the minimum system circuit for the microcontroller, the attitude detection circuit and the Bluetooth module power supply.
As shown in Figure 3, the entire system power source for the 12V model lithium battery, through the LM2596 buck output 5V power supply, and then through the AMS1117-3.3 buck output 3.3V power supply. The LM2596 buck chip is a step-down regulator integrated regulator chip designed and manufactured by TI (Texas Instruments) to produce the maximum output 3A current, which includes protection circuitry, current limiting, thermal shutdown circuitry. The chip just with some simple components can constitute an efficient voltage regulator circuit. 3.3V power supply using AMS1117-3.3 regulator chip .

Design of attitude detection circuit
As shown in Figure 4, the attitude detection circuit consists of MPU6050 chip, power supply filter circuit, IIC interface pull-up, current limiting circuit and so on.

Motor drive circuit design
In order to self-balance in the process of car can be rotated to adjust the attitude of the car, so the motor drive to have the bridge structure of the bridge. The system uses TB6612FNG driver chip to form a full bridge drive circuit, as shown in Figure 5. By the single-chip PWM module to generate the drive pulse, by changing the PWM duty cycle to achieve DC motor speed control function.

software design
The system through TIM1 set 5ms timer interrupt as the system time base, 5ms tasks include access to MPU6050 hard solution of the four elements, converted into angle and acceleration, through the complementary filter and PD regulator PWM output control; 20ms tasks include, read TIM3, TIM5 encoder mode to calculate the speed, and then through the PI regulator to control the PWM output. USART1 interrupt is responsible for receiving the Bluetooth module control instructions, adjust the PWM forward and backward and left and right steering.

System test and result analysis
Two-wheel self-balancing hardware design and software design for the self-balancing car built a model, but can run stable, accurate and fast need to be completed through a long time to debug. The system uses the modular debugging method, first debug the feasibility of the hardware circuit, and then debug the accuracy of the attitude detection system, the final PID parameter tuning, to achieve the car control and then debug the Bluetooth module to complete the system debugging. The instrumentation and tools required for system commissioning are as follows: (1) a computer (with MDK5, serial debugging software) (2) online debugger (J-LINK) one (3) a digital multimeter (4) USB to a serial port module

Attitude detection debugging
Visual Scope has four analog channels, each time through the serial port to send four channels of 16bit data and CRC verification code. Figure 7 in the blue curve is read out by the MPU6050 calculated tilt angle of the car, the red after the filter is more smooth after the curve, we can see that the angle value of the complementary filter after the high frequency component is basically not exist, and follow Strong, to achieve a good filtering effect.

PID Parameter Tuning
In the balance control of two self-balancing vehicles, the vertical ring PD regulator and the speed loop PI regulator are used. In the vertical ring, the angle proportional control P mainly amplifies the inclination of the inclination to improve the response speed; the differential control D mainly acts on the prediction of the inclination deviation tendency, which is equivalent to providing the damping force in the system, Reduce the dynamic deviation, shorten the control process time. In the speed loop, P as the speed proportional control, its main role is to enlarge the speed deviation, adjust the controller output PWM duty cycle, and then output to the motor driver, the car control P increased the effect of positive feedback, reducing the The response time of the vertical ring; I control the main role is the cumulative acceleration of the deviation, that is, displacement deviation.
After adjusting the vertical ring, then adjust the speed loop. The first given the speed of 0, if the car at this time the center of gravity is not above the axle, the car will go in one direction, because the vertical ring can only adjust the tilt angle of the car, and speed has nothing to do. Gradually increase the speed ratio factor Kp, so that the car can stand, promote the car, the car will be shock. And then reduce the Kp to 70% of the original value, slowly increase the speed integral coefficient Ki until the promotion of the car, because the integral part can eliminate the static deviation, the integral is not saturated in the case, the car can automatically run back to the origin.

Conclusion
This design mainly studies the design and realization of two-wheel self-balancing car. Through the corresponding hardware and software design, to achieve a two-wheel self-balancing car dynamic balance and motion control. The hardware structure of the system is based on STM32. The MPU6050 with gyroscope and accelerometer is used as the attitude detection system. The TB6612FNG motor drive circuit and quadrature encoder are used to realize the closed-loop control of the DC motor. To achieve the two-wheel self-balancing vehicle attitude detection and balance control.