You are on page 1of 6

MATLAB K3 Simulator

ECE3085 Introduction to Systems and Controls


Jean-Pierre de la Croix Last Updated: January 30, 2012

Manual
This manual is going to be your resource for using the K3 Simulator in the homeworks and projects for this course. It will be updated throughout the semester, so make sure to check it on a regular basis. You can access it anytime from T-Square under ECE3085/Resources.

Installation
Download MatlabK3Sim.zip from T-Square under ECE3085/Resources. Make sure to download a new copy of the simulator before you start a new homework, or whenever an announcement is made that a new version is available. It is important to stay up-to-date, since new versions may contain important bug xes or features required for the homeworks and projects. Unzip the .zip le to any directory.

Requirements
You will need a reasonably modern computer to run the K3 simulator. While the simulator will run on hardware older than a Pentium 4, it will probably be a very slow experience. You will also need a copy of MATLAB. The simulator has been tested with MATLAB R2009a, so it is recommended that you use that version or higher.

Bug Reporting
If you run into a bug (issue) with the K3 simulator, please contact jdelacroix@gatech.edu with a detailed description. The bug will get xed and an announcement will be made that a new version of the K3 simulator is available on T-Square.

Mobile Robot
The mobile robot platform you will be using in the homeworks and projects is the Khepera III (K3) mobile robot. The K3 is equipped with 11 infrared (IR) range sensors, of which nine are located in a ring around it and two are located on the underside of the robot. The IR sensors are complemented by a set of ve ultrasonic sensors. The K3 has a two-wheel dierential drive with a wheel encoder for each wheel. It is powered by a single battery on the underside and can be controlled via software on its embedded Linux computer.

Figure 1: IR range sensor conguration

4000 Sensor 5 Sensor 6 Simulated

3500

3000

2500 Sensor value

2000

1500

X: 0.06 Y: 1193

1000

500

0.02

0.04

0.06

0.08

0.1 0.12 Distance [m]

0.14

0.16

0.18

0.2

Figure 2: Sensor values vs. Measured Distance

IR Range Sensors
For the purpose of the homeworks and projects in the class, you will have access to the array of nine IR sensors that encompass the K3. IR range sensors are eective in the range 0.02 m to 0.2 m only. However, the IR sensors return raw values in the range of [18, 3960] instead of the measured distances. Figure 2 demonstrates the function that maps these sensors values to distances. The green plot represents the sensor model used in the simulator, while the blue and red plots show the sensor response of two dierent IR sensors (under dierent ambient lighting levels). The eect of ambient lighting (and other sources of noise) are not modelled in the simulator, but will be apparent on the actual hardware. The function that maps distances, denoted by , to sensor values is the following piecewise function: f () = 3960, if 0m 0.02m 3960e30(0.02) , if 0.02m 0.2m (1)

Your controller can access the IR array through the robot object that is passed into the execute function. For example,

for i=1:9 fprintf(IR #%d has a value of %d.\n, i, robot.ir_array(i).state); end The orientation (relative to the body of the K3, as shown in gure 1) of IR sensors 1 through 9 is 128 , 75 , 42 , 13 , 13 , 42 , 75 , 128 , and 180 , respectively.

Ultrasonic Range Sensors


The ultrasonice range sensors have a sensing range of 0.2m to 4m, but are not available in the simulator.

Dierential Wheel Drive


Since the K3 has a dierential wheel drive (i.e., is not a unicyle), it has to be controlled by specifying the rotational velocities of the right and left wheel. These velocities are computed by a transformation from (v, ) to (vr , v ). Recall that the dynamics of the unicycle are dened as, x = vcos() y = vsin() = . The dynamics of the dierential drive are dened as, x= R (vr + v )cos() 2 R y = (vr + v )sin() 2 R = (vr v ), L (2)

(3)

where R is the radius of the wheels and L is the distance between the wheels. The speed of the K3 can be set in the following way assuming that you have implemented the uni to diff function, which transforms (v, ) to (vr , v ): v = 0.15; % m/s w = pi/4; % rad/s % Transform from v,w to v_r,v_l and set the speed of the robot [vel_r, vel_l] = obj.uni_to_diff(robot,v,w); robot.set_speed(vel_r, vel_l);

Wheel Encoders
Each of the wheels is outtted with a wheel encoder that increments or decrements a tick counter depending on whether the wheel is moving forward or backwards, respectively. Wheel encoders may be used to infer the relative pose of the robot. This inference is called odometry. The relevant information needed for odometry is the radius of the wheel, the distance between the wheels, and the number of ticks per revolution of the wheel. R = robot.wheel_radius; % radius of the wheel L = robot.wheel_base_length; % distance between the wheels tpr = robot.encoders(1).ticks_per_rev; % ticks per revolution for the right wheel fprintf(The right wheel has a tick count of %d\n, robot.encoders(1).state); fprintf(The left wheel has a tick count of %d\n, robot.encoders(2).state);

Figure 3: K3 Simulator

Simulator
Start the simulator with the k3 sim command in MATLAB. Figure 3 is a screenshot of the graphical user interface (GUI) of the simulator. The GUI can be controlled by the bottom row of buttons (or their equivalent keyboard shortcuts). The rst button is the Play button [p] and starts the simulation. Once this button has been pressed, the second button, the Step button [s], can be used to step through the simulation for debugging purposes. The Track button [c] can be used to toggle between a xed camera view and a view that tracks the movement of the robot. The set of Pan buttons [left,right,up,down] can be used to pan around the environment if you need to inspect a certain area of the world. The set of Zoom buttons [[,]]allows you to zoom in and out to get a better view of the simulation.

Homeworks
The following sections serve as a tutorial for getting through the simulator portions of the homeworks.

Homework 2
The second homework asks you to implement the transformation from (v, ) to (vr , v ) in the @controller/controller.m le. This class serves a template for all of the controllers you will be designing. Since odometry and this particular transformation will be used in all of your controllers, it only makes sense to make these functions part of the blueprint for your other controllers. You may modify the controller any way that you would like; however, the function execute(obj,robot) is required, since the simulator will call the function at every time step ( = 0.033s). This function receives a handle to the robot, so that you can read the K3s sensor values and sent it your computed

controls. Since you will be writing several controllers, it is a good idea to keep them in separate les. Therefore, for each of the two controllers (heading and go-to-goal), there is a separate class which inherits the controller class. For example, the class skeleton for @heading/heading is the following: classdef heading < controller %HEADING Summary of this class goes here % Detailed explanation goes here properties % controller memory banks end methods function obj = heading() % initialize memory banks here end

function execute(obj, robot) % Given a constant v in [-0.3,0.3] v = 0.15; % m/s % Update the odometry obj.update_odometry(robot); % Get estimate of current pose x = obj.state_estimate.x; y = obj.state_estimate.y; theta = obj.state_estimate.theta; % Compute the w that gets you to your desired theta w = 0; % Transform from v,w to v_r,v_l and set the speed of the robot [vel_r, vel_l] = obj.uni_to_diff(robot,v,w); robot.set_speed(vel_r, vel_l); end end end Notice that at the top we inherit from the controller class, meaning that we get access to the functions uni to diff and update odometry. We are redening the function execute, because this controller has a dierent functionality. Another important tool will be properties which allow you to store information from one call of execute to the next. For example, the controller blueprint saves the current estimate of the pose at each iteration. When accessing properties of the controller use obj.<property name>, while when accessing the K3s properties, such as its sensor values, use robot.<property name>. To test your controller, you need to modify @supervisor/supervisor.m and specify the controller you want to test (e.g., let obj.current controller = obj.controllers{1}; to select your heading controller): function obj = supervisor() % initialize the controllers 5

obj.controllers{1} = heading(); obj.controllers{2} = go_to_goal(); % set the initial controller obj.current_controller = obj.controllers{1}; end For the purpose of debugging your code, you may be interested in looking at the step function of the simulator code. The simulator keeps track of (x, y, ) of the robot, so this is a good place to print out information that can help you check whether your odometry is working or you have reached a desired point or orientation. Note: You may design these controller any way you wish (though we have provided you with some nice skeletons to get started). You may also modify the simulator any way you wish if it helps you to debug your code. However, you will only turn in the les that correspond to the controllers on TSquare. When grading the homework, we will use our own copy of the simulator (the same version that is distributed) and your controller les.

You might also like