Browse Source

Added basic controls

Dmitry Yu Okunev 5 years ago
parent
commit
707d4cd78e
3 changed files with 154 additions and 13 deletions
  1. 110 5
      mainwindow.cpp
  2. 25 1
      mainwindow.h
  3. 19 7
      mainwindow.ui

+ 110 - 5
mainwindow.cpp

@@ -52,6 +52,7 @@ MainWindow::MainWindow(QWidget *parent) :
         int adcId = 0;
         while (adcId < ADCS) {
             QwtPlot *p = this->getPlotByAdcId(adcId);
+            p->installEventFilter(this);
 
             int i = 0;
             //this->ui->ADC1Plot->setAutoReplot(true);
@@ -76,6 +77,9 @@ MainWindow::MainWindow(QWidget *parent) :
     this->showCurState();
     this->redrawGraphics();
 
+    this->on_stopM0Radio_toggled(true);
+    this->on_stopM1Radio_toggled(true);
+
     this->openRemote();
 }
 
@@ -137,7 +141,7 @@ void MainWindow::showCurState()
     this->ui->ADC1Value->setText("ADC1: "+QString::number(this->curState.adc[0].value));
     this->ui->ADC2Value->setText("ADC2: "+QString::number(this->curState.adc[1].value));
 
-    this->ui->statusBar->showMessage(QString("Connected: " + QString::number(this->curState.m[0].direction) +" "+ QString::number(this->curState.m[0].speed) +" "+ QString::number(this->curState.m[1].direction) +" "+ QString::number(this->curState.m[1].speed) +" "+ QString::number(this->curState.adc[0].value) +" "+ QString::number(this->curState.adc[1].value)));
+    this->ui->statusBar->showMessage(QString("Connected: " + QString(this->curState.m[0].direction) +" "+ QString::number(this->curState.m[0].speed) +" "+ QString(this->curState.m[1].direction) +" "+ QString::number(this->curState.m[1].speed) +" "+ QString::number(this->curState.adc[0].value) +" "+ QString::number(this->curState.adc[1].value)));
 
     return;
 }
@@ -215,7 +219,7 @@ qint64 MainWindow::drawOldCurve(int adcId)
         this->getPlotByAdcId(adcId)->replot();
     } else i = 0;
 
-    qDebug("drawOldCurve(%i): pointId == %lu; i == %i", adcId, (u_int64_t)this->ADCCurPoint[adcId].pointId, i);
+    //qDebug("drawOldCurve(%i): pointId == %lu; i == %i", adcId, (u_int64_t)this->ADCCurPoint[adcId].pointId, i);
 
     return i;
 }
@@ -223,7 +227,7 @@ qint64 MainWindow::drawOldCurve(int adcId)
 void MainWindow::drawNewCurve(int adcId, qint64 startPointId)
 {
     QPolygonF points;
-    qDebug("drawNewCurve(%i, %lu)", adcId, (u_int64_t)startPointId);
+    //qDebug("drawNewCurve(%i, %lu)", adcId, (u_int64_t)startPointId);
 
     if (this->ADCCurPoint[adcId].pointId < startPointId)
         this->fillPoints(points, this->ADCData[adcId], startPointId, ADCDATALEN, 0);
@@ -254,13 +258,22 @@ void MainWindow::recvData()
     //qDebug("received data: %s", string.toStdString().c_str());
     QStringList arguments = string.split(" ");
 
-    this->curState.m[0].direction = arguments.at(0).toInt();
+    this->curState.m[0].direction = arguments.at(0).at(0).toLatin1();
     this->curState.m[0].speed     = arguments.at(1).toInt();
-    this->curState.m[1].direction = arguments.at(2).toInt();
+    this->curState.m[1].direction = arguments.at(2).at(0).toLatin1();
     this->curState.m[1].speed     = arguments.at(3).toInt();
     this->curState.adc[0].value   = arguments.at(4).toInt();
     this->curState.adc[1].value   = arguments.at(5).toInt();
 
+    int i = 0;
+    while (i < MOTORS) {
+        if (memcmp(&this->curState.m[i], &this->desiredState.m[i], sizeof(this->curState.m[i]))) {
+            //qDebug("motor %i differs: %c ? %c; %i ? %i", i, this->curState.m[i].direction, this->desiredState.m[i].direction, this->curState.m[i].speed, this->desiredState.m[i].speed);
+            this->sendDesiredState(i);
+        }
+        i++;
+    }
+
     QDateTime curDateTime = QDateTime::currentDateTime();
     qint64 ts = curDateTime.currentMSecsSinceEpoch();
 
@@ -301,6 +314,23 @@ void MainWindow::on_resize_toolsWidget(QResizeEvent *event) {
     return;
 }
 
+void MainWindow::on_resize_plot(QResizeEvent *event, int adcId, QwtPlot *p)
+{
+    QSize centerWidgetSize = this->ui->centralWidget->size();
+    /*qDebug("plot %i Resized (New Size) - Width: %d Height: %d (was: %d x %d); newY: %u", adcId,
+        event->size().width(),
+        event->size().height(),
+        event->oldSize().width(),
+        event->oldSize().height(),
+        (centerWidgetSize.height() * (adcId))/ADCS
+     );*/
+
+    this->ui->graphicsLayout->setGeometry(QRect(0, 0, centerWidgetSize.width()-105, centerWidgetSize.height()-20));
+    p->setGeometry(0, (centerWidgetSize.height() * (adcId))/ADCS, centerWidgetSize.width()-105, centerWidgetSize.height()/ADCS);
+
+    return;
+}
+
 bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
     if (event->type() == QEvent::Resize) {
         Ui::MainWindow *ui = this->ui;
@@ -309,6 +339,81 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
         else
         if (obj == ui->toolsWidget)
             this->on_resize_toolsWidget  (static_cast<QResizeEvent*>(event));
+        else {
+            int adcId = 0;
+            while (adcId < ADCS) {
+                QwtPlot *p = this->getPlotByAdcId(adcId);
+                if (obj == p) {
+                    this->on_resize_plot(static_cast<QResizeEvent*>(event), adcId, p);
+                    break;
+                }
+                adcId++;
+            }
+        }
+
     }
     return QWidget::eventFilter(obj, event);
 }
+
+void MainWindow::sendDesiredState(int motorId)
+{
+    motor_t *m = &this->desiredState.m[motorId];
+    QString sendStr = "H"+QString::number(motorId+1)+ m->direction +" "+QString::number(m->speed)+"\r\n";
+
+    this->remote->write(sendStr.toUtf8());
+    //qDebug("sending: %s", sendStr.toStdString().c_str());
+}
+
+void MainWindow::on_speedM0Slider_valueChanged(int value)
+{
+    this->desiredState.m[0].speed = value;
+    this->sendDesiredState(0);
+}
+
+void MainWindow::on_speedM1Slider_valueChanged(int value)
+{
+    this->desiredState.m[1].speed = value;
+    this->sendDesiredState(0);
+}
+
+void MainWindow::on_downM0Radio_toggled(bool checked)
+{
+    if (checked)
+        this->desiredState.m[0].direction = M_DOWN;
+    this->sendDesiredState(0);
+}
+
+void MainWindow::on_stopM0Radio_toggled(bool checked)
+{
+    if (checked)
+        this->desiredState.m[0].direction = M_STOP;
+    this->sendDesiredState(0);
+}
+
+void MainWindow::on_upM0Radio_toggled(bool checked)
+{
+    if (checked)
+        this->desiredState.m[0].direction = M_UP;
+    this->sendDesiredState(1);
+}
+
+void MainWindow::on_downM1Radio_toggled(bool checked)
+{
+    if (checked)
+        this->desiredState.m[1].direction = M_DOWN;
+    this->sendDesiredState(1);
+}
+
+void MainWindow::on_stopM1Radio_toggled(bool checked)
+{
+    if (checked)
+        this->desiredState.m[1].direction = M_STOP;
+    this->sendDesiredState(1);
+}
+
+void MainWindow::on_upM1Radio_toggled(bool checked)
+{
+    if (checked)
+        this->desiredState.m[1].direction = M_UP;
+    this->sendDesiredState(1);
+}

+ 25 - 1
mainwindow.h

@@ -15,7 +15,7 @@ class MainWindow;
 }
 
 struct motor {
-    u_int8_t direction;
+    char direction;
     u_int8_t speed;
 };
 typedef struct motor motor_t;
@@ -45,6 +45,12 @@ struct point {
 };
 typedef struct point point_t;
 
+enum m_state {
+    M_DOWN = '1',
+    M_UP   = '2',
+    M_STOP = '9',
+};
+
 class MainWindow : public QMainWindow
 {
     Q_OBJECT
@@ -60,9 +66,25 @@ private slots:
     void recvData();
     void reopenRemote();
 
+    void on_speedM0Slider_valueChanged(int value);
+    void on_speedM1Slider_valueChanged(int value);
+
+    void on_downM0Radio_toggled(bool checked);
+
+    void on_stopM0Radio_toggled(bool checked);
+
+    void on_upM0Radio_toggled(bool checked);
+
+    void on_downM1Radio_toggled(bool checked);
+
+    void on_stopM1Radio_toggled(bool checked);
+
+    void on_upM1Radio_toggled(bool checked);
+
 private:
     void on_resize_centralWidget(QResizeEvent *event);
     void on_resize_toolsWidget(QResizeEvent *event);
+    void on_resize_plot(QResizeEvent *event, int adcId, QwtPlot *p);
     void openRemote();
     void closeRemote();
     void showCurState();
@@ -72,6 +94,7 @@ private:
     void drawNewCurve(int adcId, qint64 startPointId);
     QwtPlot *getPlotByAdcId(int adcId);
     int fillPoints(QPolygonF &points, point_t *data, int startPointId, int endPointId, int skipBeforeTs);
+    void sendDesiredState(int motorId);
 
     point_t ADCData[ADCS][ADCDATALEN];
     curpoint_t ADCCurPoint[ADCS];
@@ -79,6 +102,7 @@ private:
     QwtPlotCurve ADCCurve[ADCS][2];
 
     state_t curState;
+    state_t desiredState;
     u_int64_t xMax;
 
     QSerialPort *remote;

+ 19 - 7
mainwindow.ui

@@ -43,6 +43,12 @@
        </property>
        <item>
         <widget class="QwtPlot" name="ADC1Plot">
+         <property name="sizePolicy">
+          <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+           <horstretch>0</horstretch>
+           <verstretch>0</verstretch>
+          </sizepolicy>
+         </property>
          <property name="minimumSize">
           <size>
            <width>300</width>
@@ -53,6 +59,12 @@
        </item>
        <item>
         <widget class="QwtPlot" name="ADC2Plot">
+         <property name="sizePolicy">
+          <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+           <horstretch>0</horstretch>
+           <verstretch>0</verstretch>
+          </sizepolicy>
+         </property>
          <property name="minimumSize">
           <size>
            <width>300</width>
@@ -171,7 +183,7 @@
        <x>0</x>
        <y>0</y>
        <width>258</width>
-       <height>649</height>
+       <height>1541</height>
       </rect>
      </property>
      <layout class="QVBoxLayout" name="toolsLayout">
@@ -179,7 +191,7 @@
        <enum>QLayout::SetMinimumSize</enum>
       </property>
       <item>
-       <widget class="QWidget" name="motor2Widget" native="true">
+       <widget class="QWidget" name="motor0Widget" native="true">
         <property name="minimumSize">
          <size>
           <width>250</width>
@@ -195,12 +207,12 @@
            <height>22</height>
           </rect>
          </property>
-         <layout class="QHBoxLayout" name="motor2Layout">
+         <layout class="QHBoxLayout" name="motor0Layout">
           <property name="sizeConstraint">
            <enum>QLayout::SetMinimumSize</enum>
           </property>
           <item>
-           <widget class="QSlider" name="speedM2Slider">
+           <widget class="QSlider" name="speedM0Slider">
             <property name="sizePolicy">
              <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
               <horstretch>0</horstretch>
@@ -219,7 +231,7 @@
            </widget>
           </item>
           <item>
-           <widget class="QRadioButton" name="downM2Radio">
+           <widget class="QRadioButton" name="downM0Radio">
             <property name="maximumSize">
              <size>
               <width>60</width>
@@ -232,7 +244,7 @@
            </widget>
           </item>
           <item>
-           <widget class="QRadioButton" name="stopM2Radio">
+           <widget class="QRadioButton" name="stopM0Radio">
             <property name="maximumSize">
              <size>
               <width>60</width>
@@ -248,7 +260,7 @@
            </widget>
           </item>
           <item>
-           <widget class="QRadioButton" name="upM2Radio">
+           <widget class="QRadioButton" name="upM0Radio">
             <property name="maximumSize">
              <size>
               <width>60</width>