Tuesday, 5 November 2019

Robot Operating System - ROS tutorial

Introduction ROS
ROS is A meta operating system prhttp://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning
  http://docs.ros.org/jade/api/catkin/html/user_guide/variables.htmlovides the services you would expect from an operating system, including hardware abstraction, low-level device control, implementation of commonly-used functionality, message-passing between processes, and package management. It also provides tools and libraries for obtaining, building, writing, and running code across multiple computers. A meta operating system is built on top of the operating system and allows different processes (nodes) to communicate with each other at runtime.
  The ROS runtime "graph" is a peer-to-peer network of processes (potentially distributed across machines) that are loosely coupled using the ROS communication infrastructure. ROS implements several different styles of communication, including synchronous RPC-style communication over services, asynchronous streaming of data over topics, and storage of data on a Parameter Server.

1. Navigating the ROS Filesystem

  Note: other ROS tools, will only find ROS packages that are within the directories listed in your ROS_PACKAGE_PATH (e.g: echo $ROS_PACKAGE_PATH)
  - rospack allows you to get information about packages (e.g: rospack find roscpp)
  - rospack depends1 beginner_tutorials (list dependencies of beginner_tutorials)
  - rospack depends beginner_tutorials (list all nested dependencies)
  - roscd is part of the rosbash suite. It allows you to change directory (cd) directly to a package or a stack (e.g: roscd roscpp)
  - roscd log will take you to the folder where ROS stores log files (e.g: 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 (e.g: rosls roscpp_tutorials)
  - roscd roscpp_tut<<< now push the TAB key >>> (TAB completion)

2. Creating a ROS Package

  2.1 What makes up a catkin Package? 
     Following structure:
      my_package/
        CMakeLists.txt
        package.xml
  2.2 Packages in a catkin Workspace
      workspace_folder/        -- WORKSPACE
        src/                   -- SOURCE SPACE
          CMakeLists.txt       -- 'Toplevel' CMake file, provided by catkin
          package_1/
            CMakeLists.txt     -- CMakeLists.txt file for package_1
            package.xml        -- Package manifest for package_1
          ...
          package_n/
            CMakeLists.txt     -- CMakeLists.txt file for package_n
            package.xml        -- Package manifest for package_n

  E.g: Steps to create ROS package
      Step 1: Create workspace
      $ source /opt/ros/kinetic/setup.bash
      $ mkdir -p ~/catkin_ws/src
      $ cd ~/catkin_ws/
      $ catkin_make    (catkin_make -DCMAKE_BUILD_TYPE=Release)
      $ source devel/setup.bash
      $ echo $ROS_PACKAGE_PATH  =>  /home/youruser/catkin_ws/src:/opt/ros/kinetic/share
      Step 2: Create package
      $ cd ~/catkin_ws/src
      $ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp (catkin_create_pkg requires that you give it a package_name and optionally a list of dependencies on which that package depends # catkin_create_pkg <package_name> [depend1] [depend2] [depend3])
      Step 3: Building a catkin workspace and sourcing the setup file
      $ cd ~/catkin_ws
      $ catkin_make
      $ catkin_make install
      Step 4: add the workspace to your ROS environment
      $ . ~/catkin_ws/devel/setup.bash

  Note 1: structure below:
                build
                devel
                src
  The build folder is the default location of the build space and is where cmake and make are called to configure and build your packages. The devel folder is the default location of the devel space, which is where your executables and libraries go before you install your packages.

  Note 2: catkin CMakeLists.txt
  http://wiki.ros.org/catkin/CMakeLists.txt

4. Understanding ROS Nodes
  Description: This tutorial introduces ROS graph concepts and discusses the use of roscore, rosnode, and rosrun commandline tools.
  4.1 Parts
  Nodes: A node is an executable that uses ROS to communicate with other nodes.
  Messages: ROS data type used when subscribing or publishing to a topic.
  Topics: Nodes can publish messages to a topic as well as subscribe to a topic to receive messages.
  Master: Name service for ROS (i.e. helps nodes find each other)
  rosout: ROS equivalent of stdout/stderr
  roscore: Master + rosout + parameter server
  4.2 Nodes
  A node really isn't much more than an executable file within a ROS package. ROS nodes use a ROS client library to communicate with other nodes. Nodes can publish or subscribe to a Topic. Nodes can also provide or use a Service.
  4.3 Client Libraries
  ROS client libraries allow nodes written in different programming languages to communicate:
      rospy = python client library
      roscpp = c++ client library
  4.4 Applications
    - roscore is the first thing you should run when using ROS ($ roscore)
    - rosnode displays information about the ROS nodes that are currently running ($ rosnode list)
    - rosnode info command returns information about a specific node ($ rosnode info /rosout)
    - try cleaning the rosnode list with ($ rosnode cleanup)
    - ping, to test that ros node's up ($ rosnode ping my_turtle)
    - rosrun allows you to use the package name to directly run a node within a package ($ rosrun turtlesim turtlesim_node) or (change node name $ rosrun turtlesim turtlesim_node __name:=my_turtle)

5. Understanding ROS Topics
  Run demo:
    - $ roscore (check roscore is running: ps -ef | grep -i rosmaster)
    - $ rosrun turtlesim turtlesim_node
    - $ rosrun turtlesim turtle_teleop_key
    - turtlesim_node and the turtle_teleop_key node are communicating with each other over a ROS Topic. turtle_teleop_key is publishing the key strokes on a topic, while turtlesim subscribes to the same topic to receive the key strokes.

  rqt_graph creates a dynamic graph of what's going on in the system
    - $ sudo apt-get install ros-<distro>-rqt
    - $ sudo apt-get install ros-<distro>-rqt-common-plugins
    - $ rosrun rqt_graph rqt_graph

  The rostopic tool allows you to get information about ROS topics.
    - $ rostopic -h
          rostopic bw     display bandwidth used by topic
          rostopic echo   print messages to screen
          rostopic hz     display publishing rate of topic   
          rostopic list   returns a list of all topics currently subscribed to and published
          rostopic pub    publish data to topic
          rostopic type   print topic type
    -  $ rostopic list -h
          Usage: rostopic list [/topic]
          Options:
            -h, --help                  show this help message and exit
            -b BAGFILE, --bag=BAGFILE   list topics in .bag file
            -v, --verbose               list full details about each topic
            -p                          list only publishers
            -s                          list only subscribers


6. ROS Messages
  - rostopic type returns the message type of any topic being published ($ rostopic type /turtle1/cmd_vel)
  - rosmsg looks at the details of the message ($ rosmsg show geometry_msgs/Twist)
  - rostopic pub publishes data on to a topic currently advertised (rostopic pub [topic] [msg_type] [args])
  - $ rostopic pub -1 /turtle1/command_velocity turtlesim/Velocity  -- 2.0  1.8) (-1 : causes rostopic to only publish one message then exit)
  - $ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'  (-r 1 : the turtle requires a steady stream of commands at 1 Hz to keep moving)

7. ROS Services
  Services are another way that nodes can communicate with each other. Services allow nodes to send a request and receive a response.
  - rosservice has many commands that can be used on services, as shown below:
    rosservice list         print information about active services
    rosservice call         call the service with the provided args ($ rosservice call /clear)
    rosservice type         print service type ($ rosservice type /spawn | rossrv show)
    rosservice find         find services by service type
    rosservice uri          print service ROSRPC uri
  - rosparam allows you to store and manipulate data on the ROS Parameter Server.
      - rosparam set            set parameter ($ rosparam set /background_r 150)
      - rosparam get            get parameter ($ rosparam get /background_g )
      - rosparam load           load parameters from file ($ rosparam load params.yaml copy)
      - rosparam dump           dump parameters to file ($ rosparam dump params.yaml)
      - rosparam delete         delete parameter
      - rosparam list           list parameter names ($ rosparam list)
    rosparam get / to show us the contents of the entire Parameter Server.

8. rqt_console and roslaunch
  8.1 Install: sudo apt-get install ros-<distro>-rqt ros-<distro>-rqt-common-plugins ros-<distro>-turtlesim
  8.2 rqt_console and rqt_logger_level
    - for debugging and roslaunch for starting many nodes at once.
    - rqt_console attaches to ROS's logging framework to display output from nodes. rqt_logger_level allows us to change the verbosity level (DEBUG, WARN, INFO, and ERROR) of nodes as they run.
  8.3 roslaunch
    - starts nodes as defined in a launch file ($ roslaunch [package] [filename.launch])
    $ cd ~/catkin_ws
    $ source devel/setup.bash
    $ roscd beginner_tutorials
    Then let's make a launch directory:
    $ mkdir launch
    $ cd launch
    $ cat > turtlemimic.launch
        <launch>
          <group ns="turtlesim1">
            <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
          </group>
          <group ns="turtlesim2">
            <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
          </group>
          <node pkg="turtlesim" name="mimic" type="mimic">
            <remap from="input" to="turtlesim1/turtle1"/>
            <remap from="output" to="turtlesim2/turtle1"/>
          </node>
        </launch>
    $ roslaunch beginner_tutorials turtlemimic.launch
    $ rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

9. Creating a ROS msg and srv
  Description: This tutorial covers how to create and build msg and srv files as well as the rosmsg, rossrv and roscp commandline tools.
  - msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.
  - srv: an srv file describes a service. It is composed of two parts: a request and a response.
  - msg files are stored in the msg directory of a package, and srv files are stored in the srv directory.
  - Here is an example of a msg that uses a Header, a string primitive, and two other msgs :
        Header header
        string child_frame_id
        geometry_msgs/PoseWithCovariance pose
        geometry_msgs/TwistWithCovariance twist
  - srv files are just like msg files, except they contain two parts: a request and a response. The two parts are separated by a '---' line. Here is an example of a srv file:
        int64 A
        int64 B
        ---
        int64 Sum
  9.1 Creating a msg
      $ roscd beginner_tutorials
      $ mkdir msg
      $ echo "int64 num" > msg/Num.msg
      Open package.xml, and make sure these two lines are in it:
          <build_depend>message_generation</build_depend>
          <exec_depend>message_runtime</exec_depend>
      Open CMakeLists.txt:
        Add the message_generation:
            find_package(catkin REQUIRED COMPONENTS
               roscpp
               rospy
               std_msgs
               message_generation
            )
        Export the message runtime dependency:
            catkin_package(
              ...
              CATKIN_DEPENDS message_runtime ...
              ...)
        Uncomment and update:
            add_message_files(
              FILES
              Num.msg
            )
        Uncomment:
            generate_messages(
              DEPENDENCIES
              std_msgs
            )
      $ rosmsg show beginner_tutorials/Num
      $ rosmsg show Num
  9.2 Creating a srv
      $ roscd beginner_tutorials
      $ mkdir srv
      $ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv (Instead of creating a new srv definition by hand, we will copy an existing one from another package)
      Open package.xml, and make sure these two lines are in it:
          <build_depend>message_generation</build_depend>
          <exec_depend>message_runtime</exec_depend>
      Open CMakeLists.txt:
          Add the message_generation:
              find_package(catkin REQUIRED COMPONENTS
                 roscpp
                 rospy
                 std_msgs
                 message_generation
              )
          Uncomment:
              add_service_files(
                FILES
                AddTwoInts.srv
              )
      $ rossrv show beginner_tutorials/AddTwoInts
      $ rossrv show AddTwoInts
     
Writing a Simple Publisher and Subscriber (C++):
http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
Writing a Simple Service and Client (C++): 
http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29

10. Recording and playing back data
  Description: This tutorial will teach you how to record data from a running ROS system into a .bag file, and then to play back the data to produce similar behavior in a running system.
  Open 3 Terminals and Run:
    $ roscore
    $ rosrun turtlesim turtlesim_node
    $ rosrun turtlesim turtle_teleop_key

  10.1 Recording all published topics:
    $ rostopic list -v
    $ mkdir ~/bagfiles
    $ cd ~/bagfiles
    $ rosbag record -a
  10.2 playing the bag file
    $ rosbag info <your bagfile>
    $ rosbag play -s 2 -r 2 <your bagfile> (-s: start some duration past the beginning, -r: change the rate of publishing)

  10.3 Recording a subset of the data
    $ rosbag record -O subset /turtle1/cmd_vel /turtle1/pose (-O argument tells rosbag record to log to a file named subset.bag)

11. roswtf
  Description: roswtf is a tool for diagnosing issues with a running ROS system.
  E.g: Checking your installation
    $ roscd rosmaster
    $ roswtf
12. Packaging your ROS project as a snap
  Description: This tutorial covers how to package and deploy your ROS project as a snap. Snaps are packages that bundle an application and its dependencies.
  $ sudo snap install --classic snapcraft #First, install Snapcraft.
  $ cd catkin_ws/
  $ source devel/setup.bash
  $ roscd beginner_tutorials/
  $ nano CMakeLists.txt
      ## Install talker and listener
        install(TARGETS talker listener
          RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
      )
  $ cd ~/catkin_ws
  $ snapcraft init
  $ nano snap/snapcraft.yaml
      name: publisher-subscriber
      version: '0.1'
      summary: ROS Example
      description: |
        The ROS publisher/subscriber example packaged as a snap.

      grade: stable
      confinement: strict

      parts:
        workspace:
          plugin: catkin
          rosdistro: kinetic
          catkin-packages: [beginner_tutorials]

      apps:
        roscore:
          command: roscore
          plugs: [network, network-bind]

        talker:
          command: rosrun beginner_tutorials talker
          plugs: [network, network-bind]

        listener:
          command: rosrun beginner_tutorials listener
          plugs: [network, network-bind]
  $ cd ~/catkin_ws
  $ snapcraft
  $ sudo snap install --dangerous publisher-subscriber_0.1_amd64.snap
  $ publisher-subscriber.roscore
  $ publisher-subscriber.talker
  $ publisher-subscriber.listener

13. Running ROS across multiple machines
  Description: This tutorial explains how to start a ROS system using two machines. It explains the use of ROS_MASTER_URI to configure multiple machines to use a single master. Say we want to run a talker / listener system across two machines, named marvin and hal.
  $ ssh hal
  $ roscore
  $ export ROS_MASTER_URI=http://hal:11311
  $ rosrun rospy_tutorials listener.py
  $ ssh marvin
  $ export ROS_MASTER_URI=http://hal:11311
  $ rosrun rospy_tutorials talker.py
  Test messages in/out:
  $ rostopic list
  $ rostopic echo /topic_name

Ref: http://wiki.ros.org/ROS/Tutorials

0 comments:

Post a Comment