当前位置:网站首页>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?
边栏推荐
- There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly
- 矩阵的秩和图像的秩的一些了解
- Configuring WAPI certificate security policy for Huawei wireless devices
- leetcode:162. 寻找峰值【二分寻找峰值】
- 如何在 PHP 中防止 XSS
- AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
- How to choose the appropriate data type for fields in MySQL?
- Simplified understanding: publish and subscribe
- OpenMP入门
- Best practices for preventing XSS in PHP web applications
猜你喜欢

Simplified understanding: publish and subscribe

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

Yolo5face: why reinvent the face detector

多线程(基础)

Dynamics crm: sharing records for users and teams
![Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password](/img/ae/125fcb16c9d85714c7bbd1255d1d18.png)
Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password

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

Using JS to implement click events

Dedecms editor supports automatic pasting of word pictures
随机推荐
Fast RCNN trains its own data set
Application modification log path log4j.properties
Stack and queue - 1047. Delete all adjacent duplicates in the string
Knowledge points of MySQL (12)
124 maximum path sum in binary tree
Servlet框架(servlet+jsp)+Mysql实现的增删改查+分页(功能包学生信息录入、学生信息增删改查、分页等)
'resultmap'must match' (constructor?, id*, result*, association*, collect problem solving
Multithreading (basic)
EC200U-CN模块的使用
Use of ec200u-cn module
公钥私钥传输,以及对CA证书的理解
2022/7/18 CF training
Simplified understanding: publish and subscribe
[adaptiveavgpool3d] pytorch tutorial
Kubernetes GPU's Dilemma and failure
2.19 haas506 2.0 development tutorial - Bluetooth - Bluetooth communication (only supports versions above 2.2)
Introduction to bermudagrass
22 bracket generation
[loj3247] [USACO 2020.1 platinum "non declining subsequences (DP, divide and conquer)
安信证券开户在手机开户安全吗?