mainwindow.cpp 12 KB

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