C++ 代码
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char** argv)
{
....
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
....
}
#python代码,简要示例
def callback(data):
rospy.loginfo("I heard %s",data.data)
def listener():
rospy.init_node('node_name')
rospy.Subscriber("chatter", String, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
C++ 解析图像
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
long int count_ =0000;//不能命名成count
int n=0;
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try //对错误异常进行捕获,检查数据的有效性
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
char base_name[256];
if(n%10==0)
{
sprintf(base_name,"/home/zhy/Documents/data/%04ld.jpg",count_++);
imwrite(base_name, cv_ptr->image);
}
n++;
}
catch(cv_bridge::Exception& e) //异常处理
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "msg2img2");
ros::NodeHandle nh_;
ros::Subscriber image_sub_ = nh_.subscribe("/pylon_camera_node/image_raw", 1, imageCallback);///middle/pylon_camera_node/image_raw/compressed
ros::spin();
return 0;
}
#C++代码
void chatterCallback(const std_msgs::String::ConstPtr& msg,type1 &arg1, type2 &arg2,...,typen &argN)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char** argv)
{
ros::Subscriber sub =
n.subscribe("chatter", 1000, boost::bind(&chatterCallback,_1,arg1,arg2,...,argN);
///需要注意的是,此处 _1 是占位符, 表示了const std_msgs::String::ConstPtr& msg。
}
#python代码,python中使用字典
def callback(data, args):
dict_1 = args[0]
dict_2 = args[1]
...
sub = rospy.Subscriber("text", String, callback, (dict_1, dict_2))
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
long int count_ =0000;//不能命名成count
int n=0;
void imageCallback(const sensor_msgs::ImageConstPtr& msg, int &rp,char* &ip, int &up)
{
cv_bridge::CvImagePtr cv_ptr;
try //对错误异常进行捕获,检查数据的有效性
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
char base_name[256];
if(n%10==0)
{
sprintf(base_name,"/home/zhy/Documents/data/%04ld.jpg",count_++);
imwrite(base_name, cv_ptr->image);
}
n++;
}
catch(cv_bridge::Exception& e) //异常处理
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
}
int main(int argc, char** argv)
{
int rtp=3;
char IP[40]="127.0.0.1";
int udp=4;
ros::init(argc, argv, "msg2img2");
ros::NodeHandle nh_;
ros::Subscriber image_sub_ = nh_.subscribe("/pylon_camera_node/image_raw", 1,
boost::bind(&imageCallback, _1, rtp,IP,udp));
ros::spin();
return 0;
}
boost::bind支持所有的function object, function, function pointer以及member function pointers,能够将行数形参数绑定为特定值或者调用所需要的参数,并可以将绑定的参数分配给任意形参。
定义如下函数:
int f(int a, int b)
{
return a + b;
}
bind(f,_1,5)(x)相当于
f(x,5);_1
是一个占位符,其位于f
函数形参的第一形参int a
的位置,5位于f
函数形参int b
的位置;_1
表示(x)
参数列表的第一个参数