Você está na página 1de 11

Ros Basics Notes

-prakash raj
First, you'll need to learn some Basic Concepts
Before starting to learn anything, you always need to first be introduced to some
basic concepts. For instance, you can't learn to run before you know how to walk.
And you can't learn to sing before you know how to talk. Right?

Second, you'll need to know about Topicss


Topics

The very first thing you need to know in order to learn ROS is how to
work with topics. Topics are, probably, the most important part of
ROS, so you will need to learn them carefully.

ROS handles almost all its communications through topics. Even more
complex communication systems, such as services or actions, rely, at
the end, on topics. That's why they are so important! Through ROS topics,
you will, for instance, be able to communicate with your robot in order to
make it move, to read your robot's sensor readings, and more.

Examples:
WebShell number #1 in order to start moving the Kobuki
robot.
$ roslaunch publisher_example move.launch
Whenever you want to stop moving the robot, and in order to stop the
i
program, just press Ctrl + C in the WebShell .
Note:You will notice that, even after stopping the program, the robot will
still keep moving. In order to stop it, you will have to execute another
command.
>>roslaunch publisher_example stop.launch
Reason:Even after terminating the command with Ctrl+C, the robot will
still keep moving. This is because the robot will keep listening to the last
message that you published on the topic. You will understand what this
means later.

Awesome, right? You'll probably have a lot of questions about how this
whole process works... What you've actually done is to launch a Publisher,
which is a ROS program that writes a message into a topic, in this case
into the /cmd_vel topic. This topic is the one used to send velocity
commands to the base of the robot. So, by sending a message through
this topic, you've made the robot start moving.

Third, you'll need to know about Services


So now, let's move on. You've seen that topics are a very important part of
ROS, and that they let you communicate with your robot. But is this the
only way?

As you can imagine, the answer is NO. Of course! ROS also provides
services. Services allow you to code a specific functionality for your
robot, and then provide for anyone to call it. For instance, you could create
a service that makes your robot move for a specific period of time, and
then stop.

Services are a little bit more complex than topics since they are structured
in two parts. On one side, you have the Service Server, which provides the
functionality to anyone who wants to use it (call it). On the other side, you
have the Service Client, which is the one who calls/requests the service
functionality.
$ roslaunch service_demo service_launch.launch
$ rosservice call /service_demo "{}"
Navigating through the ROS filesystem:

To get information about the packages and stacks in


our environment, such as their paths,
dependencies, and so on, we can use rospack and
rosstack . On the other hand,to move
through packages and stacks, as well as listing their
contents, we will use roscd and rosls .
For example, if you want to find the path of the
turtlesim package, you can use the following
command:
$ rospack find turtlesim
o/p: /opt/ros/kinetic/share/turtlesim
search meta package
$ rosstack find ros_comm
o/p: /opt/ros/kinetic/share/ros_comm

To list the files inside the pack or stack,


$ rosls turtlesim
o/p: cmake images srv package.xml msg
Changing the current working directory
$ roscd turtlesim
$ pwd
The new path will be as follows:
/opt/ros/kinetic/share/turtlesim

Creating our own workspace:


To see the workspace that ROS is using, use the following command:
$ echo $ROS_PACKAGE_PATH
You will see output similar to the following:

/opt/ros/kinetic/share:/opt/ros/kinetic/stacks
$ mkdir –p ~/dev/catkin_ws/src
$ cd ~/dev/catkin_ws/src
$ catkin_init_workspace

Once we've created the workspace folder, there are no


packages inside—only a
CMakeList.txt file. The next step is building the workspace.
To do this, we use the following
commands:

$ cd ~/dev/catkin_ws
$ catkin_make

To finish the configuration, use the following command:


$ source devel/setup.bash
This step is only for reloading the setup.bash file. You will
obtain the same result if you close andopen a new shell.
$ echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc

Creating an ROS package and metapackage


$ cd ~/dev/catkin_ws/src
catkin_create_pkg [package_name][dependency1]...[dependencyN]
The format of this command includes the nameof the
package and the dependencies that will
have the package, in our case, std_msgs and roscpp .
$ catkin_create_pkg chapter2_tutorials std_msgs roscpp

The following dependencies are included:


std_msgs : This contains common message types representing
primitive data types and
other basicmessage constructs, such as multiarray.
roscpp : This is a C++ implementation of ROS.It provides
a client library that enables
C++ programmers to quickly interface with ROS topics,
services, and parameters.

As we saw earlier, you can use the rospack , roscd ,and


rosls commandsto retrieve information about the new package.
rospack profile : This command informs you about the newly-added
packages to the ROS system. It is useful after installing any new
package.
rospack find chapter2_tutorials : This command helps us find the
path.
rospack depends chapter2_tutorials : This command helps us see
the dependencies.
rosls chapter2_tutorials : This command helps us see the content.
roscd chapter2_tutorials :This command changes the actual path.

Building an ROS package

Once you have your package created and you have some code,it
is necessary to build the package. When the package is
built, the code contained in it is compiled; this includes
not only
the code added by the user, but also the code generated
from the messages and services.
$ cd ~/dev/catkin_ws/
$ catkin_make
$ roscd chapter2_tutorials/
$ catkin_make
When you are in the chapter2_tutorials folder and try
to build the package using
catkin_make , you will get the following error:
The specified base path
"/home/your_user/dev/catkin_ws/src/chapter2_tutorials"
contains a CMakeLists.txtbut "catkin_make"must be invoked
in the root of
workspace
If you execute catkin_make in the catkin_ws folder, you
will obtain a good compilation.
$ catkin_make -–pkg <package name>
As we explained in the Nodes and nodelets section,
nodes are executable programs and, once
built, these executables can be found in the devel
space. To practise and learn about nodes, we
are going to use a typical package called
turtlesim .
If you have installed the desktop installation, you will
have the turtlesim package
preinstalled; if not, install it with the following
command:
$ sudo apt-get install ros-kinetic-ros-tutorials
$ roscore
To get information on nodes,
$ rosnode
Using info ping.. e.t.c you can get the information
$ rosrun turtlesim turtlesim_node
$ rosnode info / turtlesim
we can see the Publications (topics), Subscriptions (topics),
and Services (srv) that the node has and the unique
nameof each.
i
Learning how to interact with topics
rostopic echo TOPIC : This prints messages to the
screen
rostopic find TOPIC : This finds topics by their type
rostopic hz TOPIC : This displays the publishing rate of
topics
rostopic info TOPIC : This prints information about
active topics
rostopic list : This lists the active topics
rostopic pubs TOPIC : This publishes data to the topic
rostopic type TOPIC : This prints the topic type

$ rosrun turtlesim turtle_teleop_key


With this node,we can move the turtle usingthe arrow keys,

new terminal
$ rosnode info /teleop_turtle
$ rostopic list
$ rostopic echo /turtle1/cmd_vel
$ rostopic type /turtle1/cmd_vel
$ rosmsg showgeometry_msgs/Twist

-l, --latch
-r RATE
-1, --once
-f FILE
$ rostopic pub -1 /cmd_vel geometry_msgs/Twist ‘{linear: {x:
0.1, y:0.0, z:0.0}, angular: {x: 0.1, y:0.0, z:0.0}}’
Learning how to use services:

Services are another way through which nodes can


communicate witheachother. Services
allow nodes to senda request and receive a
response.
The tool that we are going to use to interact with
services is called rosservice . The accepted
parameters for this command are as follows:
rosservice args /service : Thisprints the service
arguments
rosservice call /service : Thiscallsthe service withthe
arguments provided
rosservice find msg-type : Thisfinds services by their
service type
rosservice info /service : Thisprints information about
the service
rosservice list : Thislists the active services
rosservice type /service : Thisprints the service type
rosservice uri /service : Thisprints the ROSRPC URI
service

run roscore and run the turtlesimnode:


$ rosservice list
O/P:
/clear
/kill
/reset
/rosout/get_loggers
/rosout/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
type of any service,:
$ rosservice type [list element]/clear
you will use rosservice call [service][args]:
$ rosservice call /clear

different types using code:


This service will create another turtle in another
location with a different orientation.
$ rosservice type /spawn | rossrv show
$ rosservice type /[list element]
$ rosservice type /spawn
$ rossrv show turtlesim/Spawn
$ rosservice call /spawn 3 3 0.2 "new_turtle"

Using Parameter Server:


Parameter Server is used to store datathat is accessible to al
nodes.ROS has a tool called rosparam
rosparam set parameter value : Thissets the
parameter
rosparam get parameter : Thisgets the parameter
rosparam load file :Thisloads parameters from the
file
rosparam dump file :Thisdumps parameters to the
file
rosparam delete parameter : Thisdeletes the
parameter
rosparam list : Thislists the parameter names

rosparam list
$ rosparam get /background_b
$ rosparam set /background_b 100
$ rosparam dump save.yaml
$ rosparam load load.yaml namespace

Você também pode gostar