?? invkine_codepad.html
字號:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Modeling Inverse Kinematics in a Robotic Arm</title> <meta name="generator" content="MATLAB 7.1"> <meta name="date" content="2005-06-24"> <meta name="m-file" content="invkine_codepad"> <link rel="stylesheet" type="text/css" href="../../../matlab/demos/private/style.css"> </head> <body> <div class="header"> <div class="right"><a href="matlab:invkine">Run in the Command Window</a></div> </div> <div class="content"> <h1>Modeling Inverse Kinematics in a Robotic Arm</h1> <introduction> <p>This demo illustrates using a fuzzy system to model the inverse kinematics in a two-joint robotic arm.</p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">What is Inverse Kinematics?</a></li> <li><a href="#3">Why use Fuzzy logic?</a></li> <li><a href="#4">Overview of Fuzzy Solution</a></li> <li><a href="#5">What is ANFIS?</a></li> <li><a href="#6">Data Generation</a></li> <li><a href="#12">Building ANFIS networks</a></li> <li><a href="#14">Validating the ANFIS networks</a></li> <li><a href="#20">Building a solution around the trained ANFIS networks</a></li> <li><a href="#22">Conclusion</a></li> <li><a href="#23">Glossary</a></li> </ul> </div> <h2>What is Inverse Kinematics?<a name="1"></a></h2> <p>Kinematics is the science of motion. In a two-joint robotic arm, given the angles of the joints, the kinematics equations give the location of the tip of the arm. Inverse kinematics refers to the reverse process. Given a desired location for the tip of the robotic arm, what should the angles of the joints be so as to locate the tip of the arm at the desired location. There is usually more than one solution and can at times be a difficult problem to solve. </p> <p>This is a typical problem in robotics that needs to be solved to control a robotic arm to perform tasks it is designated to do. In a 2-dimensional input space, with a two-joint robotic arm and given the desired co-ordinate, the problem reduces to finding the two angles involved. The first angle is between the first arm and the ground (or whatever it is attached to). The second angle is between the first arm and the second arm. </p> <p><img vspace="5" hspace="5" src="invkine_angles.png"> </p> <p><b>Figure 1:</b> Illustration showing the two-joint robotic arm with the two angles, <tt>theta1</tt> and <tt>theta2</tt></p> <h2>Why use Fuzzy logic?<a name="3"></a></h2> <p>For simple structures like the two-joint robotic arm, it is possible to mathematically deduce the angles at the joints given the desired location of the tip of the arm. However with more complex structures (eg: n-joint robotic arms operating in a 3-dimensional input space) deducing a mathematical solution for the inverse kinematics may prove challenging. </p> <p>Using fuzzy logic, we can construct a Fuzzy Inference System that deduces the inverse kinematics if the forward kinematics of the problem is known, hence sidestepping the need to develop an analytical solution. Also, the fuzzy solution is easily understandable and does not require special background knowledge to comprehend and evaluate it. </p> <p>In the following section, a broad outline for developing such a solution is described, and later, the detailed steps are elaborated.</p> <h2>Overview of Fuzzy Solution<a name="4"></a></h2> <p>Since the forward kinematics formulae for the two-joint robotic arm are known, x and y co-ordinates of the tip of the arm are deduced for the entire range of angles of rotation of the two joints. The co-ordinates and the angles are saved to be used as training data to train ANFIS (Adaptive Neuro-Fuzzy Inference System) network. </p> <p>During training the ANFIS network learns to map the co-ordinates <tt>(x,y)</tt> to the angles <tt>(theta1, theta2)</tt>. The trained ANFIS network is then used as a part of a larger control system to control the robotic arm. Knowing the desired location of the robotic arm, the control system uses the trained ANFIS network to deduce the angular positions of the joints and applies force to the joints of the robotic arm accordingly to move it to the desired location. </p> <h2>What is ANFIS?<a name="5"></a></h2> <p>ANFIS stands for Adaptive Neuro-Fuzzy Inference System. It is a hybrid neuro-fuzzy technique that brings learning capabilities of neural networks to fuzzy inference systems. The learning algorithm tunes the membership functions of a <a href="matlab:helpview([docroot,'/toolbox/fuzzy/fuzzy.map'],'sugeno_type_fis')">Sugeno-type Fuzzy Inference System</a> using the training input-output data. </p> <p>In this case, the input-output data refers to the "coordinates-angles" dataset. The coordinates act as input to the ANFIS and the angles act as the output. The learning algorithm "teaches" the ANFIS to map the co-ordinates to the angles through a process called training. At the end of training, the trained ANFIS network would have learned the input-output map and be ready to be deployed into the larger control system solution. </p> <h2>Data Generation<a name="6"></a></h2> <p>Let <tt>theta1</tt> be the angle between the first arm and the ground. Let <tt>theta2</tt> be the angle between the second arm and the first arm (Refer to Figure 1 for illustration). Let the length of the first arm be <tt>l1</tt> and that of the second arm be <tt>l2</tt>. </p> <p>Let us assume that the first joint has limited freedom to rotate and it can rotate between 0 and 90 degrees. Similarly, assume that the second joint has limited freedom to rotate and can rotate between 0 and 180 degrees. (This assumption takes away the need to handle some special cases which will confuse the discourse). Hence, <tt>0<=theta1<=pi/2</tt> and <tt>0<=theta2<=pi</tt>. </p> <p><img vspace="5" hspace="5" src="invkine_all_angles.png"> </p> <p><b>Figure 2:</b> Illustration showing all possible <tt>theta1</tt> and <tt>theta2</tt> values. </p> <p>Now, for every combination of <tt>theta1</tt> and <tt>theta2</tt> values the x and y coordinates are deduced using forward kinematics formulae. </p> <p>The following code snippet shows how data is generated for all combination of <tt>theta1</tt> and <tt>theta2</tt> values and saved into a matrix to be used as training data. The reason for saving the data in two matrices is explained in the following section. </p><pre class="codeinput">l1 = 10; <span class="comment">% length of first arm</span>l2 = 7; <span class="comment">% length of second arm</span>theta1 = 0:0.1:pi/2; <span class="comment">% all possible theta1 values</span>theta2 = 0:0.1:pi; <span class="comment">% all possible theta2 values</span>[THETA1, THETA2] = meshgrid(theta1, theta2); <span class="comment">% generate a grid of theta1 and theta2 values</span>X = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2); <span class="comment">% compute x coordinates</span>Y = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2); <span class="comment">% compute y coordinates</span>data1 = [X(:) Y(:) THETA1(:)]; <span class="comment">% create x-y-theta1 dataset</span>data2 = [X(:) Y(:) THETA2(:)]; <span class="comment">% create x-y-theta2 dataset</span></pre><p><a href="matlab:edit('traininv')">Click here for unvectorized code</a></p> <p>The following plot shows all the X-Y data points generated by cycling through different combinations of <tt>theta1</tt> and <tt>theta2</tt> and deducing x and y co-ordinates for each. The plot can be generated by using the code-snippet shown below. The plot is illustrated further for easier understanding. </p><pre> plot(X(:), Y(:), 'r.'); axis equal; xlabel('X') ylabel('Y') title('X-Y co-ordinates generated for all theta1 and theta2 combinations using forward kinematics formulae')</pre><p><img vspace="5" hspace="5" src="invkine_grid2.png"> </p> <p><b>Figure 3:</b> X-Y co-ordinates generated for all <tt>theta1</tt> and <tt>theta2</tt> combinations using forward kinematics formulae </p> <h2>Building ANFIS networks<a name="12"></a></h2> <p>One approach to building an ANFIS solution for this problem, is to build two ANFIS networks, one to predict <tt>theta1</tt> and the other to predict <tt>theta2</tt>. </p> <p>In order for the ANFIS networks to be able to predict the angles they have to be trained with sample input-output data. The first ANFIS network will be trained with X and Y coordinates as input and corresponding <tt>theta1</tt> values as output. The matrix <tt>data1</tt> contains the <tt>x-y-theta1</tt> dataset required to train the first ANFIS network. Therefore <tt>data2</tt> will be used as the dataset to train the first ANFIS network. </p> <p>Similarly, the second ANFIS network will be trained with X and Y coordinates as input and corresponding <tt>theta2</tt> values as output. The matrix <tt>data2</tt> contains the <tt>x-y-theta2</tt> dataset required to train the second ANFIS network. Therefore <tt>data1</tt> will be used as the dataset to train the second ANFIS network. </p> <p><tt>anfis</tt> is the function that is used to train an ANFIS network. There are several syntaxes to the function. If called with the following syntax, <tt>anfis</tt> automatically creates a Sugeno-type FIS and trains it using the training data passed to the function. </p><pre class="codeinput">anfis1 = anfis(data1, 7, 150, [0,0,0,0]); <span class="comment">% train first ANFIS network</span>anfis2 = anfis(data2, 6, 150, [0,0,0,0]); <span class="comment">% train second ANFIS network</span></pre><p>The first parameter to <tt>anfis</tt> is the training data, the second parameter is the number of membership functions used to characterize each input and output, the third parameter is the number of training <a href="#23">epochs</a> and the last parameter is the options to display progress during training. The values for number of epochs and the number of membership functions have been arrived at after a fair amount of experimentation with different values. </p> <p>The toolbox comes with GUI's that helps build and experiment with ANFIS networks.</p> <p><tt>anfis1</tt> and <tt>anfis2</tt> represent the two trained ANFIS networks that will be deployed in the larger control system. </p> <p>Once the training is complete, the two ANFIS networks would have learned to approximate the angles (<tt>theta1, theta2</tt>) as a function of the coordinates (<tt>x, y</tt>). One advantage of using the fuzzy approach is that the ANFIS network would now approximate the angles for coordinates that are similar but not exactly the same as it was trained with. For example, the trained ANFIS networks are now capable of approximating the angles for coordinates that lie between two points that were included in the training dataset. This will allow the final controller to move the arm smoothly in the input space. </p> <p>We now have two trained ANFIS networks which are ready to be deployed into the larger system that will utilize these networks to control the robotic arms. </p> <h2>Validating the ANFIS networks<a name="14"></a></h2> <p>Having trained the networks, an important follow up step is to validate the networks to determine how well the ANFIS networks would perform inside the larger control system. </p> <p>Since this demo problem deals with a two-joint robotic arm whose inverse kinematics formulae can be derived, it is possible to test the answers that the ANFIS networks produce with the answers from the derived formulae. </p> <p>Let's assume that it is important for the ANFIS networks to have low errors within the operating range <tt>0<x<2</tt> and <tt>8<y<10</tt>. </p><pre class="codeinput">x = 0:0.1:2; <span class="comment">% x coordinates for validation</span>y = 8:0.1:10; <span class="comment">% y coordinates for validation</span></pre><p>The <tt>theta1</tt> and <tt>theta2</tt> values are deduced mathematically from the x and y coordinates using inverse kinematics formulae. </p><pre class="codeinput">[X, Y] = meshgrid(x,y);c2 = (X.^2 + Y.^2 - l1^2 - l2^2)/(2*l1*l2);s2 = sqrt(1 - c2.^2);THETA2D = atan2(s2, c2); <span class="comment">% theta2 is deduced</span>k1 = l1 + l2.*c2;k2 = l2*s2;THETA1D = atan2(Y, X) - atan2(k2, k1); <span class="comment">% theta1 is deduced</span></pre><p><a href="matlab:edit('traininv')">Click here for unvectorized code</a></p> <p><tt>THETA1D</tt> and <tt>THETA2D</tt> are the variables that hold the values of <tt>theta1</tt> and <tt>theta2</tt> deduced using the inverse kinematics formulae. </p> <p><tt>theta1</tt> and <tt>theta2</tt> values predicted by the trained anfis networks are obtained by using the command <tt>evalfis</tt> which evaluates a FIS for the given inputs. </p> <p>Here, <tt>evalfis</tt> is used to find out the FIS outputs for the same x-y values used earlier in the inverse kinematics formulae. </p><pre class="codeinput">XY = [X(:) Y(:)];THETA1P = evalfis(XY, anfis1); <span class="comment">% theta1 predicted by anfis1</span>THETA2P = evalfis(XY, anfis2); <span class="comment">% theta2 predicted by anfis2</span></pre><p>Now, we can see how close the FIS outputs are with respect to the deduced values.</p><pre class="codeinput">theta1diff = THETA1D(:) - THETA1P;theta2diff = THETA2D(:) - THETA2P;subplot(2,1,1);plot(theta1diff);ylabel(<span class="string">'THETA1D - THETA1P'</span>)title(<span class="string">'Deduced theta1 - Predicted theta1'</span>)subplot(2,1,2);plot(theta2diff);ylabel(<span class="string">'THETA2D - THETA2P'</span>)title(<span class="string">'Deduced theta2 - Predicted theta2'</span>)</pre><img vspace="5" hspace="5" src="invkine_codepad_01.png"> <p>The errors are in the <tt>1e-3</tt> range which is a fairly good number for the application it is being used in. However this may not be acceptable for another application, in which case the parameters to the <tt>anfis</tt> function may be tweaked until an acceptable solution is arrived at. Also, other techniques like input selection and alternate ways to model the problem may be explored. </p> <h2>Building a solution around the trained ANFIS networks<a name="20"></a></h2> <p>Now given a specific task, such as robots picking up an object in an assembly line, the larger control system will use the
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -