Você está na página 1de 150

:

1395

1
2 -1-1 ..................................
5 -2-1 .............................. .
7 -3-1 ..................................
7 -4-1 .............................
9 -5-1 ...................................

10
11 -1-2 ................................
13 -2-2 .............................
14 -3-2 ................. turtlesim
16 -4-2 ..................................
19 -5-2 ...................................
20 -6-2 ...................................
22 -7-2 ............................
23 -1-7-2 ............................
25 -2-7-2 ........................
32 -8-2 ..............................
33 -1-8-2 ...........
34 -2-8-2 .................. .
35 -9-2 ............................
35 -10-2 ..........................

36
37 -1-3 ...................
39 -2-3 ! ................................
41 -1-2-3 .....................Hello
44 -2-2-3 ....................... hello
45 -3-3 ......................
46 -1-3-3 ............................
49 -2-3-3 ...........................
51 -3-3-3 ....................... pubvel
51 -4-3-3 .......................... pubvel


-4-3 ( ) 52 ......................
-1-4-3 57 ............. subpose
-5-3 57 ..................................

58
59 -1-4 ................................
60 -2-4 .................................
62 -3-4 ............................
65 -4-4 ...........................
66 -1-4-4...............................
67 -2-4-4 ......................... rosout
69 -3-4-4 ...........................
70 -5-4 ................
73 -6-4 ..................................

74
75 (..................... )Global names -1-5
76 ............................. -2-5
78 ............................ -3-5
79 ............................. -4-5
80 .................................. -5-5

81
-1-6 82 .........................
-2-6 85 .............................
-1-2-6 85 ................
-2-2-6 86 .............................
-3-6 92 ..............................
-1-3-6 92 ..........................
-2-3-6 93 ...................
-4-6 98 ......................
-1-4-6 99 ....................
-2-4-6 100 ........................
-3-4-6 102 .........................
-5-6 103 .................................


104
105 ................... -1-7
107 : ...................... turtlesim -2-7
109 .......................:C++ -3-7
113 .................... -4-7
115 ................................. -5-7

116
117 -1-8 .........................
118 -2-8 ............ command line
122 -3-8 ......................
127 -4-8 ...............................
130 -1-4-8 .............
131 -5-8 .................................

132
133 ............ bag -1-9
135 : bag ........................ -2-9
139 Bags ....................... -3-9
140 ................................. -4-9

141
-1-10 142 ...............................
-2-10 144 .............................

........................................
( )1-1 6 ................................
( )1-2 turtlesim 15 ..............
( rqt_graph)2-2 turtlesim . rosout
23 .............................. .
( )3-2 turtlrsim rqt_graph
23 .................................. .
( (4-2 turtlesim
25 ..................... .
( (5-2 turtlesim A B
C 33 ....................................D
( )1-3 turtlesim 52 .... pubvel
(( GUI)1-4 ) 68 ................... rqt_console
( GUI)2-4 72 ........................ .rqt_logger_level
( )1-6 ( )
doublesim.launch 90 ............................ .
( )2-6 reverse_cmd_vel
96 .............................turtlesim
( (3-6 .reversed.launch remap
97 ............................ .
( )1-7 () () 109 .... .turtlesim
( )1-8 pubvel_toggle
130 ................................... ./toggle_forward
( )1-9 136 .......... . rosbag record
( )2-9 137 ............ rosbag play
(( )3-9) .draw_square
rosbag ( .) ,bag
137 ............................... .

-1-1

.

.
.
.
() .
:

.
- - - .

1
.

.
.
.
:
. :

.

1 http://wiki.ros.org/ROS/Introduction

. "
" 1 .

.

.

.
.
:
.
-
- .
.

.
-

.
2.
.

.
:
.

1 complexity via composition


2
http://www.ros.org/browse

.
.

.

.

.

. ( )bag
( )rosbag
9 .
.
.

().
.
.
.

.
.
4
C++1 . 2 3
5 .

1
http://wiki.ros.org/roscpp
2
http://wiki.ros.org/rospy
3
http://wiki.ros.org/rosjava
4
http://wiki.ros.org/roslisp
5
http://wiki.ros.org/ClientLibraries

. (command-
)line ( )build .
( ) Integrated Development Environment.
1IDE
. command line IDE
.

-2-1

.
.
.
.
C++
.
2. 3.

.
.
.
( ) .
(
!!) .
.

1
http://wiki.ros.org/IDEs
2
http://wiki.ros.org/ROS/Tutorials
3
http://wiki.ros.org/APIs

: 1-1 .
.
.

. C++
14,04 bash
.

( )1-1

-3-1

.

.
.

.
.

.

-4-1

.
.
.

1.
.

.
) (forum
2
.

1
http://wiki.ros.org
2
http://answers.ros.org


1
.

.
:
( 2.
). ( )indigo .
jade 2015 .3
Indigo, hydro, groovy, fuerte, electric, diamondback, C turtle, box Turtle
. .
indigo

hydro
.
groovy . :
groovy ( ) trutle-
sim
.
groovy :
( )44
turtlesim geometry_msgs .
( turtlesim/Velocity.h )46
.geometry_msgs/Twist.h
turtlesim/Velocity ( linear) ( angular)
. linear.x angular.z geometry_msgs/Twist .
( command line )29 ( C++ )45 .

1
http://lists.ros.org/mailman/listinfo/ros-users
2
http://wiki.ros.org/Distributions
3
http://wiki.ros.org/jade

: groovy
. groovy rosbuild . catkin
rosbuild .
rosbuild catkin
. catkin rosbuild
1
.

-5-1

1
http://wiki.ros.org/catkin_or_rosbuild

10

.

.
.
( )
.

-1-2

.
ros-indigo-turtlesim
2-2 .
1 2.

.
) :(ROS repository ( )

/etc/apt/sources.list.d/ros-latest.list

deb http://packages.ros.org/ros/ubuntu trusty main
Ubuntu 14.04 14,04 trusty .
13 10 saucy trusty .


. 14,04
3

1
http://wiki.ros.org/ROS/Installation
2
http://wiki.ros.org/indigo/Installation/Ubuntu
3
http://wiki.ros.org/indigo/Installation/Source

11

.
lsb_release -a
.

:
. :
wget https://raw.githubusercontent.com/ros/rosdistro/master/ros.key
ros.key .
.
sudo apt-key add ros.key
( apt-key OK ) ros.key
.

:
:
sudo apt-get update

.

: .
:
sudo apt-get install ros-indigo-desktop-full
- -
.
ros-indigo-desktop ros-indigo-ros-base
.

:turtlesim
.
turtlesim . :
sudo apt-get install ros-indigo-turtlesim

:rosdep :
sudo rosdep init

12

rosdep.
rosdep
1 .
rosdep ) (front end apt-get . rosdep

. rosdep
.

rosinstall
23
deb .
rosinstall .

-2-2

rosdep : rosdep
:
rosdep update
home .ros .
.
rosdep init rosdep update
sudo.

1
http://wiki.ros.org/rosdep
2
http://wiki.ros.org/rosinstall
3
http://www.ros.org/doc/independent/api/rosinstall/html/

13
:
. setup.bash
1
. source
source /opt/ros/indigo/setup.bash

export | grep ROS
(
ROS_DISTRO )ROS_PACKAGE_PATH
. setup.bash .
command not found
setup.bash ()shell
.

source .


.
setup.bash
. .bashrc home
source .
setup.bash bash
roscd rosls .
2
rosbash .

-3-2 turtlesim

.
:

1
http://wiki.ros.org/rosbash
2
http://wiki.ros.org/rosbash

14
turtlesim 1

( ) .

( )1-2 turtlesim .

:turtlesim
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
.
2,1 .

( .
mascot ). (
turtle_teleop_key )
.

turtle_teleop_key .

.

1
http://wiki.ros.org/turtlesim

15
1.

.

.

-4-2

.

. turtlesim_node turtle_teleop_key
turtlesim.

deb .
deb

ROS .
.
3,1 .
.

:
23
:
rospack list
188 .
( ) package.xml .
.
package.xml ( . :

1 1987 .
2
http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem
3
http://wiki.ros.org/rospack

16
package.xml .).
.

catkin

. apt-get
/opt/ros/indigo . lib .

CMAKE_PREFIX_PATH setup.bash
. catkin
groovy fuerte .

.
rospack find :
rospack find package-name

. rospack tab
.
rospack find turtle

Enter Tab
turtle .
Tab
. Tab rospack
find .
tab
.
. :
.
: :
rosls package-name

17


roscd package-name

1 $ rosls turtlesim
2 cmake
3 images
4 msg
5 package.xml
6 srv
7 $ rosls turtlesim/images
8 box-turtle.png
9 fuerte.png
10 hydro.svg
11 palette.png
12 turtle.png
13 diamondback.png
14 groovy.png
15 indigo.png
16 robot-turtle.png
17 electric.png
18 hydro.png
19 indigo.svg
20 sea-turtle.png
21 $ roscd turtlesim/images/
22 $ eog box-turtle.png

( )1-2 rosls roscd turtlesim


. eog Eye of Gnome.


turtlesim . ( )1-2 rosls roscd
.

18
1 .
. groovy
- 2 3.
. -

.

-5-2

.
.

.
.
. :
roscore
turtlesim .
roscore : .
.
roscore .
roscore .
Ctrl-C .
roscore .
( )4
( )7

1
http://wiki.ros.org/rosbuild/Stacks
2
http://wiki.ros.org/catkin/conceptual_overview
3
http://wiki.ros.org/catkin/package.xml

19

. roscore

roscore .
roscore . 6


.

-6-2

roscore
1
. .
" " .

. 2,8 .
turtlesim .
turtlesim_node . turtlesim
. turtle_teleop_key teleop .
teleoperation
.
turtlesim_node .

: (
2
) rosrun:
rosrun package-name executable-name
rosrun . . 2,4
. .

1
http://wiki.ros.org/ROS/Tutorials/UnderstandingNodes
2
http://wiki.ros.org/rosbash#rosrun

20
rosrun : shell

. rosrun
. turtlesim_node
:
/opt/ros/indigo/lib/turtlesim/turtlesim_node
.rosrun
:
1
. :
rosnode list
2,3
:
/rosout
/teleop_turtle
/turtlesim
:
/rosout roscore .
( )std::cout
. /rosout 4,4,2 .
/ / rosout global
namespace . .
5 .
: turtlesim
teleop_turtle 2,3 .
rosnode list rosrun
2,3
.
rosrun .
rosrun package-name executable-name __name:=node-name

1
http://wiki.ros.org/rosnode

21
.
( .
2,8 __name
). __name
6
.
: :
rosnode kill node-name


.
Ctrl-C .
.
rosnode list .
.
:
rosnode cleanup

-7-2

turtlesim
.

.
1.

.

.

1
http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics

22

( )
/
:
rqt_graph
r ROS qt Qt GUI toolkit
. ) (GUI
. 2,2 .

. /teleop_turtle
/turtle1/cmd_vel /turtlesim/ .
( cmd_vel command velocity ).

( rqt_graph )2-2 turtlesim . rosout


.

( )3-2 turtlrsim rqt_graph


.

23
rosout 2,6 .
rqt_graph .
Hide debug . 2,3
.
rqt_graph .
/rosout /rosout
. log
. 4 .
/rosout .
/rosout
. /rosout

.
rqt_graph .
Nodes only ( Nodes/Topics (all .
Hide Debug . 2,4
.
turtlesim
. rqt_graph

.

.
.
.
turtlesim .
/ teleop_turtle
/turtle1/cmd_vel . turtlesim_node
.
:

24
( ) cmd_vel
.
cmd_vel
.
.

( )4-2 turtlesim
.

/turtle1
turtle1 . 8
turtlesim .


.
.

25
1
: :
rostopic list
:
/rosout
/rosout_agg
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose
rqt_graph
.

:
:
rostopic echo topic-name
. 2,2
:
rostopic echo /turtle1/cmd_vel
/teleop_turtle . ---
. .

1
http://wiki.ros.org/rostopic

26
1 linear :
2 x : 2.0
3 y : 0.0
4 z : 0.0
5 angular :
6 x : 0.0
7 y : 0.0
8 z : 0.0
9
10 linear :
11 x : 0.0
12 y : 0.0
13 z : 0.0
14 angular :
15 x : 0.0
16 y : 0.0
17 z : 2.0
18
19 linear :
20 x : 2.0
21 y : 0.0
22 z : 0.0
23 angular :
24 x : 0.0
25 y : 0.0
26 z : 0.0
27

( )2-2 rostopic echo

:
:
rostopic hz topic-name
rostopic bw topic-name

.


.
: rostopic info
:
27
rostopic info topic-name
:
rostopic info /turtle1/color_sensor
:
Type: turtlesim/Color
Publishers:
)* /turtlesim (http://donatello:46397/
Subscribers: None
.
turtle1/color_sensor/ turtlesim/Color . type
.
.
.
:1 2 :
rosmsg show message-type-name
/turtle1/color_sensor :
rosmsg show turtlesim/Color
:
uint8 r
uint8 g
uint8 b
( ) . ( )int8 , bool, string
. turtlesim/Color
( )unsigned 8-bit integer r , g ,b .
turtlesim/Color ( .

. ) .
geometry_msgs/Twist . /turtle1/cmd_vel
:
geometry_msgs/Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular
float64 x

1
http://wiki.ros.org/rosms
2
http://wiki.ros.org/msg

28
float64 y
float64 z
linear angular
geometry_msgs/Vector3 . x , y , z
. geometry_msgs/Twist
linear angular . float64
16 .

. C++
.
.
geometry_msgs/Vector3
. x , y , z.

.
std_msgs/Header timestamp
coordinate frame . header
.
rosmsg

.
( []
) ( ) . turtlesim
. sensor_msgs/NavSatFix
GPS .

:command line
( ) .
1
:
rostopic pub -r rate-in-hz topic-name message-type message-content

1
http://wiki.ros.org/rostopic

29

.
message content
. :
]rostopic pub -r 1 /turtle1/cmd_vel geometry_msgs/Twist [2, 0, 0] [0, 0, 0
rosmsg show .
linear
angular . ( ) . . . ( [ ) ] . . .
.
( )x
.
( z )
.
]rostopic pub -r 1 /turtle1/cmd_vel geometry_msgs/Twist [0, 0, 0] [0, 0, 1
linear.x angular.z
geometry_msgs/Twist turtlesim .
turtlesim
.


.
YAML YAML ( .
Aint Markup Language1 ) . (
)
.
rostopic pub /turtle1/cmd_vel geometry_msgs/Twist "linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
"z: 0.0

1
http://www.yaml.org/

30
bash YAML
YAML command line 1 2.
tab .
tab YAML
tab . ( false
)... .
rostopic pub :
-r rostopic pub
. ( dash 1-
)one ( )dash ell 1-

. .
-f
( -f ) .
rostopic echo.

rostopic echo rostopic pub
. ( rosbag )9
.
: .
/ :
package-name/type-name
turtlesim/Color :

1
http://wiki.ros.org/YAMLOverview
2
http://wiki.ros.org/ROS/YAMLCommandLine

31
:

. geometry_msgs/Pose turtlesim/Pose
( ).
3
.

.
.
ModelState gazebo/ModelState
Gazebo
.

-8-2


.
2,3
.
roscore . .
:
rosrun turtlesim turtlesim_node __name:=A
rosrun turtlesim turtlesim_node __name:=B
rosrun turtlesim turtle_teleop_key __name:=C
rosrun turtlesim turtle_teleop_key __name:=D
turtlesim
turtlesim .
rosrun __name.
.
.
32
( )5-2 turtlesim A B
C .D


:
[ WARN] [1369835799.391679597]: Shutdown request received.
[ WARN] [1369835799.391880002]: Reason given for shutdown:
][new node registered with same name
.

.

. rqt_graph

2,5
. .


.3(. 6
) .
/turtle1/cmd_vel .

.
33
C A B
. D A B
. .
-- .
.

turtlesim
() .
.
( .
100 turtlesim_node active x client
! )
turtlesim
/turtle1/cmd_vel
.
turtlesim -
.
.

.
( ) turtle_teleop_key
.
( ) turtlesim_node

.

-- .
. 8
.

34
-9-2

()
roswtf (1 2.
? Why The Failure
). :

roswtf


. roswtf rosdep

. roswtf
.

-10-2


.
.

1
http://wiki.ros.org/roswtf
2
http://wiki.ros.org/ROS/Tutorials/Gettingstartedwithroswtf

35

36

.

. .
hello world
.

-1-3

2,4
.
.

:
1. /home/jokane/ros .

. mkdir
. .
.
catkin 3,2,2
.

.
. () src
.
.
: src
2
:

1
http://wiki.ros.org/catkin/Tutorials/create_a_workspace
2
http://wiki.ros.org/ROS/Tutorials/CreatingPackage

37

catkin_create_pkg package-name
:
.
package.xml 2,4
.
CMakeLists.txt

- ( ) CMake .
:
include .
CMake catkin .


. catkin_create_pkg
.
.
src

catkin .


. .
catkin .

agitr
.
src :

catkin_create_pkg agitr

( ) : package.xml
- .

38

catkin_create_pkg .

.
) (description ) )maintainer .
3,1 agitr .

1 >?"<?xml version="1.0
2 ><package
3 ><name>agitr</name
4 ><version>0.0.1</version
5 ><description
6 Examples from A Gentle Introduction to ROS
7 ></description
8 >"<maintainer email="jokane@cse.sc.edu
9 Jason O' Kane
10 ></maintainer
11 ><license>TODO</license
12 ><buildtool_depend>catkin</buildtool_depend
13 ><build_depend>geometry_msgs</build_depend
14 ><run_depend>geometry_msgs</run_depend
15 ><build_depend>turtlesim</build_depend
16 ><run_depend>turtlesim</run_depend
17 ></package

( )1-3 ( )package.xml agitr

-2-3 !

3,2 !Hello, world .


hello.cpp package.xml CMakeLists.txt
.

39

1 "// This is a ROS version of the standard "hello, world


2 // program.
3
4 // This header defines the standard ROS classes.
5 >#include <ros/ros.h
6
7 { )int main(int argc, char **argv
8 // Initialize the ROS system.
9 ;)"ros::init(argc, argv, "hello_ros
10
11
// Establish this program as a ROS node.
12
;ros::NodeHandle nh
13
14
15 // Send some output as a log message.
16 ;)"!ROS_INFO_STREAM("Hello, ROS
}

( )2-3 hello.cpp

src
C++ .
.


.
( )header file ros/ros.h .
.
ros::init .
1 .
.
( launch )86
(rosrun )20 .

1
http://wiki.ros.org/roscpp/Overview/InitializationandShutdown

40

) ros::NodeHandle (object
1.
. NodeHandle
.
NodeHandle
NodeHandle .
NodeHandle . :
NodeHandle
. .
127 .
roscpp .
ROS_INFO_STREAM .
.
4 .

Hello

catkin
2
. .


. C++ catkin C++
C++ ) (flag
.

CMakeLists.txt .
:
(catkin REQUIRED) find_package

1
http://wiki.ros.org/roscpp/Overview/NodeHandles
2
http://wiki.ros.org/ROS/Tutorials/BuildingPackages

41

COMPONENTS :

(catkin REQUIRED COMPONENTS package-names) find_package

hello roscpp C++


. find_package :

(catkin REQUIRED COMPONENTS roscpp) find_package

package.xml
build_depend :run_depend

><build_depend>package-name</build_depend
><run_depend>package-name</run_depend
hello roscpp
:
><build_depend>roscpp</build_depend
><run_depend>roscpp</run_depend
( )build

.

: CMakeLists.txt
. :

)add_executable(executable-name source-files
)}target_link_libraries(executable-name ${catkin_LIBRARIES

.
. CMake

( find_package ) .
.
hello hello.cpp
CMakeLists.txt :

)add_executable(hello hello.cpp
)}target_link_libraries(hello ${catkin_LIBRARIES

42

1 # What version of CMake is needed?


2 cmake_minimum_required(VERSION 2.8.3)
3
4 # The name of this package.
5 project(agitr)
6
7 # Find the catkin build system, and any other packages on which we
8 depend.
9 find_package(catkin REQUIRED COMPONENTS roscpp)
10
11
# Declare our catkin package.
12
catkin_package()
13
14
15 # Specify locations of header files.
16 include_directories(include ${catkin_INCLUDE_DIRS})
17 # Declare the executable, along with its source files.If
18 # there are multiple executables , use multiple copies of
19 # this line
20 add_executable(hello hello.cpp)
21 # Specify libraries against which to link.Again, this
22 # line should be copied for each distinct executable in
23 # the package.
24 target_link_libraries(hello ${catkin_LIBRARIES})

hello.cpp CMakeLists.txt ) 3-3 (

CMakeLists.txt 3,3
catkin_create_pkg CMakeLists.txt .

.

CMakeLists.txt :
( )
:
catkin_make

.
build devel ( )
makefile .

43

.
devel build
.

catkin_make .
catkin_make ros/ros.h
undefined reference ros::init
roscpp CMakeLists.txt .

setup.bash : setup.bash
catkin_make devel
:

source devel/setup.bash

()
.
setup.bash 2,2 .

catkin_make .

hello

rosrun
( )2,6 . :

rosrun agitr hello

![ INFO] [1416432122.659693753]: Hello, ROS

44

roscore :
.
1970 ROS_INFO_STREAM .

rosrun :
[rospack] Error: stack/package package-name not found
setup.bash
.

-3-3

hello .
! Hello,World
. 1.
turtlesim
. C++ pubvel
3,4 .
.

1 //This program publishes randomly-generated velocity messages for turtlesim.


2 >#include <ros/ros.h
3 #include <geometry_msgs/Twist.h> // For geometry_msgs::Twist
4 #include <stdlib.h> // For rand() and RAND_MAX
5 { )int main(int argc, char **argv
6 // Initialize the ROS system and become a node.
7 ;)"ros::init(argc, argv, "publish_velocity
8 ;ros::NodeHandle nh
9 // Create a publisher object.
10 (>ros::Publisher pub = nh.advertise<geometry_msgs::Twist
11 ;)"turtle1/cmd_vel", 1000
12 // Seed the random number generator.
13 ;))srand(time(0
14 // Loop at 2Hz until the node is shut down.
15 ;)ros::Rate rate(2
16 { ))(while(ros::ok
17 // Create and fill in the message. The other four

1
)http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber(C++

45

18 // fields, which are ignored by turtlesim, default to 0.


19 ;geometry_msgs::Twist msg
20 ;)msg.linear.x = double(rand())/double(RAND_MAX
21 ;msg.angular.z = 2*double(rand())/double(RAND_MAX) - 1
22 // Publish the message.
23 ;)pub.publish(msg
24 // Send a message to rosout with the details.
25 "ROS_INFO_STREAM("Sending random velocity command:
26 << " linear=" << msg.linear.x
27 ;)<< " angular=" << msg.angular.z
28 // Wait until it's time for another iteration.
29 ;)(rate.sleep
} 30
{ 31

( )4-3 pubvel.cpp turtlesim


pubvel hello .

2,7,2 .
C++ . #include
:

>#include <package_name/type_name.h

. pubvel geometry_msgs/Twist Twist
geometry_msgs :

>#include <geometry_msgs/Twist.h
C++
. namespace .
C++ ( ) ::

46

. pubvel
geometry_msgs::Twist .


1.
:
;)ros::Publisher pub = node_handle.advertise<message_type>(topic_name, queue_size
:
node_handle ros::NodeHandle
. advertise .
message_type -

. geometry_msgs::Twist .
topic_name .
rostopic list rqt_graph .
./ /
5 .
" "turtle1/cmd_vel.
.
.
advertise
( ) .
1000 .
.

.
. -
- . ( )thread

1
http://wiki.ros.org/roscpp/Overview/PublishersandSubscribers

47

. - -
.

.
.
nodelets1 .
( ) .


ros::Publisher .
ros::Publisher .
ros::Publisher .

. pubvel while .

: .
ros::Publisher .
.

( rosmsg )2,7,2 geometry_msgs/Twist


( linear )angular ( x y )z .
64 C++ double
. 3,4 geometry_msgs::Twist
( )pseudo-random .
;geometry_msgs::Twist msg
;)msg.linear.x = double(rand())/double(RAND_MAX
;msg.angular.z = 2*double(rand())/double(RAND_MAX) - 1

-1 .1 ( msg.linear.y ,msg.linear.z , msg.angular.x , and
) msg.angular.y .
float64 .
C++ 2.

1
http://wiki.ros.org/nodelet
2
http://wiki.ros.org/msg

48

) rosmsg show
( C++ STL .

:
publish .ros::Publisher :
;)pub.publish(msg
msg
.

:
ROS_INFO_STREAM 3,4 .
ROS_INFO_STREAM string
-
. 4,3 ROS_INFO_STREAM .

. pubvel
while .
.

while :pubvel :
)(ros::ok

. true .
() ros::ok false:
rosnode kill .
Ctrl-C .
ros::init() handler Ctrl-C
. Ctrl-C () ros::ok false
.

49


.
:
)(ros::shutdown

.
.
.

1
: pubvel ros::Rate:

;)ros::Rate rate(2

. ( )constructor
( Hz ) .
. sleep :

;)(rate.sleep

.
.

( .
6300 ).
rostopic hz . pubvel
:

average rate: 2.000


min: 0.500s max: 0.500s std dev: 0.00006s window: 10
( )
.
ros::Rate
sleep usleep . ros::Rate
ros::Rate

1
http://wiki.ros.org/roscpp/Overview/Time

50

. (
) .
() sleep
.

pubvel

pubvel hello : CMakeLists.txt package.xml


catkin_make .
.

: pubvel geometry_msgs
. roscpp
3,2,2 . find_package CMakeLists.txt
geometry_msgs roscpp :

)find_package(catkin REQUIRED COMPONENTS roscpp geometry_msgs

find_package package.xml
:
><build_depend>geometry_msgs</build_depend
><run_depend>geometry_msgs</run_depend
catkin_make
geometry_msgs/Twist.h .
.

pubvel

pubvel . rosrun .

rosrun agitr pubvel

51

turtlesim
pubvel :

rosrun turtlesim turtlesim_node

3,1 .

( )1-3 turtlesim pubvel

-4-3 ( )

.
.
1
.
turtlesim /turtle1/pose
turtlesim_node ( . turtlesim_node
rostopic list , rosnode info ,
rqt_graph . ) 2,7,1
. 3,5
ROS_INFO_STREAM
. .

1
)http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber(C++

52

1 // This program subscribes to turtle1/pose and shows its


2 // messages on the screen.
3 #include <ros/ros.h>
4 #include <turtlesim/Pose.h>
5 #include <iomanip> // for std::setprecision and std::fixed
6
7 // A callback function. Executed each time a new pose
8 // message arrives.
9 void poseMessageReceived(const turtlesim::Pose& msg) {
10 ROS_INFO_STREAM(std::setprecision(2) << std::fixed
11 << "position=(" << msg.x << "," << msg.y << ")"
12 << " direction=" << msg.theta);
13 }
14
15 int main(int argc, char **argv) {
16 // Initialize the ROS system and become a node.
17 ros::init(argc, argv, "subscribe_to_pose");
18 ros::NodeHandle nh;
19
20
// Create a subscriber object.
21
ros::Subscriber sub = nh.subscribe("turtle1/pose", 1000,
22
&poseMessageReceived);
23
24
25 // Let ROS take over.
26 ros::spin();
}

subpose.cpp ) 5-3(
. turtlesim

: callback
.
callback
: callback .
void function_name(const package_name::type_name &msg) {
...
}
: type_name package_name
callback .
.
.

53

callback turtlesim::Pose
turtlesim/Pose.h ( . rostopic info
2,7,2 callback ). ROS_INFO_STREAM
yx theta ( .
rosmsg show ).2,7,2
.
callback void return
.
.

1
: ros::Subscriber :
;)ros::Subscriber sub = node_handle.subscribe(topic_name,queue_size, pointer_to_callback_function
( ros::Publisher
):
node_handle .
topic_name
( .)string " "turtle1/pose .
/ .
queue_size ( ) .
1000 .

callback .
.
.
- 46 -
:
.
.
) callback

1
http://wiki.ros.org/roscpp/Overview/PublishersandSubscribers

54

ros::spin ros::spinOnce)
.callback
callback
. C++
(& ,) . :

&poseMessageReceived
() () . ( )

( ) .
.

: C++
.
.
&
.

ros::Subscriber
. advertise C++
callback .
callback
. run-time .
.

ros::Subscriber
.
: ros::Subscriber
.
.

55

: callback
1. .
:

;)(ros::spinOnce

callback
. :

;)(ros::spin

() ros::spinOnce
callback . ros::spin)( :
{ ))(while(ros::ok
;)(ros::spinOnce
}
() ros::spinOnce () ros::spin :
callback
ros::spin .
() ros::spinOnce callback
. 3,5 () ros::spin
.
ros::spinOnce
ros::spin . callback
. ros::spin
. ros::spinOnce .
[INFO ] [1370972120.089584153]: position =(2.42 ,2.32) direction =1.93
[INFO ] [1370972120.105376510]: position =(2.41 ,2.33) direction =1.95
[INFO ] [1370972120.121365352]: position =(2.41 ,2.34) direction =1.96
[INFO ] [1370972120.137468325]: position =(2.40 ,2.36) direction =1.98
[INFO ] [1370972120.153486499]: position =(2.40 ,2.37) direction =2.00
[INFO ] [1370972120.169468546]: position =(2.39 ,2.38) direction =2.01
[INFO ] [1370972120.185472204]: position =(2.39 ,2.39) direction =2.03

( )6-3 subpose .

1
http://wiki.ros.org/roscpp/Overview/CallbacksandSpinning

56

subpose

.
turtlesim
turtlesim/Pose . 3,3,3
.

turtlesim_node pubvel 3,6


.

-5-3


( ) .
ROS_INFO_STREAM .
ROS_INFO_STREAM
.

57

58

( )3 ROS_INFO_STREAM
. .
( )logging ROS_INFO_STREAM
. .

-1-4


.

1
. :
( DEBUG )
( INFO)
( WARN)
( ERROR)
(FATAL)
( DEBUG )
. ( FATAL)

. INFO WARN ( ERROR )
.
.

1
http://wiki.ros.org/VerbosityLevels

59

( DEBUG )

( INFO)

( WARN) 5 GB

( ERROR) type.

() ros::init NodeHandle ( FATAL .)


.
. : FATAL
() . ( DEBUG
) .

-2-4

.
. turtlesim
turtlesim_node .
( FATAL)
.
4,1 .
. 4,2 .
.

60

1 // This program periodically generates log messages at


2 // various severity levels.
3 #include <ros/ros.h>
4 int main(int argc, char **argv) {
5 // Initialize the ROS system and become a node.
6 ros::init(argc, argv, "count_and_log");
7 ros::NodeHandle nh;
8 // Generate log messages of varying severity regularly.
9 ros::Rate rate(10);
10 for(int i = 1;ros::ok();i++) {
11 ROS_DEBUG_STREAM("Counted to " << i);
12 if((i % 3) == 0) {
13 ROS_INFO_STREAM(i << " is divisible by 3.");
14 }
15 if((i % 5) == 0 ) {
16 ROS_WARN_STREAM(i << " is divisible by 5.");
17 }
18 if((i % 10) == 0) {
19 ROS_ERROR_STREAM(i << " is divisible by 10.");
20 }
21 if((i % 20) == 0) {
22 ROS_FATAL_STREAM(i << " is divisible by 20.");
23 }
24 rate.sleep();
25 }
26 }

. count.cpp ) 1-4(

1 [ INFO ] [1375889196.165921375]: 3 is divisible by 3.


2 [ WARN] [1375889196.365852904]: 5 is divisible by 5.
3 [ INFO ] [1375889196.465844839]: 6 is divisible by 3.
4 [ INFO ] [1375889196.765849224]: 9 is divisible by 3.
5 [ WARN] [1375889196.865985094]: 10 is divisible by 5.
6 [ERROR] [1375889196.866608041]: 10 is divisible by 10.
7 [ INFO ] [1375889197.065870949]: 12 is divisible by 3.
8 [ INFO ] [1375889197.365847834]: 15 is divisible by 3.

DEBUG . count ) 2-4(


. INFO

61

-3-4

C++ .

: C++
:
;)ROS_DEBUG_STREAM(message
;)ROS_INFO_STREAM(message
;)ROS_WARN_STREAM(message
;)ROS_ERROR_STREAM(message
;)ROS_FATAL_STREAM(message

ostream C++
.std::cout ( >> ) int double

std::fixed std::setprecision .std::boolalpha
.
.
:
ROS_. . . _STREAM . std::stringstream

. std::stringstream
log4cxx 1. std::stringstream
.

printf C++
_ STREAM
;) ROS_INFO(format, . . .
INFO . printf
. 3,4 :
ROS_INFO("position=(%0.2f,%0.2f) direction=%0.2f",msg.x, msg.y,
;)msg.theta

1
http://wiki.apache.org/logging-log4cxx/

62

printf ( ) . . . _ONCE ( ). . . _throttled


STREAM .

std::endl
.
.

:one-time
.
static
. 4,3 C++
.
static
.
;)ROS_DEBUG_STREAM_ONCE(message
;)ROS_INFO_STREAM_ONCE(message
;)ROS_WARN_STREAM_ONCE(message
;)ROS_ERROR_STREAM_ONCE(message
;)ROS_FATAL_STREAM_ONCE(message

ONCE . .
4,4
.
1 // Don't do this directly. Use ROS_. . ._STREAM_ONCE instead .
2 {
3 ; static bool first_time = true
4 { ) if ( first_time
5 "ROS_INFO_STREAM( " Here's some important information
6 ;)"<< " that will only appear once.
7 ;first_time = false
8 }
9 }

( )3-4 C++ .ROS_. . . _STREAM_ONCE


.

63

1 // This program generates a single log message at each


2 // severity level.
3 >#include <ros/ros.h
4
5 { )int main(int argc, char **argv
6 ;)"ros::init(argc, argv, "log_once
7 ;ros::NodeHandle nh
8
9 { ))(while(ros::ok
10 ;)"ROS_DEBUG_STREAM_ONCE("This appears only once.
11 ;)"ROS_INFO_STREAM_ONCE("This appears only once.
12 ;)"ROS_WARN_STREAM_ONCE("This appears only once.
13 ;)"ROS_ERROR_STREAM_ONCE("This appears only once.
14 ;)"ROS_FATAL_STREAM_ONCE("This appears only once.
15 }
16 }

( )4-4 once.cpp .

( :) throttled log message


.
;)ROS_DEBUG_STREAM_THROTTLE(interval, message
;)ROS_INFO_STREAM_THROTTLE(interval, message
;)ROS_WARN_STREAM_THROTTLE(interval, message
;)ROS_ERROR_STREAM_THROTTLE(interval, message
;)ROS_FATAL_STREAM_THROTTLE(interval, message
interval double ( )
.
ROS_. . . _STREAM_THROTTLE
. . ()timeout
(
static .).
4,5 4,1 .
4,5
( )sleep
.
.

64

1 // This program generates log messages at varying severity


2 // levels, throttled to various maximum speeds.
3 #include <ros/ros.h>
4
5 int main(int argc, char **argv) {
6 ros::init(argc, argv, "log_throttled");
7 ros::NodeHandle nh;
8 while(ros::ok()) {
9 ROS_DEBUG_STREAM_THROTTLE(0.1,
10 "This appears every 0.1 seconds.");
11 ROS_INFO_STREAM_THROTTLE(0.3,
12 "This appears every 0.3 seconds.");
13 ROS_WARN_STREAM_THROTTLE(0.5,
14 "This appears every 0.5 seconds.");
15 ROS_ERROR_STREAM_THROTTLE(1.0,
16 "This appears every 1.0 seconds.");
17 ROS_FATAL_STREAM_THROTTLE(2.0,
18 "This appears every 2.0 seconds.");
19 }
20 }

)throttled log messages( throttle.cpp C++ ) 5-4(


.

-4-4

.
:
. rosout
.

65

.
DEBUG INFO ERRORWARN
1
FATAL .


. :
command > file
.
:
command &> file

DEBUG INFO
. stdbuf

:
stdbuf -oL command &> file
ANSI
[[ m0
.
:
less -r file

:
ROSCONSOLE_FORMAT .

.
:
}[${severity}] [${time}]: ${message

1
http://wiki.ros.org/roscpp/Overview/Logging

66


1
:

:

}${file} , ${line} , ${function


:
}${node
( roslaunch )6
. " output="screen
roslaunch
--screen ( . )85

rosout

/rosout
. rosgraph_msgs/Log . 4,6
- .
1 byte DEBUG=1
2 byte INFO=2
3 byte WARN=4
4 byte ERROR=8
5 byte FATAL=16
6 std_msgs / Header header
7 uint32 seq
8 time stamp
9 string frame_id
10 byte level
11 string name
12 string msg
13 string file
14 string function
15 uint32 line
16 string [ ] topics

( )6-4 rosgraph_msgs/Log

1
http://wiki.ros.org/rosconsole

67


. /rosout
.
/rosout
. /rosout
:

rostopic echo /rosout

/rosout .
12
/rosout :

rqt_console

4,1 GUI .
.
GUI .

(( GUI )1-4 ) rqt_console

rqt_console . rqt_console
/rosout_agg /rosout .
count /rqt_console :

1
http://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch
2
http://wiki.ros.org/rqt_console

68

_agg rosout
( .)aggregated /rosout
rosout /rosout_agg .
( ) .
-
( /rosout )
.
rosout /rosout
/rosout_agg .

/rosout_agg .
2PR TurtleBot
diagnostic /diagnostics
aggregator /diagnostics_agg
.

rosout.
callback /rosout
:
/.ros/log/run_id/rosout.log

rosout.log .
headless tail run_id .
( )universally-unique identifie -UUID

69

MAC . run_id
:
57aa1860-d765-11e2-a830-f0def1e189cc

.
: run_id run_id .
roscore .
.
setting /run_id to run_id
run_id :
rosparam get /run_id
run_id .
7 .

:
(
) . roscore roslaunch
1 GB
.
1
:
rosclean check

:
rosclean purge
.

-5-4

4,4 4,1 4,5 ( 4,2 )



ROS_DEBUG_STREAM .

1
http://wiki.ros.org/rosclean

70

INFO
.
( )logger levels
. INFO
.
.
rqt_console .

rqt_console .
.

.
ROS_INFO_STREAM
.
. )
. )
.
.
() :
:
rosservice call /node-name/set_logger_level ros.package-name level
set_logger_level
( . 8 ).
node-name .
package-name .
level FATAL ERRORWARN INFODEBUG
.
:
rosservice call /count_and_log/set_logger_level ros.agitr DEBUG

71


. rosservice
.
set_logger_level
ros.package-name .
ros.package-name rosservice logger
. log4cxx
.
ros.package-name.
( C++ )Client

callback .
set_logger_level
.
rosservice
. log4cxx
.

( GUI )2-4 .rqt_logger_level

72

:GUI GUI
:
rqt_logger_level
4,2
ros.package-name
. rosservice
.

:C++ .
log4cxx
. .
>#include <log4cxx/logger.h
...
(log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->setLevel
]ros::console::g_level_lookup[ros::console::levels::Debug
;)
;)(ros::console::notifyLoggerLevelsChanged

DEBUG . DEBUG Info ,Warn , Error , Fatal
.
)( ros::console::notifyLoggerLevelsChanged
/ ) (enabled/disabled .
.

-6-4


.
.

.

73

74

3 " "hello_ros " "publish_velocity


" "turtle1/cmd_vel " "turtle1/pose .
( )graph resource names .
( . ).

.
.

-1-5 ()Global names

.
1.
. rosnode info ros::init
rostopic echo ros::Publisher .
.
:
/teleop_turtle
/turtlesim
/turtle1/cmd_vel
/turtle1/pose
/run_id
/count_and_log/set_logger_level
global names . global names
.
.
.

global name :
/ global .

1
http://wiki.ros.org/Names

75

) (namespaces / .
.
1turtle count_and_log .
global name
( ) 11 .
/a/b/c/d/e/f/g/h/i/j/k/l
(
) global namespace.
base name .
run_id pose cmd_vel turtlesimteleop_turtle
set_logger_level.


. .

-2-5


.
relative graph resource Name
relative name .
/ . :
teleop_turtle
turtlesim
cmd_vel
turtle1/pose
run_id
count_and_log/set_logger_level


.

76

:
.
. cmd_vel
/turtle1 :


. g/h/i/j/k/l
/a/b/c/d/e/f :

:
.
( ) /
.
ns ( . 6,3 ).
.
C++ ros::init
__ns
.

__ns:=default-namespace

.
export ROS_NAMESPACE=default-namespace

77


__ns .

:
.
.

.



.
.
.

.

-3-5

Private names ()
.

.
.
/sim1/pubvel max_vel
:

78

roslaunch
. 113
.

.

.

. private C++

.

-4-5


anonymous names .
.
ros::init .
ros::init_options::AnonymousName
ros::init :

;)ros::init(argc, argv, base_name, ros::init_options::AnonymousName



.

ros::init
.

79

1 // This program starts with an anonymous name, which


2 // allows multiple copies to execute at the same time,
3 // without needing to manually create distinct names
4 // for each of them.
5 >#include <ros/ros.h
6 { )int main(int argc, char **argv
7 ros::init(argc, argv, "anon",
8 ;)ros::init_options::AnonymousName
9 ;ros::NodeHandle nh
10 ;)ros::Rate rate(1
11 { ))(while(ros::ok
12 " ROS_INFO_STREAM("This message is from
13 ;))(<< ros::this_node::getName
14 ;)(rate.sleep
15 }
16 }

( )1-5 anon.cpp .
.

5,1 .
anon
:
/anon_1376942789079547655
/anon_1376942789079550387
/anon_1376942789080356882


.

-5-5

.

( ) . roslaunch
.

80

81


roscore .
launch file
. .

. roslaunch
.

-1-6


.
XML 1. 6,1
turtlesim
. example.launch agitr
.
.

1
http://wiki.ros.org/roslaunch/XML

82

1 ><launch
2 <node
3 "pkg="turtlesim
4 "type="turtlesim_node
5 "name="turtlesim
6 "respawn="true
7 >/
8 <node
9 "pkg="turtlesim
10 "type="turtle_teleop_key
11 "name="teleop_key
12 "required="true
13 "launch-prefix="xterm -e
14 >/
15 <node
16 "pkg="agitr
17 "type="subpose
18 "name="pose_subscriber
19 "output="screen
20 >/
21 ></launch

( )1-6 example.launch .

1
: roslaunch :

roslaunch package-name launch-file-name

roslaunch agitr example.launch

. turtlesim

. roslaunch
subpose .
roslaunch roscore
.
rosrun roslaunch
.

1
http://wiki.ros.org/roslaunch/CommandlineToolshttp://wiki.ros.org/roslaunch/CommandlineTools

83

.
roslaunch .

:

roslaunch /ros/src/agitr/example.launch

.

( roslaunch )
.
.
( . 7,3
).

.
( 8-2 ).
.
.

: roslaunch
:

roslaunch -v package-name launch-file-name

6,2
.
roslaunch .

84

1 ]... loading XML file [ /opt/ros/indigo/etc/ros/roscore.xml


2 ] executing command param [ rosversion roslaunch
3 ]Added parameter [ /rosversion
4 ] executing command param [ rosversiond
5 ]Added parameter [/rosdistro
6 ]Added core node of type [rosout/rosout] in namespace [/
7 ] loading XML file [/home/jokane/ros/agitr/example.launch
8 ]Added node of type [turtlesim/turtlesim_node] in namespace [/
9 ]Added node of type [agitr/pubvel] in namespace [/
10 ]Added node of type [agitr/subpose] in namespace [/

( )2-6 roslaunch .

: roslaunch Ctrl-C .

.

-2-6

.
.launch .
.
roslaunch .
launch
.

85

: XML XML
. launch
:
><launch
...
></launch
.

:
1. :
<node
"pkg="package-name
"type="executable-name
"name="node-name
>/
node .
< >/node node .
XML . /
:

Invalid roslaunch XML syntax: mismatched tag

:
><node pkg=". . . " type=". . . " name=". . . "></node


remap .param 6,4 7,4 .

1
http://wiki.ros.org/roslaunch/XML/node

86

pkg type
. rosrun
.
name .
ros::init .

ros::init
( . 5,4 ).
1anon name
:

")name="$(anon base_name
base_name
. )
) base_name
.

: roslaunch
rosrun
-1(. roslaunch
ERROR FATAL
. roslaunch
).
:
/.ros/log/run_id/node_name-number-stdout.log
Run_id ( .
run_id 69 ).
.
6,1 :
turtlesim-1-stdout.log
Telep_key-3-stdout.log
.

1
http://wiki.ros.org/roslaunch/XML

87

: output
:
"output="screen

. subpose
INFO .
.
output roslaunch
: --screen
roslaunch --screen package-name launch-file-name
roslaunch
" output="screen .
: roslaunch
. roslaunch
: respawn
"respawn="true

.
respawn ( )
turtlesim_node .
turtlesim . (
respawn ) turtlesim
.
: respawn required
:
"required="true
required roslaunch
.
) )
respawn .
required turtle_teleop_key .
roslaunch
.

88

respawn required
roslaunch .

: roslaunch
rosrun
.
( ) .
turtle_teleop_key
.
roslaunch launch-prefix
:
"launch-prefix="command-prefix
roslaunch
. example.launch
:
"launch-prefix="xterm -e
:
xterm -e rosrun turtlesim turtle_teleop_key
xterm .
-e xterm ( rosrun turtlesim
) turtle_teleop_key .
turtle_teleop_key .
launch-prefix xterm .
( gdb )valgrind (
(nice1

1
http://wiki.ros.org/rqt_console

89

( )1-6 ( ) doublesim.launch
.

6,3

5,2
. )
pushing down (
ns :

"ns="namespace

6,3
turtlesim . 6,1
.
turtlesim (turtle1/color_sensor turtle1/pose
)turtle1/cmd_vel /sim1/sim2
. turtlesim_node
turtle1/pose ros::Publisher ros::Subscriber
( .) /turtle1/pose
.
.turtlesim_node

90


( /sim1/turtlesim_node .)/sim2/turtlesim_node
roslaunch

name .
1 ><launch
2 <node
3 "name="turtlesim_node
4 "pkg="turtlesim
5 "type="turtlesim_node
6 "ns="sim1
7 >/
8 <node
9 "pkg="turtlesim
10 "type="turtle_teleop_key
11 "name="teleop_key
12 "required="true
13 "launch-prefix="xterm -e
14 "ns="sim1
15 >/
16 <node
17 "name="turtlesim_node
18 "pkg="turtlesim
19 "type="turtlesim_node
20 "ns="sim2
21 >/
22 <node
23 "pkg="agitr
24 "type="pubvel
25 "name="velocity_publisher
26 "ns="sim2
27 >/
28 ></launch

( )3-6 doublesim.launch turtlesim .


.

91

2,8 .
turtlesim . . 2,8
. turtlesim
.
. 6,3
.
.

ns .
sim1 sim2
( ) / .
/sim1 /sim2 .
.

.
.

-3-6


1.
: .
) (client
.

1
http://wiki.ros.org/RemappingArguments

92


= : .

original-name:=new-name

turtlesim /tim
/turtle1/pose :

rosrun turtlesim turtlesim_node turtle1/pose:=tim


1
remap .
><remap from="original-name" to="new-name" /
launch
. remap
node :
> <node node-attributes
><remap from="original-name" to="new-name" /
...
></node
.
:
"<node pkg="turtlesim" type="turtlesim_node
> "name="turtlesim
><remap from="turtle1/pose" to="tim" /
></node
:
.
.

.


turtle_teleop_key turtlesim

1
http://wiki.ros.org/roslaunch/XML/remap

93

.

.

.

turtle_teleop_key
.
turtle_teleop_key .

.

6,4 : turtle1/cmd_vel
( linear
)angular /turtle1/cmd_vel_reversed .

94

1 // This program subscribes to turtle1/cmd_vel and


2 // republishes on turtle1/cmd_vel_reversed,
3 // with the signs inverted.
4 #include <ros/ros.h>
5 #include <geometry_msgs/Twist.h>
6 ros::Publisher *pubPtr;
7 void commandVelocityReceived(
8 const geometry_msgs::Twist& msgIn)
9 {
10 geometry_msgs::Twist msgOut;
11 msgOut.linear.x = -msgIn.linear.x;
12 msgOut.angular.z = -msgIn.angular.z;
13 pubPtr->publish(msgOut);
14 }
15 int main(int argc, char **argv) {
16 ros::init(argc, argv, "reverse_velocity");
17 ros::NodeHandle nh;
18 pubPtr = new ros::Publisher(
19 nh.advertise<geometry_msgs::Twist>(
20 "turtle1/cmd_vel_reversed",
21 1000));
22 ros::Subscriber sub = nh.subscribe(
23 "turtle1/cmd_vel", 1000,
24 &commandVelocityReceived);
25 ros::spin();
26 delete pubPtr;
27 }

. reverse_cmd_vel C++ ) 4-6(

turtlesim
rosrun .
6,2
turtlesim teleop_turtle .
.

95

( )2-6 reverse_cmd_vel
.turtlesim

.
turtlesim turtle1/cmd_vel
turtle1/cmd_vel_reversed . 6,5
remap .turtlesim_node 6,3
.

>1 <launch
2 <node
3 "pkg="turtlesim
4 "type="turtlesim_node
5 "name="turtlesim
> 6
7 <remap
8 "from="turtle1/cmd_vel
9 "to="turtle1/cmd_vel_reversed
10 >/
11 ></node
12 <node
13 "pkg="turtlesim
14 "type="turtle_teleop_key
15 "name="teleop_key
16 "launch-prefix="xterm -e

96

17 >/
18 <node
19 "pkg="agitr
20 "type="reverse_cmd_vel
21 "name="reverse_velocity
22 >/
23 ></launch

( )5-6 .

( )3-6 .reversed.launch remap


.

97

-4-6

1. roslaunch
. 6,6
. turtlesim

1 <launch>
2 <include
3 file="$(find agitr)/doublesim.launch"
4 />
5 <arg
6 name="use_sim3"
7 default="0"
8 />
9
10 <group ns="sim3" if="$(arg use_sim3)" >
11 <node
12 name="turtlesim_node"
13 pkg="turtlesim"
14 type="turtlesim_node"
15 />
16 <node
17 pkg="turtlesim"
18 type="turtle_teleop_key"
19 name="teleop_key"
20 required="true"
21 launch-prefix="xterm -e"
22 />
23 </group>
24 </launch>

arg include group triplesim.launch ) 6-6(


.

1
http://wiki.ros.org/ROS/Tutorials/Roslaunchtipsforlargerprojects

98

include
1
:

><include file="path-to-launch-file" /

.
include find
:

><include file="$(find package-name)/launch-file-name" /

find .
.
doublesim.launch .

roslaunch
. include
.
include roslaunch
.

include ns
:

><include file=". . . " ns="namespace" /

1
http://wiki.ros.org/roslaunch/XML/include

99

roslaunch
arguments args .
1.

.
use_sim3 turtlesim .


.

ros::param::get rosparam
( ).
.

: arg
:
><arg name="arg-name" /
(
- )
.

:
. .
roslaunch :
roslaunch package-name launch-file-name arg-name:=arg-value
arg
:
><arg name="arg-name" default="arg-value" /
><arg name="arg-name" value="arg-value" /

1
http://wiki.ros.org/roslaunch/XML/arg

100


( )default ( )value . use_sim3
default
:
roslaunch agitr triplesim.launch use_sim3:=1
default value
value .

:

include .
( )lexical .
.
arg include :
>"<include file="path-to-launch-file
><arg name="arg-name" value="arg-value"/
...
></include
arg arg .
include
.
value .

. .
:
><arg name="arg-name" value="$(arg arg-name)" /
arg-name .
arg-name ( ) .
.

101

group
1. group :
.
><group ns="namespace" /
...
></group
.
ns ( )

.
.
.
><group if="0-or-1" /
...
></group
if .
. unless
.
><group unless="1-or-0" /
...
></group
.
arg .
.
AND OR .
Group . ( ns
)sim3 ( if
.) use_sim3

1
http://wiki.ros.org/roslaunch/XML/group

102

group
ifunless ns
.
.

group .
"output="screen
.

-5-6


.

.

103

104
.

()parameter
.
(
) 1 2.

.

-1-7

.
3
: :
rosparam list
:
/rosdistro
/roslaunch/uris/host_donatello__38217
/rosversion
/run_id
( )5
.

roscore roslaunch .
.
.
( )
.

1
http://wiki.ros.org/roscpp/Overview/ParameterServer
2
http://wiki.ros.org/ParameterServer
3
http://wiki.ros.org/rosparam

105
: rosparam
get :
rosparam get parameter_name
/rosdistro :
rosparam get /rosdistro
indigo .
:
rosparam get namespace

:
rosparam get /
:
rosdistro: indigo
roslaunch:
uris: host_donatello__38217: http://donatello:38217/
rosversion: 1.11.9
run_id: e574a908-70c5-11e4-899e-60d819d10251

: :
rosparam set parameter_name parameter_value
.

.
Rosparam set /duck_colors/huey red
Rosparam set /duck_colors/dewey blue
Rosparam set /duck_colors/louie green
Rosparam set /duck_colors/webby pink
:

rosparam set namespace values

YAML
. :
rosparam set /duck_colors "huey: red
dewey: blue
louie: green
"webby: pink
.
(") bash

106
. Enter
.

rosparam
/duck_colors
duck_colors
.

:
YAML rosparam dump :
rosparam dump filename namespace
dump load
:
rosparam load filename namespace

( )/ . dump load

.

-2-7 : turtlesim


turtlesim . roscore turtlesim_node
rosparam list :
/background_b
/background_g
/background_r
/rosdistro
/roslaunch/uris/host_donatello__59636
/rosversion
/run_id

107
.
turtlesim_node .
turtlesim
.
.
turtlesim_node .
turtlesim_node
. turtlesim_node

.

turtlesim
-
.

:
rosparam get :
rosparam get /background_r
rosparam get /background_g
rosparam get /background_b
255 86 69 .
( ) 8
0 .255 ( )2558669
.

:
. turtlesim
:
rosparam set /background_r 255
rosparam set /background_g 255
rosparam set /background_b 0
.
turtlesim_node /clear
. /clear :
108
rosservice call /clear
.
7,1 .

.
.

( ) ( .
turtlesim dynamic_reconfigure
)1 .

( )1-7 () () .turtlesim


ros::param::getCached .ros::param::get

.

-3-7 :C++

2
C++ :
;)void ros::param::set(parameter_name, input_value
;)bool ros::param::get(parameter_name, output_value

1
http://wiki.ros.org/dynamic_reconfigure
2
http://wiki.ros.org/roscpp_tutorials/Tutorials/P arameters
109
.
set int boolstd::string double get
( ) . get true
false
.
.

ros::param::set 7,1 .
turtlesim .
turtle-sim /clear (
turtlesim ).
turtlesim
( . 8 ).

1 // This program waits for a turtlesim to start up, and


2 // changes its background color.
3 >#include <ros/ros.h
4 >#include <std_srvs/Empty.h
5
6 { )int main(int argc, char **argv
7 ;)"ros::init(argc, argv, "set_bg_color
8 ;ros::NodeHandle nh
9
10 // Wait until the clear service is available, which
11 // indicates that turtlesim has started up, and has
12 // set the background color parameters.
13 ;)"ros::service::waitForService("clear
14
15 // Set the background color for turtlesim,
16 // overriding the default blue color.
17 ;)ros::param::set("background_r", 255
18 ;)ros::param::set("background_g", 255
19 ;)ros::param::set("background_b", 0
20
21 // Get turtlesim to pick up the new parameter values.
22 ros::ServiceClient clearClient

110
23 ;)"= nh.serviceClient<std_srvs::Empty>("/clear
24 ;std_srvs::Empty srv
25 ;)clearClient.call(srv
26
27 }

( )1-7 C++ set_bg_color.cpp turtlesim


.

7,2 ros::param::get . ( pubvel


) 3,4 . max_vel

.
max_vel
:
rosparam set /publish_velocity/max_vel 0.1
( )fatal
.

111
1 // This program publishes random velocity commands, using
2 // a maximum linear velocity read from a parameter.
3 #include <ros/ros.h>
4 #include <geometry_msgs/Twist.h>
5 #include <stdlib.h>
6 int main(int argc, char **argv) {
7 ros::init(argc, argv, "publish_velocity");
8 ros::NodeHandle nh;
9 ros::Publisher pub = nh.advertise<geometry_msgs::Twist>(
10 "turtle1/cmd_vel", 1000);
11 srand(time(0));
12 // Get the maximum velocity parameter.
13 const std::string PARAM_NAME = "~max_vel";
14 double maxVel;
15 bool ok = ros::param::get(PARAM_NAME, maxVel);
16 if(!ok) {
17 ROS_FATAL_STREAM("Could not get parameter "
18 << PARAM_NAME);
19 exit(1);
20 }
21 ros::Rate rate(2);
22 while(ros::ok()) {
23 // Create and send a random velocity command.
24 geometry_msgs::Twist msg;
25 msg.linear.x = maxVel*double(rand())/double(RAND_MAX);
26 msg.angular.z = 2*double(rand())/double(RAND_MAX)-1;
27 pub.publish(msg);
28 // Wait until it's time for another iteration.
29 rate.sleep();
30 }
31 }

pubvel.cpp pubvel_with_max.cpp C++ ) 2-7(


.

112
-4-7

( )

( _ ) :

_param-name:=param-value

ros::param::set ros::init
_ .
pubvel_with_max :
rosrun agitr pubvel_with_max _max_vel:=1
.
: roslaunch param
1
:
><param name="param-name" value="param-value" /

. .
rosparam set 107:
>"<group ns="duck_colors
><param name="huey" value="red" /
><param name="dewey" value="blue" /
><param name="louie" value="green" /
><param name="webby" value="pink" /
></group
: param
:
> <node . . .
><param name="param-name" value="param-value" /
...
></node
.

1
http://wiki.ros.org/roslaunch/XML/param
113
.
param node
/ .
pubvel_with_max
max_vel :
<node
"pkg="agitr
"type="pubvel_with_max
"name="publish_velocity
>/
><param name="max_vel" value="3" /
></node
7,3 turtlesim .
.

: rosparam load
1
.
><rosparam command="load" file="path-to-param-file" /
rosparam dump .
( include )6,5,1
find .
<rosparam
"command="load
"file="$(find package-name)/param-file
>/

1 ><launch
2 <node
3 "pkg="turtlesim
4 "type="turtlesim_node
5 "name="turtlesim
6 >/
7 <node
8 "pkg="agitr
9 "type="pubvel_with_max
10 "name="publish_velocity

1
http://wiki.ros.org/roslaunch/XML/rosparam
114
11 >
12 ><param name="max_vel" value="3" /
13 ></node
14 <node
15 "pkg="agitr
16 "type="set_bg_color
17 "name="set_bg_color
18 >/
19 ></launch

( )3-7 fast_yellow.launch 7,1 7,2


max_vel .

rosparam load
.

-5-7


.
.

115

116
.


2 3 .
.
service call .
:
.
. .
.
.
.

subscriber .
( ) .
command line
.

-1-8

- ( )client
. (
)
.

117
( ) service data type
.
. (
) ( ).

-2-8 command line

command line
.
.
:
1
:
rosservice list
turtlesim :
/clear
/kill
/reset
/rosout/get_loggers
/rosout/set_logger_level
/spawn
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
/turtlesim/get_loggers
/turtlesim/set_logger_level
.
. ( )global
( )relative ( )private .
.

.

1
http://wiki.ros.org/rosservice
118
get_loggers set_logger_level
.
( )namespace

( )private get_loggers set_logger_level ( . 4,5
).
.
spawn
turtlesim .
. spawn

. get_loggers set_logger_level
.
.
:
:
rosnode info node-name
:turtlesim
* /turtle1/teleport_absolute
* /turtlesim/get_loggers
* /turtlesim/set_logger_level
* /reset
* /spawn
* /clear
* /turtle1/set_pen
* /turtle1/teleport_relative
* /kill
turtlesim
( . rosout ).
: (
) :
rosservice node service-name

119
/turtlesim
/rosout .

:
:
rosservice info service-name
:
rosservice info /spawn
:
Node: /turtlesim
URI: rosrpc://donatello:47441
Type: turtlesim/Spawn
Args: x y theta name
/spawn turtlesim/Spawn .
.

:
rossrv :
rossrv show service-data-type-name

rossrv show turtlesim/Spawn
:
float32 x
float32 y
float32 theta
string name
---
string name
( )--- .
( ) . ( )---
.

120
rosservice rossrv .
.
. rostopic
rosmsg:

rostopic rosservice

rosmsg rossrv

. /reset
turtlesim std_srvs/Empty
. C++
( )return void . (
) .

: command line
command line .
> rosservice call > < >
rossrv
. :
rosservice call /spawn 3 3 0 Mikey
( )x,y( = )33 =
0 .
cmd_vel , pose color_sensor
set_pen , teleport_absolute , teleport_relative .
namespace .Mikey
namespace turtle1 1 .
.
namespace .
. :
121
name: Mikey
.
. turtlesim
.
:
ERROR: service [/spawn] responded with an error:
.
: turtlesim .

C++ turtlesim
.

-3-8

command line
1. 8,1
.
.

: ( )3,3,1
C++ .
>#include <package_name/type_name.h
:
>#include <turtlesim/Spawn.h
turtlesim::Spawn .
( )request and response
.
: ( ros::init
) NodeHandle ros::ServiceClient
. ros::ServiceClient :

1
)http://wiki.ros.org/ROS/Tutorials/WritingServiceClient(C++
122
ros::ServiceClient client = .serviceClient<) )<
.
service-Client . ros::NodeHandle
.
.
. turtlesim::Spawn
string
.
." spawn" .

.

1 // This program spawns a new turtlesim turtle by calling


2 // the appropriate service.
3 #include <ros/ros.h>
4 // The srv class for the service.
5 #include <turtlesim/Spawn.h>
6 int main(int argc, char **argv) {
7 ros::init(argc, argv, "spawn_turtle");
8 ros::NodeHandle nh;
9 // Create a client object for the spawn service. This
10 // needs to know the data type of the service and its
11 // name.
12 ros::ServiceClient spawnClient
13 = nh.serviceClient<turtlesim::Spawn>("spawn");
14 // Create the request and response objects.
15 turtlesim::Spawn::Request req;
16 turtlesim::Spawn::Response resp;
17 // Fill in the request data members.
18 req.x = 2;
19 req.y = 3;

123
20 ;req.theta = M_PI / 2
21 ;"req.name = "Leo
22 // Actually call the service. This won't return until
23 // the service is complete.
24 ;)bool success = spawnClient.call(req, resp
25 // Check for success and use the response.
26 { )if(success
27 " ROS_INFO_STREAM("Spawned a turtle named
28 ;)<< resp.name
29 { } else
30 ;)"ROS_ERROR_STREAM("Failed to spawn.
31 }
32 }

( )1-8 spawn_turtle.cpp .

ros::ServiceClient
.ros::Publisher
.
. .

: ros::ServiceClient
( )request
.
Response Request .
:
package_name::service_type::Request
package_name::service_type::Response
( . rossrv show
). C++
( . )
.

124
turtlesim::Spawn::Request x, y, theta
.
Response turtlesim::Spawn::Response
.

( ) .
package_name::service_type
. srv
. ( )
call
Request Response.

: ServiceClient
:
;)bool success = service_client.call(request, response

Response .
call
. (
)
. call
.
call .
.
ROS_ERROR_STREAM
.
.

call
. call .

125
ros::ServiceClient
call
. true
( . false ) .

ros::ServiceClient client = node_handle.advertise<service_type>(service_name,


;)true
1.
( 10 )
.


( )Request call . name
Request.

: .
catkin_make
.
( 3,3,3 ) CMakeLists.txt package.xml
. find_package
CMakeLists.txt turtlesim .

)find_package(catkin REQUIRED COMPONENTS roscpp turtlesim

package.xml build_depend run_depend


.

><build_depend>turtlesim</build_depend
><run_depend>turtlesim</run_depend
catkin_make .

1
http://www.ros.org/doc/api/roscpp/html/classros_1_1NodeHandle. html
126
-4-8


. 8,2 toggle_forward
turtlesim
.
.
( ros::ServiceServer ros::Subscriber
) .
() .

:callback
callback . callback :
(bool function_name
package_name::service_type::Request &req),
)package_name::service_type::Response &resp
{)
...
}
callback .
Request . callback
Response . Request Response

. callback true
false .
std_srvs/Empty Request Response
callback .
forward main
.

1 // This program toggles between rotatation and translation


2 // commands, based on calls to a service.
3 >#include <ros/ros.h
4 >#include <std_srvs/Empty.h

127
5 #include <geometry_msgs/Twist.h>
6
7 bool forward = true;
8 bool toggleForward(
9 std_srvs::Empty::Request &req,
10 std_srvs::Empty::Response &resp
11 ){
12 forward = !forward;
13 ROS_INFO_STREAM("Now sending " << (forward ?
14 "forward" : "rotate") << " commands.");
15 return true;
16 }
17
18 int main(int argc, char **argv) {
19 ros::init(argc, argv, "pubvel_toggle");
20 ros::NodeHandle nh;
21
22 // Register our service with the master.
23 ros::ServiceServer server = nh.advertiseService(
24 "toggle_forward", &toggleForward);
25
26 // Publish commands, using the latest value for forward,
27 // until the node shuts down.
28 ros::Publisher pub = nh.advertise<geometry_msgs::Twist>(
29 "turtle1/cmd_vel", 1000);
30 ros::Rate rate(2);
31 while(ros::ok()) {
32 geometry_msgs::Twist msg;
33 msg.linear.x = forward ? 1.0 : 0.0;
34 msg.angular.z = forward ? 0.0 : 1.0;
35 pub.publish(msg);
36 ros::spinOnce();
37 rate.sleep();
38 }
39 }

pubvel_toggle.cpp ) 2-8(
.

callback :
:
128
(ros::ServiceServer server = node_handle.advertiseService
service_name,
pointer_to_callback_function
;)
:
Node_handle .
Service_name .
( ) .

ros::NodeHandle::advertiseService ( .
) . ros::NodeHandle
. - ros::NodeHandle :

;)" "(ros::NodeHandle nhPrivate

NodeHandle
.
.
/foo/bar /foo/bar/baz :
(ros::ServiceServer server = nhPrivate.advertiseService
"baz",
callback
;)
baz
NodeHandle
.

callback .
56 .
.
ros::Subscriber ros::ServiceServer .

ros::ServiceServer .

129
: callback
() ros::spin)( ros::spinOnce ( .
3,4 ).
() ros::spinOnce () ros::spin
( )
.

pubvel_toggle turtlesim_node
pubvel_toggle .
toggle_forward :
rosservice call /toggle_forward
8,1 .

( )1-8 pubvel_toggle ./toggle_forward

130
pubvel_togglerosservice call turtlesim_node .
pubvel_toggle .
sleep ros::Rate ( 2)
() .
sleep () ros::spinOnce
0,5 .
0,5 .
:
thread :
callback .
thread .
sleep / ros::spinOnce ros::spin timer
callback .
(
)
.

-5-8


. rosbag
.

131

132

bag
.


.
- .

.
rosbag .
rosbag
.
:

.

-1-9 bag

bag
. rosbag bag
12
.

: bag bag rosbag :


rosbag record -O filename.bag topic-names
rosbag .
rosbag record .

rosbag record -a
.

1
http://wiki.ros.org/rosbag
2
http://wiki.ros.org/rosbag/Commandline
133

(
) . .

.
bag . -a
bag .
bag rosbag record -j .
:
.
bag .
Ctrl-C rosbag .
:bag bag :
rosbag play filename.bag
bag
.
:bag rosbag info bag
:
rosbag info filename.bag
bag
:
path: square.bag
version: 2.0
)duration: 1:08s (68s
)start: Jan 06 2014 00:05:34.66 (1388984734.66
)end: Jan 06 2014 00:06:42.99 (1388984802.99
size: 770.8 KB
messages: 8518
]compression: none [1/1 chunks
]types: geometry_msgs/Twist [9f195f881246fdfa2798d1d3eebca84a
]turtlesim/Pose [863b248d5016ca62ea2e895ae5265cf9
topics: /turtle1/cmd_vel 4249 msgs : geometry_msgs/Twist
/turtle1/pose 4269 msgs : turtlesim/Pose
( )duration ( )messages .

134

-2-9 : bag

bag .
: roscore turtlesim_node . turtlesim
draw_square :
rosrun turtlesim draw_square
( )reset
( .
.
draw_square pubvel
) .

bag :
:
rosbag record -O square.bag /turtle1/cmd_vel /turtle1/pose
rosbag /turtle1/cmd_vel /turtle1/pose
square.bag . (
)rqt_graph 9,1 . rosbag
/record_ . . . /turtle1/cmd_vel . rosbag

3 .

135

( )1-9 . rosbag record

rosbag 5,4
. ( ). . . .
rosbag record
.

bag :
rosbag - draw_square
. bag . roscore
turtlesim :
rosbag play square.bag
. rosbag
. . ._play /turtle1/cmd_vel
9,2 .
draw_square.

136

( )2-9 rosbag play

9,3 .
rosbag .

(( )3-9) .draw_square
rosbag ( .) ,bag
.

rosbag
rosbag record . rosbag
. .
rosbag play .

137

Draw_square rosbag play


bag /turtle1/pose .
rosbag record /turtle1/pose .
( )rosbag play
. bag
.
turtlesim_node rosbag play
/turtle1/pose . 9,1
.
y .
.
1 x : 5.93630695343
2 y : 4.66894054413
3 theta : 5.85922956467
4 linear_velocity : 0.0
5 angular_velocity : 0.40000000596
6
7 x : 5.56227588654
8 y : 7.4833817482
9 theta : 4.17920017242
10 linear_velocity : 0.0
11 angular_velocity : 0.40000000596
12
13 x : 5.93630695343
14 y : 4.66894054413
15 theta : 5.865629673
16 linear_velocity : 0.0
17 angular_velocity : 0.40000000596
18
19 x : 5.56227588654
20 y : 7.4833817482
21 theta : 4.18560028076
22 linear_velocity : 0.0
23 angular_velocity : 0.4000000059

( )1-9 pubvel_toggle.cpp
.

138

rosbag
( . ).
9,3 ( )8 bag
. bag
draw_square /reset
.

Bags -3-9

rosbag record play


rosbag .
rosbag record rosbag play .
( ) bag rosrun
:
rosrun rosbag record -O filename.bag topic-names
rosrun rosbag play filename.bag
record play bag
. record
:
<node
"pkg="rosbag
"name="record
"type="record
"args="-O filename.bag topic-names
>/
play :
<node
"pkg="rosbag
"name="play
"type="play
"args="filename.bag
>/
args
.

139

bag
C++ . API
. bag 1. API
rosbag .

-4-9

.
.

1
http://wiki.ros.org/rosbag/CodeAPI
140

141
.

.
turtlesim .
.

.

-1-10

( )
.

: 1

.
.


( ) (
). 1 2 3.

.
.

:
.

1
http://wiki.ros.org/ROS/NetworkSetup
2
http://wiki.ros.org/ROS/Tutorials/MultipleMachines
3
http://wiki.ros.org/ROS/EnvironmentVariables
142
.
1
ros::Timer ros::Rate .


2.
.
: RVIZ .
.


. rviz
(
) .3
:
.
.54
: tf

.
. frame_id
.
.
. tf . tf

1
http://wiki.ros.org/roscpp/Overview/Timers
2
http://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks
3 http://wiki.ros.org/rviz
4 http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv
5http://wiki.ros.org/ROS/Tutorials/DefiningCustomMessages
143

.4321
: Gazebo

. 9
bag
. Gazebo
. Gazebo

.5

-2-10

.
.

1http://wiki.ros.org/tf/Tutorials/Introductiontotf

)2http://wiki.ros.org/tf/Tutorials/Writingatflistener(C++

3http://wiki.ros.org/tf/Overview/DataTypes

4http://ros.org/doc/indigo/api/tf/html/c++/

5http://gazebosim.org/wiki/Tutorials/1.9/Overview_of_new_ROS_integration
144

Você também pode gostar