In this lesson, we will learn the shaking action of the Alter Dog robot. You need to prepare the installed Alter Dog robot and connect all the components.
36.1 Alter Dog
The shaking motion of the Alter Dog robot is mainly based on its internal MPU6050 sensor. In the process of data processing, we use the Kalman filtering algorithm (Kalman filtering), which is an algorithm that uses the linear system state equation to obtain the optimal estimation of the system state with system input and output observation data. 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.
36.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: wiggle.py. This code program needs to be run in the server folder directory.When the robot falls to the ground, the robot's legs will swing.
Import random function library.
Import MPU6050 library.
Import Kaman filter library.
Instantiate MPU6050 object, the default I2C address is 0x68.
Instantiate the Kalman filter object on the XY axis. The Kalman filter can make the value read from the sensor smoother; since only the Y-axis reading needs to be detected, only one Kalman filter object needs to be instantiated here.
Set the height when the robot is standing.
Set the swing range of the robot's legs.
Use this variable here to set the time interval between every two loops.
Set the threshold for judging whether the robot falls down.
After detecting that the robot is righted, execute this function to control the robot to stand steady.
After detecting that the robot falls down, execute this function to control the robot's legs to swing randomly.
Use randint() to generate integers from -wiggleRange to wiggleRange.
If the I2C communication is disconnected, re-instantiate the MPU6050 object.
Obtain the Y-axis readings of MPU6050, use Kalman filter to eliminate the noise of MPU6050 readings.
If the Y-axis reading is greater than the absolute value of the threshold used to judge whether the robot falls down, the robot swings the leg; otherwise, it stands still.
Delay for a while before proceeding to the next cycle.
36.3 Run the program of this course
1. Open the terminal software MobaXterm:
2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):
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
4. Enter the command to display the contents of the current directory:
ls
5.wiggle.py is the sample code for this lesson, enter the command to run this program:
sudo python3 wiggle.py
6. After successfully running the program, the command window will display the data of MPU6050 sensor.