#include "mainwindow.h" #include "ui_mainwindow.h" #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); mpLabelTip = new QLabel(this); mpLabelTip->setText(tr("回车确认输入")); mpLabelTip->setMidLineWidth(50); mpLabelInfo = new QLabel(this); mpLabelInfo->setText(tr("")); mpLabelInfo->setMidLineWidth(150); ui->statusbar->addWidget(mpLabelTip); ui->statusbar->addWidget(mpLabelInfo); ui->checkBox_left->setChecked(mbleft); ui->checkBox_right->setChecked(mbright); ui->lineEdit_wheel->setText(QString::number(mfwheel,'g')); ui->lineEdit_drive->setText(QString::number(mftorque,'g')); ui->lineEdit_brake->setText(QString::number(mfbrake,'g')); QString styleLabel = "QLabel {" " background-color: rgb(72, 116, 203);" // 背景颜色 " color: white;" // 字体颜色 "}"; QString styleEdit = "QLineEdit {" " background-color: rgb(90, 141, 52);" // 背景颜色 " color: white;" // 字体颜色 "}"; QString styleCheck = "QCheckBox {" " background-color: rgb(90, 141, 52);" // 背景颜色 " color: white;" // 字体颜色 "}"; QString styleMainWindow = "QMainWindow {" " background-color: rgb(223, 226, 241);" // 背景颜色 "}"; ui->label_1->setStyleSheet(styleLabel); ui->label_2->setStyleSheet(styleLabel); ui->label_3->setStyleSheet(styleLabel); ui->label_4->setStyleSheet(styleLabel); ui->label_1->setAlignment(Qt::AlignCenter); ui->label_2->setAlignment(Qt::AlignCenter); ui->label_3->setAlignment(Qt::AlignCenter); ui->label_4->setAlignment(Qt::AlignCenter); ui->checkBox_left->setStyleSheet(styleCheck); ui->checkBox_right->setStyleSheet(styleCheck); ui->lineEdit_wheel->setStyleSheet(styleEdit); ui->lineEdit_drive->setStyleSheet(styleEdit); ui->lineEdit_brake->setStyleSheet(styleEdit); setStyleSheet(styleMainWindow); updatevalue(); mpadecision = iv::modulecomm::RegisterSend("deciton",1000,1); mbthreadrun = true; mpthreadsend = new std::thread(&MainWindow::threadsend,this); mpTimer =new QTimer(this); connect(mpTimer,SIGNAL(timeout()),this,SLOT(onTimer())); mpTimer->start(10); setWindowTitle(tr("线控底盘检测项目")); } MainWindow::~MainWindow() { mbthreadrun = false; mpthreadsend->join(); iv::modulecomm::Unregister(mpadecision); delete ui; } void MainWindow::on_checkBox_left_stateChanged(int arg1) { (void)arg1; } void MainWindow::on_checkBox_right_stateChanged(int arg1) { (void)arg1; } void MainWindow::on_checkBox_left_clicked() { if(ui->checkBox_left->isChecked()) { if(ui->checkBox_right->isChecked())ui->checkBox_right->setChecked(false); } updatevalue(); } void MainWindow::on_checkBox_right_clicked() { if(ui->checkBox_right->isChecked()){ if(ui->checkBox_left->isChecked())ui->checkBox_left->setChecked(false); } updatevalue(); } void MainWindow::keyPressEvent(QKeyEvent *event){ (void)event; if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_E) { updatevalue(); } } void MainWindow::on_lineEdit_wheel_returnPressed() { updatevalue(); } void MainWindow::on_lineEdit_drive_returnPressed() { updatevalue(); } void MainWindow::on_lineEdit_brake_returnPressed() { updatevalue(); } void MainWindow::updatevalue() { const double MAXWHEEL = 430.0; const double MINWHEEL = -430.0; const double MAXTORQUE = 260; const double MAXBRAKE = -3.0; if(ui->checkBox_left->isChecked())mbleft = true; else mbleft = false; if(ui->checkBox_right->isChecked())mbright = true; else mbright = false; double fwheel = ui->lineEdit_wheel->text().toDouble(); if(fwheel>MAXWHEEL){ char strtip[256]; snprintf(strtip,256,"Wheel exceed maximum wheel. Wheel Range is %6.1f --- %6.1f",MINWHEEL,MAXWHEEL); QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll); return; } if(fwheellineEdit_drive->text().toDouble(); if(ftorque > MAXTORQUE){ char strtip[256]; snprintf(strtip,256,"Torque exceed maximum torque. Torque Range is %6.1f --- %6.1f",0.0,MAXTORQUE); QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll); return; } if(ftorque<-0.0000001) { char strtip[256]; snprintf(strtip,256,"Torque must bigger than 0. Torque Range is %6.1f --- %6.1f",0.0,MAXTORQUE); QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll); return; } mftorque = ftorque; double fbrake = ui->lineEdit_brake->text().toDouble(); if(fbrake < MAXBRAKE) { char strtip[256]; snprintf(strtip,256,"Brake exceed maximum brake. Brake Range is %6.1f --- %6.1f",MAXBRAKE,0.0); QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll); return; } mfbrake = fbrake; char strout[1000]; snprintf(strout,1000,"Left:%d Right:%d Wheel:%6.1f Torque:%6.1f Brake:%6.1f", mbleft,mbright,mfwheel,mftorque,mfbrake); mpLabelInfo->setText(strout); } void MainWindow::onTimer() { static int ntorquebig = 0; if(mftorque>30)ntorquebig++; else ntorquebig = 0; if(ntorquebig>1000) { ui->lineEdit_drive->setText("0"); updatevalue(); } } void MainWindow::threadsend() { while(mbthreadrun) { iv::brain::decition xdecition; if(mftorque>0) xdecition.set_accelerator(0.1); else xdecition.set_accelerator(0.0); xdecition.set_brake(mfbrake); xdecition.set_leftlamp(mbleft); xdecition.set_rightlamp(mbright); xdecition.set_speed(3); xdecition.set_wheelangle(mfwheel); xdecition.set_wheelspeed(300); xdecition.set_torque(mftorque); xdecition.set_mode(1); xdecition.set_gear(1); xdecition.set_handbrake(0); xdecition.set_grade(1); xdecition.set_engine(0); xdecition.set_brakelamp(false); xdecition.set_ttc(15); xdecition.set_roof_light(0); xdecition.set_home_light(0); xdecition.set_door(0); xdecition.set_dippedlight(0); int nbytesize = (int)xdecition.ByteSize(); std::shared_ptr pstr_ptr = std::shared_ptr(new char[nbytesize]); if(!xdecition.SerializeToArray(pstr_ptr.get(),nbytesize)) { std::cout<<" ctrlcmd2decition::ListenCtrlCmd Serialize Fail. "<