Robot Operating System (ROS)学习笔记3---键盘控制
搭建环境:XMWare Ubuntu14.04 ROS(indigo)
转载自古月居 转载连接:http://www.guyuehome.com/253
一、创建控制包
catkin_create-pkg smartcar_teleop rospy geometry_msgs std_msgs roscpp
catkin_make
建包,参考:http://www.ros.org/wiki/ROS/Tutorials/CreatingPackage
二、简单的消息发布
在smartcar_teleop下 创建scripts文件夹,在其文件夹下创建teleop.py文件
#!/usr/bin/env python
import roslib;
roslib.load_manifest('smartcar_teleop')
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import String class Teleop:
"""docstring fos Teleop"""
def __init__(self):
pub = rospy.Publisher('cmd_vel',Twist)
rospy.init_node('smartcar_teleop')
rate = rospy.Rate(rospy.get_param('~hz',1))
self.cmd = None cmd =Twist()
cmd.linear.x = 0.2
cmd.linear.y = 0
cmd.linear.z = 0 cmd.angular.x = 0
cmd.angular.y = 0
cmd.angular.z = 0.5 self.cmd = cmd while not rospy.is_shutdown():
str = "Hello world %s" %rospy.get_time()
rospy.loginfo(str)
pub.publish(self.cmd)
rate.sleep()
if __name__ == "__main__":Teleop()

sudo chmod +x teleop.py
rosrun smartcar_teleop teleop.py

可以建立一个launch文件(teleop.launch)运行
<launch>
<arg name="cmd_topic" default="cmd_vel" />
<node pkg="smartcar_teleop" type="teleop.py" name="smartcar_teleop">
<remap from="cmd_vel" to="$(arg cmd_topic)" />
</node>
</launch>
三、加入键盘控制
1、移植
首先,我们将其中src文件夹下的keyboard.cpp代码文件直接拷贝到我们smartcar_teleop包的src文件夹下,然后修改CMakeLists.txt文件,将下列代码加入文件底部:
add_executable(smartcar_keyboard_teleop src/keyboard.cpp)
target_link_libraries(smartcar_keyboard_teleop boost_thread ${catkin_LIBRARIES})
keyboard.cpp代码如下:
#include <termios.h>
#include <signal.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/poll.h> #include <boost/thread/thread.hpp>
#include <ros/ros.h>
#include <geometry_msgs/Twist.h> #define KEYCODE_W 0x77
#define KEYCODE_A 0x61
#define KEYCODE_S 0x73
#define KEYCODE_D 0x64 #define KEYCODE_A_CAP 0x41
#define KEYCODE_D_CAP 0x44
#define KEYCODE_S_CAP 0x53
#define KEYCODE_W_CAP 0x57 class SmartCarKeyboardTeleopNode
{
private:
double walk_vel_;
double run_vel_;
double yaw_rate_;
double yaw_rate_run_; geometry_msgs::Twist cmdvel_;
ros::NodeHandle n_;
ros::Publisher pub_; public:
SmartCarKeyboardTeleopNode()
{
pub_ = n_.advertise<geometry_msgs::Twist>("cmd_vel", 1); ros::NodeHandle n_private("~");
n_private.param("walk_vel", walk_vel_, 0.5);
n_private.param("run_vel", run_vel_, 1.0);
n_private.param("yaw_rate", yaw_rate_, 1.0);
n_private.param("yaw_rate_run", yaw_rate_run_, 1.5);
} ~SmartCarKeyboardTeleopNode() { }
void keyboardLoop(); void stopRobot()
{
cmdvel_.linear.x = 0.0;
cmdvel_.angular.z = 0.0;
pub_.publish(cmdvel_);
}
}; SmartCarKeyboardTeleopNode* tbk;
int kfd = 0;
struct termios cooked, raw;
bool done; int main(int argc, char** argv)
{
ros::init(argc,argv,"tbk", ros::init_options::AnonymousName | ros::init_options::NoSigintHandler);
SmartCarKeyboardTeleopNode tbk; boost::thread t = boost::thread(boost::bind(&SmartCarKeyboardTeleopNode::keyboardLoop, &tbk)); ros::spin(); t.interrupt();
t.join();
tbk.stopRobot();
tcsetattr(kfd, TCSANOW, &cooked); return(0);
} void SmartCarKeyboardTeleopNode::keyboardLoop()
{
char c;
double max_tv = walk_vel_;
double max_rv = yaw_rate_;
bool dirty = false;
int speed = 0;
int turn = 0; // get the console in raw mode
tcgetattr(kfd, &cooked);
memcpy(&raw, &cooked, sizeof(struct termios));
raw.c_lflag &=~ (ICANON | ECHO);
raw.c_cc[VEOL] = 1;
raw.c_cc[VEOF] = 2;
tcsetattr(kfd, TCSANOW, &raw); puts("Reading from keyboard");
puts("Use WASD keys to control the robot");
puts("Press Shift to move faster"); struct pollfd ufd;
ufd.fd = kfd;
ufd.events = POLLIN; for(;;)
{
boost::this_thread::interruption_point(); // get the next event from the keyboard
int num; if ((num = poll(&ufd, 1, 250)) <)
{
perror("poll():");
return;
}
else if(num > 0)
{
if(read(kfd, &c, 1) <)
{
perror("read():");
return;
}
}
else
{
if (dirty == true)
{
stopRobot();
dirty = false;
} continue;
} switch(c)
{
case KEYCODE_W:
max_tv = walk_vel_;
speed = 1;
turn = 0;
dirty = true;
break;
case KEYCODE_S:
max_tv = walk_vel_;
speed = -1;
turn = 0;
dirty = true;
break;
case KEYCODE_A:
max_rv = yaw_rate_;
speed = 0;
turn = 1;
dirty = true;
break;
case KEYCODE_D:
max_rv = yaw_rate_;
speed = 0;
turn = -1;
dirty = true;
break; case KEYCODE_W_CAP:
max_tv = run_vel_;
speed = 1;
turn = 0;
dirty = true;
break;
case KEYCODE_S_CAP:
max_tv = run_vel_;
speed = -1;
turn = 0;
dirty = true;
break;
case KEYCODE_A_CAP:
max_rv = yaw_rate_run_;
speed = 0;
turn = 1;
dirty = true;
break;
case KEYCODE_D_CAP:
max_rv = yaw_rate_run_;
speed = 0;
turn = -1;
dirty = true;
break;
default:
max_tv = walk_vel_;
max_rv = yaw_rate_;
speed = 0;
turn = 0;
dirty = false;
}
cmdvel_.linear.x = speed * max_tv;
cmdvel_.angular.z = turn * max_rv;
pub_.publish(cmdvel_);
}
}
编译完成后,运行smartcar模型。重新打开一个终端,打开键盘控制节点:
rosrun smartcar_teleop smartcar_keyboard_teleop
在终端中按下键盘里的“W”、“S”、“D”、“A”以及“Shift”键进行机器人的控制。
Robot Operating System (ROS)学习笔记3---键盘控制的更多相关文章
- Robot Operating System (ROS)学习笔记4---语音控制
搭建环境:XMWare Ubuntu14.04 ROS(indigo) 转载自古月居 转载连接:http://www.guyuehome.com/260 一.语音识别包 1.安装 ...
- Robot Operating System (ROS)学习笔记2---使用smartcar进行仿真
搭建环境:XMWare Ubuntu14.04 ROS(indigo) 转载自古月居 转载连接:http://www.guyuehome.com/248 一.模型完善 文件夹urdf下,创建ga ...
- Robot Operating System (ROS)学习笔记---创建简单的机器人模型smartcar
搭建环境:XMWare Ubuntu14.04 ROS(indigo) 转载自古月居 转载连接:http://www.guyuehome.com/243 一.创建硬件描述包 已创建catkin_ ...
- ROS是Robot Operating System
ROS是Robot Operating System 机器人操作系统ROS | 简介篇 同样,从个人微信公众号Nao(ID:qRobotics)搬运. 前言 先放一个ROS Industrial一 ...
- 快速了解 Robot Operating System(ROS) 机器人操作系统
http://www.ros.org/ 关于ROS About ROS http://www.ros.org/about-ros/ 机器人操作系统(ROS)是用于编写机器人软件的灵活框架.目的在简化 ...
- ROS学习笔记1-引言
该学习笔记参考ROS官方wiki的内容,见:http://wiki.ros.org/ROS/Introduction 什么是ROSROS的全称是Robot Operating System,即机器人操 ...
- Learning Roadmap of Robotic Operating System (ROS)
ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS ...
- System类学习笔记
最近在学习源码的过程中发现:很多深层次的代码都用到了一个类System类,所以决定对System类一探究竟 本文先对System类进行了剖析,然后对System类做了总结 一.首先对该类的中的所有字段 ...
- system generator学习笔记【02】
作者:桂. 时间:2018-05-20 23:28:04 链接:https://www.cnblogs.com/xingshansi/p/9059668.html 前言 继续学习sysgen.接触s ...
随机推荐
- win764位系统上让32位程序能申请到4GB内存方法
win764位系统上让32位程序能申请到4GB内存方法. 2016年09月18日 18:36:26 阅读数:1550 最近测试一个32位程序总是在1.2G左右内存时崩溃,怀疑是内存申请失败,本身32位 ...
- jQuery位置操作
position();获取当前标签相对于最近一个父标签中有positon:relative属性的位置. height();标签纯高度 innerHeight();标签内边距padding加上纯高度 o ...
- Azure SQL 数据库仓库Data Warehouse (1) 入门
<Windows Azure Platform 系列文章目录> 在之前的项目中遇到了客户使用SQL数据仓库的场景,在这里记录一下 1.什么是SQL 数据库仓库 (SQL DW) SQL D ...
- 外观(Facade)模式
外观模式:为子系统中的一组接口提供一个一致的界面.此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用 在软件开发中,有时候为了完成一项较为复杂的功能,一个客户类需要和多个业务类交互,而这些需 ...
- 时间复杂度On和空间复杂度O1是什么意思?
(1).把输入规模看成x轴,所花时间/空间看成y轴 O(n)就是y=x,y随x的增长而线性增长.也就是成正比,一条斜线. O(1)就是y=1,是一个常量,不管x怎么变,y不变,一条与x轴平行的线. ( ...
- PAT 乙级 1041 考试座位号(15) C++版
1041. 考试座位号(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 每个PAT考生在参加考试时都会被分 ...
- 开发框架-Web-Java:JeePlus
ylbtech-开发框架-Web-Java:JeePlus 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. http://www.jeeplus.org/ ...
- linux采集CPU温度并上传数据到云平台判断是否需要beep
如果要beep肯定要apt install beep的 但光安装好还不够,需要执行模块加载 /sbin/modprobe pcspkr 再写一个bash脚本 data=$(/usr/bin/senso ...
- Linux下设置固定IP的方法
本文转自http://blog.163.com/liulina_517@126/blog/static/3766198320118231431594/ linux系统安装完,以后通过命令模式配置网卡I ...
- 非ECS阿里云安装插件,给阿里云云监控平台
linux的init学习: https://blog.csdn.net/kunkliu/article/details/80942279 阿里云官方文档: https://help.aliyun.co ...