相比于笨拙的全局变量和全局函数,将suscriber和publisher成一个class,形式更加简洁和容易管理,一个节点就是一个类

参考资料

http://answers.ros.org/question/59725/publishing-to-a-topic-via-subscriber-callback-function/

http://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks

下面是自己写的示例代码:

#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>
#include <geometry_msgs/Pose2D.h>
#include <tf/transform_listener.h>
#include <stdio.h>
class test
{
private:
ros::NodeHandle nh_;
ros::Subscriber sub_;
tf::TransformBroadcaster br_;
bool data_ready; public:
test(ros::NodeHandle& nh)
{
nh_=nh;
sub_ = nh_.subscribe("/user_set", , &test::call_back,this);
data_ready = false;
}
//注意这里需要加const
void call_back(const geometry_msgs::Pose2DPtr& msgs)
{
ROS_INFO("recive");
tf::Transform dest_transform;
dest_transform.setOrigin(tf::Vector3(msgs->x,msgs->y,));
tf::Quaternion q;
q.setRPY(, , msgs->theta);
dest_transform.setRotation(q);
br_.sendTransform(tf::StampedTransform(dest_transform,ros::Time::now(),"world","user_set_frame"));
data_ready = true;
} bool is_data_ready()
{
if(data_ready)
return true;
else
return false;
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "tf_broadcaster");
ros::NodeHandle node;
test Otest(node);
tf::TransformListener listener;
tf::StampedTransform transform; while(ros::ok())
{
if(!Otest.is_data_ready())
{
ros::spinOnce(); continue;
}
 ROS_INFO("lookup_transfoem;");
try
{
listener.waitForTransform("world","user_set_frame",ros::Time::now(), ros::Duration(1.0));
listener.lookupTransform("world","user_set_frame",ros::Time(),transform);
}
catch (tf::TransformException ex)
{
ROS_ERROR("%s",ex.what());
break;
}
tf::Vector3 vectortf = transform.getOrigin();
ROS_INFO("transform.x:%f,transform.y:%f,transform.z:%f",vectortf.x(),vectortf.y(),vectortf.z());
ros::spinOnce();
}
return ;
}

下面是publisher,上面那段代码调好了,下面还没有

#include <iostream>

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <geometry_msgs/Pose2D.h>
#include <tf/transform_listener.h>
#include <tf/transform_broadcaster.h> class RobotDriver
{
private:
//! The node handle we'll be using
ros::NodeHandle nh_;
//! We will be publishing to the "cmd_vel" topic to issue commands
ros::Publisher cmd_vel_pub_;
//! we will be suscribing the topic "pos_dest"
ros::Subscriber sub = n.subscribe("pos_dest", 10, pos_dest_callback);
//! pos_dest_callback
tf::Transform dest_transform;
void pos_dest_callback(geometry_msgs::Pose2DPtr& msgs)
{
static tf::TransformBroadcaster br;
dest_transform.setOrigin(tf::Vector3(msgs->x,msgs->y,0));
tf::Quaternion q;
q.setRPY(0, 0, msgs->theta);
dest_transform.setRotation(q);
br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "odom", "pos_dest")); }
//! We will be listening to TF transforms as well
tf::TransformListener listener_;
//! cmd
geometry_msgs::Twist base_cmd_straight;
geometry_msgs::Twist base_cmd_turn;
geometry_msgs::Twist stop_cmd;
tf::Transform current_transform; public:
//! ROS node initialization
RobotDriver(ros::NodeHandle &nh)
{
nh_ = nh;
//set up the publisher for the cmd_vel topic
cmd_vel_pub_ = nh_.advertise<geometry_msgs::Twist>("cmd_vel", 1);
//cmd
//the command will be to go forward at 0.25 m/s
base_cmd_straight.linear.y = base_cmd_straight.angular.z = 0;
base_cmd.base_cmd_straight.x = 0.1; base_cmd_turn.linear.x = base_cmd_turn.linear.y = 0.0;
base_cmd_turn.angular.z = 0.75; stop_cmd.linear.y = stop_cmd.angular.z = 0;
stop_cmd.linear.x = 0;
} bool turnOdom(bool clockwise, double radians)
{
while(radians < 0) radians += 2*M_PI;
while(radians > 2*M_PI) radians -= 2*M_PI; //wait for the listener to get the first message
listener_.waitForTransform("base_link", "odom",
ros::Time(0), ros::Duration(1.0)); //we will record transforms here
tf::StampedTransform start_transform;
tf::StampedTransform current_transform; //record the starting transform from the odometry to the base frame
listener_.lookupTransform("base_link", "odom",
ros::Time(0), start_transform); //we will be sending commands of type "twist"
geometry_msgs::Twist base_cmd;
//the command will be to turn at 0.75 rad/s
base_cmd.linear.x = base_cmd.linear.y = 0.0;
base_cmd.angular.z = 0.75; geometry_msgs::Twist stop_cmd;
stop_cmd.linear.y = stop_cmd.angular.z = 0;
stop_cmd.linear.x = 0; if (clockwise) base_cmd.angular.z = -base_cmd.angular.z; //the axis we want to be rotating by
tf::Vector3 desired_turn_axis(0,0,1);
if (!clockwise) desired_turn_axis = -desired_turn_axis; ros::Rate rate(10.0);
bool done = false;
while (!done && nh_.ok())
{
//send the drive command
cmd_vel_pub_.publish(base_cmd);
rate.sleep();
//get the current transform
try
{
listener_.lookupTransform("base_link", "odom",
ros::Time(0), current_transform);
}
catch (tf::TransformException ex)
{
ROS_ERROR("%s",ex.what());
break;
}
tf::Transform global_transform =
current_transform;
tf::Vector3 actual_turn_axis =
global_transform.getRotation().getAxis();
ROS_INFO("actual_turn_axis.x=%f,actual_turn_axis.y=%f,actual_turn_axis.z=%f",(float)actual_turn_axis.getX(),(float)actual_turn_axis.getY(),(float)actual_turn_axis.getZ());
double angle_turned = global_transform.getRotation().getAngle();
if ( fabs(angle_turned) < 1.0e-2) continue; if ( actual_turn_axis.dot( desired_turn_axis ) < 0 )
angle_turned = 2 * M_PI - angle_turned; if (angle_turned > radians) done = true;
}
if (done)
{
cmd_vel_pub_.publish(stop_cmd); return true;
}
return false;
} }; int main(int argc, char** argv)
{
//init the ROS node
ros::init(argc, argv, "robot_driver");
ros::NodeHandle nh; RobotDriver driver(nh);
driver.driveForwardOdom(0.2);
}

  

将ros中suscriber和publisher写入class中的更多相关文章

  1. elasticsearch备份与恢复4_使用ES-Hadoop将ES中的索引数据写入HDFS中

    背景知识见链接:elasticsearch备份与恢复3_使用ES-Hadoop将HDFS数据写入Elasticsearch中 项目参考<Elasticsearch集成Hadoop最佳实践> ...

  2. ListView中的数据表格写入Excel中

    SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = "xls"; sfd.Filter = "Exce ...

  3. Excelvba从文件中逐行读取并写入excel中

    Sub 読み込む() Dim result As Long Dim dialog As FileDialog Set dialog = Application.FileDialog(msoFileDi ...

  4. 将Datagridview中的数据导出至Excel中

        首先添加一个模块ImportToExcel,并添加引用         然后导入命名空间: Imports Microsoft.Office.Interop Imports System.Da ...

  5. 将gridFS中的图片文件写入硬盘

    开启用户验证下的gridfs 连接使用,在执行脚本前可以在python shell中 from pymongo import Connectionfrom gridfs import *con = C ...

  6. 简单通过java的socket&serversocket以及多线程技术实现多客户端的数据的传输,并将数据写入hbase中

    业务需求说明,由于公司数据中心处于刚开始部署的阶段,这需要涉及其它部分将数据全部汇总到数据中心,这实现的方式是同上传json文件,通过采用socket&serversocket实现传输. 其中 ...

  7. Java基础知识强化之IO流笔记51:IO流练习之 键盘录入学生信息按照总分排序写入文本文件中的案例

    1.  键盘录入学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分排序写入文本文件中 分析:   A:创建学生类   B:创建集合对象      TreeSet<Student>   ...

  8. Scrapy基础(九)————将不定长度的URL进行固定长度写入Item中

    前面讲到将每篇文章的URL写入Item,但是每个url的长度是不同的,可以在Item中设置一个字段怎样使得每个URL的长度相同,这就需要对每个URL进行md5运算,使得长度统一,再加入到设定的Item ...

  9. spark读取 kafka nginx网站日志消息 并写入HDFS中(转)

    原文链接:spark读取 kafka nginx网站日志消息 并写入HDFS中 spark 版本为1.0 kafka 版本为0.8 首先来看看kafka的架构图 详细了解请参考官方 我这边有三台机器用 ...

随机推荐

  1. hihocoder #1394 : 网络流四·最小路径覆盖(最小路径覆盖)

    #1394 : 网络流四·最小路径覆盖 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机 ...

  2. 剑指Offer - 九度1373 - 整数中1出现的次数(从1到n整数中1出现的次数)

    剑指Offer - 九度1373 - 整数中1出现的次数(从1到n整数中1出现的次数)2014-02-05 23:03 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直 ...

  3. 剑指Offer - 九度1511 - 从尾到头打印链表

    剑指Offer - 九度1511 - 从尾到头打印链表2013-11-29 21:08 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例.每一组测试案例 ...

  4. Windows 2003 IIS服务器配置环境(放.net开发的网站)

    配置IIS 安装.Net Framework3.5 安装SQL数据库 IIS的配置与安装步骤: 开始->管理工具->管理您的服务器->添加角色->(配置您的服务器向导)下一步- ...

  5. 常用模块(sys)

    import sys# sys.argv() # 命令参数List,第一个元素是程序本身路径,如:python test.py run db# sys.exit('shh') # 退出程序,正常退出时 ...

  6. linux部署环境配置

    https://blog.csdn.net/dsczxcc/article/details/78728330

  7. [转]LVS+Keepalived负载均衡配置

    简介 来源:https://www.cnblogs.com/MacoLee/p/5858995.html lvs一般是和keepalived一起组合使用的,虽然也可以单独使用lvs,但配置比较繁琐,且 ...

  8. HDU 4027 Can you answer these queries(线段树 + 观察 )

    这题主要考察观察能力. 2^63最多只需要开7次根号就会变成1,当数字变成1之后就不需要再对其进行操作. 对于含有大于1数字的区间,向下更新. 对于数字全为1的区间,直接返回. #include &l ...

  9. ALPHA(10)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  10. HDU 2491

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Priest John's Busiest Day Time Limit: 4000/2000 MS (Java/Others)    ...