热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ROS学习笔记(五)ROS键盘按键读取模块

key_ctrl主要用于监控键盘按钮,定义按钮功能,便于智能车的控制和调试该包主要包含:key.hkey_ctrl_node.cpp

key_ctrl主要用于监控键盘按钮,定义按钮功能,便于智能车的控制和调试

该包主要包含:

key.h

key_ctrl_node.cpp

common.h

keyboard_ctrl.msg

CMakeLists.txt

package.xml

详细代码如下:

key.h


#ifndef KEY_H
#define KEY_H
#include
#include
#include
#include class Keyboard_ctrl {struct termios initial_settings, new_settings;int peek_character = -1;public:Keyboard_ctrl() { init_keyboard(); };~Keyboard_ctrl() { close_keyboard(); };int get_keyboard_press_key() {kbhit();return readch();// printf("%02x \n", readch());};private:void init_keyboard() {tcgetattr(0, &initial_settings);new_settings = initial_settings;new_settings.c_lflag &= ~(ICANON | ECHO);new_settings.c_cc[VEOL] = 1;new_settings.c_cc[VEOF] = 2;tcsetattr(0, TCSANOW, &new_settings);}void close_keyboard() { tcsetattr(0, TCSANOW, &initial_settings); }int kbhit() {unsigned char ch;int nread;if (peek_character != -1) return 1;new_settings.c_cc[VMIN] = 0;tcsetattr(0, TCSANOW, &new_settings);nread = read(0, &ch, 1);new_settings.c_cc[VMIN] = 1;tcsetattr(0, TCSANOW, &new_settings);if (nread == 1) {peek_character = ch;return 1;}return 0;}int readch() {char ch;if (peek_character != -1) {ch = peek_character;peek_character = -1;return ch;}read(0, &ch, 1);return ch;}
};#endif // KEY_H​

key_ctrl_node.cpp

#include "common.h"
#include "control_msgs/keyboard_ctrl.h"
#include "key.h"
#include "ros/ros.h"
const int KEYCODE_W = 0x77;
const int KEYCODE_A = 0x61;
const int KEYCODE_S = 0x73;
const int KEYCODE_D = 0x64;const int KEYCODE_A_CAP = 0x41;
const int KEYCODE_D_CAP = 0x44;
const int KEYCODE_S_CAP = 0x53;
const int KEYCODE_W_CAP = 0x57;const int KEYCODE_0 = 0x30;
const int KEYCODE_9 = 0x39;const int KEYCODE_z_P = 0x7A;
const int KEYCODE_x_D = 0x78;
const int KEYCODE_c_R = 0x63;
const int KEYCODE_v_N = 0x76;
const int KEYCODE_z_CAP_P = 0x5A;
const int KEYCODE_x_CAP_D = 0x58;
const int KEYCODE_c_CAP_R = 0x43;
const int KEYCODE_v_CAP_N = 0x56;const int KEYCODE_BACKSPACE = 0x7F;
// const int KEYCODE_SHIFT = 0x10;
// const int KEYCODE_CTRL = 0x11;
// const int KEYCODE_ALT = 0x11;
const int KEYCODE_ESC = 0x1B;
const int KEYCODE_ENTER = 0x0A;
const int KEYCODE_SPACE = 0x20;using namespace KEY_CTRL;
using namespace CONTROL_COMMON;
int main(int argc, char **argv) {ros::init(argc, argv, "keyboard_control");ros::NodeHandle nh;ros::Publisher pub_keyboard &#61;nh.advertise("/keyboard_ctrl", 1000);ros::Rate loop_rate(10);auto KBC &#61; Keyboard_ctrl();while (ros::ok()) {auto key &#61; KBC.get_keyboard_press_key();ROS_INFO("get keyboard press 0x%02X \n", key);static GEER geer_mode &#61; GEER::P;int value &#61; 0;KEY_CLASS order;if (key >&#61; KEYCODE_0 && key <&#61; KEYCODE_9) {order &#61; KEY_CLASS::SPEED;value &#61; key - KEYCODE_0;ROS_INFO("set car speed : %d ", value);} else if (key &#61;&#61; KEYCODE_SPACE) {static bool is_start &#61; false;order &#61; KEY_CLASS::START;is_start &#61; !is_start;value &#61; is_start;ROS_INFO("start/stop car: %d ", is_start);} else if (key &#61;&#61; KEYCODE_W || key &#61;&#61; KEYCODE_W_CAP) {order &#61; KEY_CLASS::DIR;value &#61; int(KB::UP);ROS_INFO("manu ctrl up %d", value);} else if (key &#61;&#61; KEYCODE_S || key &#61;&#61; KEYCODE_S_CAP) {order &#61; KEY_CLASS::DIR;value &#61; int(KB::DOWN);ROS_INFO("manu ctrl down %d", value);} else if (key &#61;&#61; KEYCODE_A || key &#61;&#61; KEYCODE_A_CAP) {order &#61; KEY_CLASS::DIR;value &#61; int(KB::LEFT);ROS_INFO("manu ctrl left %d", value);} else if (key &#61;&#61; KEYCODE_D || key &#61;&#61; KEYCODE_D_CAP) {order &#61; KEY_CLASS::DIR;value &#61; int(KB::RIGHT);ROS_INFO("manu ctrl right %d", value);} else if (key &#61;&#61; KEYCODE_z_P || key &#61;&#61; KEYCODE_z_CAP_P) {order &#61; KEY_CLASS::GEER;value &#61; int(GEER::P);ROS_INFO("manu ctrl geer P %d", value);} else if (key &#61;&#61; KEYCODE_x_D || key &#61;&#61; KEYCODE_x_CAP_D) {order &#61; KEY_CLASS::GEER;value &#61; int(GEER::D);ROS_INFO("manu ctrl geer D %d", value);} else if (key &#61;&#61; KEYCODE_c_R || key &#61;&#61; KEYCODE_c_CAP_R) {order &#61; KEY_CLASS::GEER;value &#61; int(GEER::R);ROS_INFO("manu ctrl geer R %d", value);} else if (key &#61;&#61; KEYCODE_v_N || key &#61;&#61; KEYCODE_v_CAP_N) {order &#61; KEY_CLASS::GEER;value &#61; int(GEER::N);ROS_INFO("manu ctrl geer N %d", value);}if (order &#61;&#61; KEY_CLASS::GEER) {geer_mode &#61; GEER(value);}control_msgs::keyboard_ctrl kbc_msg;kbc_msg.order &#61; int(order);kbc_msg.value &#61; value;pub_keyboard.publish(kbc_msg);ros::spinOnce();loop_rate.sleep();}return 0;
}

common.h

#ifndef COMMON_H
#define COMMON_H
#include
#include
#include
using namespace std;namespace KEY_CTRL {
enum class KB { UP &#61; 1, DOWN, LEFT, RIGHT };
enum class KEY_CLASS { GEER &#61; 1, DIR, START, SPEED };enum class GEER { N &#61; 0, D, R, P };
} // namespace KEY_CTRL#endif // COMMON_H

keyboard_ctrl.msg

int32 order
int32 value

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(key_ctrl)
add_compile_options(-std&#61;c&#43;&#43;11)
find_package(catkin REQUIRED COMPONENTSroscppstd_msgscontrol_msgs)catkin_package(CATKIN_DEPENDS)include_directories(include${catkin_INCLUDE_DIRS}${PROJECT_SOURCE_DIR}/../../common/include
)add_executable(${PROJECT_NAME} src/${PROJECT_NAME}_node.cpp)
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} )
add_dependencies(${PROJECT_NAME} ${catkin_EXPORTED_TARGETS})install(TARGETS ${PROJECT_NAME}ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)install(DIRECTORY launchDESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)install(DIRECTORY srcDESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}FILES_MATCHING PATTERN "*.h"PATTERN ".svn" EXCLUDE
)message("make ${PROJECT_NAME} finish !")

package.xml


key_ctrl0.1.0The key_ctrl packagepsdzApache 2.0catkincontrol_msgscontrol_msgs


推荐阅读
author-avatar
酱油丸子-310
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有