当前位置:网站首页>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?
边栏推荐
- 20. Shell programming variables
- 在 PHP Web 应用程序中防止 XSS 的最佳实践
- 降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行
- Memcache cache application (lnmp+memcache)
- Application modification log path log4j.properties
- Scala functions and their higher-order applications
- 287 finding duplicates
- Urban safety series popular science - enter the high incidence period of drowning, if you know the common sense of life-saving
- Host PSQL connecting virtual machine Oracle
- 105 constructing binary trees from preorder and inorder traversal sequences
猜你喜欢

Leetcode 223. 矩形面积

2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)

2.19 haas506 2.0 development tutorial - Bluetooth - Bluetooth communication (only supports versions above 2.2)

Research on the efficiency of numpy array access

如何在 PHP 中防止 XSS

Urban safety series popular science - enter the high incidence period of drowning, if you know the common sense of life-saving

Simplified understanding: publish and subscribe

After taking aiyouteng's medicine, Naifei's condition improved

如何防止跨站点脚本 (XSS) 攻击完整指南

Fine tune layoutlm V3 for bill data processing and content recognition
随机推荐
Simplified understanding: publish and subscribe
Yolov6 trains its own data set
MySQL write lock does not take effect
My first blog
Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password
降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行
Use of ec200u-cn module
Using JS to implement click events
自适应设计和响应式设计
Host PSQL connecting virtual machine Oracle
电话系统规则
[tf.keras]: a problem encountered in upgrading the version from 1.x to 2.x: invalidargumenterror: cannot assign a device for operation embedding_
Getting started with OpenMP
Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images
How to generate complex flow chart of XMIND
C# TCP客户端窗体应用程序异步接收方式
【LeetCode】Day102-螺旋矩阵 II
Configuring WAPI certificate security policy for Huawei wireless devices
2.19 haas506 2.0 development tutorial - Bluetooth - Bluetooth communication (only supports versions above 2.2)
leetcode:162. 寻找峰值【二分寻找峰值】