Skip to content

Commit 5075aa7

Browse files
committed
Updated README with video and blog links.
1 parent 73edc79 commit 5075aa7

27 files changed

+822
-812
lines changed

README.md

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
1-
# Getting Started with MATLAB, Simulink, and ROS
2-
### Copyright 2017-2019 The MathWorks, Inc.
3-
Resources for getting started with MATLAB and Simulink and the Robot Operating System (ROS). This functionality is provided by ROS Toolbox.
4-
5-
## templateFiles Folder
6-
Contains files provide a basic structure for creating your own MATLAB and Simulink files.
7-
8-
### MATLAB Templates
9-
* **loopTemplate:** Algorithm runs in a simple loop, with an optional pause between iterations
10-
* **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
11-
* **async/asyncTemplate:** Algorithm runs asynchronously, whenever a new ROS message is received
12-
* **timer/timerTemplate:** Algorithm is scheduled to run on a timer in the background
13-
14-
### Simulink Templates
15-
* **simulationTemplate:** Model is slowed down using the wall clock, to run approximately in real time
16-
* **codeGenerationTemplate:** Model is configured to generate a standalone C++ ROS node using Embedded Coder
17-
* **multirateTemplate:** Model contains multiple subscribe and algorithm rates, as well as displays the color-coded rates in the model
18-
* **architecture/mdlArchTemplate:** Model contains MATLAB, Simulink, and Stateflow elements. Links to reusable MATLAB files and Simulink block library in the **architecture** folder
19-
* **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.
20-
21-
## demoFiles Folder
22-
Contains filled out versions of the templates above, that will run on several platforms.
23-
24-
### Algorithms
25-
26-
#### Object Detection
27-
* Uses camera information to detect a blue object and return its position and size
28-
* MATLAB code can be found in **Algorithms\detectCircle.m**
29-
* The same function is called in the Simulink examples using a MATLAB Function block
30-
31-
#### Object Tracking
32-
* 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)
33-
* MATLAB code can be found in **Algorithms\trackCircle.m**
34-
* Simulink and Stateflow versions of the same algorithm can be found in **Algorithms\controlLib.slx**
35-
36-
### Target Platforms
37-
38-
#### Webcam
39-
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
40-
41-
#### Gazebo Examples
42-
These examples have been tested using the Linux virtual machine at https://www.mathworks.com/support/product/robotics/v3-installation-instructions.html.
43-
44-
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.
45-
46-
#### TurtleBot
47-
These examples have been tested using the TurtleBot 2.
48-
49-
You can find a printable blue circle in the **Images** folder.
50-
51-
### MATLAB Examples
52-
The **MATLAB** subfolder contains loop examples, as well as a **timer** subfolder showing the timer object and handle class approach.
53-
54-
### Simulink Examples
55-
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).
56-
57-
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
1+
# Getting Started with MATLAB, Simulink, and ROS
2+
Copyright 2017-2019 The MathWorks, Inc.
3+
4+
## Getting Started
5+
This repository contains resources for getting started with MATLAB and Simulink
6+
and the Robot Operating System (ROS). This functionality is provided by ROS Toolbox.
7+
8+
To learn more, refer to our [blog post](https://blogs.mathworks.com/racing-lounge/2017/11/08/matlab-simulink-ros/) and the following videos.
9+
10+
* [Getting Started with MATLAB and ROS](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-getting-started-with-matlab-and-ros-1508263034047.html)
11+
* [Getting Started with Simulink and ROS](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-getting-started-with-simulink-and-ros-1509397202143.html)
12+
* [Deploying Standalone ROS Nodes from Simulink](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-deploying-algorithms-to-ros-1510659362460.html)
13+
* [Distributed Computing with MATLAB, Simulink, and ROS](https://www.mathworks.com/videos/matlab-and-simulink-robotics-arena-designing-distributed-systems-with-ros-1514584072926.html)
14+
15+
## templateFiles Folder
16+
Contains files provide a basic structure for creating your own MATLAB and Simulink files.
17+
18+
### MATLAB Templates
19+
* **loopTemplate:** Algorithm runs in a simple loop, with an optional pause between iterations
20+
* **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
21+
* **async/asyncTemplate:** Algorithm runs asynchronously, whenever a new ROS message is received
22+
* **timer/timerTemplate:** Algorithm is scheduled to run on a timer in the background
23+
24+
### Simulink Templates
25+
* **simulationTemplate:** Model is slowed down using the wall clock, to run approximately in real time
26+
* **codeGenerationTemplate:** Model is configured to generate a standalone C++ ROS node using Embedded Coder
27+
* **multirateTemplate:** Model contains multiple subscribe and algorithm rates, as well as displays the color-coded rates in the model
28+
* **architecture/mdlArchTemplate:** Model contains MATLAB, Simulink, and Stateflow elements. Links to reusable MATLAB files and Simulink block library in the **architecture** folder
29+
* **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.
30+
31+
## demoFiles Folder
32+
Contains filled out versions of the templates above, that will run on several platforms.
33+
34+
### Algorithms
35+
36+
#### Object Detection
37+
* Uses camera information to detect a blue object and return its position and size
38+
* MATLAB code can be found in **Algorithms\detectCircle.m**
39+
* The same function is called in the Simulink examples using a MATLAB Function block
40+
41+
#### Object Tracking
42+
* 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)
43+
* MATLAB code can be found in **Algorithms\trackCircle.m**
44+
* Simulink and Stateflow versions of the same algorithm can be found in **Algorithms\controlLib.slx**
45+
46+
### Target Platforms
47+
48+
#### Webcam
49+
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
50+
51+
#### Gazebo Examples
52+
These examples have been tested using the Linux virtual machine at https://www.mathworks.com/support/product/robotics/v3-installation-instructions.html.
53+
54+
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.
55+
56+
#### TurtleBot
57+
These examples have been tested using the TurtleBot 2.
58+
59+
You can find a printable blue circle in the **Images** folder.
60+
61+
### MATLAB Examples
62+
The **MATLAB** subfolder contains loop examples, as well as a **timer** subfolder showing the timer object and handle class approach.
63+
64+
### Simulink Examples
65+
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).
66+
67+
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

demoFiles/Algorithms/controlParams.m

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
function params = controlParams
2-
% Creates object tracking controller parameters (Webcam and TurtleBot)
3-
% Copyright 2017 The MathWorks, Inc.
4-
5-
params.Ts = 0.1; % Sample time
6-
7-
params.bufSize = 5; % Filter buffer size
8-
params.maxDisp = 300; % Max object displacement [pixels]
9-
params.minSize = 50; % Min object size [pixels]
10-
params.maxSize = 500; % Max object size [pixels]
11-
params.maxCounts = 5; % Max outlier counts before stopping
12-
13-
params.linVelGain = 2e-3; % Linear control gain
14-
params.angVelGain = 4e-3; % Angular control gain
15-
params.maxLinVel = 0.2; % Max linear speed
16-
params.maxAngVel = 0.75; % Max angular speed
17-
18-
params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
19-
params.targetSize = 180; % Linear speed control target blob size [pixels]
20-
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
21-
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down
22-
1+
function params = controlParams
2+
% Creates object tracking controller parameters (Webcam and TurtleBot)
3+
% Copyright 2017 The MathWorks, Inc.
4+
5+
params.Ts = 0.1; % Sample time
6+
7+
params.bufSize = 5; % Filter buffer size
8+
params.maxDisp = 300; % Max object displacement [pixels]
9+
params.minSize = 50; % Min object size [pixels]
10+
params.maxSize = 500; % Max object size [pixels]
11+
params.maxCounts = 5; % Max outlier counts before stopping
12+
13+
params.linVelGain = 2e-3; % Linear control gain
14+
params.angVelGain = 4e-3; % Angular control gain
15+
params.maxLinVel = 0.2; % Max linear speed
16+
params.maxAngVel = 0.75; % Max angular speed
17+
18+
params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
19+
params.targetSize = 180; % Linear speed control target blob size [pixels]
20+
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
21+
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down
22+
2323
end
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
function params = controlParamsGazebo
2-
% Creates object tracking controller parameters (Gazebo)
3-
% Copyright 2017 The MathWorks, Inc.
4-
5-
params.Ts = 0.1; % Sample time
6-
7-
params.bufSize = 5; % Filter buffer size
8-
params.maxDisp = 300; % Max object displacement [pixels]
9-
params.minSize = 50; % Min object size [pixels]
10-
params.maxSize = 500; % Max object size [pixels]
11-
params.maxCounts = 5; % Max outlier counts before stopping
12-
13-
params.linVelGain = 2e-3; % Linear control gain
14-
params.angVelGain = 5e-4; % Angular control gain
15-
params.maxLinVel = 0.2; % Max linear speed
16-
params.maxAngVel = 0.75; % Max angular speed
17-
18-
params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
19-
params.targetSize = 180; % Linear speed control target blob size [pixels]
20-
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
21-
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down
22-
1+
function params = controlParamsGazebo
2+
% Creates object tracking controller parameters (Gazebo)
3+
% Copyright 2017 The MathWorks, Inc.
4+
5+
params.Ts = 0.1; % Sample time
6+
7+
params.bufSize = 5; % Filter buffer size
8+
params.maxDisp = 300; % Max object displacement [pixels]
9+
params.minSize = 50; % Min object size [pixels]
10+
params.maxSize = 500; % Max object size [pixels]
11+
params.maxCounts = 5; % Max outlier counts before stopping
12+
13+
params.linVelGain = 2e-3; % Linear control gain
14+
params.angVelGain = 5e-4; % Angular control gain
15+
params.maxLinVel = 0.2; % Max linear speed
16+
params.maxAngVel = 0.75; % Max angular speed
17+
18+
params.posDeadZone = 30; % Steering control marker position dead zone [pixels]
19+
params.targetSize = 180; % Linear speed control target blob size [pixels]
20+
params.sizeDeadZone = 30; % Linear speed control size dead zone [pixels]
21+
params.speedRedSize = 100; % Minimum pixel value before turning speed is ramped down
22+
2323
end

demoFiles/Algorithms/trackCircle.m

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
function [v,w] = trackCircle(x,blobSize,imgWidth,params)
2-
% Tracks location and distance of detected circle by assigning
3-
% linear and angular velocities
4-
% Copyright 2017 The MathWorks, Inc.
5-
6-
%% Initialize persistent variables
7-
persistent xBuffer xPrev sizeBuffer outlierCount
8-
if isempty(xBuffer); xBuffer = zeros(1,params.bufSize); end
9-
if isempty(sizeBuffer); sizeBuffer = zeros(1,params.bufSize); end
10-
if isempty(xPrev); xPrev = x; end
11-
if isempty(outlierCount); outlierCount = 0; end
12-
13-
%% Input processing
14-
% Count outliers
15-
if (abs(x-xPrev)<params.maxDisp) && (blobSize>params.minSize) && (blobSize<params.maxSize)
16-
outlierCount = 0;
17-
else
18-
outlierCount = min(outlierCount+1,1000); % Increment with saturation
19-
end
20-
% Update and average the measurement buffers
21-
xBuffer = [xBuffer(2:params.bufSize) x];
22-
sizeBuffer = [sizeBuffer(2:params.bufSize) blobSize];
23-
xFilt = mean(xBuffer);
24-
sizeFilt = mean(sizeBuffer);
25-
xPrev = x;
26-
27-
%% Angular velocity control: Keep the marker centered
28-
w = 0;
29-
if outlierCount < params.maxCounts
30-
posError = xFilt - imgWidth/2;
31-
if abs(posError) > params.posDeadZone
32-
speedReduction = max(sizeFilt/params.speedRedSize,1);
33-
w = -params.angVelGain*posError*speedReduction;
34-
end
35-
if w > params.maxAngVel
36-
w = params.maxAngVel;
37-
elseif w < -params.maxAngVel
38-
w = -params.maxAngVel;
39-
end
40-
end
41-
42-
%% Linear velocity control: Keep the marker at a certain distance
43-
v = 0;
44-
if outlierCount < params.maxCounts
45-
sizeError = params.targetSize - sizeFilt;
46-
if abs(sizeError) > params.sizeDeadZone
47-
v = params.linVelGain*sizeError;
48-
end
49-
if v > params.maxLinVel
50-
v = params.maxLinVel;
51-
elseif v < -params.maxLinVel
52-
v = -params.maxLinVel;
53-
end
54-
end
55-
56-
%% Scan autonomously for a while if the outlier count has been exceeded
57-
if outlierCount > 50 && outlierCount < 500
58-
w = params.maxAngVel/2;
59-
end
60-
61-
end
62-
1+
function [v,w] = trackCircle(x,blobSize,imgWidth,params)
2+
% Tracks location and distance of detected circle by assigning
3+
% linear and angular velocities
4+
% Copyright 2017 The MathWorks, Inc.
5+
6+
%% Initialize persistent variables
7+
persistent xBuffer xPrev sizeBuffer outlierCount
8+
if isempty(xBuffer); xBuffer = zeros(1,params.bufSize); end
9+
if isempty(sizeBuffer); sizeBuffer = zeros(1,params.bufSize); end
10+
if isempty(xPrev); xPrev = x; end
11+
if isempty(outlierCount); outlierCount = 0; end
12+
13+
%% Input processing
14+
% Count outliers
15+
if (abs(x-xPrev)<params.maxDisp) && (blobSize>params.minSize) && (blobSize<params.maxSize)
16+
outlierCount = 0;
17+
else
18+
outlierCount = min(outlierCount+1,1000); % Increment with saturation
19+
end
20+
% Update and average the measurement buffers
21+
xBuffer = [xBuffer(2:params.bufSize) x];
22+
sizeBuffer = [sizeBuffer(2:params.bufSize) blobSize];
23+
xFilt = mean(xBuffer);
24+
sizeFilt = mean(sizeBuffer);
25+
xPrev = x;
26+
27+
%% Angular velocity control: Keep the marker centered
28+
w = 0;
29+
if outlierCount < params.maxCounts
30+
posError = xFilt - imgWidth/2;
31+
if abs(posError) > params.posDeadZone
32+
speedReduction = max(sizeFilt/params.speedRedSize,1);
33+
w = -params.angVelGain*posError*speedReduction;
34+
end
35+
if w > params.maxAngVel
36+
w = params.maxAngVel;
37+
elseif w < -params.maxAngVel
38+
w = -params.maxAngVel;
39+
end
40+
end
41+
42+
%% Linear velocity control: Keep the marker at a certain distance
43+
v = 0;
44+
if outlierCount < params.maxCounts
45+
sizeError = params.targetSize - sizeFilt;
46+
if abs(sizeError) > params.sizeDeadZone
47+
v = params.linVelGain*sizeError;
48+
end
49+
if v > params.maxLinVel
50+
v = params.maxLinVel;
51+
elseif v < -params.maxLinVel
52+
v = -params.maxLinVel;
53+
end
54+
end
55+
56+
%% Scan autonomously for a while if the outlier count has been exceeded
57+
if outlierCount > 50 && outlierCount < 500
58+
w = params.maxAngVel/2;
59+
end
60+
61+
end
62+

0 commit comments

Comments
 (0)