当前位置:网站首页>Qt设计机器人仿真控制器——按键控制机器人关节转动
Qt设计机器人仿真控制器——按键控制机器人关节转动
2022-07-24 16:18:00 【用户6557940】
引言
本文结合Qt按键,实现通过按键控制机器人的姿态。
01
引言及本文简介
在上两篇博客里,Jungle介绍了Qt键盘事件,并在小程序中应用Qt键盘事件监测按键输入:
Qt键盘事件(二)——长按按键反复触发event事件问题解决
在昨天的文章里Qt设计仿真机器人控制器,Jungle结合Qt和Coin3D设计实现了机器人仿真控制器,鼠标拖拽控制器界面6个轴的滑条,分别控制机器人6个关节转动。本文Jungle将结合Qt键盘事件和机器人仿真控制器,实现一下功能:
- 按键按下1、2、3、4、5、6中的某个键n,表示接下来的按键操作将控制第n个关节转动;
- 按键按下“+”或“-”,控制第n个关节向正向或负向转动。
02
头文件设计
- 增加成员变量axisNum,用于记录用户想要控制哪个轴(1、2、3、4、5、6)
- 声明Qt按键事件函数
//按键事件
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
//用户按键控制轴号
int axisNum;03
实现
在Qt键盘事件(二)——长按按键反复触发event事件问题解决里提到关于按键event反复被触发的问题,但是在本文里将不作处理,因为本文要实现的效果是:比如用户想按键控制第一个关节,先按下数字“1”键,axisNum被置为1;再按下“-”键,在用户按下过程中(直至用户松键),机器人的第一个关节应该是持续转动,而不是在用户松键的时候才突然转动某个角度。基于上述考虑,keyPressEvent和keyReleaseEvent 实现如下:
void Robot::keyPressEvent(QKeyEvent *event)
{
double curValue[6] = {0};
//获取当前机器人各个轴的转动角度
getAxis(curValue);
switch(event->key()){
case Qt::Key_Equal:
{
switch(this->axisNum){
case 1:
ui.horizontalSlider_Axis1->setValue(curValue[0]*100+1000);
break;
case 2:
ui.horizontalSlider_Axis2->setValue(curValue[1]*100+1000);
break;
case 3:
ui.horizontalSlider_Axis3->setValue(curValue[2]*100+1000);
break;
case 4:
ui.horizontalSlider_Axis4->setValue(curValue[3]*100+1000);
break;
case 5:
ui.horizontalSlider_Axis5->setValue(curValue[4]*100+1000);
break;
case 6:
ui.horizontalSlider_Axis6->setValue(curValue[5]*100+1000);
break;
default:
break;
}
break;
}
case Qt::Key_Minus:
{
switch(this->axisNum){
case 1:
ui.horizontalSlider_Axis1->setValue(curValue[0]*100-1000);
break;
case 2:
ui.horizontalSlider_Axis2->setValue(curValue[1]*100-1000);
break;
case 3:
ui.horizontalSlider_Axis3->setValue(curValue[2]*100-1000);
break;
case 4:
ui.horizontalSlider_Axis4->setValue(curValue[3]*100-1000);
break;
case 5:
ui.horizontalSlider_Axis5->setValue(curValue[4]*100-1000);
break;
case 6:
ui.horizontalSlider_Axis6->setValue(curValue[5]*100-1000);
break;
default:
break;
}
break;
}
}
}
void Robot::keyReleaseEvent(QKeyEvent *event)
{
//在松键的时候记录用户按下的哪个键
switch(event->key()){
case Qt::Key_1:
axisNum = 1;
break;
case Qt::Key_2:
axisNum = 2;
break;
case Qt::Key_3:
axisNum = 3;
break;
case Qt::Key_4:
axisNum = 4;
break;
case Qt::Key_5:
axisNum = 5;
break;
case Qt::Key_6:
axisNum = 6;
break;
default:
break;
}
}需要说明的是,在keyPressEvent中,只是通过代码改变horizontalSlider_Axis1(代表控制器界面上机器人第一根轴的滑条)的值来控制机器人的第一个轴,这是因为horizontalSlider_Axis1值改变会自动触发控制机器人运动的槽函数:
connect(ui.horizontalSlider_Axis1,SIGNAL(valueChanged(int)),this,SLOT(setRobotPose()));
connect(ui.horizontalSlider_Axis2,SIGNAL(valueChanged(int)),this,SLOT(setRobotPose()));
connect(ui.horizontalSlider_Axis3,SIGNAL(valueChanged(int)),this,SLOT(setRobotPose()));
connect(ui.horizontalSlider_Axis4,SIGNAL(valueChanged(int)),this,SLOT(setRobotPose()));
connect(ui.horizontalSlider_Axis5,SIGNAL(valueChanged(int)),this,SLOT(setRobotPose()));
connect(ui.horizontalSlider_Axis6,SIGNAL(valueChanged(int)),this,SLOT(setRobotPose()));04
效果
http://mpvideo.qpic.cn/0af227dgze7fkcambigqcayfaecvhxfwfqs37vqqaiaa4ayjauha.f10002.mp4?
边栏推荐
- Chapter 2 using API Mgmnt service
- My first blog
- Kubernetes version docking object storage
- Simplified understanding: publish and subscribe
- 机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤
- 在 PHP Web 应用程序中防止 XSS 的最佳实践
- Yolov6 trains its own data set
- leetcode:162. 寻找峰值【二分寻找峰值】
- Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password
- C TCP client form application asynchronous receiving mode
猜你喜欢

Dynamics crm: mailbox configuration (III) - configure email server profiles and mailboxes

降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行

Leetcode 220. duplicate element III exists
[email protected]"/>ZBar project introduction and installation configuration| [email protected]

Which is a good noise reduction Bluetooth headset? Ranking of the most cost-effective noise reduction Bluetooth headsets

普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像

C TCP client form application asynchronous receiving mode

TCP协议调试工具TcpEngine V1.3.0使用教程

yolov6训练自己的数据集

Error 1053: the service did not respond to the start or control request in a timely fashion
随机推荐
Dynamics crm: how to set the order of forms
Knowledge points of MySQL (12)
Error 1053: the service did not respond to the start or control request in a timely fashion
Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
微调LayoutLM v3进行票据数据的处理和内容识别
Fast RCNN trains its own data set
Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st
收益率在百分之六以上的理财产品,请说一下
R language ggplot2 visualization: ggplot2 visualization basic scatter plot, through in theme_ Specify the parameter base in BW_ Size to change the size of axis labels and control the size of gridlines
Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board
LaneATT
Adaptive design and responsive design
mysql源码分析——索引的数据结构
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
Traverse, delete, judge whether JSON data is empty, and recursion
Some understanding of the rank sum of matrix and the rank of image
[leetcode] day103 search two-dimensional matrix II
20. Shell programming variables
聊聊C指针
If this.$router Push the same address with different parameters, and the page does not refresh