odom_out_and_back.cpp
点击打开链接原文在这,搬运以便学习记录,感谢原作者
<span style="font-family: Arial, Helvetica, sans-serif;">#include "ros/ros.h" </span>
#include "std_msgs/String.h"//geometry_msgs
#include "geometry_msgs/Twist.h"//包含elocity space消息
#include <tf/transform_listener.h>
#include "math.h"
#include <sstream>
#include <iostream>
#include <rbx1_nav/CalibrateAngularConfig.h>
#include <rbx1_nav/CalibrateLinearConfig.h>
using namespace std; int main(int argc, char **argv)
{ ros::init(argc,argv,"out_and_back");//指定节点“out_and_back” ros::NodeHandle n;//创造一个节点句柄 ros::Publisher cmd_vel_pub=n.advertise<geometry_msgs::Twist>("/cmd_vel",1000);//将在/cmd_vel话题上发布一个geometry_msgs::Twist消息 int rate=20;//定义更新频率 ros::Rate loop_rate(rate);//更新频率20Hz,它会追踪记录自上一次调用Rate::sleep()后时间的流逝,并休眠直到一个频率周期的时间 //初始化操作 double linear_speed=0.2;//向前的线速度0.2m/s double goal_distance=1.0;//行进记录1.0m double angular_speed=1.0;//角度素1.0rad/s double goal_angle=M_PI; double angular_tolerance = 0.5*M_PI/180;//角度容忍度 tf::TransformListener listener; geometry_msgs::Twist move_cmd;//定义消息对象 move_cmd.linear.x=move_cmd.linear.y=move_cmd.linear.z=0; move_cmd.angular.x=move_cmd.angular.y=move_cmd.angular.z=0; tf::StampedTransform transform; try{ listener.waitForTransform("odom", "base_link", ros::Time(0), ros::Duration(1.0)); listener.lookupTransform("odom", "base_link", ros::Time(0), transform); } catch (tf::TransformException &ex) { ROS_ERROR("%s",ex.what()); ros::Duration(1.0).sleep(); } double x_start = transform.getOrigin().x(); double y_start = transform.getOrigin().y(); double angle_start = acos(transform.getRotation().z())*2; double distance = 0.0; double angle = 0.0; cout<<"angle_start: "<<angle_start<<endl; while(ros::ok())//等待键盘ctrl+C操作则停止 { tf::StampedTransform transform_; try{ listener.waitForTransform("odom", "base_link", ros::Time(0), ros::Duration(1.0)); listener.lookupTransform("odom", "base_link", ros::Time(0), transform_); } catch (tf::TransformException &ex) { ROS_ERROR("%s",ex.what()); ros::Duration(1.0).sleep(); } move_cmd.linear.x = linear_speed; if(distance < goal_distance){ cmd_vel_pub.publish(move_cmd); loop_rate.sleep(); distance = sqrt(pow((transform_.getOrigin().x() - x_start),2)+pow((transform_.getOrigin().y() - y_start),2)); cout<<"distance: "<<distance<<endl; cout<<"odom.x: "<<transform_.getOrigin().x()<<endl; cout<<"odom.y: "<<transform_.getOrigin().y()<<endl; } else { if(angle + angular_tolerance < goal_angle){ move_cmd.linear.x=0.0; move_cmd.angular.z=angular_speed; cmd_vel_pub.publish(move_cmd); loop_rate.sleep(); angle = abs(acos(transform_.getRotation().z())*2 - angle_start); cout<<"angle: "<<angle<<endl; } else{ move_cmd.linear.x=0.0; move_cmd.angular.z=0.0; cmd_vel_pub.publish(move_cmd); loop_rate.sleep(); cout<<"stop!"<<endl; } } } return 0;
}
转载于:https://www.cnblogs.com/siahekai/p/5840319.html
odom_out_and_back.cpp相关推荐
- C++ .h(头文件) 与 .cpp(源文件) 的使用
.h 文件: .h是头文件 ,里面主要是写类的声明(包括类里面的成员和方法的声明).函数原型.#define常数等, 注意.h文件写的时候有特定的格式就是开头和结尾 #ifndef TEST_HEAD ...
- 利用VS+MFC+Opencv显示图像和视频所需添加类(CvvImage.h和CvvImage.cpp的源码)。
CvvImage.h代码: #pragma once #ifndef CVVIMAGE_CLASS_DEF #define CVVIMAGE_CLASS_DEF #include "open ...
- UTF-8 CPP的使用
UTF-8 CPP是一个简单.小巧.轻量级.跨平台的UTF-8编码字符串库. 下面对其使用方法进行简单的介绍: 1. 从http://sourceforge.net/projects/utf ...
- 拇指接龙游戏升级记录03(升级MainScene.cpp)
MainScene是拇指接龙游戏的主游戏场景文件,拥有近5000行代码. 说实在的,实现自cocos2d-x 2.x版本向当下最新的3.8.1版本的升级过程,其中涉及的技术不是一下能够说明的.有些是形 ...
- GATB的使用小例子test.cpp
1.touch test.cpp,,文件夹中 出现test.cpp touch test.cpp 2. test.cpp的内容 #include <gatb/gatb_core.hpp>i ...
- Linux下运行.cpp文件
如何在Ubuntu16下运行一个简单的.cpp文件呢,做法如下: 假设我在桌面上写了一个hell,world程序; 保存为abc.cpp 然后在终端打开: 1.点击保存的文件的属性,看在哪里,我的是在 ...
- 2020-10-26runtime error: member access within null pointer of type ‘struct ListNode‘ (solution.cpp)错
runtime error: member access within null pointer of type 'struct ListNode' (solution.cpp)错误 /*** Def ...
- matlab怎么跑.cpp程序,MATLAB编译cpp文件
目的:打通MATLAB与VC之间的通道,实现用MATLAB调用VC程序,以及VC调用MATLAB程序. 上篇博客实现了将MATLAB的M文件编译成C/C++文件,exe应用程序.这篇实现MATLAB编 ...
- python如何调用cpp文件的接口函数_C++中嵌入Python调用
python嵌入到C++中 把python嵌入的C++里面需要做一些步骤 安装python程序,这样才能使用python的头文件和库 在我们写的源文件中增加"Python.h"头文 ...
最新文章
- L8.1 lvs+heartbeat-ldirectord实现高可用负载均衡
- HDU 1325 Is It A Tree? 并查集
- Python自动化办公之Excel对比工具
- 平板电脑安装软件_分享一个将平板作为电脑分屏的软件(文末有链接)
- mac下IPhone开发环境配置
- java8方法引用符合_Java8 方法引用和构造方法引用
- Binary XML file line #17vector tag requires viewportWidth 0
- matlab学习--语言基础
- vs2017可以写python_『vs python 使用教程』怎么用VS2017写一个最简单的Python程序,比如hello world?...
- 特斯拉和SolarCity推出太阳能屋顶瓦片
- jmeter录制脚本的步骤(很详细)
- Carthagecocopads 摘抄笔记
- [4G5G专题-83]:架构 - 移动通信网2G/3G/4G/5G/6G网络架构的演进历程
- 怎么修改docker镜像的名字_docker镜像如何重命名
- 三星内存编码_内存颗粒版本判断方法和编号解析(三星、美光、海力士)
- 集合框架ArrayList 源码分析(二)
- Macos安装lightgbm时遇到的坑
- logback filter过滤器简介说明
- 机器学习/深度学习/图机器学习 学习小结
- 编程练习2--编写一个程序,提示用户输入大写字母。使用嵌套循环以下面金字塔型的格式打印字母:
热门文章
- a king读后感 love of the_A华语电影高清合集
- java元注解_Java的元注解
- oracle清理表空间文件,如何自动删除表空间的文件?
- pyqt5示例_木辛老师的编程课堂:Python和Qt第一讲之初识PyQt5
- python apply_async执行不了_python – Django Celery apply_async不起作用
- Intel处理器CPUID指令学习
- Maven常见警告解决办法
- fullcalendar next 不变化_让不懂编程的人爱上iPhone开发(2017秋iOS11+Swift4+Xcode9版)-第3篇...
- 【算法】java 用 数组 模拟 队列
- 【Elasticsearch】es一个奇怪的问题 is_write_index fasle的时候还在写入 索引滚动无效