mainwindow.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QMessageBox>
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent)
  6. , ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. mpLabelTip = new QLabel(this);
  10. mpLabelTip->setText(tr("回车确认输入"));
  11. mpLabelTip->setMidLineWidth(50);
  12. mpLabelInfo = new QLabel(this);
  13. mpLabelInfo->setText(tr(""));
  14. mpLabelInfo->setMidLineWidth(150);
  15. ui->statusbar->addWidget(mpLabelTip);
  16. ui->statusbar->addWidget(mpLabelInfo);
  17. ui->checkBox_left->setChecked(mbleft);
  18. ui->checkBox_right->setChecked(mbright);
  19. ui->lineEdit_wheel->setText(QString::number(mfwheel,'g'));
  20. ui->lineEdit_drive->setText(QString::number(mftorque,'g'));
  21. ui->lineEdit_brake->setText(QString::number(mfbrake,'g'));
  22. QString styleLabel = "QLabel {"
  23. " background-color: rgb(72, 116, 203);" // 背景颜色
  24. " color: white;" // 字体颜色
  25. "}";
  26. QString styleEdit = "QLineEdit {"
  27. " background-color: rgb(90, 141, 52);" // 背景颜色
  28. " color: white;" // 字体颜色
  29. "}";
  30. QString styleCheck = "QCheckBox {"
  31. " background-color: rgb(90, 141, 52);" // 背景颜色
  32. " color: white;" // 字体颜色
  33. "}";
  34. QString styleMainWindow = "QMainWindow {"
  35. " background-color: rgb(223, 226, 241);" // 背景颜色
  36. "}";
  37. ui->label_1->setStyleSheet(styleLabel);
  38. ui->label_2->setStyleSheet(styleLabel);
  39. ui->label_3->setStyleSheet(styleLabel);
  40. ui->label_4->setStyleSheet(styleLabel);
  41. ui->label_1->setAlignment(Qt::AlignCenter);
  42. ui->label_2->setAlignment(Qt::AlignCenter);
  43. ui->label_3->setAlignment(Qt::AlignCenter);
  44. ui->label_4->setAlignment(Qt::AlignCenter);
  45. ui->checkBox_left->setStyleSheet(styleCheck);
  46. ui->checkBox_right->setStyleSheet(styleCheck);
  47. ui->lineEdit_wheel->setStyleSheet(styleEdit);
  48. ui->lineEdit_drive->setStyleSheet(styleEdit);
  49. ui->lineEdit_brake->setStyleSheet(styleEdit);
  50. setStyleSheet(styleMainWindow);
  51. updatevalue();
  52. mpadecision = iv::modulecomm::RegisterSend("deciton",1000,1);
  53. mbthreadrun = true;
  54. mpthreadsend = new std::thread(&MainWindow::threadsend,this);
  55. mpTimer =new QTimer(this);
  56. connect(mpTimer,SIGNAL(timeout()),this,SLOT(onTimer()));
  57. mpTimer->start(10);
  58. setWindowTitle(tr("线控底盘检测项目"));
  59. }
  60. MainWindow::~MainWindow()
  61. {
  62. mbthreadrun = false;
  63. mpthreadsend->join();
  64. iv::modulecomm::Unregister(mpadecision);
  65. delete ui;
  66. }
  67. void MainWindow::on_checkBox_left_stateChanged(int arg1)
  68. {
  69. (void)arg1;
  70. }
  71. void MainWindow::on_checkBox_right_stateChanged(int arg1)
  72. {
  73. (void)arg1;
  74. }
  75. void MainWindow::on_checkBox_left_clicked()
  76. {
  77. if(ui->checkBox_left->isChecked())
  78. {
  79. if(ui->checkBox_right->isChecked())ui->checkBox_right->setChecked(false);
  80. }
  81. updatevalue();
  82. }
  83. void MainWindow::on_checkBox_right_clicked()
  84. {
  85. if(ui->checkBox_right->isChecked()){
  86. if(ui->checkBox_left->isChecked())ui->checkBox_left->setChecked(false);
  87. }
  88. updatevalue();
  89. }
  90. void MainWindow::keyPressEvent(QKeyEvent *event){
  91. (void)event;
  92. if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_E) {
  93. updatevalue();
  94. }
  95. }
  96. void MainWindow::on_lineEdit_wheel_returnPressed()
  97. {
  98. updatevalue();
  99. }
  100. void MainWindow::on_lineEdit_drive_returnPressed()
  101. {
  102. updatevalue();
  103. }
  104. void MainWindow::on_lineEdit_brake_returnPressed()
  105. {
  106. updatevalue();
  107. }
  108. void MainWindow::updatevalue()
  109. {
  110. const double MAXWHEEL = 430.0;
  111. const double MINWHEEL = -430.0;
  112. const double MAXTORQUE = 260;
  113. const double MAXBRAKE = -3.0;
  114. if(ui->checkBox_left->isChecked())mbleft = true;
  115. else mbleft = false;
  116. if(ui->checkBox_right->isChecked())mbright = true;
  117. else mbright = false;
  118. double fwheel = ui->lineEdit_wheel->text().toDouble();
  119. if(fwheel>MAXWHEEL){
  120. char strtip[256];
  121. snprintf(strtip,256,"Wheel exceed maximum wheel. Wheel Range is %6.1f --- %6.1f",MINWHEEL,MAXWHEEL);
  122. QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll);
  123. return;
  124. }
  125. if(fwheel<MINWHEEL){
  126. char strtip[256];
  127. snprintf(strtip,256,"Wheel exceed maximum wheel. Wheel Range is %6.1f --- %6.1f",MINWHEEL,MAXWHEEL);
  128. QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll);
  129. return;
  130. }
  131. mfwheel = fwheel;
  132. double ftorque = ui->lineEdit_drive->text().toDouble();
  133. if(ftorque > MAXTORQUE){
  134. char strtip[256];
  135. snprintf(strtip,256,"Torque exceed maximum torque. Torque Range is %6.1f --- %6.1f",0.0,MAXTORQUE);
  136. QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll);
  137. return;
  138. }
  139. if(ftorque<-0.0000001)
  140. {
  141. char strtip[256];
  142. snprintf(strtip,256,"Torque must bigger than 0. Torque Range is %6.1f --- %6.1f",0.0,MAXTORQUE);
  143. QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll);
  144. return;
  145. }
  146. mftorque = ftorque;
  147. double fbrake = ui->lineEdit_brake->text().toDouble();
  148. if(fbrake < MAXBRAKE)
  149. {
  150. char strtip[256];
  151. snprintf(strtip,256,"Brake exceed maximum brake. Brake Range is %6.1f --- %6.1f",MAXBRAKE,0.0);
  152. QMessageBox::warning(this,tr("Warning"),strtip,QMessageBox::YesAll);
  153. return;
  154. }
  155. mfbrake = fbrake;
  156. char strout[1000];
  157. snprintf(strout,1000,"Left:%d Right:%d Wheel:%6.1f Torque:%6.1f Brake:%6.1f",
  158. mbleft,mbright,mfwheel,mftorque,mfbrake);
  159. mpLabelInfo->setText(strout);
  160. }
  161. void MainWindow::onTimer()
  162. {
  163. static int ntorquebig = 0;
  164. if(mftorque>30)ntorquebig++;
  165. else ntorquebig = 0;
  166. if(ntorquebig>1000)
  167. {
  168. ui->lineEdit_drive->setText("0");
  169. updatevalue();
  170. }
  171. }
  172. void MainWindow::threadsend()
  173. {
  174. while(mbthreadrun)
  175. {
  176. iv::brain::decition xdecition;
  177. if(mftorque>0)
  178. xdecition.set_accelerator(0.1);
  179. else
  180. xdecition.set_accelerator(0.0);
  181. xdecition.set_brake(mfbrake);
  182. xdecition.set_leftlamp(mbleft);
  183. xdecition.set_rightlamp(mbright);
  184. xdecition.set_speed(3);
  185. xdecition.set_wheelangle(mfwheel);
  186. xdecition.set_wheelspeed(300);
  187. xdecition.set_torque(mftorque);
  188. xdecition.set_mode(1);
  189. xdecition.set_gear(1);
  190. xdecition.set_handbrake(0);
  191. xdecition.set_grade(1);
  192. xdecition.set_engine(0);
  193. xdecition.set_brakelamp(false);
  194. xdecition.set_ttc(15);
  195. xdecition.set_roof_light(0);
  196. xdecition.set_home_light(0);
  197. xdecition.set_door(0);
  198. xdecition.set_dippedlight(0);
  199. int nbytesize = (int)xdecition.ByteSize();
  200. std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[nbytesize]);
  201. if(!xdecition.SerializeToArray(pstr_ptr.get(),nbytesize))
  202. {
  203. std::cout<<" ctrlcmd2decition::ListenCtrlCmd Serialize Fail. "<<std::endl;
  204. return;
  205. }
  206. iv::modulecomm::ModuleSendMsg(mpadecision,pstr_ptr.get(),nbytesize);
  207. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  208. }
  209. }