Skip to content

Commit

Permalink
Updated README with video and blog links.
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass committed Oct 15, 2019
1 parent 73edc79 commit 5075aa7
Show file tree
Hide file tree
Showing 27 changed files with 822 additions and 812 deletions.
124 changes: 67 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,67 @@
# Getting Started with MATLAB, Simulink, and ROS
### Copyright 2017-2019 The MathWorks, Inc.
Resources for getting started with MATLAB and Simulink and the Robot Operating System (ROS). This functionality is provided by ROS Toolbox.

## templateFiles Folder
Contains files provide a basic structure for creating your own MATLAB and Simulink files.

### MATLAB Templates
* **loopTemplate:** Algorithm runs in a simple loop, with an optional pause between iterations
* **rateTemplate:** Algorithm runs in a simple loop, which runs at a fixed rate dictated by either the wall clock or the global ROS node clock
* **async/asyncTemplate:** Algorithm runs asynchronously, whenever a new ROS message is received
* **timer/timerTemplate:** Algorithm is scheduled to run on a timer in the background

### Simulink Templates
* **simulationTemplate:** Model is slowed down using the wall clock, to run approximately in real time
* **codeGenerationTemplate:** Model is configured to generate a standalone C++ ROS node using Embedded Coder
* **multirateTemplate:** Model contains multiple subscribe and algorithm rates, as well as displays the color-coded rates in the model
* **architecture/mdlArchTemplate:** Model contains MATLAB, Simulink, and Stateflow elements. Links to reusable MATLAB files and Simulink block library in the **architecture** folder
* **paramServerGetterTemplate + paramServerSetterTemplate**: The "getter" model can run on the desktop or deployed standalone, and receives parameters from the ROS parameter server. To modify the parameter value, you can either run the "setter" model, or use **rosparam** in MATLAB or a Terminal window.

## demoFiles Folder
Contains filled out versions of the templates above, that will run on several platforms.

### Algorithms

#### Object Detection
* Uses camera information to detect a blue object and return its position and size
* MATLAB code can be found in **Algorithms\detectCircle.m**
* The same function is called in the Simulink examples using a MATLAB Function block

#### Object Tracking
* Uses the output of the object detection algorithm to assign linear and angular velocities such that the object is centered in the field of vision and has a certain pixel size (or distance)
* MATLAB code can be found in **Algorithms\trackCircle.m**
* Simulink and Stateflow versions of the same algorithm can be found in **Algorithms\controlLib.slx**

### Target Platforms

#### Webcam
These examples have been tested on Windows using the MATLAB Support Package for USB Webcams at https://www.mathworks.com/matlabcentral/fileexchange/45182-matlab-support-package-for-usb-webcams

#### Gazebo Examples
These examples have been tested using the Linux virtual machine at https://www.mathworks.com/support/product/robotics/v3-installation-instructions.html.

When you open the virtual machine, you can track blue objects in either the **Gazebo Playground** or **Gazebo TurtleBot World** worlds. To test the tracking algorithm, you can use Gazebo to move the robot and/or the blue objects in the world.

#### TurtleBot
These examples have been tested using the TurtleBot 2.

You can find a printable blue circle in the **Images** folder.

### MATLAB Examples
The **MATLAB** subfolder contains loop examples, as well as a **timer** subfolder showing the timer object and handle class approach.

### Simulink Examples
The **Simulink** subfolder contains examples with Simulink blocks, with Simulink blocks and a Stateflow chart, and using External Mode and the ROS Parameter Server (see the **paramServer** subfolder).

In addition, the **distributed** subfolder contains examples of the perception, control, and visualization components broken into multiple tasks and separate models. The models can communicate with each other with a standard ROS message or with a custom message which can be found in the **custom_robot_msgs** folder. To get the latter example working, you will need to use the ROS Toolbox Interface for ROS Custom Messages add-on and follow the steps shown at https://www.mathworks.com/help/ros/ug/ros-custom-message-support.html
# Getting Started with MATLAB, Simulink, and ROS
Copyright 2017-2019 The MathWorks, Inc.

## Getting Started
This repository contains resources for getting started with MATLAB and Simulink
and the Robot Operating System (ROS). This functionality is provided by ROS Toolbox.

To learn more, refer to our [blog post](https://blogs.mathworks.com/racing-lounge/2017/11/08/matlab-simulink-ros/) and the following videos.

* [Getting Started with MATLAB and ROS](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-getting-started-with-matlab-and-ros-1508263034047.html)
* [Getting Started with Simulink and ROS](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-getting-started-with-simulink-and-ros-1509397202143.html)
* [Deploying Standalone ROS Nodes from Simulink](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-deploying-algorithms-to-ros-1510659362460.html)
* [Distributed Computing with MATLAB, Simulink, and ROS](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-designing-distributed-systems-with-ros-1514584072926.html)

## templateFiles Folder
Contains files provide a basic structure for creating your own MATLAB and Simulink files.

### MATLAB Templates
* **loopTemplate:** Algorithm runs in a simple loop, with an optional pause between iterations
* **rateTemplate:** Algorithm runs in a simple loop, which runs at a fixed rate dictated by either the wall clock or the global ROS node clock
* **async/asyncTemplate:** Algorithm runs asynchronously, whenever a new ROS message is received
* **timer/timerTemplate:** Algorithm is scheduled to run on a timer in the background

### Simulink Templates
* **simulationTemplate:** Model is slowed down using the wall clock, to run approximately in real time
* **codeGenerationTemplate:** Model is configured to generate a standalone C++ ROS node using Embedded Coder
* **multirateTemplate:** Model contains multiple subscribe and algorithm rates, as well as displays the color-coded rates in the model
* **architecture/mdlArchTemplate:** Model contains MATLAB, Simulink, and Stateflow elements. Links to reusable MATLAB files and Simulink block library in the **architecture** folder
* **paramServerGetterTemplate + paramServerSetterTemplate**: The "getter" model can run on the desktop or deployed standalone, and receives parameters from the ROS parameter server. To modify the parameter value, you can either run the "setter" model, or use **rosparam** in MATLAB or a Terminal window.

## demoFiles Folder
Contains filled out versions of the templates above, that will run on several platforms.

### Algorithms

#### Object Detection
* Uses camera information to detect a blue object and return its position and size
* MATLAB code can be found in **Algorithms\detectCircle.m**
* The same function is called in the Simulink examples using a MATLAB Function block

#### Object Tracking
* Uses the output of the object detection algorithm to assign linear and angular velocities such that the object is centered in the field of vision and has a certain pixel size (or distance)
* MATLAB code can be found in **Algorithms\trackCircle.m**
* Simulink and Stateflow versions of the same algorithm can be found in **Algorithms\controlLib.slx**

### Target Platforms

#### Webcam
These examples have been tested on Windows using the MATLAB Support Package for USB Webcams at https://www.mathworks.com/matlabcentral/fileexchange/45182-matlab-support-package-for-usb-webcams

#### Gazebo Examples
These examples have been tested using the Linux virtual machine at https://www.mathworks.com/support/product/robotics/v3-installation-instructions.html.

When you open the virtual machine, you can track blue objects in either the **Gazebo Playground** or **Gazebo TurtleBot World** worlds. To test the tracking algorithm, you can use Gazebo to move the robot and/or the blue objects in the world.

#### TurtleBot
These examples have been tested using the TurtleBot 2.

You can find a printable blue circle in the **Images** folder.

### MATLAB Examples
The **MATLAB** subfolder contains loop examples, as well as a **timer** subfolder showing the timer object and handle class approach.

### Simulink Examples
The **Simulink** subfolder contains examples with Simulink blocks, with Simulink blocks and a Stateflow chart, and using External Mode and the ROS Parameter Server (see the **paramServer** subfolder).

In addition, the **distributed** subfolder contains examples of the perception, control, and visualization components broken into multiple tasks and separate models. The models can communicate with each other with a standard ROS message or with a custom message which can be found in the **custom_robot_msgs** folder. To get the latter example working, you will need to use the ROS Toolbox Interface for ROS Custom Messages add-on and follow the steps shown at https://www.mathworks.com/help/ros/ug/ros-custom-message-support.html
44 changes: 22 additions & 22 deletions demoFiles/Algorithms/controlParams.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
function params = controlParams
% Creates object tracking controller parameters (Webcam and TurtleBot)
% Copyright 2017 The MathWorks, Inc.

params.Ts = 0.1; % Sample time

params.bufSize = 5; % Filter buffer size
params.maxDisp = 300; % Max object displacement [pixels]
params.minSize = 50; % Min object size [pixels]
params.maxSize = 500; % Max object size [pixels]
params.maxCounts = 5; % Max outlier counts before stopping

params.linVelGain = 2e-3; % Linear control gain
params.angVelGain = 4e-3; % Angular control gain
params.maxLinVel = 0.2; % Max linear speed
params.maxAngVel = 0.75; % Max angular speed

params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
params.targetSize = 180; % Linear speed control target blob size [pixels]
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down

function params = controlParams
% Creates object tracking controller parameters (Webcam and TurtleBot)
% Copyright 2017 The MathWorks, Inc.

params.Ts = 0.1; % Sample time

params.bufSize = 5; % Filter buffer size
params.maxDisp = 300; % Max object displacement [pixels]
params.minSize = 50; % Min object size [pixels]
params.maxSize = 500; % Max object size [pixels]
params.maxCounts = 5; % Max outlier counts before stopping

params.linVelGain = 2e-3; % Linear control gain
params.angVelGain = 4e-3; % Angular control gain
params.maxLinVel = 0.2; % Max linear speed
params.maxAngVel = 0.75; % Max angular speed

params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
params.targetSize = 180; % Linear speed control target blob size [pixels]
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down

end
44 changes: 22 additions & 22 deletions demoFiles/Algorithms/controlParamsGazebo.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
function params = controlParamsGazebo
% Creates object tracking controller parameters (Gazebo)
% Copyright 2017 The MathWorks, Inc.

params.Ts = 0.1; % Sample time

params.bufSize = 5; % Filter buffer size
params.maxDisp = 300; % Max object displacement [pixels]
params.minSize = 50; % Min object size [pixels]
params.maxSize = 500; % Max object size [pixels]
params.maxCounts = 5; % Max outlier counts before stopping

params.linVelGain = 2e-3; % Linear control gain
params.angVelGain = 5e-4; % Angular control gain
params.maxLinVel = 0.2; % Max linear speed
params.maxAngVel = 0.75; % Max angular speed

params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
params.targetSize = 180; % Linear speed control target blob size [pixels]
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down

function params = controlParamsGazebo
% Creates object tracking controller parameters (Gazebo)
% Copyright 2017 The MathWorks, Inc.

params.Ts = 0.1; % Sample time

params.bufSize = 5; % Filter buffer size
params.maxDisp = 300; % Max object displacement [pixels]
params.minSize = 50; % Min object size [pixels]
params.maxSize = 500; % Max object size [pixels]
params.maxCounts = 5; % Max outlier counts before stopping

params.linVelGain = 2e-3; % Linear control gain
params.angVelGain = 5e-4; % Angular control gain
params.maxLinVel = 0.2; % Max linear speed
params.maxAngVel = 0.75; % Max angular speed

params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
params.targetSize = 180; % Linear speed control target blob size [pixels]
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down

end
124 changes: 62 additions & 62 deletions demoFiles/Algorithms/trackCircle.m
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
function [v,w] = trackCircle(x,blobSize,imgWidth,params)
% Tracks location and distance of detected circle by assigning
% linear and angular velocities
% Copyright 2017 The MathWorks, Inc.

%% Initialize persistent variables
persistent xBuffer xPrev sizeBuffer outlierCount
if isempty(xBuffer); xBuffer = zeros(1,params.bufSize); end
if isempty(sizeBuffer); sizeBuffer = zeros(1,params.bufSize); end
if isempty(xPrev); xPrev = x; end
if isempty(outlierCount); outlierCount = 0; end

%% Input processing
% Count outliers
if (abs(x-xPrev)<params.maxDisp) && (blobSize>params.minSize) && (blobSize<params.maxSize)
outlierCount = 0;
else
outlierCount = min(outlierCount+1,1000); % Increment with saturation
end
% Update and average the measurement buffers
xBuffer = [xBuffer(2:params.bufSize) x];
sizeBuffer = [sizeBuffer(2:params.bufSize) blobSize];
xFilt = mean(xBuffer);
sizeFilt = mean(sizeBuffer);
xPrev = x;

%% Angular velocity control: Keep the marker centered
w = 0;
if outlierCount < params.maxCounts
posError = xFilt - imgWidth/2;
if abs(posError) > params.posDeadZone
speedReduction = max(sizeFilt/params.speedRedSize,1);
w = -params.angVelGain*posError*speedReduction;
end
if w > params.maxAngVel
w = params.maxAngVel;
elseif w < -params.maxAngVel
w = -params.maxAngVel;
end
end

%% Linear velocity control: Keep the marker at a certain distance
v = 0;
if outlierCount < params.maxCounts
sizeError = params.targetSize - sizeFilt;
if abs(sizeError) > params.sizeDeadZone
v = params.linVelGain*sizeError;
end
if v > params.maxLinVel
v = params.maxLinVel;
elseif v < -params.maxLinVel
v = -params.maxLinVel;
end
end

%% Scan autonomously for a while if the outlier count has been exceeded
if outlierCount > 50 && outlierCount < 500
w = params.maxAngVel/2;
end

end

function [v,w] = trackCircle(x,blobSize,imgWidth,params)
% Tracks location and distance of detected circle by assigning
% linear and angular velocities
% Copyright 2017 The MathWorks, Inc.

%% Initialize persistent variables
persistent xBuffer xPrev sizeBuffer outlierCount
if isempty(xBuffer); xBuffer = zeros(1,params.bufSize); end
if isempty(sizeBuffer); sizeBuffer = zeros(1,params.bufSize); end
if isempty(xPrev); xPrev = x; end
if isempty(outlierCount); outlierCount = 0; end

%% Input processing
% Count outliers
if (abs(x-xPrev)<params.maxDisp) && (blobSize>params.minSize) && (blobSize<params.maxSize)
outlierCount = 0;
else
outlierCount = min(outlierCount+1,1000); % Increment with saturation
end
% Update and average the measurement buffers
xBuffer = [xBuffer(2:params.bufSize) x];
sizeBuffer = [sizeBuffer(2:params.bufSize) blobSize];
xFilt = mean(xBuffer);
sizeFilt = mean(sizeBuffer);
xPrev = x;

%% Angular velocity control: Keep the marker centered
w = 0;
if outlierCount < params.maxCounts
posError = xFilt - imgWidth/2;
if abs(posError) > params.posDeadZone
speedReduction = max(sizeFilt/params.speedRedSize,1);
w = -params.angVelGain*posError*speedReduction;
end
if w > params.maxAngVel
w = params.maxAngVel;
elseif w < -params.maxAngVel
w = -params.maxAngVel;
end
end

%% Linear velocity control: Keep the marker at a certain distance
v = 0;
if outlierCount < params.maxCounts
sizeError = params.targetSize - sizeFilt;
if abs(sizeError) > params.sizeDeadZone
v = params.linVelGain*sizeError;
end
if v > params.maxLinVel
v = params.maxLinVel;
elseif v < -params.maxLinVel
v = -params.maxLinVel;
end
end

%% Scan autonomously for a while if the outlier count has been exceeded
if outlierCount > 50 && outlierCount < 500
w = params.maxAngVel/2;
end

end

Loading

0 comments on commit 5075aa7

Please sign in to comment.