Lab Manual, Robotics
Lab Manual, Robotics
LAB
Submitted By :
Reg No : 20-ME-
Introduction
This Lab manual has been intended for a course of Robotics taught at Department of Mechatronics
Engineering, University of Engineering & Technology, Taxila. The manual is designed to aid the
teacher in effectively teaching the concepts related to Laboratory as well as helping the students in
conveniently preparing Lab reports. Every effort has been made to ensure correctness and up-to-date
content. However, if any deficiency is found, that may immediately be reported to the concerned
teacher.
2
ROBOTICS, LAB MANUAL
Outcomes Assessed
CLO Learning
Course Learning Outcomes (CLOs) PLOs
# Level
Design an algorithm for a robotic system to meet
1
kinematic/dynamic requirements. C5 3
Utilize modern tools for modeling and simulation of the robotic
2 C5 5
system.
3
ROBOTICS, LAB MANUAL
4
ROBOTICS, LAB MANUAL
5
ROBOTICS, LAB MANUAL
LAB. # 01
1 Title
Introduction to MATLAB, its environments and installation
1.1 Objective
To install and activate MATLAB.
To introduce MATLAB and its environments and working.
The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide
easy access to matrix software developed by the LINPACK (linear system package) and EISPACK
(Eigen system package) projects.
6
ROBOTICS, LAB MANUAL
The Command Window is where the user type MATLAB Commands and expressions at the prompt
>> and where the output of those commands are displayed.
MATLAB defines the workspace as the set of variables that the user creates in a work session. The
Workspace Browser shows these variables and some information about them. Double Clicking on a
variable in the workspace browser launches the Array Editor, which can be used to obtain information
and, in some instances, edit certain properties of the variable
The Current Directory tab above the workspace tab shows the contents of the current directory, whose
path is shown in the Current Directory Window. For example, in the windows operating system the
path might be as follows:
The Command history Window contains a record of the commands a user has entered in the command
window, including both current and previous MATLAB sessions. Previously entered MATLAB
commands can be selected and re-executed from the command history window by right clicking on a
command or sequence of commands. This action launches a menu from which to select various
options in addition to executing the commands.
Getting Help:
The principal way to get help is to use the MATLAB Help Browser, opened as a separate window
either by clicking on the question mark symbol (?) on the desktop toolbar, or by typing help browser
at the prompt in the Command Window
[MATLAB help browser will be opened where you can get information about various topics]
The Help Browser consists of two panes, the help navigator pane, used to find information, and the
7
ROBOTICS, LAB MANUAL
Search field also exists on the navigator pane where you can type either specific words, function
names or any phrase to get information which you are looking for:
Another way to get help for a specific function is by typing doc followed by the function name at the
command prompt. [e.g. typing doc format followed displays documentation for the function called
format in the display pane of the Help Browser]
Online help is available from the MATLAB prompt (a double arrow), both generally (listing all
available commands):
>> help
[A long list of help topics follows]
and for specific commands:
[A help message on the fft function follows. It contains both the H1 line and the help text for fft
function].
The answer to the most popular question concerning any program is this: leave a MATLAB session
by typing ‘quit’ or by typing ‘exit’ to the MATLAB prompt.
The MATLAB editor is both a text editor specialized for creating M-files and a graphical MATLAB
debugger. The editor can appear in the window by itself, or it can be a sub window in the desktop. M-
files are denoted by the extension .m as in introlab.m.
The MATLAB editor window has numerous pull-down menus for tasks such as saving, viewing and
debugging files. Because it performs some simple checks and uses colors to differentiate between
various elements of code, this text editor is recommended as the tool of choice for writing and editing
M-Functions
8
ROBOTICS, LAB MANUAL
To open the editor type, edit at the prompt in the command window.
M- Functions have two types of information that can be displayed by the user.
The first is called the H1 line, which contains the function name and the one-line description.
The second is a block of explanation called the Help text block.
9
ROBOTICS, LAB MANUAL
LAB. # 02
2 Title
Basic mathematics and loops in MATLAB
2.1 Objective
Use of vectors and Matrix in MATLAB
Use of conditions & loops in MATLAB
This is the basic introduction to MATLAB. Creation of vectors includes a few basic operations.
Topics include the following:
Defining a vector
Accessing elements within a vector
Defining a Vector
MATLAB is a software package that makes it easier for you to enter matrices and vectors to
manipulate them. The interface follows a language that is designed to look a lot like the notation used
in linear algebra.
Almost all of MATLAB 's basic commands revolve around the use of vectors. A vector is defined by
placing a sequence of numbers within square braces:
>> v = [9 8 3 4 25 -45 6]
v=9 8 3 4 25 -45 6
This creates a row vector which has the label "v". The first entry in the vector is a 9 and the second
entry is a 8. Note that MATLAB printed out a copy of the vector after you hit the enter key. If you do
not want to print out the result, put a semi-colon at the end of the line:
10
ROBOTICS, LAB MANUAL
>> v
v=
9 8 3 4 25 -45 6
Notice, though, that this always creates a row vector. If you want to create a column vector you need
to take the transpose of a row vector. The transpose is defined using an apostrophe (`):
v=
9
8
3
4
25
-45
6
To access block of elements, we use MATLAB’s colons notation, e.g. to access the first three
elements of v we write.
>> v (1:3)
ans =
9 8 3
11
ROBOTICS, LAB MANUAL
8 3 4
Similarly, to access all elements from some specific location say the 3 rd through the last element.
>> v (:)
Produces a column vector.
v = 9
8
3
4
25
-45
6
12
ROBOTICS, LAB MANUAL
A common task is to create a large vector with numbers that fit a repetitive pattern. MATLAB can
define a set of numbers with a common increment using colons. For example, to define a vector
whose first entry is 1, the second entry is 2, the third is three, up to 8 you enter the following:
>> v = [1:8]
v=
1 2 3 4 5 6 7 8
If you wish to use an increment other than one, you must define the start number, the value of the
increment, and the last number. For example, to define a vector that starts with 2 and ends in 4 with
steps of .25 you enter the following:
>> v = [2:.25:4]
v= Columns 1 through 7
2.0000 2.2500 2.5000 2.7500 3.0000 3.2500 3.5000 Columns 8 through 9
3.7500 4.0000
You can view individual entries in this vector. For example, to view the first entry just type in the
following:
>> v(1)
ans =
2
This command prints out entry 1 in the vector. Also notice that a new variable called ANS has been
created. Any time you perform an action that does not include an assignment MATLAB will put the
label ANS on the result.
A vector can be used as an index into another vector. For example, we can pick the first, third sixth
and seventh elements of v using the command.
13
ROBOTICS, LAB MANUAL
v=
3 7 6 4
A basic introduction to defining and manipulating matrices is given here. It is assumed that you know
the basics on how to define and manipulate vectors using MATLAB.
Defining Matrices
Defining a matrix is like defining a vector. To define a matrix, you can treat it like a column of row
vectors (note that the spaces are required!):
>> A = [1 7 8; 9 4 2; 6 3 5]
A=
1 7 8
9 4 2
6 3 5
14
ROBOTICS, LAB MANUAL
If you have been putting in variables through this and the tutorial on vectors, then you probably have
a lot of variables defined. If you lose track of what variables you have defined, the WHOS command
will let you know all of the variables you have in your workspace.
>> whos
Name Size Bytes Class
A 3x3 72 double array
B 3x3 72 double array
v 1x5 40 double array
The grand total is 23 elements using 184 bytes.
You can work with different parts of a matrix, just as you can with vectors. Again, you have to be
careful to make sure that the operation is legal.
To extract the element in the 2nd row and 3rd column we write,
>> A (2,3)
ans =
2
The colon operator is used in the matrix indexing to select a two-dimensional block of elements out
of matrix:
>> C= A (: , 3) % this statement picks the third column of the matrix
C=
8
2
5
The colon operator can also be used in the matrix indexing to extract the row of the matrix
>> C= A (2, :) % this statement picks the 2nd row of the matrix
C=
9 4 2
>> A (1:2,2:3)
ans =
15
ROBOTICS, LAB MANUAL
7 8
4 2
To create a matrix B equal to A but with its last column set to 0’s we write
> B = A;
> B (:, 3) = 0
B=
1 7 0
9 4 0
6 3 0
Now if we write.
>> A(E)
ans =
1
2
16
ROBOTICS, LAB MANUAL
The use of a colon is useful in finding the sum of all elements of a matrix.
>> s = sum (A (:))
s=
45 Some important standard arrays are:
For example,
>> A = 5 * ones (3, 3)
A=
5 5 5
5 5 5
5 5 5
Finally, sometimes you would like to clear all your data and start over. You do this with the "clear"
command. Be careful though, it does not ask you for a second opinion and its results are final.
>> clear
This group of control statements enables you to select at run-time which block of code is executed. To
make this selection based on whether a condition is true or false, use the ‘if’ statement (which may
include else or elseif). To select from a number of possible options depending on the value of an
expression, use the switch and case statements.
if evaluates a logical expression and executes a group of statements based on the value of the
expression. In its simplest form, its syntax is
if logical_expression
statements
17
ROBOTICS, LAB MANUAL
end
If the logical expression is true (1), MATLAB executes all the statements between the if and end
lines. It resumes execution at the line following the end statement. If the condition is false (0),
MATLAB skips all the statements between the if and end lines and resumes execution at the line
following the end statement.
For example,
You can nest any number of if statements.
Switch executes certain statements based on the value of a variable or expression. Its basic form is
switch expression (scalar or string)
case value1
18
ROBOTICS, LAB MANUAL
Loops
With loop control statements, you can repeatedly execute a block of code, looping back through
the block while keeping track of each iteration with an incrementing index variable. Use the “for
statement” to loop a specific number of times.
For Loop
The for loop executes a statement or group of statements a predetermined number of times.
Its syntax is
for index = initial: increment: final
statements
end
The default increment is 1. You can specify any increment, including a negative one. For positive
indices, execution terminates when the value of the index exceeds the end value; for negative
increments, it terminates when the index is less than the end value.
for n = 2:6
x(n) = 2 * x(n - 1);
end
for m = 1:5
for n = 1:100
A(m, n) = 1/(m + n - 1);
end
end
while Loop
The while loop executes a statement or group of statements repeatedly as long as the controlling
expression is true (1).
Its syntax is
while expression
statements
end
19
ROBOTICS, LAB MANUAL
If the expression evaluates to a matrix, all its elements must be 1 for execution to continue. To reduce
a matrix to a scalar value, use all and any functions.
For example, this while loop finds the first integer n for which n! (n factorial) is a 100-digit number.
And n = 1.
Continue
The continue statement passes control to the next iteration of the ‘for’ or ‘while’ loop in which it
appears, skipping any remaining statements in the body of the loop. The example below shows a
continue loop that counts the lines of code in the file, skipping all blank lines and comments.
Break
The break statement terminates the execution of a “for loop” or while loop. When a break statement
is encountered, execution continues with the next statement outside of the loop. In nested loops, break
exits from the innermost loop only.
The example below shows a while loop that reads the contents of the file fft.m into a MATLAB
character array. A break statement is used to exit the while loop when the first empty line is
encountered.
The resulting character array contains the M-file help for the fft program.
fid = fopen('fft.m', 'r');
s = '';
20
ROBOTICS, LAB MANUAL
Task:
Write a MATLAB code of Factorial Program with the help of loop Constructs (Use For or While).
MATLAB CODE:
21
ROBOTICS, LAB MANUAL
LAB. # 03
3 Title
Introduction to Simulink and Simscape library in MATLAB
3.1 Objective
Explore elements of Simulink and Simscape library and get used to it.
22
ROBOTICS, LAB MANUAL
LAB. # 04
4 Title
Design a Robotic manipulator (rigid body tree) in Simulink (MATLAB)
4.1 Objective
Design a two-link manipulator (rigid body tree) having revolute joints and an end effector with fix
joint.
2. Access SimScape Toolbox: Ensure that SimScape Toolbox is installed and accessible in your
MATLAB environment.
3. Create a New Simulink Model: Open a new Simulink model by selecting "File" -> "New" ->
"Model".
9. Run the Simulation: Run the Simulink model to visualize the robot's structure and verify its
correctness.
10. Debug and Iterate: If necessary, debug any errors or misalignments in the model, and iterate on
the design until the desired robotic manipulator is achieved.
23
ROBOTICS, LAB MANUAL
24
ROBOTICS, LAB MANUAL
25
ROBOTICS, LAB MANUAL
26
ROBOTICS, LAB MANUAL
LAB. # 05
5 Title
Design forward kinematics to simulate the Robot movement in Simulink (MATLAB)
5.1 Objective
Attach forward kinematics on previously designed robot to control the joints and simulate the robot
using sinusoid.
To simulate the robot and perform forward kinematics based on the provided instructions, follow
these steps:
Start by creating a new Simulink model where you will simulate the robot and perform forward
kinematics.
Use an m-file or script to import the robot model defined in Simulink, which contains the SimScape
model of the robot we Made in Lab 4.
dof2_arm = importrobot('Rigidbodytree');
Joint 1: Set limits (`-90` to `90` degrees), actuation (motion provided by input), and sensing
(position).
Joint 2: Set limits (`-60` to `60` degrees), actuation (motion provided by input), and sensing
(position).
27
ROBOTICS, LAB MANUAL
Then we make Subsystem of whole this Robot. And named that as ROBOT.
Use sine wave generators to produce motion commands for each joint (`q1` for Joint 1 and `q2` for
Joint 2), respecting the joint limits.
Get Transform Block: Use the "Get Transform" block from the Robotic System Toolbox.
Configure the block with the rigid body tree (`dof2_arm`) and joint configuration signals
(`[q1, q2]`).
Coordinate Transformation Block: Use a coordinate transformation block to extract the
end effector position from the homogeneous transformation matrix.
7. Run Simulation:
Run the Simulink simulation to observe the robot's motion and the calculated end effector position
based on the provided joint commands.
8. Analysis:
Analyze the simulation results from the scope to verify that the robot follows the desired joint
motions and to observe the end effector's position.
9. Further Development:
Experiment with different trajectories, joint configurations, and robotic algorithms using the
Robotic System Toolbox and Simulink to explore more complex behaviors and functionalities.
28
ROBOTICS, LAB MANUAL
29
ROBOTICS, LAB MANUAL
30
ROBOTICS, LAB MANUAL
LAB. # 06
6 Title
Design inverse kinematics to move the robot on predefined trajectory in Simulink (MATLAB)
6.1 Objective
Attach inverse kinematics on previously designed robot to calculate the joints angles from the
coordinate values and simulate the robot by defining the trajectory using signal builder.
To implement the described process of using inverse kinematics to move a robot on a predefined
trajectory in Simulink (MATLAB), follow this step-by-step design.
Ensure you have a rigid body tree (`DOF2_arm`) representing your robot's kinematic structure,
defining its joint configuration and end effector.
Load this tree into your MATLAB workspace for use in Simulink.
Create or open a Simulink model where you'll implement the trajectory generation and
inverse kinematics.
Include necessary blocks and components for signal generation, inverse kinematics, and
visualization.
3. Trajectory Generation:
Use a Signal Builder block to generate the trajectory defining the end effector's path.
Configure the Signal Builder to produce custom signals for X, Y, and Z coordinates of the
end effector over time.
31
ROBOTICS, LAB MANUAL
Connect the output of the Coordinate Transformation block to the Inverse Kinematics block.
7. Visualizing Results:
Use a Demux block to separate the joint angle outputs from the Inverse Kinematics
block.
Implement a Forward Kinematics block to calculate and display the resulting end
effector positions.
Run the simulation to observe the robot's movement along the predefined square
trajectory.
32
ROBOTICS, LAB MANUAL
33
ROBOTICS, LAB MANUAL
34
ROBOTICS, LAB MANUAL
LAB. # 07
7 Title
Design the trajectory using Signal editor for inverse kinematics of the robot in Simulink (MATLAB)
7.1 Objective
Use signal editor to design the input trajectory of the inverse kinematics on previously designed robot
to calculate the joints angles from the coordinate values and simulate the robot.
35
ROBOTICS, LAB MANUAL
LAB. # 08
8 Title
Design the modified robot (Mid Evaluation task)
8.1 Objective
36