ros talker listener主要基本api 介绍

ros::init

void ros::init ( const M_string &  remappings,


const std::string &  name,


uint32_t  options = 0

)


alternate ROS initialization function.

Parameters:

remappings  A map<string, string> where each one constitutes a name remapping, or one of the special remappings like __name, __master, __ns, etc.

name  Name of this node. The name must be a base name, ie. it cannot contain namespaces.

options  [optional] Options to start the node with (a set of bit flags from ros::init_options)
Exceptions:

InvalidNodeNameException  If the name passed in is not a valid "base" name

Definition at line 386 of file init.cpp.

void ros::init ( int &  argc,


char **  argv,


const std::string &  name,


uint32_t  options = 0

)


ROS initialization function.

This function will parse any ROS arguments (e.g., topic name remappings), and will consume them (i.e., argc and argv may be modified as a result of this call).

Use this version if you are using the NodeHandle API

Parameters:

argc 

argv 

name  Name of this node. The name must be a base name, ie. it cannot contain namespaces.

options  [optional] Options to start the node with (a set of bit flags from ros::init_options)
Exceptions:

InvalidNodeNameException  If the name passed in is not a valid "base" name
对name中部分做些解释

ROS中图资源命名包括四种格式: base/relative/global/private, 大概是这个样子:

  • base
  • relative/name
  • /global/name
  • ~private/name

也就是说直接名字开头的不以/开头的表示base names, 这种方式是最常用的, 因为可以方便的remapping到其他namespace.而以/开头的表示global names, 这种方式不推荐用, 因为这样写出来的代码不太方便移植.

2 ros::publisher

ros::Publisher::Publisher ( const std::string &  topic,


const std::string &  md5sum,


const std::string &  datatype,


const NodeHandle &  node_handle,


const SubscriberCallbacksPtr &  callbacks 

)
[private]

Definition at line 59 of file publisher.cpp.


Member Function Documentation

其中有一个publish的成员比较常用

void ros::Publisher::publish ( const M &  message ) const [inline]

Publish a message on the topic associated with this Publisher.

Definition at line 95 of file publisher.h.

void ros::Publisher::publish ( const boost::function< SerializedMessage(void)> &  serfunc,
    SerializedMessage & 
  )   const [private]

Definition at line 78 of file publisher.cpp.

使用用法:

ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);chatter_pub.publish(msg);
ros::Subscriber 

Public Member Functions

uint32_t  getNumPublishers () const
  Returns the number of publishers this subscriber is connected to. 
std::string  getTopic () const
  operator void * () const
bool  operator!= (const Subscriber &rhs) const
bool  operator< (const Subscriber &rhs) const
bool  operator== (const Subscriber &rhs) const
void  shutdown ()
  Unsubscribe the callback associated with this Subscriber
  Subscriber ()
  Subscriber (const Subscriber &rhs)
  ~Subscriber ()


Private Member Functions

  Subscriber (const std::string &topic, const NodeHandle &node_handle, const SubscriptionCallbackHelperPtr &helper)


ros::Subscriber::Subscriber ( const std::string &  topic,
    const NodeHandle &  node_handle,
    const SubscriptionCallbackHelperPtr &  helper 
  )


Detailed Description

Manages an subscription callback on a specific topic.

Subscriber should always be created through a call to NodeHandle::subscribe(), or copied from one that was. Once all copies of a specific Subscriber go out of scope, the subscription callback associated with that handle will stop being called. Once all Subscriber for a given topic go out of scope the topic will be unsubscribed.

使用示例:ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);


4 ros::Rate

Public Member Functions

Duration  cycleTime ()
Get the actual run time of a cycle from start to sleep. 
Duration  expectedCycleTime ()
Get the expected cycle time -- one over the frequency passed in to the constructor. 
Rate (double frequency)
Constructor, creates a Rate
void  reset ()
Sets the start time for the rate to now. 
bool  sleep ()
Sleeps for any leftover time in a cycle. Calculated from the last time sleep, reset, or the constructor was called.

ROS_INFO 类似与格式化输出函数printf()

ros::spinOnce();开始整个node回调函数处理


原文地址:https://www.cnblogs.com/siahekai/p/5840317.html