Navigating the Ros Filesystem (optional)

From here

#sudo apt-get install ros-<distro>-ros-tutorials
sudo apt-get install ros-melodic-ros-tutorials
#rospack find [package_name]
rospack find roscpp
#roscd [locationname[/subdir]]
roscd roscpp
#print the working directory using the Unix command
#To see what is in your ROS_PACKAGE_PATH, type:

#roscd can also move to a subdirectory of a package or stack.  Try:
roscd roscpp/cmake

#roscd log will take you to the folder where ROS stores log files. Note that if you have not run any ROS programs yet, this will yield an error saying that it does not yet exist.
#If you have run some ROS program before, try:
roscd log

#rosls is part of the rosbash suite. It allows you to ls directly in a package by name rather than by absolute path.
#rosls [locationname[/subdir]]
rosls roscpp_tutorials

Create A Package

From here

# You should have created this in the Creating a Workspace Tutorial
cd ~/catkin_ws/src
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

#Now you need to build the packages in the catkin workspace:
cd ~/catkin_ws

#To add the workspace to your ROS environment you need to source the generated setup file:
. ~/catkin_ws/devel/setup.bash

#first-order dependencies can now be reviewed with the rospack tool.
rospack depends1 beginner_tutorials

#dependencies for a package are stored in the package.xml file:
roscd beginner_tutorials
cat package.xml

#In many cases, a dependency will also have its own dependencies. For instance, rospy has other dependencies.
rospack depends1 rospy

#rospack can recursively determine all nested dependencies.
rospack depends beginner_tutorials

open up package.xml and check whether there is an exec_depend tag for each of the dependencies in package.xml as in the tutorial




Build a ROS Package

from here

#source your environment setup file if you have not already
# source /opt/ros/%YOUR_ROS_DISTRO%/setup.bash
# this has been added to bashrc so is not necessary
source /opt/ros/melodic/setup.bash
# In a catkin workspace
#catkin_make [make_targets] [-DCMAKE_VARIABLES=...]
#catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
#catkin_make install #optional

#If your source code is in a different place, say my_src then you would call catkin_make like this:
# In a catkin workspace
#catkin_make --source my_src
#catkin_make install --source my_src  # (optionally)

cd ~/catkin_ws/
ls src

Running ROS

From here

  1. In Terminal 1 (ctrl+alt+t)

  2. Open up a new terminal tab (ctrl+shift+t)

    rosnode list
    rosnode info /rosout
  3. Open up a new terminal tab (ctrl+shift+t)

    #rosrun [package_name] [node_name]
    rosrun turtlesim turtlesim_node
  4. In terminal 2(ctrl+pg_up):

    rosnode list
  5. In terminal 3(ctrl+pg_dn):

    1. ctrl+c
    2. rosrun turtlesim turtlesim_node __name:=my_turtle
  6. In terminal 2(ctrl+pg_up):

    rosnode list

Understanding Topics

See the tutorial

#run ros

in a new terminal

rosrun turtlesim turtlesim_node

in a new terminal:

rosrun turtlesim turtle_teleop_key

in a new terminal:

rosrun rqt_graph rqt_graph

in a new terminal:

# list all commands within rostopic
rostopic -h

#how to subscribe to and print a topic
#rostopic echo [topic]
#rostopic echo [topic]
rostopic echo /turtle1/cmd_vel

in a new terminal:

# list full details
rostopic list -v

# list publishers of
#rostopic list -p [topic]
rostopic list -p /turtle1/cmd_vel

#list subscribers to
#rostopic list -s [topic]
rostopic list -s /turtle1/cmd_vel

# list message type
#rostopic type [topic]
rostopic type /turtle1/cmd_vel
# returns geometry_msgs/Twist

#message type details
#rosmsg show [msg]
rosmsg show geometry_msgs/Twist

More details here

Understanding Services and Parameters

See the tutorial

RQTConsole and ROSLaunch

From this tutorial

#roslaunch starts nodes as defined in a launch file.
#roslaunch [package] [filename.launch]

#you will need to source the environment setup file like you did at the end of the create_a_workspace tutorial:
cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials

#Then let's make a launch directory:
mkdir launch
cd launch

echo "<launch>

  <group ns=\"turtlesim1\">
    <node pkg=\"turtlesim\" name=\"sim\" type=\"turtlesim_node\"/>

  <group ns=\"turtlesim2\">
    <node pkg=\"turtlesim\" name=\"sim\" type=\"turtlesim_node\"/>

  <node pkg=\"turtlesim\" name=\"mimic\" type=\"mimic\">
    <remap from=\"input\" to=\"turtlesim1/turtle1\"/>
    <remap from=\"output\" to=\"turtlesim2/turtle1\"/>

</launch>" >> turtlemimic.launch

roslaunch beginner_tutorials turtlemimic.launch

in a new terminal

rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

in a new terminal


Creating a Message and Service

From this tutorial

cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials

mkdir msg
echo "int64 num
string first_name
string last_name
uint8 age
uint32 score" > msg/Num.msg

nano package.xml

add these lines:


save and exit

nano CMakeLists.txt

Add the message_generation dependency to the find_package call which already exists in your CMakeLists.txt so that you can generate messages. You can do this by simply adding message_generation to the list of COMPONENTS such that it looks like this:

# Do not just add this to your CMakeLists.txt, modify the existing text to add message_generation before the closing parenthesis
find_package(catkin REQUIRED COMPONENTS

Also make sure you export the message runtime dependency.

  CATKIN_DEPENDS message_runtime ...

Find the following block of code:

# add_message_files(
#   Message1.msg
#   Message2.msg
# )

Uncomment it by removing the # symbols and then replace the stand in Message*.msg files with your .msg file, such that it looks like this:


By adding the .msg files manually, we make sure that CMake knows when it has to reconfigure the project after you add other .msg files.

Now we must ensure the generate_messages() function is called.

For ROS Hydro and later, you need to uncomment these lines:

# generate_messages(
#   std_msgs
# )

so it looks like:

#rosmsg show [message type]
rosmsg show beginner_tutorials/Num

#Now that we have made some new messages we need to make our package again:

# In your catkin workspace
roscd beginner_tutorials
cd ../..
catkin_make install
cd -

Simple Publisher and Subscriber (Python)

From here

cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials
mkdir scripts
cd scripts
chmod +x
roscd beginner_tutorials/scripts/
chmod +x
cd ~/catkin_ws

Examining Publisher and Subscriber

From here

Make sure that a roscore is up and running:

# In your catkin workspace
cd ~/catkin_ws
source ./devel/setup.bash

#In the last tutorial we made a publisher called "talker". Let's run it:
#rosrun beginner_tutorials talker      (C++)
rosrun beginner_tutorials  # (Python)

In another terminal:

#rosrun beginner_tutorials listener     (C++)
rosrun beginner_tutorials  #(Python)

Recording data (creating a bag file)

  1. In Terminal 1:

  2. In Terminal 2:

    rosrun turtlesim turtlesim_node
  3. In Terminal 3:

    rosrun turtlesim turtle_teleop_key
  4. Start Recording. In Terminal 4:

    rostopic list -v
    mkdir ~/bagfiles
    cd ~/bagfiles
    rosbag record -a -O test.bag --duration=10s
    #Wait ten seconds
    #Examine your bagfile
    rosbag info test.bag
    #Play back at twice the rate
    #rosbag play -r 2 <your bagfile>
    rosbag play -r 2 test.bag
    rosbag record -O subset.bag --duration /turtle1/cmd_vel /turtle1/pose
    rosbag info subset.bag