#include "ros/ros.h"
#include "std_msgs/Int32.h"
#include <termio.h>
using namespace std;
int main(int argc, char *argv[])
{
ros::init(argc, argv, "switch_flag");
ros::NodeHandle nh;
ros::Publisher pub;
pub = nh.advertise<std_msgs::Int32>("/switch", 10);
std_msgs::Int32 key;
while (true)
{
int in;
struct termios new_settings;
struct termios stored_settings;
//设置终端参数
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr(0, &stored_settings);
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
in = getchar();
if (!ros::ok())
break;
tcsetattr(0, TCSANOW, &stored_settings);
key.data = in;
cout << "key: " << in << endl;
cout << "key.data: " << key.data << endl;
pub.publish(key);
ros::spinOnce();
// cout << "123" << endl;
}
return 0;
}
C++将键盘数据作为ROS话题发布出去供订阅
最新推荐文章于 2024-04-30 15:09:50 发布