TA的每日心情 | 慵懒 2024-5-31 23:20 |
---|
签到天数: 302 天 连续签到: 2 天 [LV.8]以坛为家I
|
本帖最后由 ky123 于 2017-10-24 10:11 编辑
(一)关于舵机控制与qt调用
对于qt如何调用wiringpi和控制舵机的基本操作可以看我上一篇帖子,这里不再赘述:- https://www.cirmall.com/bbs/forum.php?mod=viewthread&tid=97546&page=1&extra=#pid442108
复制代码 (二)字符转化
安卓和qt通讯一个核心问题就是tcp socket的字符转化问题。
安卓一般使用writeUTF函数发送utf-8格式的数据,接收到它的qt必须转化为qt专用的QString类型才方便操作:- QString utf8str = utf8codec->toUnicode(alldata.mid(2));
复制代码 utf8codec有个成员函数toUnicode解决了这一问题。
(三)校核位
至于如何预防一些错误以及识别不同舵机目标,我采用了第二位作为区分校核:- //如果符合条件,改变数值
- if (utf8str.mid(1, 1) == "-")
- {
- //将第一个字段转化为int
- QString stur = QString("%1-%2").arg(utf8str.mid(0, 1)).arg(utf8str.section("-", 1));
- lable_order[utf8str.mid(0, 1).toInt()]->setText(stur);
- qDebug() << "the order is:" << utf8str.section("-", 0);
- //qDebug()<<"0:"<<utf8str.mid(0,1);
- //qDebug()<<"1:"<<utf8str.section("-",1);
- //同步到硬件
- pca9685_setmk(iic_fd,utf8str.mid(0, 1).toInt(),utf8str.section("-", 1).toInt());
- }
复制代码 (四)核心代码
qt核心工程代码:- #include "dialog.h"
- #include "ui_dialog.h"
- #include "dialog.h"
- #include "ui_dialog.h"
- #include <QDebug>
- #include <QTextCodec>
- #include <QString>
- #include <QtGui>
- Dialog::Dialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Dialog)
- {
- ui->setupUi(this);
- //1
- iic_fd=pca9685_init(0x40);
- //2、ui界面
- order = 3;
- lable_statu = new QLabel("statu:waitting");
- lable_value = new QLabel("value:null");
- button_box = new QDialogButtonBox;
- ok_button = button_box->addButton(tr("ok"), QDialogButtonBox::ActionRole);
- cancal_button = button_box->addButton(tr("cancel"), QDialogButtonBox::ActionRole);
- for (int i = 0; i<order; i++)
- lable_order.push_back(new QLabel(QString("%1-").
- arg(QString::number(lable_order.size() + 1))
- ));
- connect((QObject *)ok_button, SIGNAL(clicked()), this, SLOT(disConnect()));
- connect((QObject *)ok_button, SIGNAL(clicked()), this, SLOT(close()));
- sv_layout = new QVBoxLayout;
- sv_layout->addWidget(lable_statu);
- sv_layout->addWidget(lable_value);
- for (int i = 0; i<order; i++)
- sv_layout->addWidget(lable_order[i]);
- sv_layout->addWidget(button_box);
- setLayout(sv_layout);
- setWindowTitle(tr("服务器"));
- //3、tcp
- m_tcpServer = new QTcpServer(this);
- if (!m_tcpServer->listen(QHostAddress::Any, 6000))
- {
- close();
- return;
- }
- connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,可以随便取。
- }
- Dialog::~Dialog()
- {
- delete ui;
- }
- void Dialog::newConnect()
- {
- m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket
- lable_statu->setText(QString("statu:success-ip:%1").arg(m_tcpSocket->peerAddress().toString()));
- connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(recmsg()));
- }
- void Dialog::recmsg()
- {
- qint64 len = m_tcpSocket->bytesAvailable();
- //qDebug()<<"socket data len:"<< len;
- QByteArray alldata = m_tcpSocket->read(len);
- //开始转换编码
- QTextCodec *utf8codec = QTextCodec::codecForName("UTF-8");
- QString utf8str = utf8codec->toUnicode(alldata.mid(2));
- //qDebug()<<"hex:["<<alldata.toHex().toUpper()<<"]";
- //qDebug()<<"utf-8 ["<< (utf8str) << "]";
- //显示到控件上
- lable_value->setText(utf8str);
- //如果符合条件,改变数值
- if (utf8str.mid(1, 1) == "-")
- {
- //将第一个字段转化为int
- QString stur = QString("%1-%2").arg(utf8str.mid(0, 1)).arg(utf8str.section("-", 1));
- lable_order[utf8str.mid(0, 1).toInt()]->setText(stur);
- qDebug() << "the order is:" << utf8str.section("-", 0);
- //qDebug()<<"0:"<<utf8str.mid(0,1);
- //qDebug()<<"1:"<<utf8str.section("-",1);
- //同步到硬件
- pca9685_setmk(iic_fd,utf8str.mid(0, 1).toInt(),utf8str.section("-", 1).toInt());
- }
- }
- void Dialog::disConnect()
- {
- // if(m_tcpSocket->isValid())
- // m_tcpSocket->disconnected();
- close();
- }
复制代码 android核心工程代码:- //重载的滑块监听器
- class BarListener implements SeekBar.OnSeekBarChangeListener {
- @Override
- //1、每一次改变时调用:同步pwm_value和对应SeekBar的progress值
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- switch (seekBar.getId()) {
- case R.id.bar_for_lr:
- text_for_lr.setText("lr当前进度值:" + progress + " / 100 ");
- pwm_value[0] = progress;
- break;
- case R.id.bar_for_fb:
- text_for_fb.setText("fb当前进度值:" + progress + " / 100 ");
- pwm_value[1] = progress;
- break;
- case R.id.bar_for_ud:
- text_for_ud.setText("ud当前进度值:" + progress + " / 100 ");
- pwm_value[2] = progress;
- break;
- }
- }
- //2、每次点下时调用:
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- //3、每次松开时调用
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- if (seekBar.getProgress() < 500)
- seekBar.setProgress(500);
- if (seekBar.getProgress() > 1500)
- seekBar.setProgress(1500);
- int seekbar_order = 0;
- switch (seekBar.getId()){
- case R.id.bar_for_lr:
- seekbar_order=0;
- break;
- case R.id.bar_for_fb:
- seekbar_order = 1;
- break;
- case R.id.bar_for_ud:
- seekbar_order=2;
- break;
- default:
- break;
- }
- if(socket.isConnected()){
- final int finalSeekbar_order = seekbar_order;
- new Thread() {
- @Override
- public void run() {
- //启动子线程创建socket并发送字符
- String command=String.valueOf(finalSeekbar_order)+"-"+String.valueOf(pwm_value[finalSeekbar_order]);
- try {
- out.writeUTF(command);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- }
- }
复制代码 (五)编译
必须要在.pro文件中加上两行:- QT += network
- LIBS += -lwiringPi -lcrypt
复制代码 (六)硬件连接
这里使用SDA1、SCL1,对应系统第二条iic总线。
(六)视频演示与工程分享
|
|