ROS学习笔记
资源
教程
安装及入门
vi /etc/apt/sources.list.d/ros-latest.list #deb http://packages.ros.org/ros/ubuntu trusty main
wget https://raw.githubusercontent.com/ros/rosdistro/master/ros.key
sudo apt-key add ros.key
sudo apt-get update
sudo apt-get install ros-indigo-desktop-full
sudo apt-get install ros-indigo-turtlesim
sudo rosdep init
rosdep update
source /opt/ros/indigo/setup.bash
export | grep ROS
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
#功能包/软件包
rospack list
rospack find package-name
rospack find turtle
rosls package-name
roscd package-name
rosnode list
#节点
roscore
rosrun package-name executable-name
rosnode info node-name
rosnode kill node-name
# 话题和消息
rqt_graph
rostopic list
rostopic echo topic-name
rostopic echo /turtle1/cmd_ve
rostopic hz topic-name
rostopic bw topic-name
rostopic info topic-name
rostopic info /turtle1/color_sensor
rosmsg show message-type-name
rosmsg show turtlesim/Color
rostopic pub –r rate-in-hz topic-name message-type message-content
rostopic pub –r 1 /turtle1/cmd_vel geometry_msgs/Twist ’[2,0,0]’ ’[0,0,0]’
rostopic pub –r 1 /turtle1/cmd_vel geometry_msgs/Twist ’[0,0,0]’ ’[0,0,1]’
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
roswtf
编写ROS程序
catkin_create_pkg package-name
catkin_create_pkg agitr
<?xml version ="1.0"?>
<package>
<name>agir </name>
<version >0.0.1 </version >
<description >
Examples from A Gentle Introduction to ROS
</description >
<maintainer email="jokane@cse.sc.edu">
Jason O' Kane
</maintainer >
<license >TODO</ license >
<buildtool_depend>catkin </buildtool_depend>
<build_depend>geometry_msgs</build_depend>
<exec_depend>geometry_msgs</exec_depend>
<build_depend>turtlesim </build_depend>
<exec_depend>turtlesim </exec_depend>
</package>
// This is a ROS version of the standard "hello , world"
// program.
// This header defines the standard ROS classes .
#include <ros/ros.h>
int main ( int argc , char ** argv ) {
// Initialize the ROS system .
ros::init ( argc , argv , "hello_ros" ) ;
// Establ ish this program as a ROS node .
ros::NodeHandle nh ;
// Send some output as a log message .
ROS_INFO_STREAM( " Hello , ROS! " ) ;
}
# What version of CMake is needed ?
cmake_minimum_required (VERSION 2 . 8 . 3 )
# Name of this package .
project ( agitr )
# Find the catkin build system , and any other packages on
# which we depend .
find_package ( catkin REQUIRED COMPONENTS roscpp )
# Declare our catkin package .
catkin_package ( )
# Specify locations of header files .
include_directories ( include ${catkin_INCLUDE_DIRS } )
# Declare the executable , along with its sourcefiles . If
# there are multiple executables ,use multiple copies of
# this line .
add_executable ( hello hello.cpp )
# Specify libraries against which to link. Again , this
# line should be copied for each distinct executable in
# the package .
target_link_libraries ( hello ${catkin_LIBRARIES })
catkin_make
source devel/setup.bash
rosrun agitr hello
// 发布消息
// This program publishes randomly−generated velocity
// messages for turtlesim .
#include <ros / ros.h>
#include <geometry_msgs / Twist . h> // For geometry_msgs:: Twist
#include <stdlib.h> // For rand() and RAND_MAX
int main ( int argc , char ** argv ) {
// Initialize the ROS system and become a node .
ros::init ( argc , argv , " publish _ velocity " ) ;
ros::NodeHandle nh ;
// Create a publisher obj ect .
ros::Publisher pub = nh.advertise<geometry_msgs::Twist >(
" turtle1 /cmd_vel" , 1 0 0 0 ) ;
// Seed the random number generator .
srand ( time ( 0 ) ) ;
// Loop at 2Hz until the node is shut down.
ros::Raterate ( 2 ) ;
while ( ros::ok ( ) ) {
// Create and fill in the message . The other four
// fields , which are ignored by turtl esim , default to 0.
geometry_msgs : : Twist msg ;
msg.linear.x = double ( rand ( ) ) / double (RAND_MAX) ;
msg.angular.z = 2* double ( rand ( ) )/double (RAND_MAX) − 1 ;
// Publish the message .
pub.publish ( msg ) ;
// Send a message to rosout with the details .
ROS_INFO_STREAM( "Sending random velocity command : "
<< " linear=" << msg.linear.x
<< " angular=" << msg.angular.z) ;
// Wait untilit's time for another iteration .
rate.sleep ( ) ;
}
}
rosrun agitr pubvel
rosrun turtlesim turtlesim_node
// This program subscribes to turtle1/pose and shows its
// messages on the screen .
#include <ros / ros.h>
#include <turtlesim / Pose.h>
#include <iomanip> // for std::setprecision and std::fixed
// A callback function . Executed each time a new pose
// message arrives .
void poseMessageReceived ( const turtlesim::Pose& msg ) {
ROS_INFO_STREAM( std::setprecision ( 2 ) << std::fixed
<< " position =(" << msg . x << " , " << msg . y << " ) "
<< " *direction=" << msg . theta ) ;
}
int main ( int argc , char ** argv ) {
// Initialize the ROS system and become a node .
ros::init ( argc , argv , " subscribe_to _pose " ) ;
ros::NodeHandle nh ;
// Create a subscriber object .
ros::Subscriber sub = nh.subscribe ( " turtle1/pose " ,1000,
&poseMessageReceived ) ;
// Let ROS take over .
ros::spin ( ) ;
}
日志消息
服务级别 | 示例消息 |
---|---|
DEBUG | reading header from buffer |
INFO | Waiting for all connections to establish |
WARN | Less than 5GB of space free on disk |
ERROR | Publisher header did not have required element: type |
FATAL | You must call ros::init() before creating the first NodeHandle |
// This program periodical ly generates log messages at
// various severity levels .
#include <ros/ros.h>
int main (int argc,char **argv) {
// Initialize the ROS system and become a node .
ros::init(argc, argv, "count_and_log");
ros::NodeHandle nh;
// Generate log messages of varying severi ty regularly .
ros::Rate rate (10);
for(inti=1; ros::ok(); i++){
ROS_DEBUG_STREAM("Counted?to?"<<i);
if((i%3)==0){
ROS_INFO_STREAM(i<<"?is?divisible?by?3.");
}
if((i%5)==0){
ROS_WARN_STREAM(i<<"?is?divisible?by?5.");
}
if((i%10)==0){
ROS_ERROR_STREAM(i<<"?is?divisible?by?10.");
}
if((i%20)==0){
ROS_FATAL_STREAM(i<<"?is?divisible?by?20.");
}
rate.sleep();
}
}
// This program generates a single log message at each
// severity level .
#include <ros/ros.h>
int main ( int argc , char **argv ) {
ros::init(argc, argv, "log_once");
ros::NodeHandle nh ;
while( ros::ok( )){
ROS_DEBUG_STREAM_ONCE( "This␣ appear s␣ only␣ once . " ) ;
ROS_INFO_STREAM_ONCE( "This␣ appear s␣ only␣ once . " ) ;
ROS_WARN_STREAM_ONCE( "This␣ appear s␣ only␣ once . " ) ;
ROS_ERROR_STREAM_ONCE( "This␣ appear s␣ only␣ once . " ) ;
ROS_FATAL_STREAM_ONCE( "This␣ appear s␣ only␣ once . " ) ;
}
}
// This program generates log messages at varying severity
// levels, throttled to various maximum speeds.
#include <ros/ros.h>
int main (int argc, char argv) {
ros::init (argc, argv, "log_throttled");
ros::NodeHandle nh ;
while(ros::ok( )){
ROS_DEBUG_STREAM_THROTTLE(0.1,
"This␣ appears␣ every␣ 0.1 seconds." ) ;
ROS_INFO_STREAM_THROTTLE(0.3,
"This␣ appears␣ every␣ 0.3 seconds." ) ;
ROS_WARN_STREAM_THROTTLE(0.5,
"This␣ appears␣ every␣ 0.5 seconds." ) ;
ROS_ERROR_STREAM_THROTTLE(1.0,
"This␣ appears␣ every␣ 1.0 seconds." ) ;
ROS_FATAL_STREAM_THROTTLE(2.0,
"This␣ appears␣ every␣ 2.0 seconds." ) ;
}
}
// [${severity}] [${time}]: ${message}
rostopic echo /rosout
rqt_console
# ~/.ros/log/run_id/rosout.log
rosparam get /run_id
rosclean check
rosclean purge
rosservice call /node-name/set_logger_level ros.package-name leve
rosservice call /count_and_log/set_logger_level ros.agitr DEBUG
rqt_logger_level
计算图源命名
- 全局名称 相对名称 私有名称 匿名名称
// This program starts with an anonymous name, which
// allows multiple copies to execute at the same time ,
// without needing to manually create distinct names
// for each of them.
#include <ros/ros.h>
int main ( int argc, char **argv ) {
ros::init ( argc, argv, "anon",
ros::init_options::AnonymousName );
ros::NodeHandle nh ;
ros::Rate rate (1) ;
while (ros::ok( )) {
ROS_INFO_STREAM( "This␣ message␣ is␣ from␣ " << ros::this_node::getName ( ));
rate.sleep( );
}
}
启动文件
<launch>
<node
pkg="turtlesim "
type="turtlesim_node"
name="turtlesim "
respawn="true "
/>
<node
pkg="turtlesim "
type="turtle_teleop_key"
name="teleop_key"
required="true "
launch −prefix="xterm −e"
/>
<node
pkg="agitr "
type="subpose"
name="pose_subscriber "
output="screen "
/>
</launch>
<launch>
<node
name="turtlesim_node"
pkg="turtlesim "
type="turtlesim_node"
ns="sim1"
/>
<node
pkg="turtlesim "
type="turtle_teleop_key"
name="teleop_key"
required="true "
launch −prefix="xterm −e"
ns="sim1"
/>
<node
name="turtlesim_node"
pkg="turtlesim "
type="turtlesim_node"
ns="sim2"
/>
<node
pkg="agitr "
type="pubvel"
name="velocity_publisher "
ns="sim2"
/>
</launch>
roslaunch package-name launch-file-name
roslaunch agitr example.launch
roslaunch –v package-name launch-file-name
// This program subscribes to turtle1/cmd_vel and
// republishes on turtle1/cmd_vel_reversed ,
// with the signs inverted .
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
ros::Publisher *pubPtr ;
void commandVelocityReceived (
const geometry_msgs::Twist& msgIn
) {
geometry_msgs::Twist msgOut ;
msgOut.linear.x = −msgIn.linear. x ;
msgOut.angular.z = −msgIn.angular.z ;
pubPtr−>publish (msgOut) ;
}
int main( int argc , char ∗∗ argv ) {
ros::init ( argc , argv , " reverse_velocity ") ;
ros::NodeHandle nh ;
pubPtr = new ros::Publisher (
nh.advertise<geometry_msgs::Twist>( " turtle1/cmd_vel_reversed" , 1000) ) ;
ros::Subscriber sub = nh.subscribe ( " turtle1/cmd_vel" , 1000 , &commandVelocityReceived ) ;
ros::spin () ;
delete pubPtr ;
}
reverse.launch
<launch>
<node
pkg="turtlesim "
type="turtlesim_node"
name="turtlesim "
>
<remap
from="turtle1 /cmd_vel"
to="turtle1 /cmd_vel_reversed"
/>
</node>
<node
pkg="turtlesim "
type="turtle_teleop_key"
name="teleop_key"
launch −prefix="xterm −e"
/>
<node
pkg="agitr "
type="reverse_cmd_vel"
name="reverse_velocity "
/>
</launch>
triplesim.luanch
<launch>
<include
file ="$( find agitr )/doublesim . launch"
/>
<arg
name="use_sim3"
default ="0"
/>
<group ns="sim3" if ="$( arg use_sim3 )" >
<node
name="turtlesim_node"
pkg="turtlesim "
type="turtlesim_node"
/>
<node
pkg="turtlesim "
type="turtle_teleop_key"
name="teleop_key"
required="true "
launch−prefix="xterm −e"
/>
</group>
</launch>
参数
rosparam list
rosparam get parameter_name
rosparam get /rosdistro
rosparam get namespace
rosparam get /
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 dump filename namespace
rosparam load filename namespace
rosparam get /background_r
rosparam get /background_g
rosparam get /background_b
rosparam set /background_r 255
rosparam set /background_g 255
rosparam set /background_b 0
rosservice call /clear
// This program waits for a turtlesim to start up , and
// changes its background color .
// C++编写的 set_bg_color.cpp 例程,设置 turtlesim 窗口背景颜色
#include <ros/ros . h>
#include <std_srvs/Empty . h>
int main( int argc , char ** argv ) {
ros::init ( argc , argv , "set_bg_color") ;
ros::NodeHandle nh ;
// Wait until the clear service is available , which
// indicates that turtlesim has started up , and has
// set the background color parameters .
ros::service::waitForService (" clear ") ;
// Set the background color for turtlesim ,
// overriding the default blue color .
ros::param::set ("background_r" , 255) ;
ros::param::set ("background_g" , 255) ;
ros::param::set ("background_b" , 0) ;
// Get turtlesim to pick up the new parameter values .
ros::ServiceClient clearClient = nh . serviceClient <std_srvs::Empty>("/ clear ") ;
std_srvs::Empty srv ;
clearClient . call ( srv ) ;
}
// C++编写的 pubvel_with_max.cpp 例程,通过从参数中读取线性速度来扩展原来的 pubvel.cpp 例程
// This program publishes random velocity commands, using
// a maximum linear velocity read from a parameter.
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>
int main( int argc , char ** argv ) {
ros::init ( argc , argv , " publish_velocity ") ;
ros::NodeHandle nh ;
ros::Publisher pub = nh.advertise <geometry_msgs::Twist>(" turtle1/cmd_vel" , 1000) ;
srand ( time (0) ) ;
// Get the maximum velocity parameter .
const std::string PARAM_NAME = "~max_vel" ;
double maxVel ;
bool ok = ros::param::get (PARAM_NAME, maxVel) ;
if ( !ok) {
ROS_FATAL_STREAM("Could␣ not␣ get␣ parameter " << PARAM_NAME) ;
exit (1) ;
}
ros::Rate rate (2) ;
while ( ros::ok () ) {
// Create and send a random velocity command.
geometry_msgs::Twist msg ;
msg.linear.x = maxVel* double ( rand () )/double (RAND_MAX) ;
msg.angular.z = 2*double ( rand () )/double (RAND_MAX) −1;
pub.publish(msg) ;
// Wait untilit's time for another iteration .
rate.sleep () ;
}
}
rosparam set /publish_velocity/max_vel 0.1
<!-- 启动文件 fast_yellow.launch,启动 7.1 和 7.2 中的例子并且设置 max_vel 参数 -->
<launch>
<node
pkg="turtlesim "
type="turtlesim_node"
name="turtlesim "
/>
<node
pkg="agitr "
type="pubvel_with_max"
name="publish_velocity "
>
<param name="max_vel" value="3" />
</node>
<node
pkg="agitr "
type="set_bg_color"
name="set_bg_color"
/>
</launch>
服务
rosservice list
rosnode info node-name
rosservice node service-name
rosservice info service-name
rosservice info /spawn
rossrv show service-data-type-name
rossrv show turtlesim/Spawn