3.1. About the Python Interface#

3.1.1. Overview#

To program the HSR, a ROS interface and a Python interface that wraps ROS are provided. In this section, you will learn how to easily operate HSR using the Python interface.

3.1.2. Creating a Workspace#

Create a workspace for using the Python interface. Follow the steps below.

  1. Run the following to install the tools required for the build.

    $ sudo apt update
    $ sudo apt install -y python3-colcon-common-extensions python3-rosdep
    $ sudo rosdep init
    $ rosdep update
    
  2. Run the following to create the workspace, place the required packages, and build them.

    $ source /opt/ros/<ROS_DISTRO>/setup.bash
    $ mkdir -p ~/hsr_ros2_ws/src && cd ~/hsr_ros2_ws/src
    $ git clone -b $ROS_DISTRO https://github.com/hsr-project/hsrb_interfaces.git
    
    $ cd ~/hsr_ros2_ws/
    $ rosdep install --from-paths . -y --ignore-src
    $ colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Release
    $ source install/setup.bash
    

3.1.3. Using the Python Interface#

This section explains how to use the Python interface.

  1. Follow the steps below to launch the console for the Python interface.

    • Launching HSR

      1. Refer to Simulator startup and launch HSR on the simulator.

    • Launching the console

      1. Run the following to set the environment variables. Set ROS_DOMAIN_ID to the value specified when launching the simulator.

        $ export ROS_DOMAIN_ID=XXX
        $ source /opt/ros/<ROS_DISTRO>/setup.bash
        
      2. Run the following to launch the console.

        $ cd ~/hsr_ros2_ws/
        $ source install/setup.bash
        $ ros2 run hsrb_interface_py ihsrb.py --ros-args -p use_sim_time:=true
        
  2. After launching the console, you will see the following console.

    HSR-B Interactive Shell 0.2.0
    
    
          ____________  ______  _________       __  _______ ____
         /_  __/ __ \ \/ / __ \/_  __/   |     / / / / ___// __ \
          / / / / / /\  / / / / / / / /| |    / /_/ /\__ \/ /_/ /
         / / / /_/ / / / /_/ / / / / ___ |   / __  /___/ / _, _/
        /_/  \____/ /_/\____/ /_/ /_/  |_|  /_/ /_//____/_/ |_|
    
    
    In [1]:
    

    By entering commands in this console, you can operate HSR.

  3. To confirm HSR operation, you will use the omni base movement function here.

    Enter up to omni_base. in the console and press the “Tab” key twice.

    In []: omni_base.<Tab><Tab>
    

    You can see several suggestions as shown below.

    In []: omni_base.add_on_set_parameters_callback
     add_on_set_parameters_callback()              destroy_client()                              get_parameter_or()                            is_moving()
     add_waitable()                                destroy_guard_condition()                     get_parameter_type()                          is_succeeded()
     cancel_goal()                                 destroy_node()                                get_parameter_types()                         move()
     clients                                       destroy_publisher()                           get_parameters()                              PARAM_REL_TOL
     context                                       destroy_rate()                                get_parameters_by_prefix()                    pose
     count_publishers()                            destroy_service()                             get_pose()                                    publishers
     count_subscribers()                           destroy_subscription()                        get_publisher_names_and_types_by_node()       remove_on_set_parameters_callback()
     create_client()                               destroy_timer()                               get_publishers_info_by_topic()                remove_waitable()
     create_follow_trajectory_goal()               execute()                                     get_service_names_and_types()                 resolve_service_name()
     create_go_pose_goal()                         executor                                      get_service_names_and_types_by_node()         resolve_topic_name()
     create_guard_condition()                      follow_trajectory()                           get_state()                                   services
     create_publisher()                            get_client_names_and_types_by_node()          get_subscriber_names_and_types_by_node()      set_descriptor()
     create_rate()                                 get_clock()                                   get_subscriptions_info_by_topic()             set_parameters()
     create_service()                              get_fully_qualified_name()                    get_topic_names_and_types()                   set_parameters_atomically()
     create_subscription()                         get_logger()                                  go()                                          subscriptions
     create_timer()                                get_name()                                    go_abs()                                      timers
     declare_parameter()                           get_namespace()                               go_pose()                                     undeclare_parameter()
     declare_parameters()                          get_node_names()                              go_rel()                                      waitables
     default_callback_group                        get_node_names_and_namespaces()               guards
     describe_parameter()                          get_node_names_and_namespaces_with_enclaves() handle
     describe_parameters()                         get_parameter()                               has_parameter()
     function(callback: Callable[[List[rclpy.parameter.Parameter]], rcl_interfaces.msg._set_parameters_result.SetParametersResult])
    
  4. From the options above, use the go_rel function.

    By appending “?” after omni_base.go_rel and executing it, you can view the reference.

    In []: omni_base.go_rel?
    Signature: omni_base.go_rel(x=0.0, y=0.0, yaw=0.0, timeout=0.0)
    Docstring:
    Move base from current position.
    
    Args:
        x   (float): X-axis position on ``robot`` frame [m]
        y   (float): Y-axis position on ``robot`` frame [m]
        yaw (float): Yaw position on ``robot`` frame [rad]
        timeout (float): Timeout until movement finish [sec].
            Default is 0.0 and wait forever.
    
    Examples:
        .. sourcecode:: python
    
           with hsrb_interface.Robot() as robot:
               base = robot.try_get('omni_base')
               base.go_rel(1.0, 0.0, 0.0)
    File:      ~/hsr_ros2_ws/install/hsrb_interface_py/local/lib/python3.10/dist-packages/hsrb_interface/mobile_base.py
    Type:      method
    

    From the displayed reference, you can confirm that it takes four arguments.

    In this way, you can interactively inspect functions while executing them. You can also inspect other functions in the same way.

  5. Run the following to confirm that HSR moves 0.5 [m] to the left.

    In []: omni_base.go_rel(0.0, 0.5, 0.0, 100.0)
    

    This command specifies x = 0.0 [m], y = 0.5 [m], yaw-axis rotation = 0.0 [rad], and a timeout of 100.0 [s].

  6. To exit, press “Ctrl” + “D” or enter “quit” in the console.