123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- #include "mainwindow.h"
- #include "./ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- connect(ui->InPut, SIGNAL(triggered()), this, SLOT(onInputTriggered()));
- connect(ui->OutPut, SIGNAL(triggered()), this, SLOT(onOutputTriggered()));
- initPLC();
- onInputTriggered();
- }
- MainWindow::~MainWindow()
- {
- onOutputTriggered();
- delete ui;
- }
- bool MainWindow::init(const std::string &filename) {
- if (!proto_tool::read_proto_param(filename, velodyne_parameters)) {
- std::cout << "失败" << std::endl;
- return false;
- }
- record_id = 0;
- record_id_max = 0;
- ui->Forward->setChecked(true);
- ui->EntranceComboBox->clear();
- for (int i = 0; i < velodyne_parameters.region_size(); ++i) {
- ui->EntranceComboBox->addItem(QString::number(velodyne_parameters.region(i).region_id()));
- }
- proto_tool::write_proto_param(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt", velodyne_parameters);
- showStatu();
- return true;
- }
- bool MainWindow::initPLC() {
- m_snap7_client = &Snap7Clamp::get_instance_references();
- m_snap7_client->communication_init();
- Snap7Clamp::Snap7_communication_statu status = m_snap7_client->get_status();
- while (status == Snap7Clamp::SNAP7_COMMUNICATION_READY || status == Snap7Clamp::SNAP7_COMMUNICATION_RECEIVE) {
- sleep(1);
- status = m_snap7_client->get_status();
- }
- return true;
- }
- void MainWindow::showStatu() {
- QString entrance_txt = "入口id: " + ui->EntranceComboBox->currentText();
- QString direction;
- if (ui->Forward->isChecked()) {
- direction = "正向";
- }
- else if (ui->Reverse->isChecked()) {
- direction = "反向";
- }
- QString record_txt = "当前记录id: " + QString::number(record_id) + ", 最大记录id: " + QString::number(record_id_max);
- ui->statusbar->showMessage(entrance_txt + ", " +
- direction + ", " +
- record_txt);
- }
- void MainWindow::saveRecord(const std::string &fileName) {
- /**************** 作缓存记录 ****************/
- proto_tool::write_proto_param(fileName, velodyne_parameters);
- }
- int MainWindow::findRegionIndex(const int ®ion_id) {
- for (int i = 0; i < velodyne_parameters.region().size(); i++) {
- if (velodyne_parameters.region(i).has_region_id() && velodyne_parameters.region(i).region_id() == region_id) {
- return i;
- }
- }
- return -1;
- }
- void MainWindow::on_ComputeButton_clicked()
- {
- WheelDistance wd;
- wd.currentText = ui->EntranceComboBox->currentText();
- wd.lf = ui->LeftFrontDIstance->value();
- wd.lr = ui->LeftRearDistance->value();
- wd.rf = ui->RightFrontDistance->value();
- wd.rr = ui->RightRearDistance->value();
- wd.wheel_base = ui->WheelBase->value();
- if (wd.lf == 0 && wd.lr == 0 && wd.rf == 0 && wd.rr == 0) {
- ui->textEdit->append("*?* 大哥,四个0懒得算了");
- return;
- }
- if (ui->Forward->isChecked()) {
- wd.forward = true;
- } else {
- wd.forward = false;
- }
-
- auto iter = m_distance_map.find(record_id);
- if (iter != m_distance_map.end()) {
- iter->second = wd;
- } else {
- m_distance_map.insert(std::pair<int, WheelDistance>(record_id, wd));
- }
-
- int region_index = findRegionIndex(ui->EntranceComboBox->currentText().toInt());
- /**************** 作计算处理 ****************/
- double left_dis = ui->LeftFrontDIstance->value() - ui->LeftRearDistance->value();
- double right_dis = ui->RightFrontDistance->value() - ui->RightRearDistance->value();
- double angle_M = std::asin((left_dis - right_dis) * 0.5 / ui->WheelBase->value());
- double offset_angle = -(angle_M * 180.0 / M_PI);
- double front_dis = ui->LeftFrontDIstance->value() - ui->RightFrontDistance->value();
- // double rear_dis = ui->LeftRearDistance->value() - ui->RightRearDistance->value();
- double offset_x = ((front_dis) * 0.5 - (left_dis - right_dis) * 0.25) / 1000;
- record_id++;
- for (int i = record_id; i < record_id_max; i++) {
- m_distance_map.erase(i);
- }
- record_id_max = record_id;
- velodyne::PlcOffset offset;
- if (ui->Forward->isChecked()) {
- offset.CopyFrom(velodyne_parameters.region(region_index).plc_forward_offset());
- offset.set_plc_offsetx(offset.plc_offsetx() + offset_x);
- offset.set_plc_offset_degree(offset.plc_offset_degree() + offset_angle);
- velodyne_parameters.mutable_region(region_index)->mutable_plc_forward_offset()->CopyFrom(offset);
- ui->textEdit->append("*" + QString::number(record_id) + "* " + ui->EntranceComboBox->currentText() + "正向 计算结果: x轴需要调整" + QString::number(offset_x, 'f', 4) + ", 角度需要调整" + QString::number(offset_angle, 'f', 4) + "度。");
- }
- else if (ui->Reverse->isChecked()) {
- offset.CopyFrom(velodyne_parameters.region(region_index).plc_reverse_offset());
- offset.set_plc_offsetx(offset.plc_offsetx() + offset_x);
- offset.set_plc_offset_degree(offset.plc_offset_degree() + offset_angle);
- velodyne_parameters.mutable_region(region_index)->mutable_plc_reverse_offset()->CopyFrom(offset);
- ui->textEdit->append("*" + QString::number(record_id) + "* " + ui->EntranceComboBox->currentText() + "反向 计算结果: x轴需要调整" + QString::number(offset_x, 'f', 4) + ", 角度需要调整" + QString::number(offset_angle, 'f', 4) + "度。");
- }
- ui->plc_offsetx->setText(QString::number(offset.plc_offsetx(), 'f', 4));
- ui->plc_offsety->setText(QString::number(offset.plc_offsety(), 'f', 4));
- ui->plc_offset_degree->setText(QString::number(offset.plc_offset_degree(), 'f', 4));
- ui->plc_offset_wheel_base->setText(QString::number(offset.plc_offset_wheel_base(), 'f', 4));
- /**************** 计算参数清零,防止多次误计算 ****************/
- ui->LeftFrontDIstance->setValue(0);
- ui->LeftRearDistance->setValue(0);
- ui->RightFrontDistance->setValue(0);
- ui->RightRearDistance->setValue(0);
- /**************** 作缓存记录 ****************/
- saveRecord(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt");
- showStatu();
- }
- void MainWindow::on_EntranceComboBox_currentTextChanged(const QString &arg1)
- {
- if (arg1.isEmpty()) {
- return;
- }
- int region_index = findRegionIndex(ui->EntranceComboBox->currentText().toInt());
- if (region_index == -1) {
- ui->textEdit->append("find region id error: " + arg1);
- return;
- } else {
- // ui->textEdit->append("on_EntranceComboBox_currentTextChanged "+ arg1);
- }
- auto iter_dis = m_distance_map.find(record_id);
- if (iter_dis != m_distance_map.end()) {
- ui->LeftFrontDIstance->setValue(iter_dis->second.lf);
- ui->LeftRearDistance->setValue(iter_dis->second.lr);
- ui->RightFrontDistance->setValue(iter_dis->second.rf);
- ui->RightRearDistance->setValue(iter_dis->second.rr);
- ui->WheelBase->setValue(iter_dis->second.wheel_base);
- } else {
- ui->LeftFrontDIstance->setValue(0);
- ui->LeftRearDistance->setValue(0);
- ui->RightFrontDistance->setValue(0);
- ui->RightRearDistance->setValue(0);
- }
- if (ui->Forward->isChecked()) {
- ui->LF_Label->setText("Y3(mm)");
- ui->RF_Label->setText("Y4(mm)");
- ui->LR_Label->setText("Y5(mm)");
- ui->RR_Label->setText("Y6(mm)");
- ui->plc_offsetx->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offsetx(), 'f', 4));
- ui->plc_offsety->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offsety(), 'f', 4));
- ui->plc_offset_degree->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offset_degree(), 'f', 4));
- ui->plc_offset_wheel_base->setText(QString::number(velodyne_parameters.region(region_index).plc_forward_offset().plc_offset_wheel_base(), 'f', 4));
- }
- else if (ui->Reverse->isChecked()) {
- ui->LF_Label->setText("Y6(mm)");
- ui->RF_Label->setText("Y5(mm)");
- ui->LR_Label->setText("Y4(mm)");
- ui->RR_Label->setText("Y3(mm)");
- ui->plc_offsetx->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offsetx(), 'f', 4));
- ui->plc_offsety->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offsety(), 'f', 4));
- ui->plc_offset_degree->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offset_degree(), 'f', 4));
- ui->plc_offset_wheel_base->setText(QString::number(velodyne_parameters.region(region_index).plc_reverse_offset().plc_offset_wheel_base(), 'f', 4));
- }
- showStatu();
- }
- void MainWindow::on_Forward_clicked()
- {
- on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
- }
- void MainWindow::on_Reverse_clicked()
- {
- on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
- }
- void MainWindow::on_RedoButton_clicked()
- {
- record_id++;
- if (record_id <= record_id_max && record_id >= 0) {
- if (!proto_tool::read_proto_param(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt", velodyne_parameters)) {
- std::cout << "失败" << std::endl;
- }
- auto iter = m_distance_map.find(record_id);
- if (iter != m_distance_map.end()) {
- if (iter->second.forward) {
- ui->Forward->setChecked(true);
- ui->Reverse->setChecked(false);
- } else {
- ui->Forward->setChecked(false);
- ui->Reverse->setChecked(true);
- }
- ui->EntranceComboBox->setCurrentText(iter->second.currentText);
- }
- on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
- ui->textEdit->append("往后进行一次重做");
- } else {
- ui->textEdit->append("*?* 大哥,没动西重做了!!!!别点了!!!");
- record_id = record_id_max;
- }
- }
- void MainWindow::on_QuashButton_clicked()
- {
- record_id--;
- if (record_id <= record_id_max && record_id >= 0) {
- if (!proto_tool::read_proto_param(ETC_PATH"/record/record_" + std::to_string(record_id) + ".prototxt", velodyne_parameters)) {
- std::cout << "失败" << std::endl;
- }
- auto iter = m_distance_map.find(record_id);
- if (iter != m_distance_map.end()) {
- if (iter->second.forward) {
- ui->Forward->setChecked(1);
- } else {
- ui->Reverse->setChecked(1);
- }
- ui->EntranceComboBox->setCurrentText(iter->second.currentText);
- }
- on_EntranceComboBox_currentTextChanged(ui->EntranceComboBox->currentText());
- ui->textEdit->append("撤销到上一次计算");
- } else {
- ui->textEdit->append("*?* 大哥,没动西撤销了!!!!别点了!!!");
- record_id = 0;
- }
- }
- void MainWindow::onInputTriggered() {
- QString fileName = QFileDialog::getOpenFileName(
- this,
- tr("Open File"),
- "~/",
- tr("protobuf文件 (*.prototxt *.txt)"));
- if (fileName.isEmpty()) {
- return;
- }
- init(fileName.toStdString());
- }
- void MainWindow::onOutputTriggered() {
- QString fileName = QFileDialog::getSaveFileName(
- this,
- tr("Save File"),
- "~/",
- tr("protobuf文件 (*.prototxt *.txt)"));
- if (fileName.isEmpty()) {
- return;
- }
- saveRecord(fileName.toStdString());
- }
- void MainWindow::on_AutoGetDistance_clicked()
- {
- if (m_snap7_client == nullptr) {
- return;
- }
- std::unique_lock<std::mutex> t_lock1(m_snap7_client->m_data_lock);
- Snap7Clamp::PLCData plc_value = m_snap7_client->plcData;
- if (ui->Forward->isChecked()) {
- ui->LeftFrontDIstance->setValue(plc_value.wheels[0].gap);
- ui->RightFrontDistance->setValue(plc_value.wheels[1].gap);
- ui->LeftRearDistance->setValue(plc_value.wheels[2].gap);
- ui->RightRearDistance->setValue(plc_value.wheels[3].gap);
- } else {
- ui->LeftFrontDIstance->setValue(plc_value.wheels[3].gap);
- ui->RightFrontDistance->setValue(plc_value.wheels[2].gap);
- ui->LeftRearDistance->setValue(plc_value.wheels[1].gap);
- ui->RightRearDistance->setValue(plc_value.wheels[0].gap);
- }
- }
|