You are on page 1of 14

Matlab Tutorial Handout

Visual Servo Control / Prof. Ricky Lee / TA Christian Ivancsits


Intelligent Control Systems / Prof. Ricky Lee / TA Chang Tzu-Yuan
Version 1.0 – 03/2010
Christian Ivancsits
Table of Contents
1 Matlab in General............................................................................................................................................................ 1
1.1 Matlab Help / Documentation .......................................................................................................................... 1
1.2 Matlab Editor Shortcuts ...................................................................................................................................... 1
2 Matlab Visualization tools ........................................................................................................................................... 2
2.1 Figures in Matlab ................................................................................................................................................... 2
2.2 Matlab Figure- and Plot Functions ................................................................................................................. 2
2.2.1 Figures and Axes .......................................................................................................................................... 2
2.2.2 Axes Scaling .................................................................................................................................................... 2
2.2.3 Plot styles ........................................................................................................................................................ 2
2.2.4 Line Styles ....................................................................................................................................................... 2
2.2.5 Plot Annotations ........................................................................................................................................... 3
2.3 Examples: Figure- and Plot Functions .......................................................................................................... 3
3 Matlab Image Processing Toolbox ........................................................................................................................... 4
3.1 Images in Matlab .................................................................................................................................................... 4
3.2 Image Processing Toolbox Functions ............................................................................................................ 4
3.2.1 Image I/O and Displaying ......................................................................................................................... 4
3.2.2 Spatial Transformations ............................................................................................................................ 4
3.2.3 Color- and Type-Conversion.................................................................................................................... 4
3.2.4 Image Adjustment ........................................................................................................................................ 4
3.2.5 Linear Filters .................................................................................................................................................. 5
3.2.6 Morphological Operations ........................................................................................................................ 5
3.2.7 Labeling Objects ........................................................................................................................................... 5
3.3 Examples: Image Processing ............................................................................................................................. 5
4 Matlab Image Acquisition Toolbox.......................................................................................................................... 6
4.1 Image Acquisition Toolbox Functions ........................................................................................................... 6
4.1.1 Create and modify video input object.................................................................................................. 6
4.1.2 Display preview of video stream ........................................................................................................... 6
4.1.3 Camera dependent options ...................................................................................................................... 6
4.1.4 Camera trigger options .............................................................................................................................. 6
4.1.5 Image capturing ............................................................................................................................................ 7
4.2 Examples: Image Acquisition ............................................................................................................................ 7

I|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

5 Matlab Aria Interface .................................................................................................................................................... 8


5.1 Matlab Aria Interface Functions ...................................................................................................................... 8
5.1.1 Connect to the robot or mobileSim ....................................................................................................... 8
5.1.2 Sonar sensor commands ........................................................................................................................... 8
5.1.3 Robot pose commands ............................................................................................................................... 8
5.1.4 Motion parameter setup ........................................................................................................................... 8
5.1.5 Motion commands – velocity .................................................................................................................. 9
5.1.6 Motion commands – distance / orientation ...................................................................................... 9
5.2 Examples: Matlab Aria Interface ..................................................................................................................... 9
6 Matlab IPC Interface ................................................................................................................................................... 10
6.1 Matlab IPC Interface Functions ..................................................................................................................... 10
6.1.1 Connect to the central IPC server ....................................................................................................... 10
6.1.2 Robot pose ................................................................................................................................................... 10
6.1.3 Obstacle positions ..................................................................................................................................... 10
6.1.4 Image of the test area (ceiling camera)............................................................................................ 10
6.2 Examples: Matlab IPC Interface .................................................................................................................... 10
7 Appendix: Example Scripts for Matlab ............................................................................................................... 11

II | P a g e
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

Matlab Tutorial Handout


Visual Servo Control / Prof. Ricky Lee / TA Christian Ivancsits
Intelligent Control Systems / Prof. Ricky Lee / TA Chang Tzu-Yuan
Version 1.0 – 03/2010
Christian Ivancsits

This document is not intended to be an introduction to Matlab, but is an (incomplete) collection of


convenient and most frequently required functions of several Matlab Toolboxes.

The listed functions should help to find the adequate command for a given problem. However it is
up to the user to refer to the Matlab help to get more detailed information, since here only the
function name and the standard parameters are provided.

The second part of this handout contains several Matlab scripts which demonstrate the use of most
mentioned functions.

1 Matlab in General
This document is based on Matlab version 7.5.0 (R2007b).

1.1 Matlab Help / Documentation


The Matlab help has vital information which will assist you in finding the solution of a problem.
Furthermore, Matlab is a very popular tool and there is abundant information on the internet. You
may get excellent tutorials and introductions to Matlab (at least in English language), as well as very
specific solutions for many problems.

Please refer to the Matlab help, section “Contents” for detailed information on a specific toolbox: e.g.
Matlab -> Help -> Contents -> Image Processing Toolbox -> Getting started

1.2 Matlab Editor Shortcuts


There are some shortcuts that can be used for convenience:

• “Ctrl + r” … comment line


• “Ctrl + t” … un-comment line
• F1 … place curser on keyword and press F1 to get Matlab help information
• F5 … execute the current script
• F9 … executes the high-lighted part of a script (works also with examples in
the Matlab help)
• F12 … set / delete breakpoint in current line
• “Ctrl + Enter” … Evaluate cell
1|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

2 Matlab Visualization tools


2.1 Figures in Matlab
Objects that can contain elements for data visualization in matlab are denoted figures. Each figure
can contain one or several axes, where an axes-object contains one of several types of data-
visualization, e.g. data-plots or images.

2.2 Matlab Figure- and Plot Functions


This is a small, incomplete collection of frequently used functions for visualizing data with Matlab.
Here only the function name and the standard parameters are listed, please refer to the Matlab help
for detailed information.

2.2.1 Figures and Axes


• h = figure … create figure and pass on handle
• figure(h) … set focus to the figure corresponding to the handle
• v = get(h) … get a list of all figure properties
• set(h, ‘property’, value) … set a certain figure property
• hold on / hold off … if the current figure is hold, the next plot command
will draw over the current image
• subplot(m,n,p) … create an axes-object within the current figure,
where m,p and n define the position of the object
m … number of rows
n … number of columns
p … index of the current axes
• close(h) … close the figure corresponding to the handler
• close all … close all currently open figures

2.2.2 Axes Scaling


• axis([xmin xmax ymin ymax]) … set the scaling (range) of axes
• axis ji / axis xy … places the coordinate system of the plot in the
upper left corner / lower left corner

2.2.3 Plot styles


See Matlab help “Basic plots and graphs” for a list of all plot styles.

• plot(data) … plot the values in data against the value-index


• plot(xData,yData) … plot the values in xData against the values in yData

2.2.4 Line Styles


See Matlab help “Line properties” for detailed information.

• plot(data, ‘linewidth’, val) … plot the line with the specified width
• plot(data, param) ... plot line in the specified color, param = [‘r’,’g’,’b’,’k’,’m’,’c’]
• plot(data, ‘- -‘) … plot dashed line
• plot(data, ‘-.’) … plot dash-dotted line

2|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

• plot(data, marker) … plot markers instead of a line, marker = [‘.’|’o’|’x’|’*’ ]


• plot(data, marker, ‘markersize’, val) … plot marker with the specified size
• Line style attributes can also be combined:
e.g. plot(data,'g--x','linewidth',2,'markersize',15)

2.2.5 Plot Annotations


• title(‘sting’) … add title to current axis-object
• xlabel(‘string) / ylabel(‘string’) … add label to the x- or y-axis
• legend(‘string1’, ‘string2’, …) … insert legend
• text(x, y, ‘string’) …print the specified string at the given position

• text properties are applicable to many text-related functions(see Matlab help)


e.g. title(‘sting’, ‘fontsize’, value) … set the font size of the title
• special characters
e.g. title(‘Greek letters: \alpha \beta) … plot Greek letters in the title
xlabel(‘Values of k_1, k_2, k_3’) … print index as subscript

2.3 Examples: Figure- and Plot Functions


Please see Matlab scipt “Matlab_Plotting_Functions_and_Figure_Properties.m”.

3|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

3 Matlab Image Processing Toolbox


3.1 Images in Matlab
An image is represented as a matrix, where gray scale images have the dimension “rows x cols” and
color images have the dimension “rows x cols x dim” (dim = 3 for RGB mode). Pixels of the image are
indexed as any other matrix element.

The data type of images is “uint8”. Some functions of the image processing toolbox only accept
“uint8”, whereas the standard data type of Matlab is “double”.

3.2 Image Processing Toolbox Functions


This is a small, incomplete collection of frequently used functions for image processing with Matlab.
Here only the function name and the standard parameters are listed, please refer to the Matlab help
for detailed information.

3.2.1 Image I/O and Displaying


• A = imread(filename) … read image from file
• imwrite(A, filename) … write image to file

• imshow(I) … displays image in figure


• imagesc(C) … scales displayed image in size as well as intensity-range
• colormap gray … display image in grayscale (other color-schemes are available)

• [x,y] = ginput(n) … select points from the figure using the mouse

3.2.2 Spatial Transformations


• B = imresize(A, scale) … resize image
• B = imrotate(A, angle) … rotate image
• J = imcrop(I) … interactive cropping of the image

3.2.3 Color- and Type-Conversion


• I = rgb2gray(RGB) … convert RGB image to grayscale
• hsv_image = rgb2hsv(M) … convert RGB image to HSV colormap
• YCBCR = rgb2ycbcr(RGB) … convert RGB image to YCbCr color space

• cform = makecform(type) … create color transformation structure


• B = applyform(A, cform) … apply color space transformation

• I2 = im2double(I) … convert to data type double


• I2 = im2unit8(I) … convert to data type uint8

3.2.4 Image Adjustment


• imhist(I) … display histogram of image data
• J = imadjust(I) … adjusts image intensity values (improves contrast)
• J = histeq(I) … enhance contrast using histogram equalization
4|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

3.2.5 Linear Filters


• B = imfilter(A,H) … perform filtering of images (correlation or convolution)
• h = fspecial(type) … create predefined response-matrix for imfilter (average,
gaussian, sobel …)
• BW = edge(I) … find edges in grayscale image

3.2.6 Morphological Operations


Morphology Fundamentals
• se = strel(shape, parameter) … create structuring element for erode/dilate
• IM2 = imerode(IM, se) … erodes the grayscale image IM
• IM2 = imdilate(IM, se) ... dilates the grayscale image IM
• IM2 = imopen(IM, se) ... morphological opening of the grayscale image
(opening = erosion followed by dilation)
• IM2 = imclose(IM, se) … morphological closing of the grayscale image
(closing = dilation followed by erosion)

• IM2 = imtophat(IM, se) … performs morphological top-hat filtering on the input


(corrects uneven illumination when background is dark)

Distance Transform
• D = bwdist(bw) … distance transform of binary image

• BW2 = bwmorph(BW, operation) … applies different morphological operations to the


binary image, depending on the parameter

3.2.7 Labeling Objects


• BW = im2bw(I, level) … convert to binary image, based on threshold
• level = graythresh(I) … determines global threshold value

Labeling and Measuring Objects


• L = bwlabel(BW, n) … label connected components in binary image
get corresponding indices with “[r,c] = find(bwlabel(BW)==2)”
• RGB = label2rgb(L) … convert label matrix to RGB image
• STATS = regionprops(L, properties)
… measures a set of properties for each labeled region in the label matrix L (e.g. ‘Area’,
‘Centroid’, ‘Orientation’)

3.3 Examples: Image Processing


Please see Matlab scipt “Matlab_Image_Processing_Basics.m”.

5|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

4 Matlab Image Acquisition Toolbox


The Matlab image acquisition toolbox allows capturing images from a camera (e.g. usb webcam) and
it supports many device properties.

4.1 Image Acquisition Toolbox Functions


This is a small, incomplete collection of functions for capturing camera images with Matlab. Here
only the function name and the standard parameters are listed, please refer to the Matlab help for
detailed information.

4.1.1 Create and modify video input object


• imaqhwinfo … determine camera adapter name (e.g. winvideo)
• info = imaqhwinfo(‘winvideo’) … determine device id of the camera

• source = info.DeviceIDs{“your id”} … get camera device id info


• dev_info = imaqhwinfo(‘winvideo’, source) … get camera info object
• dev_info.SupportedFormats … display supported formats

• videoFormat = dev_info.SupportedFormats(“your selection”) … choose image format


• vid = videoinput(‘winvideo’, source, videoFormat{1}) … create video input object

4.1.2 Display preview of video stream


• preview(vid) … show a preview of the camera stream
• closepreview(vid) … close the preview window

4.1.3 Camera dependent options


• get(vid) … display all properties of the video input object “vid”

• get(getselectedsource(vid)) … display all properties of the current camera

• src = getselectedsource(vid) … get handler to camera properties


• set(src, ‘property’, value) … set the value to for certain property
e.g. set(src,’FrameRate’, 30)
• inspect(src) … starts GUI for adjusting camera properties

4.1.4 Camera trigger options


• config = trigger(vid) … get info about the trigger

• triggerconfig(vid, ‘manual’) … set trigger to manual (command starts capturing)

• set(vid,’TriggerRepeat’, Inf) … allows triggering unlimited number of captures (infinit)


• set(vid,’FramesPerTrigger’, 1) … capture one frame per trigger event
• vid.FrameGrabInterval = 1 … take every frame from the video stream

6|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

4.1.5 Image capturing


• start(vid) … start the video stream
• trigger(vid) … cause a trigger event
• img = getdata(vid,1) …get one image from video object frame buffer
• stop(vid) … stop the video stream, release capturing device

4.2 Examples: Image Acquisition


Please see Matlab scipt “Matlab_Image_Acqisition_Basics.m”.

7|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

5 Matlab Aria Interface


The Matlab Aria Interface makes the functionality of the Aria software available within the Matlab
environment. The purpose of the interface is to control the sensors and motion commands of the
P3DX and Amigo robots with Matlab.

The original version was created by Luis Felipe Posada of the Technical University Dortmund. The
additional functions “robotInitWifi”, “switchSonar”, “getRobotCycleTime” and “setRobotCycleTime”
were integrated by the author of this handout.

5.1 Matlab Aria Interface Functions


This is a small, incomplete collection of functions for capturing controlling the robots P3DX and
Amigo with Matlab. Here only the function name and the standard parameters are listed, please
refer to the function definition (.m file) for detailed information. To open the script in the editor you
can place the cursor on the command and press right mouse button --> Open Selection, or press
“Ctrl+D”.

5.1.1 Connect to the robot or mobileSim


The Matlab Aria Interface can connect to the real robot as well as to the mobileSim simulator.
(Please note that the use of the Matlab IPC Interface is not possible in connection with mobileSim.)
The P3DX robot has currently the IP 140.118.38.108. The cycle time defines the rate at which data
packets are sent to the robot by the Aria software.

• robotInit() … connect to the MobileSim simulator


• robotInitWifi( ‘IP_address’ ) … connect to the mobile robot, IP_address is <string>
• getRobotCycleTime() … read the cycle time of the packet transmission to the robot
• setRobotCycleTime(cycleTime) ... set the cycle time where cycleTime [ms]
• shutdown() … shutdown robot and terminate wifi connection

5.1.2 Sonar sensor commands


• sonarSensorAngles() … returns the angles of sonar sensors
• sonarSensorPositions() … returns the positions of sonar sensors
• switchSonar(state) … switch sonar on/off where state [0,1]
• sonarRange = sonarSensorRange() … get readings of the sonar sensors

5.1.3 Robot pose commands


• setPosition(x,y,theta) … set the absolute pose which is stored inside the robot
x [mm], y [mm], theta [deg]
• pose = position() … read the current position from robot odometry (not from
camera!) where pose = [x, y, theta] in [mm, mm, deg]

5.1.4 Motion parameter setup


• setTransAccel(val) … set the translational acceleration where val [mm/sec2]
• setTransDecel(val) … set the translational deceleration where val [mm/ sec2]
• setRotAccel(val) … set the rotational acceleration where val[deg/ sec2]
• setRotDecel(val) … set the rotational deceleration where val[deg/ sec2]

8|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

5.1.5 Motion commands – velocity


• setVel(linVel) … set the linear velocity where linVel [mm/sec]
• setRotVel(rotVel) … set the rotational velocity where rotVel [deg/sec]
• setVel2(leftVel,rightVel) … set the velocity for both wheels separately [deg/sec]
• stop() … stop the robot

5.1.6 Motion commands – distance / orientation


• move(dist) … move straight forward/backward for the defined distance in [mm]
• setHeading(deg) … set heading of robot w.r.t. global coordinate system in [deg]
• setDeltaHeading(deg) … set heading relative to current robot pose in [deg]

5.2 Examples: Matlab Aria Interface


All the practical assignments in the Visual Servo Control course you can use the computer in the lab
T1-102 where all necessary packages are already installed. However if you want to install the
Matlab Aria Interface and the MobileSim simulation program you can follow the instructions in the
documents “Interface Compilation Guide.pdf” and “Matlab Aria Interface Tutorial.pdf”.

A simple example for the use of robot motion commands can be found in the Matlab script
“demoDirectMotionCommands.m” in the folder “Matlab_Aria_Interface\test”.

9|Page
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

6 Matlab IPC Interface


The Matlab IPC Interface allows receiving data messages from the server that processes the images
from the ceiling camera (pose measurement and obstacle detection). The functionality is based on
the Inter Process Communication library by Reid Simmons of the Carnegie Mellon University. The
interface was created by the author of this handout.

Please be aware that the central server has to be running on the PC that processes the image data
from the ceiling camera (IP: 140.118.38.91), before you can use the functions of the interface.

More detailed information about the IPC library can be found in


http://www.cs.cmu.edu/afs/cs/project/TCA/www/ipc/index.html.

6.1 Matlab IPC Interface Functions


The functions of the Matlab IPC Interface are listed in this section.

6.1.1 Connect to the central IPC server


• IPC_Init(‘server_IP’) … connect to the central server, where server_ip is <string>
• IPC_Disconnect() … terminate the connection to the server

6.1.2 Robot pose


• pose = IPC_get_P3DX_pose()
… Returns the most recent pose of P3DX with respect to the origin of the global coordinate
system (center of the image). The pose is [x, y, theta] in [mm, mm, rad].
Please note: This function returns the angle theta in rad.

6.1.3 Obstacle positions


• obstacles = IPC_get_Obstacle_positions()
… Returns a matrix of points which belong to an obstacle. Each row contains the coordinates
of one point [x,y] with respect the origin of the global coordinate system (center of the
image). Be aware that the image processing algorithm of the server can only detect
obstacles that have relatively high pixel intensity in the ceiling camera image (“bright
objects”).

6.1.4 Image of the test area (ceiling camera)


• img = IPC_get_Cam_image() … returns the most recent image from the ceiling camera

6.2 Examples: Matlab IPC Interface


Matlab_IPC_Interface_Examples.m

10 | P a g e
Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST

7 Appendix: Example Scripts for Matlab

a) Matlab_Plotting_Functions_and_Figure_Properties.m
b) Matlab_Image_Processing_Basics.m
c) Matlab_Image_Acquisition_Basics.m
d) Matlab_Aria_Interface_Motion_Commands.m
e) Matlab_IPC_Interface_Example.m

11 | P a g e

You might also like