(1)ui设计:
(2)写代码:
在main_window.cpp中连接:
//laser 9-1
connect(ui.laser_btn,SIGNAL(clicked()),this,SLOT(slot_quick_cmd_clicked()));
在main_window.hpp中声明槽函数
//laser 9-2
void slot_quick_cmd_clicked();
首先需要在main_window.hpp中导入头文件
#include <QProcess>
在main_window.cpp中实现槽函数:
这里又进行了ui设计,添加了一个textEdit 改变他的命名,然后右键点击,选择改变样式表:输入
background:rgb(0, 0, 0)
即可将他的背景变成黑色
同时为了 laser_cmd这个对象能在回显函数也可以访问到:把他声明在main_window.hpp中
//laser 9-4
QProcess *laser_cmd;
void MainWindow::slot_quick_cmd_clicked()
{
laser_cmd=new QProcess;
laser_cmd->start("bash"); //运行终端的命令
//通过write方法,写入终端命令行,toPlainText()获取文本框中的文字
laser_cmd->write(ui.textEdit_laser_cmd->toPlainText().toLocal8Bit()+'\n');
connect(laser_cmd,SIGNAL(readyReadStandardError()),this,SLOT(slot_quick_output()));
connect(laser_cmd,SIGNAL(readyReadStandardOutput()),this,SLOT(slot_quick_output()));
}
配置回显函数:
//laser 回显函数 9-6
void MainWindow::slot_quick_output()
{
//获取错误输出readAllStandardError()
ui.textEdit_quick_output->append("<font color=\"#FF0000\">"+laser_cmd->readAllStandardError()+"</font>");
ui.textEdit_quick_output->append("<font color=\"#FFFFFF\">"+laser_cmd->readAllStandardOutput()+"</font>");
}
需要配置机器人免密登陆,不然无法启动激光雷达节点
先在本地主机生成一个密匙:
ssh-keygen -t rsa
默认,敲入回车
然后上传到远程主机:
ssh-copy-id 主机名称
然后就可以不用密码登陆远程机器人系统了
可以现在终端测试一下,是否成功
ssh 主机名
遇到这个问题解决参考博客:
QT QProcess执行终端命令并实时输出回显
如果你想运行自己的命令的时候,我们是通过QProcess这个qt中的类来操作命令的,这个方式无法加载远程机器人的环境变量,所以需要我们自己手动刷新一下环境变量,不然会出现找不到roslaunch命令的
ssh huike@192.168.12.1
source /home/huike/robot_ws/devel/setup.bash
roslaunch huanyu_robot_start Huanyu_robot_start.launch
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.9.80-v7+ armv7l) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 615 packages can be updated. 254 updates are security updates. New release '18.04.4 LTS' available. Run 'do-release-upgrade' to upgrade to it.
欢迎使用Ubuntu 16.04.2 LTS(GNU/Linux 4.9.80-v7+armv7l)文档:https://help.ubuntu.com管理层:https://landscape.canonical.com*支持:https://ubuntu.com/advantage615包可以更新。254个更新是安全更新。提供新版本“18.04.4 LTS”。运行“do release upgrade”升级到它。
又会出现问题,当你退出QT界面的时候,你会发现小车的激光雷达依然在运行,也就是说此时机器人并没有关闭,怎么解决?
并且还会提示这样一句话:
Pseudo-terminal will not be allocated because stdin is not a terminal.
不会分配伪终端,因为stdin不是终端。
目前解决办法,在连接到机器人的终端上运行同样的节点,然后在ctrl+c 中断进程,也可以让机器人停止运动