]> rtime.felk.cvut.cz Git - eurobot/public.git/blobdiff - src/robomon/RobomonTuning.cpp
Robomon sources moved directly under robomon directory
[eurobot/public.git] / src / robomon / RobomonTuning.cpp
diff --git a/src/robomon/RobomonTuning.cpp b/src/robomon/RobomonTuning.cpp
new file mode 100644 (file)
index 0000000..623e63f
--- /dev/null
@@ -0,0 +1,479 @@
+/*
+ * RobomonTuning.cpp                   07/10/31
+ *
+ * Robot`s visualization and control GUI for robot of the
+ * Eurobot 2008 (Mission to Mars).
+ *
+ * Copyright: (c) 2008 DCE Eurobot Dragon Team
+ *            CTU FEE - Department of Control Engineering
+ * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
+ * License: GNU GPL v.2
+ */
+
+#include <QtGui>
+#include <queue>
+#include <cstdlib>
+#include <sys/shm.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <math.h>
+
+#include <orte.h>
+#include <path_planner.h>
+#include <robodim.h>
+#include <sharp.h>
+#include <trgen.h>
+#include <map.h>
+#include <robomath.h>
+#include "PlaygroundScene.h"
+#include "MiscGui.h"
+#include "RobomonTuning.h"
+
+#include <QCoreApplication>
+#include <QEvent>
+#include <QKeyEvent>
+#include <QDebug>
+#include <QMessageBox>
+
+RobomonTuning::RobomonTuning(QWidget *parent)
+       : QWidget(parent)
+{
+       QFont font;
+       font.setPointSize(7);
+       setFont(font);
+
+
+       createLeftLayout();
+//     createRightLayout();
+
+       QHBoxLayout *mainLayout = new QHBoxLayout;
+       mainLayout->addLayout(leftLayout);
+//     mainLayout->addLayout(rightLayout);
+       setLayout(mainLayout);
+
+       createOrte();
+       createActions();
+
+       setFocusPolicy(Qt::StrongFocus);
+}
+
+/**********************************************************************
+ * GUI
+ **********************************************************************/
+void RobomonTuning::createLeftLayout()
+{
+       leftLayout = new QVBoxLayout();
+       
+       createMotorsGroupBox();
+       createServosGroupBox();
+       createPwrGroupBox();
+       createCanGroupBox();
+
+
+       leftLayout->addWidget(enginesGroupBox);
+       leftLayout->addWidget(servosGroupBox);
+       leftLayout->addWidget(pwrGroupBox);
+       leftLayout->addWidget(canGroupBox);
+}
+
+
+void RobomonTuning::createMotorsGroupBox()
+{
+       enginesGroupBox = new QGroupBox(tr("Motors"));
+       QGridLayout *layout = new QGridLayout();
+       
+       brushLeft = new QDial();
+       brushRight = new QDial();
+       roboBagr = new QDial();
+       carousel = new QDial();
+
+       brushLeftLEdit = new QLineEdit();
+       brushRightLEdit = new QLineEdit();
+       roboBagrLEdit = new QLineEdit();
+       carouselLEdit = new QLineEdit();
+
+       brushLeft->setMinimum(0);
+       brushLeft->setMaximum(200);
+       brushLeft->setValue(100);
+       
+       brushRight->setMinimum(0);
+       brushRight->setMaximum(200);
+       brushRight->setValue(100);
+       
+       roboBagr->setMinimum(0);
+       roboBagr->setMaximum(200);
+       roboBagr->setValue(100);
+
+       carousel->setMinimum(1);
+       carousel->setMaximum(5);
+       carousel->setValue(1);
+       carousel->setNotchesVisible(1);
+       
+       layout->addWidget(brushLeft,0,0);
+       layout->addWidget(MiscGui::createLabel("Left brush", Qt::AlignLeft), 1, 0);
+       layout->addWidget(brushLeftLEdit, 2, 0);
+       layout->addWidget(brushRight,0,1);
+       layout->addWidget(MiscGui::createLabel("Right brush", Qt::AlignLeft), 1, 1);
+       layout->addWidget(brushRightLEdit, 2, 1);
+       layout->addWidget(roboBagr,3,0);
+       layout->addWidget(MiscGui::createLabel("Robo bagr", Qt::AlignLeft), 4, 0);
+       layout->addWidget(roboBagrLEdit, 5, 0);
+       layout->addWidget(carousel,3,1);
+       layout->addWidget(MiscGui::createLabel("Carousel", Qt::AlignLeft), 4, 1);
+       layout->addWidget(carouselLEdit, 5, 1);
+
+
+       enginesGroupBox->setLayout(layout);
+}
+
+void RobomonTuning::createPwrGroupBox()
+{
+       pwrGroupBox = new QGroupBox(tr("Pwr"));
+       QGridLayout *layout = new QGridLayout();
+
+       pwrAlertCBox = new QCheckBox();
+       
+       layout->addWidget(pwrAlertCBox,0,0);
+
+
+       pwrGroupBox->setLayout(layout);
+}
+
+void RobomonTuning::createCanGroupBox()
+{
+       canGroupBox = new QGroupBox(tr("Can"));
+       QHBoxLayout *layout = new QHBoxLayout();
+
+       canData1 = new QLineEdit(tr("data1"));
+       canData2 = new QLineEdit(tr("data2"));
+       canData3 = new QLineEdit(tr("data3"));
+       canData4 = new QLineEdit(tr("data4"));
+       canData5 = new QLineEdit(tr("data5"));
+       canData6 = new QLineEdit(tr("data6"));
+       canData7 = new QLineEdit(tr("data7"));
+       canData8 = new QLineEdit(tr("data8"));
+       canMsgLen = new QLineEdit(tr("len"));
+       canId = new QLineEdit(tr("ID"));
+
+       canSendPButton = new QPushButton(tr("Send"));
+
+       layout->addWidget(canData1);
+       layout->addWidget(canData2);
+       layout->addWidget(canData3);
+       layout->addWidget(canData4);
+       layout->addWidget(canData5);
+       layout->addWidget(canData6);
+       layout->addWidget(canData7);
+       layout->addWidget(canData8);
+       layout->addWidget(canMsgLen);
+       layout->addWidget(canId);
+       layout->addWidget(canSendPButton);
+
+       canGroupBox->setLayout(layout);
+}
+
+void RobomonTuning::createServosGroupBox()
+{
+       servosGroupBox = new QGroupBox(tr("Servos"));
+       QGridLayout *layout = new QGridLayout();
+
+       servoBrushLeft = new QDial();
+       servoBrushRight = new QDial();
+       servoDoorTop = new QDial();
+       servoDoorBottom = new QDial();
+       servoDoorBack = new QDial();
+       servoReserve = new QDial();
+       
+       servoBrushLeftLEdit = new QLineEdit();
+       servoBrushRightLEdit = new QLineEdit();
+       servoDoorTopLEdit = new QLineEdit();
+       servoDoorBackLEdit = new QLineEdit();
+       servoDoorBottomLEdit = new QLineEdit();
+
+       servoBrushLeft->setMinimum(0);
+       servoBrushLeft->setMaximum(255);
+       servoBrushLeft->setValue(127);
+       
+       servoBrushRight->setMinimum(0);
+       servoBrushRight->setMaximum(255);
+       servoBrushRight->setValue(127);
+
+       servoDoorTop->setMinimum(0);
+       servoDoorTop->setMaximum(255);
+       servoDoorTop->setValue(127);
+
+       servoDoorBottom->setMinimum(0);
+       servoDoorBottom->setMaximum(255);
+       servoDoorBottom->setValue(127);
+
+       servoDoorBack->setMinimum(0);
+       servoDoorBack->setMaximum(255);
+       servoDoorBack->setValue(127);
+
+       servoReserve->setMinimum(0);
+       servoReserve->setMaximum(255);
+       servoReserve->setValue(127);
+
+       layout->addWidget(servoBrushLeft, 0, 0);
+       layout->addWidget(MiscGui::createLabel("Left brush", Qt::AlignLeft), 1, 0);
+       layout->addWidget(servoBrushLeftLEdit, 2, 0);
+       layout->addWidget(servoBrushRight, 0, 1);
+       layout->addWidget(MiscGui::createLabel("Right brush", Qt::AlignLeft), 1, 1);
+       layout->addWidget(servoBrushRightLEdit, 2, 1);
+       layout->addWidget(servoDoorTop, 0, 2);
+       layout->addWidget(MiscGui::createLabel("Top door", Qt::AlignLeft), 1, 2);
+       layout->addWidget(servoDoorTopLEdit, 2, 2);
+       layout->addWidget(servoDoorBottom, 3, 0);
+       layout->addWidget(MiscGui::createLabel("Bottom Door", Qt::AlignLeft), 4, 0);
+       layout->addWidget(servoDoorBottomLEdit, 5, 0);
+       layout->addWidget(servoDoorBack, 3, 1);
+       layout->addWidget(MiscGui::createLabel("Back door", Qt::AlignLeft), 4, 1);
+       layout->addWidget(servoDoorBackLEdit, 5, 1);
+       layout->addWidget(servoReserve, 3, 2);
+       layout->addWidget(MiscGui::createLabel("Reserve", Qt::AlignLeft), 4, 2);
+       servosGroupBox->setLayout(layout);
+}
+
+
+
+/**********************************************************************
+ * GUI actions
+ **********************************************************************/
+void RobomonTuning::createActions()
+{
+       /* motors */
+       connect(brushLeft, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setMotors(int)));
+       connect(brushRight, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setMotors(int)));
+       connect(roboBagr, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setMotors(int)));
+       connect(carousel, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setMotors(int)));
+
+
+       /* servos */
+       connect(servoBrushLeft, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setServos(int)));
+       connect(servoBrushRight, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setServos(int)));
+       connect(servoDoorTop, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setServos(int)));
+       connect(servoDoorBack, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setServos(int)));
+       connect(servoDoorBottom, SIGNAL(valueChanged(int)), 
+                       this, SLOT(setServos(int)));
+
+       connect(pwrAlertCBox, SIGNAL(stateChanged(int)), 
+                       this, SLOT(setPwrAlert(int)));
+
+       connect(canSendPButton, SIGNAL(clicked()), 
+                       this, SLOT(setCanMsg()));
+}
+
+void RobomonTuning::setServos(int value)
+{
+       
+       orte.servos.door_top = (u_int8_t)servoDoorTop->value();
+       servoDoorTopLEdit->setText(QString("%1").arg(orte.servos.door_top));
+       orte.servos.door_back = (u_int8_t)servoDoorBack->value();
+       servoDoorBackLEdit->setText(QString("%1").arg(orte.servos.door_back));
+       orte.servos.door_bottom = (u_int8_t)servoDoorBottom->value();
+       servoDoorBottomLEdit->setText(QString("%1").arg(orte.servos.door_bottom));
+       orte.servos.brush_left = (u_int8_t)servoBrushLeft->value();
+       servoBrushLeftLEdit->setText(QString("%1").arg(orte.servos.brush_left));
+       orte.servos.brush_right = (u_int8_t)servoBrushRight->value();
+       servoBrushRightLEdit->setText(QString("%1").arg(orte.servos.brush_right));
+
+       printf("setting servos: %d, %d, %d, %d, %d\n",orte.servos.door_top,orte.servos.door_back,
+                               orte.servos.door_bottom,orte.servos.brush_left,orte.servos.brush_right);
+
+       ORTEPublicationSend(orte.publication_servos);
+}
+
+void RobomonTuning::setPwrAlert(int value)
+{
+       if(value) {
+               orte.pwr_alert.value = 2;
+       }
+       else {
+               orte.pwr_alert.value = 0;
+       }
+
+
+}
+
+void RobomonTuning::setCanMsg()
+{
+       orte.can_msg.data1 = canData1->text().toInt();
+       orte.can_msg.data2 = canData2->text().toInt();
+       orte.can_msg.data3 = canData3->text().toInt();
+       orte.can_msg.data4 = canData4->text().toInt();
+       orte.can_msg.data5 = canData5->text().toInt();
+       orte.can_msg.data6 = canData6->text().toInt();
+       orte.can_msg.data7 = canData7->text().toInt();
+       orte.can_msg.data8 = canData8->text().toInt();
+
+       orte.can_msg.len = canMsgLen->text().toInt();
+       orte.can_msg.id = canId->text().toInt();
+
+       ORTEPublicationSend(orte.publication_can_msg);
+
+}
+void RobomonTuning::setMotors(int state)
+{
+       orte.drives.brush_left = brushLeft->value();
+       orte.drives.brush_right = brushRight->value();
+       orte.drives.bagr = roboBagr->value();
+       orte.drives.carousel_pos = carousel->value();
+       ORTEPublicationSend(orte.publication_drives);
+
+       brushLeftLEdit->setText(QString("%1").arg(orte.drives.brush_left));
+       brushRightLEdit->setText(QString("%1").arg(orte.drives.brush_right));
+       roboBagrLEdit->setText(QString("%1").arg(orte.drives.bagr));
+       carouselLEdit->setText(QString("%1").arg(orte.drives.carousel_pos));
+}
+
+
+/**********************************************************************
+ * EVENTS
+ **********************************************************************/
+bool RobomonTuning::event(QEvent *event)
+{
+       switch (event->type()) {
+               case QEVENT(QEV_MOTION_STATUS):
+                       break;
+               case QEVENT(QEV_ACTUAL_POSITION):
+                       break;
+               case QEVENT(QEV_ESTIMATED_POSITION):
+                       break;
+               case QEVENT(QEV_SHARP_LONGS):
+                       break;
+               case QEVENT(QEV_SHARP_SHORTS):
+                       break;
+               case QEVENT(QEV_DI):
+                       break;
+               case QEVENT(QEV_ACCELEROMETER):
+                       break;
+               case QEVENT(QEV_ACCUMULATOR):
+                       break;
+               case QEVENT(QEV_POWER_VOLTAGE):
+                       break;
+               default:
+                       if (event->type() == QEvent::Close)
+                               closeEvent((QCloseEvent *)event);
+                       else if (event->type() == QEvent::KeyPress)
+                               keyPressEvent((QKeyEvent *)event);
+                       else if (event->type() == QEvent::KeyRelease)
+                               keyReleaseEvent((QKeyEvent *)event);
+                       else if (event->type() == QEvent::FocusIn)
+                               grabKeyboard();
+                       else if (event->type() == QEvent::FocusOut)
+                               releaseKeyboard();
+                       else {
+                               event->ignore();
+                               return false;
+                       }
+                       break;
+       }
+       event->accept();
+       return true;
+}
+
+void RobomonTuning::keyPressEvent(QKeyEvent *event)
+{
+       double peak, gain;
+
+       if (event->isAutoRepeat()) {
+               switch (event->key()) {
+                       case Qt::Key_Down:
+                               break;
+
+                       case Qt::Key_Up:
+                       case Qt::Key_Left:
+                       case Qt::Key_Right:
+                               break;
+
+                       default:
+                               event->ignore();
+                               break;
+               }
+               return;
+       }
+
+       switch (event->key()) {
+               case Qt::Key_Up:
+                       break;
+               case Qt::Key_Down:
+                       break;
+               case Qt::Key_Left:
+                       break;
+               case Qt::Key_Right:
+                       break;
+               default:
+                       event->ignore();
+                       break;
+       }
+       event->accept();
+}
+
+void RobomonTuning::keyReleaseEvent(QKeyEvent *event)
+{
+       if (event->isAutoRepeat()) {
+               event->ignore();
+               return;
+       }
+
+       switch (event->key()) {
+               case Qt::Key_Up:
+               case Qt::Key_Down:
+               case Qt::Key_Left:
+               case Qt::Key_Right:
+                       break;
+               default:
+                       event->ignore();
+                       break;
+       }
+       event->accept();
+}
+
+void RobomonTuning::closeEvent(QCloseEvent *event)
+{
+       robottype_roboorte_destroy(&orte);
+}
+
+/**********************************************************************
+ * ORTE
+ **********************************************************************/
+void RobomonTuning::createOrte()
+{
+
+       orte.strength = 12;
+
+       robottype_roboorte_init(&orte);
+
+       orte.pwr_alert.value = 0;
+
+       /* publishers */
+       robottype_publisher_can_msg_create(&orte, NULL, NULL);
+       robottype_publisher_pwr_ctrl_create(&orte, NULL, NULL);
+       robottype_publisher_pwr_alert_create(&orte, dummy_publisher_callback, NULL);
+
+       robottype_publisher_servos_create(&orte, NULL, NULL);
+       robottype_publisher_drives_create(&orte, NULL, NULL);
+
+       /*Power supply hack*/
+       orte.pwr_ctrl.voltage33 = true;
+       orte.pwr_ctrl.voltage50 = true;
+       orte.pwr_ctrl.voltage80 = true;
+}
+
+/**********************************************************************
+ * MISCELLANEOUS
+ **********************************************************************/
+
+