본문 바로가기

Enginius/Robotics

ROS tutorial (ROS package / node / topic / message / service / parameter/ launch)

http://wiki.ros.org/catkin/Tutorials


Creating a workspace for catkin


source /opt/ros/kinetic/setup.bash

- I have to run this every time since I have two workspaces, one with catkin_ws and the other with ws_baxter running with Baxter. 


cd ~/catkin_ws

- Move into the folder. All packages are in src subfolder. 

catkin_make

- Compile 


source ~/catkin_ws/devel/setup.bash

- This, I also have to run every time. (in my .bashrc file, source ~/ws_baxter/devel/setup.bash overwrites this command.)


echo $ROS_PACKAGE_PATH

- Check ROS package folders. 


Now, it is time for making my own package. 

1.1 With catkin_make

- Followings are typical workflow

cd ~/catkin_ws/src/beginner_tutorials/src

- Add/Edit source files

cd ~/catkin_ws/src/beginner_tutorials

- Update CMakeFiles.txt to reflect any changes 

cd ~/catkin_ws

catkin_make -DCMAKE_BUILD_TYPE=Release

This will build any packages in the source space (~/catkin_ws/src) to the build space (~/catkin_ws/build). Any source files, python libraries, scripts or any other static files will remain in the source space. 



Creating a ROS package


For a package to be considered a catkin package it must meet a few requirements:

- The package must contain a catkin compliant package.xml file.

  - It provides meta information about the package. 

- The package must contain a CMakeList.txt which uses catkin. 

- Each package must have its own folder

  - No nesting nor multiple packages sharing the same directory. 


The simplest possible package might look like this:

my_package/

   CMakeLists.txt

    package.xml


A trivial workspace (catkin_ws) might look like this:

worksapce_folder/

src/

CMakeList.txt             -- 'Top level' CMake file, provided by catkin

package_1/

CMakeLists.txt     

package.xml

...

package_n/

CMakeLists.txt

package.xml


Again, before making a package, run:

source ~/catkin_ws/devel/setup.bash


Move to catkin_ws source folder

cd ~/catkin_ws/src


Create package

catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

- This will create a beginner_tutorials under catkin_ws/src which contains a package.xml and a CMakeLists.txt. 

# This is how we use catkin_create_pkg

catkin_create_pkg <package_name> [depend1] [depend2] [depend3]


Let's build the package

cd ~/catkin_ws

catkin_make


To add the workspace to ROS environment, we need to source the generated setup file:

. ~/catkin_ws/devel/setup.bash

- Do we have to do this every time we build the packages? 


To check the (first order) dependencies, 

rospack depends1 beginner_tutorials

- Then you will see, 

roscpp

rospy

std_msgs


To move the folder, 
roscd beginner_tutorials


To check full dependencies, 

rospack depends beginner_tutorials

cpp_common

rostime

roscpp_traits

roscpp_serialization

catkin

genmsg

genpy

message_runtime

gencpp

geneus

gennodejs

genlisp

message_generation

rosbuild

rosconsole

std_msgs

rosgraph_msgs

xmlrpcpp

roscpp

rosgraph

ros_environment

rospack

roslib

rospy


Now, let us customize the package:

- Modify the package.xml file. 

cd ~/catkin_ws/src/beginner_tutorials

vim package.xml


- Description tag:

 <description>The beginner_tutorials package</description>


- Maintainer tags:

  <!-- One maintainer tag required, multiple allowed, one person per tag --> 

  <!-- Example:  -->

  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->

  <maintainer email="sj@todo.todo">user</maintainer>


- License tags

  <!-- One license tag required, multiple allowed, one license per tag -->

  <!-- Commonly used license strings: -->

  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->

  <license>TODO</license>


- Dependencies tags

  <!-- The *_depend tags are used to specify dependencies -->

  <!-- Dependencies can be catkin packages or system dependencies -->

  <!-- Examples: -->

  <!-- Use build_depend for packages you need at compile time: -->

  <!--   <build_depend>genmsg</build_depend> -->

  <!-- Use buildtool_depend for build tool packages: -->

  <!--   <buildtool_depend>catkin</buildtool_depend> -->

  <!-- Use exec_depend for packages you need at runtime: -->

  <!--   <exec_depend>python-yaml</exec_depend> -->

  <!-- Use test_depend for packages you need only for testing: -->

  <!--   <test_depend>gtest</test_depend> -->

  <buildtool_depend>catkin</buildtool_depend>


  <build_depend>roscpp</build_depend>

  <build_depend>rospy</build_depend>

  <build_depend>std_msgs</build_depend>


  <exec_depend>roscpp</exec_depend>

  <exec_depend>rospy</exec_depend>

  <exec_depend>std_msgs</exec_depend>

- Build tool depend: catkin / build depend / execute depend


Following is the final package.xml without comments:

<?xml version="1.0"?>

<package format="2">

  <name>beginner_tutorials</name>

  <version>0.1.0</version>

  <description>The beginner_tutorials package</description>


  <maintainer email="you@yourdomain.tld">Your Name</maintainer>

  <license>BSD</license>

  <url type="website">http://wiki.ros.org/beginner_tutorials</url>

  <author email="you@yourdomain.tld">Jane Doe</author>


  <buildtool_depend>catkin</buildtool_depend>


  <build_depend>roscpp</build_depend>

  <build_depend>rospy</build_depend>

  <build_depend>std_msgs</build_depend>


  <exec_depend>roscpp</exec_depend>

  <exec_depend>rospy</exec_depend>

  <exec_depend>std_msgs</exec_depend>


</package>


If we run

catkin_make

Then, we will have following folders under ~/catkin_ws/

build / devel / src





Understanding ROS nodes


First, download following tutorials

sudo apt-get install ros-kinetic-ros-tutorials


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. Node can also provide or use a Service. ROS client library allow nodes written in different programming languages to communicate. 


- Run roscore

roscore 


- Check available nodes

rosnode list

/rosout

- This is always running as it collects and logs nodes' debugging output. 


To get more information, 

rosnode info /rosout


To run a node,

rosrun [package_name] [node_name]

rosrun turtlesim turtlesim_node

- We have this!

- To see available noise,

rosnode list

/rosout

/turtlesim

- Now we have, turtlesim

- We can change the name of a node with:

rosrun turtlesim turtlesim_node __name:=my_turtle


- To test whether it is up:

rosnode ping turtlesim

rosnode: node is [/turtlesim]

pinging /turtlesim with a timeout of 3.0s

xmlrpc reply from http://DR-Robotics:42329/ time=0.313997ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.163006ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.190901ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.160860ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.157045ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.123905ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.178026ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.157999ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.161098ms

xmlrpc reply from http://DR-Robotics:42329/ time=1.166105ms


Understanding ROS topics


roscore

- ROS

rosrun turtlesim turtlesim_node

- Run turtle sim node

rosrun turtlesim turtle_teleop_key

- Teleoperation with keyboard 

[ INFO] 1254264546.878445000: Started node [/teleop_turtle], pid [5528], bound on [aqy], xmlrpc port [43918], tcpros port [55936], logging to [~/ros/ros/log/teleop_turtle_5528.log], using [real] time

Reading from keyboard

---------------------------

Use arrow keys to move the turtle.


rosrun rqt_graph rqt_graph

- RQT graph 


Using rostopic echo

Usage:

rostopic echo [topic]


To see topic

rostopic echo /turtle1/cmd_vel

linear: 

  x: 2.0

  y: 0.0

  z: 0.0

angular: 

  x: 0.0

  y: 0.0

  z: 0.0

---


rosrun rqt_graph rqt_graph

- RQT graph 

- We can see that /rostopic_xxx corresponding to rqt_graph


rostopic list -v

- ROS topic list with VERBOSE option 

Published topics:

 * /turtle1/color_sensor [turtlesim/Color] 1 publisher

 * /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher

 * /rosout [rosgraph_msgs/Log] 3 publishers

 * /rosout_agg [rosgraph_msgs/Log] 1 publisher

 * /turtle1/pose [turtlesim/Pose] 1 publisher


Subscribed topics:

 * /turtle1/cmd_vel [geometry_msgs/Twist] 1 subscriber

 * /rosout [rosgraph_msgs/Log] 1 subscriber

 * /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber


ROS Messages


To see the type of rostopic

rostopic type [topic]


rostopic type /turtle1/cmd_vel

geometry_msgs/Twist


To take a look at the details of the message:

rosmsg show geometry_msgs/Twist

geometry_msgs/Vector3 linear

  float64 x

  float64 y

  float64 z

geometry_msgs/Vector3 angular

  float64 x

  float64 y

  float64 z


To send a message:

rostopic pub [topic] [msg_type] [args]

rostopic pub [topic] [msg_type] [args]


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

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

rostopic pub: sends (publishes) 

-1: publish one message then exit

/turtle1/cmd_vel: name of the topic to publish to

geometry_msgs/Twist: The message type to use

--: no option

'[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]': YAML syntax. 

geometry_msgs/Vector3 linear

  float64 x

  float64 y

  float64 z

geometry_msgs/Vector3 angular

  float64 x

  float64 y

  float64 z


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

 -r 1 : This publishes the velocity commands at a rate of 1 Hz on the topic. 


Following the rqt: rostopic pub is shown /rostopic_xxxx


To see the published data, 

rostopic echo /turtle1/pose

---

x: 7.50337123871

y: 5.01182460785

theta: -5.22140264511

linear_velocity: 2.0

angular_velocity: -1.79999995232

---


- To see the rate at which data is published:

rostopic hz /turtle1/pose

subscribed to [/turtle1/pose]

average rate: 59.609

min: 0.015s max: 0.058s std dev: 0.00564s window: 57

average rate: 61.105

min: 0.015s max: 0.058s std dev: 0.00389s window: 120


- To see the type:

rostopic type /turtle1/cmd_vel | rosmsg show

geometry_msgs/Vector3 linear

  float64 x

  float64 y

  float64 z

geometry_msgs/Vector3 angular

  float64 x

  float64 y

  float64 z


- To plot:

rosrun rqt_plot rqt_plot

- And, type 

/turtle1/pose/x

/turtle1/pose/y


ROS Services


rosservice list         print information about active services

rosservice call         call the service with the provided args

rosservice type         print service type

rosservice find         find services by service type

rosservice uri          print service ROSRPC uri


To see existing ROS services:

rosservice list

/clear

/kill

/reset

/rosout/get_loggers

/rosout/set_logger_level

/rostopic_4003_1533676174632/get_loggers

/rostopic_4003_1533676174632/set_logger_level

/rqt_gui_py_node_27253/get_loggers

/rqt_gui_py_node_27253/set_logger_level

/spawn

/teleop_turtle/get_loggers

/teleop_turtle/set_logger_level

/turtle1/set_pen

/turtle1/teleport_absolute

/turtle1/teleport_relative

/turtlesim/get_loggers

/turtlesim/set_logger_level


To check type:

rosservice type [service]

rosservice type /clear

std_srvs/Empty

: This means that the service call is made it takes no arguments. 


To call ROS service:

rosservice call [service] [args]

rosservice call /clear

: This clears the background of the turtle simulator. 


To check the arguments of ROS service:

rosservice type /spawn | rossrv show

float32 x

float32 y

float32 theta

string name

---

string name


To call:

rosservice call /spawn 2 2 0.2 ""

rosservice call /spawn 2 2 0.2 ""

float32 x

float32 y

float32 theta

string name

---

string name


ROS parameter


rosparam list

: Check existing ROS parameters

/background_b

/background_g

/background_r

/rosdistro

/roslaunch/uris/host_dr_robotics__41801

/rosversion

/run_id


To change the background color

rosparam set /background_r 150

rosservice call /clear

: To make it happen 


To get the color

rosparam get /background_g 

86


rosparam get /

background_b: 255

background_g: 86

background_r: 150

rosdistro: 'kinetic

  '

roslaunch:

  uris: {host_dr_robotics__41801: 'http://DR-Robotics:41801/'}

rosversion: '1.12.13

  '

run_id: 64e3ad00-9a7b-11e8-a85a-309c23907600



Using ROS launch


roslaunch [package] [filename.launch]


- Move to exising package

roscd beginner_tutorials

roscd: No such package/stack 'beginner_tutorials'


Above error occurs because we haven't call setup.bash

source devel/setup.bash

roscd beginner_tutorials


Then let's make a launch directory:

mkdir launch

cd launch


Let's create a launch file called 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>


Let's launch the lauch file:

roslaunch beginner_tutorials turtlemimic.launch

: It opens two turtle simulators


rosrun rqt_plot rqt_plot


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

: Publish topic !!













'Enginius > Robotics' 카테고리의 다른 글

ROS cheat sheet  (0) 2018.08.08
ROS tutorial (publisher / subscriber)  (0) 2018.08.08
MuJoCo Walker2d  (0) 2018.07.27
Baxter in Gazebo  (0) 2018.07.27
OpenManipulator  (1) 2018.07.27