Index>Robot Kit>Alter All in One Kit for RPi>Lesson 32 Principle of Self-Balance Function (Dog)
No related product information!

Lesson 32 Principle of Self-Balance Function (Dog)

1008

       In this lesson, we will learn the principle of the Alter Dog robot's self-balancing function. You need to prepare the installed Alter Dog robot.

image.png

image.png


32.1 The realization principle of Alter Dog's self-balancing function

The self-balancing function of the Alter Dog robot is mainly realized based on its internal MPU6050 sensor. By acquiring the data of MPU6050, the robot's posture change is judged, and the servo control system is used to accurately drive the servo to adjust the corresponding posture to maintain the balance of the robot. In the process of data processing, we use the Kalman filtering algorithm, which is an algorithm that uses the linear system state equation to make the best estimation of the system state with the input and output observation data of the system. Since the observation data includes the influence of noise and interference in the system, the optimal estimation can also be regarded as a filtering process.


32.2 Learning the code program of this lesson

Here we use Subline IDE to view and edit the code program of this lesson, please see the content of lesson 2 for specific methods. The specific code and comments are as follows:

In the file manager of the MobaXterm terminal, find the directory:

adeept_alter/server  

And open the code of this lesson: steady.py. This code program needs to be run in the server folder directory.

Import MPU6050 library.

image.png 


Import Kaman filter library.

image.png 


Instantiate MPU6050 object, the default I2C address is 0x68.

image.png 


Instantiate the Kalman filter object on the XY axis, the Kalman filter can make the value read from the sensor smoother.

image.png 


Set the maximum height and minimum height; and calculate the middle height by the maximum height and the minimum height; when the robot is on a horizontal plane, each leg of the robot is in the middle height state; the maximum and minimum heights are used to limit the range of motion of the robot's legs.

image.png 


Since the automatic balance mode needs to continuously read information from the MPU6050; read the information once to calculate the change trend and then apply the new angle to the servo. These actions are called a cycle; so use this variable here to set the time interval between every two loops.

image.png 


If the robot is pitched up and down at a certain angle when placed on a horizontal plane, we modify xMiddle; if the robot is tilted to the left and right at a certain angle when placed on a horizontal surface,we modify yMiddle; this can also be used in reverse. For example, when you want the robot to maintain a certain angle, you can adjust these two variables.

image.png 


P value in simple PID controller, proportional parameter, increase this value if the robot moves slowly; If the robot does not stabilize and shakes too much, reduce this value.

image.png 


Because this method is a closed-loop control; therefore, the accumulated error (error) is stored in these two variables; this allows the robot to remain level at any time even if it is on a constantly shaking panel.

image.png 


This function is used to limit the size of a variable; enter the maximum and minimum values, and enter the variable that needs to be limited; the return value is between the maximum and minimum (including the maximum and minimum).

image.png 


This function is used to adjust the angle of the robot: pitch is the pitch motion; roll is the roll motion; pIn is used to adjust the pitch axis motion, the larger the value of pIn, the more the robot heads up; rIn is used to adjust the roll axis motion, the larger the value of rIn, the more the robot tilt to the left.

image.png 


Since the back and forth movement of the leg is not required in the automatic stabilization mode; therefore, the X-axis coordinate point of the end point of each leg is 0.

image.png 


Calculate the coordinate value Y of the end point of each leg with pIn and rIn.

image.png 


Apply the new coordinate values to the four legs.

image.png 


It is a self-stabilization function; call it cyclically to realize the self-stabilization function.

image.png 


Declared as a global variable.

image.png 


If the I2C communication is disconnected, we re-instantiate the MPU6050 object.

image.png 


Obtain the X-axis and Y-axis readings of MPU6050, and use Karman filter to eliminate the noise of MPU6050 readings.

image.png 


Calculate the deviation value of each axis.

image.png 


Calculate pitchValue and rollValue based on the deviation value, and apply the result to pitchRoll().

image.png 


Delay for a while before proceeding to the next cycle.

image.png



32.3 Run the program of this course

1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. Enter the Course Code folder, this folder stores the sample code program to control the robot, enter the following command:

cd adeept_alter/server

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5. steady.py is the sample code for this lesson, enter the command to run this program:

sudo  python3  steady.py

image.png


6.After running the program successfully, you can observe the movement of the robot.