Page tree
Skip to end of metadata
Go to start of metadata

References and Prerequisites

MATLAB Activation

Try to open MATLAB on your ME4823 computer.  If you get a message saying that the MATLAB license has expired or needs re-activation, follow these steps to re-activate the license:

  1.  Start the activation client:

    sudo /usr/local/MATLAB/R2017b/bin/activate_matlab.sh

    which should open a window like this...

  2. Get an instructor to login with their credentials to update the license.

Git Repository 

Just as in the last assignment, in this assignment will be submitted as a git repository on the GitLab server.  Create a new repository named "mrc_hw3" using the server's web interface at  https://gitlab.nps.edu

When you create repository us on your local machine, do so in your catkin workspace (~/catkin_ws/src).   You can do this by first

cd ~/catkin_ws/src

Then following the directions from the gitlab site which start with...

mkdir mrc_hw3
cd mrc_hw3
git init 
...
git push -u origin master

As we know, it may take some time for the remote (server) repository to be come available.  If the push fails, just keep working on your local copy and when the server catches up you can push your local repository to the server.

Make it a ROS Package

As we did in Assignment 2, make your mrc_hw3 directory into a ROS package.

cd ~/catkin_ws/src
catkin_create_pkg mrc_hw3 
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash  # Causes ROS to become aware of your new package immediately, instead of waiting to open a new terminal

You can check that this operation was successful by using roscd to move into the mrc_hw3 directory...

roscd mrc_hw3

Another way to check that everything is setup is to ask ROS where the mrc_hw3 package is located...

rospack find mrc_hw3

Finally we want to make sure to add the files created by catkin_create_pkg to our git repository...

roscd mrc_hw3
git add CMakeLists.txt
git add package.xml

at this point it is a good idea to commit your changes locally and push them to the server as a clean starting point.

Exercise 1: Create a Launch File

Add a new directory called launch to your ROS package, then add a new file called turtle.launch

This launch script should do three things...

  1. Start roscore (roslaunch does this automatically), so nothing to do here.
  2. Start a turtlesim_node
  3. Start a turtle_teleop_key node as we did in this tutorial http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics#turtle_keyboard_teleoperation with the output sent to the screen (output="screen")

You should be able call this launch file like this...

roslaunch mrc_hw3 turtle.launch 

and then drive a turtle around with the keyboard.

Commit and push your launch/turtle.launch file to your mrc_hw3 repository.

Exercise 2: Recording and Playing Back Data

This is an extension of the tutorial on recording and playing back ROS data.

For the exercise, we will use this ROS bag file that you can download: remapped_turtle.bag

Use the rosbag program (rosbag info) to inspect the file.  What topics are included in the file?

The bag file was generated using the system described in the exercise above, but the velocity commands (of message type geometry_msgs/Twist) were remapped from the turtle1/cmd_vel topic to a new topic name.  You should be able to determine that new topic name.

Now write a new launch file called turtle_remap.launch in the launch directory of your package, i.e.,launch/turtle_remap.launch .  This launch file will start the turtlesim_node and use the remap functionality to have that node listen/subscribe to the twist commands on the topic in the  remapped_turtle.bag file. That launch file will be as shown below, but you will need to replace the "XXXXXX" with the appropriate topic.

<launch>
<node pkg="turtlesim" type="turtlesim_node" name="simulated_turtle">
	<remap from="turtle1/cmd_vel" to="XXXXXX" />
</node>
</launch>
  • Open a terminal and use roslaunch to execute your launch file.

    roslaunch mrc_hw3 turtle_remap.launch   # You might experiment with tab-complete when issuing this command.
  • Open a second terminal and start recording all the ROS data to a new bag file called myturtle.bag 

  • Open a third terminal and plot the rosgraph: rosrun rqt_graph rqt_graph
  • Open yet another terminal and playback the remapped_turtle.bag file.

You should see your turtle do something like this...

  • Stop your data logging by using Cntrl-C in the terminal where you are recording the bag file.
  • Move the file you recorded, myturtle.bag, to a new directory in your package called data, i.e., mrc_hw3/data/myturtle.bag
  • Save your rosgraph to a PNG file called rosgraph.png , in a new directory within your package called images, i.e, mrc_hw3/images/rosgraph.png

To complete the assignment, make sure that your myturtle.bag and rosgraph.png files are added to your git repository, commit and push.

Note: In general it is not recommended to use git repositories for storing bag files.  These log files can get quite large (many GB's) and they are binary files that are note amenable to the incremental changes.  We are making an exception here for convenience - and your bag file shoudl be relatively small.

Exercise 3: Plotting rosbag Data with MATLAB

This exercise uses the tools described in the Mathworks tutorials above.

When you first open MATLAB, type the command rosinit in the command window.  You should see something like this...

 
>> rosinit
Initializing ROS master on http://aku:11311/.
Initializing global node /matlab_global_node_12356 with NodeURI http://aku:56098/

If not, the ROS toolbox may not be installed - see the instructor!

Use MATLAB to generate three figures based on the recorded data from Exercise 3:

  1. A plot the X and Y location of your turtle. Save this file as matlab/pose_xy.png
  2. A plot of the heading angle (Theta) of your turtle as a function of time. Save this file as matlab/pose_theta.png
  3. A quiver plot showing the location and heading of the turtle.  Save this file as matlab/pose_quiver.png

Following the Working with rosbag Log Files example, develop a script called plotbag.m and put it in a directory of your package called matlab.  Your script will need to do the following...

  1. Load the  myturtle.bag  file
  2. Select the messages on the turtle1/pose topic
  3. Convert these pose messages to a timeseries
  4. Generate the figures.  (Make sure to include labels and titles for your figures.)

Here is a snippet, with comments, to get started

% Create a bag file object with the file name
% by omitting the semicolon this displays some information about 
% the bag file
bag = rosbag('../data/myturtle.bag')

% Display a list of the topics and message types in the bag file
bag.AvailableTopics

% Since the messages on topic turtle1/pose are of type Twist, 
% let's see some of the attributes of the Twist message
msg_pose = rosmessage('turtlesim/Pose')
showdetails(msg_pose)

% Get just the topic we are interested in
bagselect = select(bag,'Topic','turtle1/pose');

% Create a time series object based on the fields of the turtlesim/Pose
% message we are interested in
ts = timeseries(bagselect,'X','Y','Theta','LinearVelocity','AngularVelocity');

 

It is recommended that you read the MATLAB documentation on the quiver plot.  Here is an example to illustrate how it works to show the state of the turtle...

% x is the x position of the turtle
% y is the y position of the turtle
% vel is the LinearVelocity of the turtle
% th is the heading angle of the turtle in radians

u = vel.*cos(th);
v = vel.*sin(th);
ii = 1:10:length(x);  % Decimate the data so that it plot only every Nth point.
quiver(x(ii),y(ii),u(ii),v(ii))

The resulting figure should look something like this...

To complete the exercise, add figures (*.png files) and your  plotbag.m file to your git repository, commit and push.

 

Exercise 4: Simulink Timing

You should have already completed the tutorial Getting Started with ROS in Simulink and read about the timing issues in the  Simulink and ROS Interaction article.

Measuring Offline Update Rate

In the Getting Started with ROS in Simulink tutorial you created a Simulink program that looks like this:

which publishes and subscribes to the to the /location topic.

This example is an offline simulation were the simulation clock runs faster than real-world time (aka the wall clock).  We can demonstrate this concept with the following experiment.

  • Within your mrc_hw3 repository, save the program as simulink/getting_started.slx
  • Set the solver as follows:
    • Fixed-step solver
    • Stop time: inf (for infinite)
    • Fixed-step size of 0.01
  • Run your simulation
  • Using the ROS tools you've learned about, measure the update rate, in Hertz, of messages published to the /location topic
  • Within the mrc_hw4/simulink directory, create a text file called updaterate.txt.  On the first line of this file, record the measured update rate (should be just a single number on the first line).
  • Now repeat the experiment, but change the Fixed-step size to 10.0
  • Add a second line in the file mrc_hw3/simulink/updaterate.txt and record this new numeric value for the update rate with the larger Fixed-step size

The updaterate.txt should have just two lines, where each line contains a single number for the update rate results under the two conditions. Save, commit and push the file updaterate.txt.

Pacing Simulink for Near Real-Time

If we want to adjust the execution time of Simulink so that it executes in sync with the wall clock, i.e., real-time simulation, we can use Real-Time Pacer for Simulink

Follow these directions to add the Simulink Pacer Block to your system.

Pacing Simulink for Real-Time Execution

Modify the simulation from Getting Started with ROS in Simulink so that it executes at approximately 10 Hz as measured by the wall clock, i.e., real-time.

Save your new Simulink program within your mrc_hw3 repository as simulink/getting_started_paced.slx

The turtlesim_node is a good first example of a ROS-enabled robot.  It receives commands on the /turtle1/cmd_vel topic and reports is position on the /turtle1/pose topic.

In this exercise we will use a Simulink program to implement simple waypoint guidance, driving the turtle to four sequential waypoints - resulting in a turtle path that looks something like this...

 

  1. A (hopefully) working is example is supplied in the following GitLab mrc_examples repository.
    In the simulink folder is the Simulink program turtle_waypoint_stateflow.slx.  Make a copy of this program and put it in your mrc_hw4/simulink directory.  Add the turtle_waypoint_stateflow.slx file to your git repository.


  2. In the MATLAB command window, start the ROS toolbox with the command

    rosinit
  3. In a Linux terminal window, start the turtlesim_node
  4. Run the Simulink program to guide the turtle to the waypoints.  If you take a look at the MATLAB function, inside the Simulink program, you can tweak the guidance law to adjust the performance of your turtle.
  5. The intention is for the provided example to work out of the box.  There may be problems with the specific setup if your topic names are different, etc.
  6. Tune the waypoint guidance algorithm so that the turtle accomplishes the following:
    1. Gets to within 0.5 turtle units of each waypoint.
    2. Does not hit the wall when doing so (if the turtle does hit a wall a warning ("Oh no! I hit a wall!") is printed in the terminal where you launched the turtlesim_node)

Once you have a successful run, save, commit and push the mrc_hw3/simulink/turtle_waypoint_stateflow.slx file.

Notes:

  • If you want to reset your turtle simulation without restarting the ROS nodes, you can use services!  Like this...

    rosservice call /turtle1/teleport_absolute '{x: 5.5, y: 5.5, theta: 0.0}'
    rosservice call /clear

     

Summary

After completing all the required exercises your ROS package should look something like this...

It is fine if there are other, extra files, but the files illustrated above are what you are asked to submit via git.

  • No labels