ros基本要素:

1.节点 :节点与节点用tcp/ip通信
2.节点管理器 :(相当于域名解析器)所有节点的枢纽,节点之间要实现互相通信,都要通过节点管理器
3.消息 :
4.参数服务器  :
5.主题 :某一类的节点通信 例如:scan主题,所有扫描的订阅和发布都可以放在这个主题上
6.服务 :
7.消息记录包:
8.订阅:
9.发布:
 
 
c++中的例子提取出的步骤
1.创建消息和服务:
消息文件(.msg)一般带Header+基本类型数据+消息文件类型
服务文件(.srv)请求+响应 ---划分,请求,响应也是由基本类型组成
 
 
// 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! ");

c++发布订阅端程序

// 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",1000);
 
// 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 ();
}
}
表3

订阅者的程序

// 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 subscri ber obj ect .
 ros::Subscriber sub = nh.subscribe (" turtle1/pose ",1000,
&poseMessageReceived );
 
// Let ROS take over .
 ros::spin ();
}