File size: 7,249 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2013-07-13

  (C) Copyright 2013-2018 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstMultiTask/mtsIntervalStatisticsQtWidget.h>

#include <QHeaderView>
#include <QScrollBar>

mtsIntervalStatisticsQtWidget::mtsIntervalStatisticsQtWidget(void):
    QTableWidget()
{
    this->setRowCount(3);
    this->setColumnCount(3);
    this->verticalHeader()->hide();
    this->horizontalHeader()->hide();
    this->verticalScrollBar()->hide();
    this->horizontalScrollBar()->hide();
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    this->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    int colIndex = 0;

    QTWIPeriodAverage = new QTableWidgetItem();
    QTWIPeriodAverage->setToolTip("Average period");
    QTWIPeriodAverage->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWIPeriodAverage->setFlags(QTWIPeriodAverage->flags() ^ Qt::ItemIsEditable);
    this->setItem(0, colIndex, QTWIPeriodAverage);
    QTWIPeriodHz = new QTableWidgetItem();
    QTWIPeriodHz->setToolTip("Average frequency");
    QTWIPeriodHz->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWIPeriodHz->setFlags(QTWIPeriodHz->flags() ^ Qt::ItemIsEditable);
    this->setItem(1, colIndex, QTWIPeriodHz);
    QTWIPeriodRange = new QTableWidgetItem();
    QTWIPeriodRange->setToolTip("Min/max period");
    QTWIPeriodRange->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWIPeriodRange->setFlags(QTWIPeriodRange->flags() ^ Qt::ItemIsEditable);
    this->setItem(2, colIndex, QTWIPeriodRange);

    colIndex++;
    QTWILoadAverage = new QTableWidgetItem();
    QTWILoadAverage->setToolTip("Average computation time");
    QTWILoadAverage->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWILoadAverage->setFlags(QTWILoadAverage->flags() ^ Qt::ItemIsEditable);
    this->setItem(0, colIndex, QTWILoadAverage);
    QTWILoadPercent = new QTableWidgetItem();
    QTWILoadPercent->setToolTip("Compute time vs period");
    QTWILoadPercent->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWILoadPercent->setFlags(QTWILoadPercent->flags() ^ Qt::ItemIsEditable);
    this->setItem(1, colIndex, QTWILoadPercent);
    QTWILoadRange = new QTableWidgetItem();
    QTWILoadRange->setToolTip("Min/max compute time");
    QTWILoadRange->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWILoadRange->setFlags(QTWILoadRange->flags() ^ Qt::ItemIsEditable);
    this->setItem(2, colIndex, QTWILoadRange);

    colIndex++;
    QTWIInterval = new QTableWidgetItem();
    QTWIInterval->setToolTip("Interval duration");
    QTWIInterval->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWIInterval->setFlags(QTWIInterval->flags() ^ Qt::ItemIsEditable);
    this->setItem(0, colIndex, QTWIInterval);
    QTWINumberOfSamples = new QTableWidgetItem();
    QTWINumberOfSamples->setToolTip("Number of period samples");
    QTWINumberOfSamples->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWINumberOfSamples->setFlags(QTWINumberOfSamples->flags() ^ Qt::ItemIsEditable);
    this->setItem(1, colIndex, QTWINumberOfSamples);
    QTWINumberOfOverruns = new QTableWidgetItem();
    QTWINumberOfOverruns->setToolTip("Number of times computation times execeeded average period");
    QTWINumberOfOverruns->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    QTWINumberOfOverruns->setFlags(QTWINumberOfOverruns->flags() ^ Qt::ItemIsEditable);
    this->setItem(2, colIndex, QTWINumberOfOverruns);

    // set value to initialize content size
    this->SetValue(mtsIntervalStatistics());

     // compute height and width, using ResizeToContents breaks auto resize, so we set a fixed size
    int height = 0;
    for (int rowIndex = 0; rowIndex < this->model()->rowCount(); rowIndex++) {
        height += this->rowHeight(rowIndex);
    }
    this->setFixedHeight(height);
    int width = 0;
    for (int colIndex = 0; colIndex < this->model()->columnCount(); colIndex++) {
        width += this->columnWidth(colIndex);
    }
    this->setFixedWidth(width);
}

void mtsIntervalStatisticsQtWidget::SetValue(const mtsIntervalStatistics & newValue)
{
    double periodAvg = newValue.PeriodAvg();
    if (periodAvg == 0.0) {
        periodAvg = 1.0;
    }
    QTWIPeriodAverage->setText(QString("%1±%2ms")
                               .arg(periodAvg * 1000.0, -6, 'f', 3)
                               .arg(newValue.PeriodStdDev() * 1000.0, -6, 'f', 3));
    QTWIPeriodHz->setText(QString("%1 KHz").arg(0.001 / periodAvg, -6, 'f', 3));
    QTWIPeriodRange->setText(QString("%1/%2ms")
                             .arg(newValue.PeriodMin() * 1000.0, -6, 'f', 3)
                             .arg(newValue.PeriodMax() * 1000.0, -6, 'f', 3));

    const double loadAvg = newValue.ComputeTimeAvg();
    const double periodPercent = 100.0 / periodAvg;
    QTWILoadAverage->setText(QString("%1±%2ms")
                             .arg(loadAvg * 1000.0, -6, 'f', 3)
                             .arg(newValue.ComputeTimeStdDev() * 1000.0, -6, 'f', 3));
    QTWILoadPercent->setText(QString("%1\%").arg(loadAvg * periodPercent , -5, 'f', 1));
    QTWILoadRange->setText(QString("%1/%2\%")
                           .arg(newValue.ComputeTimeMin() * periodPercent, -5, 'f', 1)
                           .arg(newValue.ComputeTimeMax() * periodPercent, -5, 'f', 1));

    QTWIInterval->setText(QString("%1s").arg(newValue.StatisticsInterval(), -6, 'f', 3));
    QTWINumberOfSamples->setText(QString("%1 samples").arg(newValue.NumberOfSamples()));
    QTWINumberOfOverruns->setText(QString("%1 > period").arg(newValue.NumberOfOverruns()));
}



CMN_IMPLEMENT_SERVICES_DERIVED_ONEARG(mtsIntervalStatisticsQtWidgetComponent, mtsComponent, std::string);

mtsIntervalStatisticsQtWidgetComponent::mtsIntervalStatisticsQtWidgetComponent(const std::string & componentName, double periodInSeconds):
    mtsComponent(componentName),
    TimerPeriodInMilliseconds(periodInSeconds * 1000)
{
    // Setup CISST Interface
    mtsInterfaceRequired * interfaceRequired = AddInterfaceRequired("Component");
    if (interfaceRequired) {
        interfaceRequired->AddFunction("period_statistics", period_statistics);
    }
}

void mtsIntervalStatisticsQtWidgetComponent::Startup(void)
{
    startTimer(TimerPeriodInMilliseconds); // ms
}

void mtsIntervalStatisticsQtWidgetComponent::timerEvent(QTimerEvent * CMN_UNUSED(event))
{
    // make sure we should update the display
    if (this->isHidden()) {
        return;
    }

    period_statistics(IntervalStatistics);
    mtsIntervalStatisticsQtWidget::SetValue(IntervalStatistics);
}