Index>Robot Kit>Alter All in One Kit for RPi>Lesson 36 Shaking Motion
No related product information!

Lesson 36 Shaking Motion

995

      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.


image.png

    

   image.png

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.

image.png 


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; since only the Y-axis reading needs to be detected, only one Kalman filter object needs to be instantiated here.

image.png 


Set the height when the robot is standing.

image.png 


Set the swing range of the robot's legs.

image.png 


Use this variable here to set the time interval between every two loops.

image.png 


Set the threshold for judging whether the robot falls down.

image.png 


After detecting that the robot is righted, execute this function to control the robot to stand steady.

image.png 


After detecting that the robot falls down, execute this function to control the robot's legs to swing randomly.

image.png 


Use randint() to generate integers from -wiggleRange to wiggleRange.

image.png 


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

image.png 


Obtain the Y-axis readings of MPU6050, use Kalman filter to eliminate the noise of MPU6050 readings.

image.png 


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.

image.png 


Delay for a while before proceeding to the next cycle.

image.png 



36.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.wiggle.py is the sample code for this lesson, enter the command to run this program:

sudo  python3  wiggle.py


image.png 


6. After successfully running the program, the command window will display the data of MPU6050 sensor.