mainwindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "mainwindow.h"
  2. #include "./ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent)
  4. : QMainWindow(parent)
  5. , ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. connect(ui->InPut, SIGNAL(triggered()), this, SLOT(onInputTriggered()));
  9. connect(ui->OutPut, SIGNAL(triggered()), this, SLOT(onOutputTriggered()));
  10. initPLC();
  11. onInputTriggered();
  12. }
  13. MainWindow::~MainWindow()
  14. {
  15. onOutputTriggered();
  16. delete ui;
  17. }
  18. bool MainWindow::init(const std::string &filename) {
  19. if (!proto_tool::read_proto_param(filename, velodyne_parameters)) {
  20. std::cout << "失败" << std::endl;
  21. return false;
  22. }
  23. ui->Forward->setChecked(true);
  24. ui->EntranceComboBox->clear();
  25. for (int i = 0; i < velodyne_parameters.region_size(); ++i) {
  26. ui->EntranceComboBox->addItem(QString::number(velodyne_parameters.region(i).region_id()));
  27. }
  28. proto_tool::write_proto_param(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt", velodyne_parameters);
  29. showStatu();
  30. return true;
  31. }
  32. bool MainWindow::initPLC() {
  33. m_snap7_client = &Snap7Clamp::get_instance_references();
  34. m_snap7_client->communication_init();
  35. Snap7Clamp::Snap7_communication_statu status = m_snap7_client->get_status();
  36. while (status == Snap7Clamp::SNAP7_COMMUNICATION_READY || status == Snap7Clamp::SNAP7_COMMUNICATION_RECEIVE) {
  37. sleep(1);
  38. status = m_snap7_client->get_status();
  39. }
  40. return true;
  41. }
  42. void MainWindow::showStatu() {
  43. QString entrance_txt = "入口id: " + ui->EntranceComboBox->currentText();
  44. QString direction;
  45. if (ui->Forward->isChecked()) {
  46. direction = "正向";
  47. }
  48. else if (ui->Reverse->isChecked()) {
  49. direction = "反向";
  50. }
  51. QString record_txt = "当前记录id: " + QString::number(record_id) + ", 最大记录id: " + QString::number(record_id_max);
  52. ui->statusbar->showMessage(entrance_txt + ", " +
  53. direction + ", " +
  54. record_txt);
  55. }
  56. void MainWindow::saveRecord(const std::string &fileName) {
  57. /**************** 作缓存记录 ****************/
  58. proto_tool::write_proto_param(fileName, velodyne_parameters);
  59. }
  60. int MainWindow::findRegionIndex(const int &region_id) {
  61. for (int i = 0; i < velodyne_parameters.region().size(); i++) {
  62. if (velodyne_parameters.region(i).has_region_id() && velodyne_parameters.region(i).region_id() == region_id) {
  63. return i;
  64. }
  65. }
  66. return -1;
  67. }
  68. void MainWindow::on_ComputeButton_clicked()
  69. {
  70. WheelDistance wd;
  71. wd.currentText = ui->EntranceComboBox->currentText();
  72. wd.lf = ui->LeftFrontDIstance->value();
  73. wd.lr = ui->LeftRearDistance->value();
  74. wd.rf = ui->RightFrontDistance->value();
  75. wd.rr = ui->RightRearDistance->value();
  76. wd.wheel_base = ui->WheelBase->value();
  77. if (wd.lf == 0 && wd.lr == 0 && wd.rf == 0 && wd.rr == 0) {
  78. ui->textEdit->append("*?* 大哥,四个0懒得算了");
  79. return;
  80. }
  81. if (ui->Forward->isChecked()) {
  82. wd.forward = true;
  83. } else {
  84. wd.forward = false;
  85. }
  86. m_distance_map.insert(std::pair<int, WheelDistance>(record_id, wd));
  87. // auto iter = m_ground_region_map.find(ui->EntranceComboBox->currentText().toInt());
  88. int region_index = findRegionIndex(ui->EntranceComboBox->currentText().toInt());
  89. /**************** 作计算处理 ****************/
  90. double left_dis = ui->LeftFrontDIstance->value() - ui->LeftRearDistance->value();
  91. double right_dis = ui->RightFrontDistance->value() - ui->RightRearDistance->value();
  92. double angle_M = std::asin((left_dis - right_dis) * 0.5 / ui->WheelBase->value());
  93. double offset_angle = -(angle_M * 180.0 / M_PI);
  94. double front_dis = ui->LeftFrontDIstance->value() - ui->RightFrontDistance->value();
  95. // double rear_dis = ui->LeftRearDistance->value() - ui->RightRearDistance->value();
  96. double offset_x = ((front_dis) * 0.5 - (left_dis - right_dis) * 0.25) / 1000;
  97. record_id++;
  98. for (int i = record_id; i < record_id_max; i++) {
  99. m_distance_map.erase(i);
  100. }
  101. record_id_max = record_id;
  102. velodyne::PlcOffset offset;
  103. if (ui->Forward->isChecked()) {
  104. offset.CopyFrom(velodyne_parameters.region(region_index).plc_forward_offset());
  105. offset.set_plc_offsetx(offset.plc_offsetx() + offset_x);
  106. offset.set_plc_offset_degree(offset.plc_offset_degree() + offset_angle);
  107. velodyne_parameters.mutable_region(region_index)->mutable_plc_forward_offset()->CopyFrom(offset);
  108. ui->textEdit->append("*" + QString::number(record_id) + "* " + ui->EntranceComboBox->currentText() + "正向 计算结果: x轴需要调整" + QString::number(offset_x, 'f', 4) + ", 角度需要调整" + QString::number(offset_angle, 'f', 4) + "度。");
  109. }
  110. else if (ui->Reverse->isChecked()) {
  111. offset.CopyFrom(velodyne_parameters.region(region_index).plc_reverse_offset());
  112. offset.set_plc_offsetx(offset.plc_offsetx() + offset_x);
  113. offset.set_plc_offset_degree(offset.plc_offset_degree() + offset_angle);
  114. velodyne_parameters.mutable_region(region_index)->mutable_plc_reverse_offset()->CopyFrom(offset);
  115. ui->textEdit->append("*" + QString::number(record_id) + "* " + ui->EntranceComboBox->currentText() + "反向 计算结果: x轴需要调整" + QString::number(offset_x, 'f', 4) + ", 角度需要调整" + QString::number(offset_angle, 'f', 4) + "度。");
  116. }
  117. ui->plc_offsetx->setText(QString::number(offset.plc_offsetx(), 'f', 4));
  118. ui->plc_offsety->setText(QString::number(offset.plc_offsety(), 'f', 4));
  119. ui->plc_offset_degree->setText(QString::number(offset.plc_offset_degree(), 'f', 4));
  120. ui->plc_offset_wheel_base->setText(QString::number(offset.plc_offset_wheel_base(), 'f', 4));
  121. /**************** 计算参数清零,防止多次误计算 ****************/
  122. ui->LeftFrontDIstance->setValue(0);
  123. ui->LeftRearDistance->setValue(0);
  124. ui->RightFrontDistance->setValue(0);
  125. ui->RightRearDistance->setValue(0);
  126. /**************** 作缓存记录 ****************/
  127. saveRecord(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt");
  128. showStatu();
  129. }
  130. void MainWindow::on_EntranceComboBox_currentTextChanged(const QString &arg1)
  131. {
  132. int region_index = findRegionIndex(ui->EntranceComboBox->currentText().toInt());
  133. if (region_index == -1) {
  134. ui->textEdit->append("find region id error");
  135. return;
  136. } else {
  137. // ui->textEdit->append("on_EntranceComboBox_currentTextChanged "+ arg1);
  138. }
  139. auto iter_dis = m_distance_map.find(record_id);
  140. if (iter_dis != m_distance_map.end()) {
  141. ui->LeftFrontDIstance->setValue(iter_dis->second.lf);
  142. ui->LeftRearDistance->setValue(iter_dis->second.lr);
  143. ui->RightFrontDistance->setValue(iter_dis->second.rf);
  144. ui->RightRearDistance->setValue(iter_dis->second.rr);
  145. ui->WheelBase->setValue(iter_dis->second.wheel_base);
  146. } else {
  147. ui->LeftFrontDIstance->setValue(0);
  148. ui->LeftRearDistance->setValue(0);
  149. ui->RightFrontDistance->setValue(0);
  150. ui->RightRearDistance->setValue(0);
  151. }
  152. if (ui->Forward->isChecked()) {
  153. ui->LF_Label->setText("Y3(mm)");
  154. ui->RF_Label->setText("Y4(mm)");
  155. ui->LR_Label->setText("Y5(mm)");
  156. ui->RR_Label->setText("Y6(mm)");
  157. ui->plc_offsetx->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offsetx(), 'f', 4));
  158. ui->plc_offsety->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offsety(), 'f', 4));
  159. ui->plc_offset_degree->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offset_degree(), 'f', 4));
  160. ui->plc_offset_wheel_base->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offset_wheel_base(), 'f', 4));
  161. }
  162. else if (ui->Reverse->isChecked()) {
  163. ui->LF_Label->setText("Y6(mm)");
  164. ui->RF_Label->setText("Y5(mm)");
  165. ui->LR_Label->setText("Y4(mm)");
  166. ui->RR_Label->setText("Y3(mm)");
  167. ui->plc_offsetx->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offsetx(), 'f', 4));
  168. ui->plc_offsety->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offsety(), 'f', 4));
  169. ui->plc_offset_degree->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offset_degree(), 'f', 4));
  170. ui->plc_offset_wheel_base->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offset_wheel_base(), 'f', 4));
  171. }
  172. showStatu();
  173. }
  174. void MainWindow::on_Forward_clicked()
  175. {
  176. on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
  177. }
  178. void MainWindow::on_Reverse_clicked()
  179. {
  180. on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
  181. }
  182. void MainWindow::on_RedoButton_clicked()
  183. {
  184. record_id++;
  185. if (record_id <= record_id_max && record_id >= 0) {
  186. if (!proto_tool::read_proto_param(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt", velodyne_parameters)) {
  187. std::cout << "失败" << std::endl;
  188. }
  189. auto iter = m_distance_map.find(record_id);
  190. if (iter != m_distance_map.end()) {
  191. if (iter->second.forward) {
  192. ui->Forward->setChecked(true);
  193. ui->Reverse->setChecked(false);
  194. } else {
  195. ui->Forward->setChecked(false);
  196. ui->Reverse->setChecked(true);
  197. }
  198. ui->EntranceComboBox->setCurrentText(iter->second.currentText);
  199. }
  200. on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
  201. ui->textEdit->append("往后进行一次重做");
  202. } else {
  203. ui->textEdit->append("*?* 大哥,没动西重做了!!!!别点了!!!");
  204. record_id = record_id_max;
  205. }
  206. }
  207. void MainWindow::on_QuashButton_clicked()
  208. {
  209. record_id--;
  210. if (record_id <= record_id_max && record_id >= 0) {
  211. if (!proto_tool::read_proto_param(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt", velodyne_parameters)) {
  212. std::cout << "失败" << std::endl;
  213. }
  214. auto iter = m_distance_map.find(record_id);
  215. if (iter != m_distance_map.end()) {
  216. if (iter->second.forward) {
  217. ui->Forward->setChecked(1);
  218. } else {
  219. ui->Reverse->setChecked(1);
  220. }
  221. ui->EntranceComboBox->setCurrentText(iter->second.currentText);
  222. }
  223. on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
  224. ui->textEdit->append("撤销到上一次计算");
  225. } else {
  226. ui->textEdit->append("*?* 大哥,没动西撤销了!!!!别点了!!!");
  227. record_id = 0;
  228. }
  229. }
  230. void MainWindow::onInputTriggered() {
  231. QString fileName = QFileDialog::getOpenFileName(
  232. this,
  233. tr("Open File"),
  234. "~/",
  235. tr("protobuf文件 (*.prototxt *.txt)"));
  236. if (fileName.isEmpty()) {
  237. return;
  238. }
  239. init(fileName.toStdString());
  240. }
  241. void MainWindow::onOutputTriggered() {
  242. QString fileName = QFileDialog::getSaveFileName(
  243. this,
  244. tr("Save File"),
  245. "~/",
  246. tr("protobuf文件 (*.prototxt *.txt)"));
  247. if (fileName.isEmpty()) {
  248. return;
  249. }
  250. saveRecord(fileName.toStdString());
  251. }
  252. void MainWindow::on_AutoGetDistance_clicked()
  253. {
  254. if (m_snap7_client == nullptr) {
  255. return;
  256. }
  257. std::unique_lock<std::mutex> t_lock1(m_snap7_client->m_data_lock);
  258. Snap7Clamp::PLCData plc_value = m_snap7_client->plcData;
  259. std::cout << plc_value.wheels[0].gap << std::endl;
  260. std::cout << plc_value.wheels[1].gap << std::endl;
  261. std::cout << plc_value.wheels[2].gap << std::endl;
  262. std::cout << plc_value.wheels[3].gap << std::endl;
  263. }