Index>Robot Kit>RaspArm-S Kit for RPi>Lesson 21 Simulating a Plane Link with Matplotlib

Lesson 21 Simulating a Plane Link with Matplotlib

2059

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 buttonimage.png 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.

image.png 


Import matplotlib.

image.png 


Import the library for animation.

image.png 


Set the length of AB to 70, the length of BC to 150.0, and the length of CD to 10.

image.png 


Define the initial position deviation of the robotic arm.

image.png 


Define the text size displayed here.

image.png 


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.

image.png 


The array linkageLenCtrl composed of the lengths of links AB and BC.

image.png 


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.

image.png 


In the planeLinkageReverse() function, first calculate the incremental error, and use debugPos to correct the initial position error. Generally, debugPos is [0,0].

image.png 


Used to calculate the angle error between the end point of the link and the rotation axis of the servo B.

image.png 


The input links AB, BC, and CD are abstracted into AB and BD to facilitate subsequent calculations.

image.png 


Perform trigonometric calculations.

image.png 


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.

image.png 


Initialize the link segment to be moved.

image.png 


Initialize the text to be displayed.

image.png 


Animate() is the animation function.

image.png 


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.

image.png 


Call the animateLine() function to draw the line segment.

image.png 


Apply animation to linkage.

image.png 


Show some key information on the link.

image.png 


Text application animation.

image.png 


Note here that there must be a',' after the last return value, do not delete.

image.png 


Create a new artboard for animation drawing.

image.png 


Use animation to define range() to determine the range of animation variables.

image.png 


Display simulation motion model.

image.png 


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:

image.png 


2. In the opened command window, enter the command to install matplotlib:

pip  install  matplotlib


image.png 


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.

image.png