In this lesson, we will learn how to use matplotlib to simulate a plane link.
21.1 Brief description
Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+. There is also a procedural "pylab" interface based on a state machine (like OpenGL), designed to closely resemble that of MATLAB, though its use is discouraged. SciPy makes use of Matplotlib.
Matplotlib was originally written by John D. Hunter, since then it has an active development community, and is distributed under a BSD-style license. Michael Droettboom was nominated as matplotlib's lead developer shortly before John Hunter's death in August 2012, and further joined by Thomas Caswell.
21.2 Learning the code program of linkageR.py
Here we use Subline IDE to view and edit the code program of this course. For the specific method, please see "2.4 Editing the Code Program in Raspberry Pi" in Lesson 2.
In the file manager of the MobaXterm terminal, find the adeept_rasparms/CourseCode, download the folder 09linkageR to your PC, select this folder, click the button to download the file to the PC, save it in the path of English letters, and use The Subline IDE opens the linkageR.py in the 09linkageR folder. The main code and comments are as follows:
Import numpy.
Import matplotlib.
Import the library for animation.
Set the length of AB to 70, the length of BC to 150.0, and the length of CD to 10.
Define the initial position deviation of the robotic arm.
Define the text size displayed here.
servoNumCtrl = [0,1] is the number of the AB servos; servoDirection = [1,-1] corresponds to the movement direction of the AB servos, set by 1 and -1.
The array linkageLenCtrl composed of the lengths of links AB and BC.
The function planeLinkageReverse() is used to input the length of the connecting rod and the error parameters, the end position of the link and the number of the servos, and the rotation angle of the AB servos of this link mechanism can be obtained.
In the planeLinkageReverse() function, first calculate the incremental error, and use debugPos to correct the initial position error. Generally, debugPos is [0,0].
Used to calculate the angle error between the end point of the link and the rotation axis of the servo B.
The input links AB, BC, and CD are abstracted into AB and BD to facilitate subsequent calculations.
Perform trigonometric calculations.
The animateLine() function is used to draw a line segment. The input parameters are the initial coordinate point X and Y values of the line segment, the length of the line segment, the angle of the line segment, and the angle deviation.
Initialize the link segment to be moved.
Initialize the text to be displayed.
Animate() is the animation function.
In the Animate() function, call the planeLinkageReverse() function to get the rotation angle of the A and B servos. Note that there is a variable i, which is used to make animations, which will be explained later.
Call the animateLine() function to draw the line segment.
Apply animation to linkage.
Show some key information on the link.
Text application animation.
Note here that there must be a',' after the last return value, do not delete.
Create a new artboard for animation drawing.
Use animation to define range() to determine the range of animation variables.
Display simulation motion model.
21.3 Running the linkageR.py program on the PC
On your PC, find the folder 09linkageR that you downloaded in step 21.2, copy the linkageR.py program inside to the C drive directory, and double-click to open the linkageR.py program inside. Make sure you have downloaded in accordance with “Lesson 4 Downloading and Installing Python".
1. First open the cmd command window on the PC:
2. In the opened command window, enter the command to install matplotlib:
pip install matplotlib
3. After the installation is complete, you can use the mouse to double-click to run linkageR.py, and then you will see the following figure, which is the plane link simulation of matplotlib.