mainwindow.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. extern CarControl car_control_module;
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. QTimer * timerTask = new QTimer();
  10. connect(timerTask,SIGNAL(timeout()),this,SLOT(onTimerTask()));
  11. timerTask->setTimerType(Qt::PreciseTimer);
  12. timerTask->start(20);
  13. QTimer * timer = new QTimer();
  14. connect(timer,SIGNAL(timeout()),this,SLOT(onTimer()));
  15. timer->setTimerType(Qt::PreciseTimer);
  16. timer->start(50);
  17. }
  18. MainWindow::~MainWindow()
  19. {
  20. car_control_module.set_lat_lgt_ctrl_req(false);
  21. car_control_module.set_target_gear(GearPrkgAssistReq::kTargetGearP);
  22. delete ui;
  23. }
  24. void MainWindow::on_cBox_Handshake_clicked(bool checked)
  25. {
  26. bctrlAct = checked;
  27. ui->cBoxGearD->setEnabled(checked);
  28. ui->cBoxGearR->setEnabled(checked);
  29. ui->cBoxGearP->setEnabled(checked);
  30. ui->cBoxLampR->setEnabled(checked);
  31. ui->cBoxLampL->setEnabled(checked);
  32. ui->cBoxLampH->setEnabled(checked);
  33. ui->sBoxEpsSet->setEnabled(checked);
  34. ui->sBoxSpeedSet->setEnabled(checked);
  35. ui->vSliderEpsSet->setEnabled(checked);
  36. ui->vSliderSpeedSet->setEnabled(checked);
  37. }
  38. void MainWindow::on_cBoxGearD_clicked(bool checked)
  39. {
  40. if(checked)
  41. {
  42. gearSetVal = GearPrkgAssistReq::kTargetGearD;
  43. ui->cBoxGearP->setChecked(false);
  44. ui->cBoxGearR->setChecked(false);
  45. }
  46. else
  47. {
  48. gearSetVal = GearPrkgAssistReq::kTargetGearP;
  49. ui->cBoxGearD->setChecked(false);
  50. ui->cBoxGearR->setChecked(false);
  51. ui->cBoxGearP->setChecked(true);
  52. }
  53. }
  54. void MainWindow::on_cBoxGearR_clicked(bool checked)
  55. {
  56. if(checked)
  57. {
  58. gearSetVal = GearPrkgAssistReq::kTargetGearR;
  59. ui->cBoxGearP->setChecked(false);
  60. ui->cBoxGearD->setChecked(false);
  61. }
  62. else
  63. {
  64. gearSetVal =GearPrkgAssistReq::kTargetGearP;
  65. ui->cBoxGearD->setChecked(false);
  66. ui->cBoxGearR->setChecked(false);
  67. ui->cBoxGearP->setChecked(true);
  68. }
  69. }
  70. void MainWindow::on_cBoxGearP_clicked(bool checked)
  71. {
  72. gearSetVal =GearPrkgAssistReq::kTargetGearP;
  73. ui->cBoxGearP->setChecked(true);
  74. if(checked)
  75. {
  76. ui->cBoxGearD->setChecked(false);
  77. ui->cBoxGearR->setChecked(false);
  78. }
  79. }
  80. void MainWindow::on_cBoxLampL_clicked(bool checked)
  81. {
  82. if(checked)
  83. {
  84. turnLightSetVal = TurnLightIndicReq::kLeft;
  85. ui->cBoxLampR->setChecked(false);
  86. ui->cBoxLampH->setChecked(false);
  87. }
  88. else
  89. {
  90. turnLightSetVal = TurnLightIndicReq::kOff;
  91. ui->cBoxLampL->setChecked(false);
  92. ui->cBoxLampR->setChecked(false);
  93. ui->cBoxLampH->setChecked(true);
  94. }
  95. }
  96. void MainWindow::on_cBoxLampR_clicked(bool checked)
  97. {
  98. if(checked)
  99. {
  100. turnLightSetVal = TurnLightIndicReq::kRight;
  101. ui->cBoxLampL->setChecked(false);
  102. ui->cBoxLampH->setChecked(false);
  103. }
  104. else
  105. {
  106. turnLightSetVal = TurnLightIndicReq::kOff;
  107. ui->cBoxLampL->setChecked(false);
  108. ui->cBoxLampR->setChecked(false);
  109. ui->cBoxLampH->setChecked(true);
  110. }
  111. }
  112. void MainWindow::on_cBoxLampH_clicked(bool checked)
  113. {
  114. turnLightSetVal = TurnLightIndicReq::kOff;
  115. ui->cBoxLampH->setChecked(true);
  116. if(checked)
  117. {
  118. ui->cBoxLampL->setChecked(false);
  119. ui->cBoxLampR->setChecked(false);
  120. }
  121. }
  122. void MainWindow::on_sBoxSpeedSet_valueChanged(double arg1)
  123. {
  124. speedSetVal = arg1;
  125. ui->vSliderSpeedSet->setValue(int(arg1));
  126. }
  127. void MainWindow::on_vSliderSpeedSet_valueChanged(int value)
  128. {
  129. speedSetVal = double(value);
  130. ui->sBoxSpeedSet->setValue(double(value));
  131. }
  132. void MainWindow::on_sBoxEpsSet_valueChanged(double arg1)
  133. {
  134. EpsSetVal = arg1;
  135. ui->vSliderEpsSet->setValue(int(arg1));
  136. }
  137. void MainWindow::on_vSliderEpsSet_valueChanged(int value)
  138. {
  139. EpsSetVal = double(value);
  140. ui->sBoxEpsSet->setValue(double(value));
  141. }
  142. void MainWindow::onTimerTask()
  143. {
  144. if(!car_control_module.is_lat_lgt_ctrl_active() && bctrlAct){
  145. car_control_module.set_lat_lgt_ctrl_req(true);
  146. }
  147. else if(car_control_module.is_lat_lgt_ctrl_active() && !bctrlAct){
  148. car_control_module.set_lat_lgt_ctrl_req(false);
  149. car_control_module.set_target_gear( GearPrkgAssistReq::kNoRequest);
  150. car_control_module.set_target_pinion_ag_in_deg(0);
  151. car_control_module.set_target_acc_mps2(0);
  152. car_control_module.set_turn_light_status(TurnLightIndicReq::kOff);
  153. }
  154. else if(car_control_module.is_lat_lgt_ctrl_active() && bctrlAct){
  155. car_control_module.set_target_gear( gearSetVal);
  156. car_control_module.set_target_pinion_ag_in_deg(EpsSetVal);
  157. double speed = car_control_module.get_current_vehicle_spd_in_ms();
  158. if(speed < 10)
  159. car_control_module.set_target_acc_mps2(speedSetVal);
  160. else
  161. car_control_module.set_target_acc_mps2(0);
  162. car_control_module.set_turn_light_status(turnLightSetVal);
  163. }
  164. car_control_module.sm_task_20ms();
  165. }
  166. void MainWindow::onTimer()
  167. {
  168. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  169. // std::cout<<"time: "<<std::chrono::system_clock::now().time_since_epoch().count()/1000000<<std::endl;
  170. // return;
  171. bool status;
  172. GearPrkgAssistReq gearSetVal;
  173. GearLevelIndicate gearRealVal;
  174. ChassisErrCode chassErr;
  175. StsMach stsMach;
  176. float steerDeg, speed;
  177. status = car_control_module.is_lat_lgt_ctrl_active();
  178. if (status)
  179. ui->lineEHandSake->setText("true");
  180. else
  181. ui->lineEHandSake->setText("false");
  182. gearSetVal = car_control_module.get_setted_tar_gear();
  183. switch (gearSetVal) {
  184. case GearPrkgAssistReq::kNoRequest :
  185. ui->lineESetGear->setText("N");
  186. break;
  187. case GearPrkgAssistReq::kTargetGearD :
  188. ui->lineESetGear->setText("D");
  189. break;
  190. case GearPrkgAssistReq::kTargetGearP :
  191. ui->lineESetGear->setText("P");
  192. break;
  193. case GearPrkgAssistReq::kTargetGearR :
  194. ui->lineESetGear->setText("R");
  195. break;
  196. default:
  197. ui->lineESetGear->setText("-");
  198. break;
  199. }
  200. gearRealVal = car_control_module.get_cur_disp_gear();
  201. switch (gearRealVal) {
  202. case GearLevelIndicate::kManMode :
  203. ui->lineECurGear->setText("Man");
  204. break;
  205. case GearLevelIndicate::kDrive :
  206. ui->lineECurGear->setText("Auto");
  207. break;
  208. case GearLevelIndicate::kPark :
  209. ui->lineECurGear->setText("Park");
  210. break;
  211. case GearLevelIndicate::kNeutro :
  212. ui->lineECurGear->setText("Neutro");
  213. break;
  214. default:
  215. ui->lineECurGear->setText("Reserved");
  216. break;
  217. }
  218. int tmp1[10] = {0,1,2,3,4,5,6,7,8,9};
  219. chassErr = car_control_module.get_chassis_err_state();
  220. ui->lineEChaissErr->setText(QString::number(tmp1[static_cast<int>(chassErr)]));
  221. stsMach = car_control_module.get_chassis_statemachine_sts();
  222. ui->lineEMachErr->setText(QString::number(tmp1[static_cast<int>(stsMach)]));
  223. QString strLog;
  224. strLog.sprintf("%s %d %s %d","chassisErr ", chassErr, "state machine ",stsMach);
  225. ui->textBrLog->append(strLog);
  226. speed = car_control_module.get_current_vehicle_spd_in_ms();
  227. ui->lineESpeed->setText(QString::number(speed));
  228. steerDeg = car_control_module.get_current_steer_ang_in_deg();
  229. ui->lineEEPS->setText(QString::number(steerDeg));
  230. }
  231. void MainWindow::keyPressEvent(QKeyEvent* event){
  232. qDebug()<<"keypress!";
  233. //(event->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier)) &&
  234. if((event->key() == Qt::Key_W))
  235. {
  236. qDebug()<<"acc up";
  237. speedSetVal+=0.1;
  238. ui->sBoxSpeedSet->setValue(speedSetVal);
  239. }
  240. else if((event->key() == Qt::Key_S))
  241. {
  242. qDebug()<<"acc Down";
  243. speedSetVal-=0.1;
  244. ui->sBoxSpeedSet->setValue(speedSetVal);
  245. }
  246. //(event->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier)) &&
  247. else if( (event->key() == Qt::Key_A))
  248. {
  249. qDebug()<<"turn left"<<EpsSetVal;
  250. ui->sBoxEpsSet->setValue(EpsSetVal);
  251. EpsSetVal+=5;
  252. }
  253. else if((event->key() == Qt::Key_D))
  254. {
  255. qDebug()<<"turn right"<<EpsSetVal;
  256. ui->sBoxEpsSet->setValue(EpsSetVal);
  257. EpsSetVal-=5;
  258. }
  259. };