File size: 26,570 Bytes
bd5cb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/* -*- 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):  Zihan Chen, Anton Deguet
  Created on: 2013-02-20

  (C) Copyright 2013-2025 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 ---
*/


// system include
#include <iostream>

// Qt include
#include <QMessageBox>
#include <QGridLayout>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QCloseEvent>
#include <QCoreApplication>
#include <QFileDialog>

// cisst
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstCommon/cmnConstants.h>
#include <cisstCommon/cmnUnits.h>

#include <sawControllers/mtsPIDQtWidget.h>

CMN_IMPLEMENT_SERVICES_DERIVED_ONEARG(mtsPIDQtWidget, mtsComponent, mtsComponentConstructorNameAndUInt)

mtsPIDQtWidget::mtsPIDQtWidget(const std::string & componentName,
                               unsigned int numberOfAxis,
                               double periodInSeconds):
    mtsComponent(componentName),
    TimerPeriodInMilliseconds(periodInSeconds * 1000), // Qt timer are in milliseconds
    m_number_of_joints(numberOfAxis)
{
    Init();
}


mtsPIDQtWidget::mtsPIDQtWidget(const mtsComponentConstructorNameAndUInt & arg):
    mtsComponent(arg.Name),
    TimerPeriodInMilliseconds(50),
    m_number_of_joints(arg.Arg)
{
    Init();
}


void mtsPIDQtWidget::Init(void)
{
    this->AddTag("UI");
    PID.m_measured_js.Position().SetSize(m_number_of_joints);
    PID.m_measured_js.Velocity().SetSize(m_number_of_joints);
    PID.m_measured_js.Effort().SetSize(m_number_of_joints);
    PID.m_setpoint_js.Position().SetSize(m_number_of_joints);
    PID.m_setpoint_js.Velocity().SetSize(0);
    PID.m_setpoint_js.Effort().SetSize(m_number_of_joints);
    PID.m_error_state.Position().SetSize(m_number_of_joints);
    PID.m_error_state.Velocity().SetSize(m_number_of_joints);
    PID.m_error_state.Effort().SetSize(m_number_of_joints);

    SetpointPosition.SetSize(m_number_of_joints);
    SetpointPosition.SetAll(0.0);
    UnitFactor.SetSize(m_number_of_joints);
    UnitFactor.SetAll(1.0);

    DirectControl = false;
    PlotIndex = 0;

    // Setup cisst interface
    mtsInterfaceRequired * interfaceRequired = AddInterfaceRequired("Controller");
    if (interfaceRequired) {
        interfaceRequired->AddFunction("reset_controller", PID.reset_controller);
        interfaceRequired->AddFunction("enable", PID.enable);
        interfaceRequired->AddFunction("enable_joints", PID.enable_joints);
        interfaceRequired->AddFunction("joints_enabled", PID.joints_enabled);
        interfaceRequired->AddFunction("use_setpoint_v", PID.use_setpoint_v);
        interfaceRequired->AddFunction("setpoint_v_used", PID.setpoint_v_used);
        interfaceRequired->AddFunction("enable_measured_setpoint_check", PID.enable_measured_setpoint_check);
        interfaceRequired->AddFunction("measured_setpoint_check_enabled", PID.measured_setpoint_check_enabled);
        interfaceRequired->AddFunction("enforce_position_limits", PID.enforce_position_limits);
        interfaceRequired->AddFunction("position_limits_enforced", PID.position_limits_enforced);
        interfaceRequired->AddFunction("configuration", PID.configuration);
        interfaceRequired->AddFunction("configuration_js", PID.configuration_js);
        interfaceRequired->AddFunction("configure", PID.configure);
        interfaceRequired->AddFunction("servo_jp", PID.servo_jp);
        interfaceRequired->AddFunction("measured_js", PID.measured_js);
        interfaceRequired->AddFunction("setpoint_js", PID.setpoint_js);
        interfaceRequired->AddFunction("error_state/measured_js", PID.error_state_measured_js);
        // Events
        interfaceRequired->AddEventHandlerWrite(&mtsPIDQtWidget::ErrorEventHandler, this, "error");
        interfaceRequired->AddEventHandlerWrite(&mtsPIDQtWidget::EnabledEventHandler, this, "enabled");
        interfaceRequired->AddEventHandlerWrite(&mtsPIDQtWidget::SetpointVUsedEventHandler, this, "setpoint_v_used");
    }
    setupUi();
    startTimer(TimerPeriodInMilliseconds); // ms
}


void mtsPIDQtWidget::GetConfiguration(void)
{
    // get configuration
    mtsExecutionResult result = PID.configuration(PID.m_configuration);
    if (!result) {
        CMN_LOG_CLASS_INIT_ERROR << "GetConfiguration: Robot interface isn't connected properly, unable to get PID configuration.  Function call returned: "
                                 << result << std::endl;
        return;
    }

    // convert to vectors
    vctDoubleVec pg, ig, dg, db, co;
    pg.SetSize(m_number_of_joints);
    ig.SetSize(m_number_of_joints);
    dg.SetSize(m_number_of_joints);
    db.SetSize(m_number_of_joints);
    co.SetSize(m_number_of_joints);
    size_t index = 0;
    for (const auto & v : PID.m_configuration) {
        pg.at(index) = v.p_gain;
        ig.at(index) = v.i_gain;
        dg.at(index) = v.d_gain;
        db.at(index) = v.p_deadband;
        co.at(index) = v.v_low_pass_cutoff;
        ++index;
    }
    // update widgets
    QVWPGain->SetValue(pg);
    QVWIGain->SetValue(ig);
    QVWDGain->SetValue(dg);
    QVWDeadband->SetValue(db);
    QVWCutoff->SetValue(co);
}


void mtsPIDQtWidget::Configure(const std::string & filename)
{
    CMN_LOG_CLASS_INIT_VERBOSE << "Configure: " << filename << std::endl;
}


void mtsPIDQtWidget::Startup(void)
{
    CMN_LOG_CLASS_INIT_VERBOSE << "mtsPIDQtWidget::Startup" << std::endl;
    // get joint state just to compute conversion factors
    GetConfiguration();
    mtsExecutionResult result = PID.configuration_js(PID.m_configuration_js);
    if (!result) {
        CMN_LOG_CLASS_INIT_ERROR << "Startup: Robot interface isn't connected properly, unable to get joint type.  Function call returned: "
                                 << result << std::endl;
        UnitFactor.SetAll(0.0);
    } else {
        // set unitFactor;
        cmnJointTypeToFactor(PID.m_configuration_js.Type(), 1.0 / cmn_mm, cmn180_PI, UnitFactor);
    }

    // get other configuration from PID
    bool flag;
    PID.setpoint_v_used(flag);
    QCBUseSetpointV->setChecked(flag);

    // Show the GUI
    if (!parent()) {
        show();
    }
}


void mtsPIDQtWidget::Cleanup(void)
{
    this->hide();
    CMN_LOG_CLASS_INIT_VERBOSE << "mtsPIDQtWidget::Cleanup" << std::endl;
}


void mtsPIDQtWidget::closeEvent(QCloseEvent * event)
{
    int answer = QMessageBox::warning(this, tr("mtsPIDQtWidget"),
                                      tr("Do you really want to quit this application?"),
                                      QMessageBox::No | QMessageBox::Yes);
    if (answer == QMessageBox::Yes) {
        event->accept();
        QCoreApplication::exit();
    } else {
        event->ignore();
    }
}


void mtsPIDQtWidget::SlotEnable(bool toggle)
{
    PID.enable(toggle);
}


void mtsPIDQtWidget::SlotEnabledJointsChanged(void)
{
    vctBoolVec enable(m_number_of_joints, false);
    QVWJointsEnabled->GetValue(enable);
    PID.enable_joints(enable);
}


void mtsPIDQtWidget::SlotEnableMeasuredSetpointCheck(bool toggle)
{
    PID.enable_measured_setpoint_check(toggle);
}


void mtsPIDQtWidget::SlotEnforcePositionLimits(bool toggle)
{
    PID.enforce_position_limits(toggle);
}


void mtsPIDQtWidget::SlotPositionChanged(void)
{
    SetpointPosition.SetAll(0.0);
    QVWSetpointPosition->GetValue(SetpointPosition);
    SetpointPositionParam.SetGoal(SetpointPosition);
    SetpointPositionParam.Goal().ElementwiseDivide(UnitFactor);
    PID.servo_jp(SetpointPositionParam);
}


void mtsPIDQtWidget::SlotConfigurationChanged(void)
{
    // make sure we have the latest configuration
    PID.configuration(PID.m_configuration);
    // get values from GUI
    vctDoubleVec pg(m_number_of_joints);
    QVWPGain->GetValue(pg);
    vctDoubleVec dg(m_number_of_joints);
    QVWDGain->GetValue(dg);
    vctDoubleVec ig(m_number_of_joints);
    QVWIGain->GetValue(ig);
    vctDoubleVec db(m_number_of_joints);
    QVWDeadband->GetValue(db);
    vctDoubleVec co(m_number_of_joints);
    QVWCutoff->GetValue(co);
    // copy to configuration
    size_t index = 0;
    for (auto & v : PID.m_configuration) {
        v.p_gain = pg.at(index);
        v.i_gain = ig.at(index);
        v.d_gain = dg.at(index);
        v.p_deadband = db.at(index);
        v.v_low_pass_cutoff = co.at(index);
        ++index;
    }
    // set
    PID.configure(PID.m_configuration);
}


void mtsPIDQtWidget::SlotMaintainPosition(void)
{
    // reset setpoint position
    vctDoubleVec goal(PID.m_measured_js.Position());
    QVWSetpointPosition->SetValue(goal);
    SlotPositionChanged();
}


void mtsPIDQtWidget::SlotSave(void)
{
    QString filename
        = QFileDialog::getSaveFileName(this,
                                       "Save PID configuration",
                                       QDir::currentPath(),
                                       "sawControllersPID JSON (sawControllersPID*.json)");
    if (!filename.isNull()) {
        // save
        std::string message;
        std::string actualFile = filename.toStdString();
        try {
            std::ofstream rawFile;
            rawFile.open(actualFile);
            if (rawFile.is_open()) {
                Json::Value jsonValue;
                cmnDataJSON<mtsPIDConfiguration>::SerializeText(PID.m_configuration, jsonValue);
                rawFile << jsonValue;
                rawFile.close();
                message = "Configuration saved to:\n" + actualFile;
            } else {
                message = "Failed to open:\n" + actualFile;
            }
        } catch (const std::runtime_error & e) {
            message = "Failed to save configuration to:\n" +  actualFile
                + "\nException:\n" + e.what();
        }
        // confirmation message
        QMessageBox * msgBox = new QMessageBox(this);
        msgBox->setAttribute(Qt::WA_DeleteOnClose);
        msgBox->setStandardButtons(QMessageBox::Ok);
        msgBox->setWindowTitle("Information");
        msgBox->setText(message.c_str());
        msgBox->setModal(true);
        msgBox->show();
    }
}


void mtsPIDQtWidget::SlotPlotIndex(int newAxis)
{
    PlotIndex = newAxis;
    QVPlot->SetContinuousExpandYResetSlot();
}


void mtsPIDQtWidget::SlotEnabledEventHandler(bool enable)
{
    QCBEnable->setChecked(enable);
}


void mtsPIDQtWidget::SlotEnableDirectControl(bool toggle)
{
    if (toggle) {
        int answer = QMessageBox::warning(this, tr("mtsPIDQtWidget"),
                                          tr("In direct control mode you can potentially harm your robot.\nAre you sure you want to continue?"),
                                          QMessageBox::No | QMessageBox::Yes);
        if (answer == QMessageBox::No) {
            toggle = false;
        }
    }
    QCBEnableDirectControl->setChecked(toggle);
    DirectControl = toggle;
    QVWJointsEnabled->setEnabled(toggle);
    QVWSetpointPosition->setEnabled(toggle);
    QVWPGain->setEnabled(toggle);
    QVWDGain->setEnabled(toggle);
    QVWIGain->setEnabled(toggle);
    QVWDeadband->setEnabled(toggle);
    QVWCutoff->setEnabled(toggle);
    QCBEnable->setEnabled(toggle);
    QCBEnableMeasuredSetpointCheck->setEnabled(toggle);
    QCBEnforcePositionLimits->setEnabled(toggle);
    QCBUseSetpointV->setEnabled(toggle);
    QPBMaintainPosition->setEnabled(toggle);
    QPBSave->setEnabled(toggle);
}


void mtsPIDQtWidget::SlotUseSetpointV(bool use)
{
    PID.use_setpoint_v(use);
}


void mtsPIDQtWidget::SlotSetpointVUsedEventHandler(bool use)
{
    QCBUseSetpointV->setChecked(use);
}


void mtsPIDQtWidget::timerEvent(QTimerEvent * CMN_UNUSED(event))
{
    // get data from the PID
    PID.joints_enabled(JointsEnabled);
    PID.measured_js(PID.m_measured_js);
    PID.m_measured_js.Position().ElementwiseMultiply(UnitFactor);
    PID.m_measured_js.Velocity().ElementwiseMultiply(UnitFactor);
    PID.setpoint_js(PID.m_setpoint_js);
    PID.m_setpoint_js.Position().ElementwiseMultiply(UnitFactor);
    bool has_setpoint_v = (PID.m_setpoint_js.Position().size() == PID.m_setpoint_js.Velocity().size());
    if (has_setpoint_v) {
        PID.m_setpoint_js.Velocity().ElementwiseMultiply(UnitFactor);
    }
    PID.error_state_measured_js(PID.m_error_state);
    bool measuredSetpointCheckEnabled;
    PID.measured_setpoint_check_enabled(measuredSetpointCheckEnabled);
    bool positionLimitsEnforced;
    PID.position_limits_enforced(positionLimitsEnforced);

    // add data to plots (without displaying it)
    signal_measured_p->AppendPoint(vctDouble2(PID.m_measured_js.Timestamp(),
        PID.m_measured_js.Position().Element(PlotIndex)));
    signal_setpoint_p->AppendPoint(vctDouble2(PID.m_setpoint_js.Timestamp(),
        PID.m_setpoint_js.Position().Element(PlotIndex)));
    signal_measured_v->AppendPoint(vctDouble2(PID.m_measured_js.Timestamp(),
        PID.m_measured_js.Velocity().Element(PlotIndex)));
    if (has_setpoint_v) {
    signal_setpoint_v->AppendPoint(vctDouble2(PID.m_setpoint_js.Timestamp(),
            PID.m_setpoint_js.Velocity().Element(PlotIndex)));
    } else {
    signal_setpoint_v->AppendPoint(vctDouble2(PID.m_setpoint_js.Timestamp(),
            0.0));
    }
    // negate effort to plot the same direction
    signal_measured_f->AppendPoint(vctDouble2(PID.m_measured_js.Timestamp(),
        -PID.m_measured_js.Effort().Element(PlotIndex)));
    signal_setpoint_f->AppendPoint(vctDouble2(PID.m_setpoint_js.Timestamp(),
        -PID.m_setpoint_js.Effort().Element(PlotIndex)));
    signal_disturbance->AppendPoint(vctDouble2(PID.m_error_state.Timestamp(),
        -PID.m_error_state.Position().Element(PlotIndex)));

    // make sure we should update the display
    if (this->isHidden()) {
        return;
    }

    // update GUI
    QVWJointsEnabled->SetValue(JointsEnabled);
    QVRMeasuredPosition->SetValue(PID.m_measured_js.Position());
    QVRMeasuredEffort->SetValue(PID.m_measured_js.Effort());
    QCBEnableMeasuredSetpointCheck->setChecked(measuredSetpointCheckEnabled);
    QCBEnforcePositionLimits->setChecked(positionLimitsEnforced);

    // display requested joint positions when we are not trying to set it using GUI
    if (!DirectControl) {
        QVWSetpointPosition->SetValue(PID.m_setpoint_js.Position());
        QVWSetpointEffort->SetValue(PID.m_setpoint_js.Effort());
    }

    // update plot graphics
    QVPlot->update();
}


void mtsPIDQtWidget::setupUi(void)
{
    QFont font;
    font.setBold(true);
    font.setPointSize(12);

    const double maximum = 30000.0;

    QGridLayout * gridLayout = new QGridLayout();
    gridLayout->setSpacing(1);

    int row = 0;

    QLabel * jointsEnabledLabel = new QLabel("Joints enabled");
    jointsEnabledLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(jointsEnabledLabel, row, 0);
    QVWJointsEnabled = new vctQtWidgetDynamicVectorBoolWrite();
    gridLayout->addWidget(QVWJointsEnabled, row, 1);
    row++;

    QLabel * measuredPosLabel = new QLabel("Measured position (deg)");
    measuredPosLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(measuredPosLabel, row, 0);
    QVRMeasuredPosition = new vctQtWidgetDynamicVectorDoubleRead();
    QVRMeasuredPosition->SetPrecision(3);
    gridLayout->addWidget(QVRMeasuredPosition, row, 1);
    row++;

    QLabel * setpointPosLabel = new QLabel("Setpoint position (deg)");
    setpointPosLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(setpointPosLabel, row, 0);
    QVWSetpointPosition = new vctQtWidgetDynamicVectorDoubleWrite(vctQtWidgetDynamicVectorDoubleWrite::SPINBOX_WIDGET);
    QVWSetpointPosition->SetStep(0.1);
    QVWSetpointPosition->SetRange(-360.0, 360.0);
    gridLayout->addWidget(QVWSetpointPosition, row, 1);
    row++;

    QLabel * measuredEffortLabel = new QLabel("Measured effort (Nm)");
    measuredEffortLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(measuredEffortLabel, row, 0);
    QVRMeasuredEffort = new vctQtWidgetDynamicVectorDoubleRead();
    QVRMeasuredEffort->SetPrecision(3);
    gridLayout->addWidget(QVRMeasuredEffort, row, 1);
    row++;

    QLabel * setpointEffortLabel = new QLabel("Setpoint effort (Nm)");
    setpointEffortLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(setpointEffortLabel, row, 0);
    QVWSetpointEffort = new vctQtWidgetDynamicVectorDoubleRead();
    QVWSetpointEffort->SetPrecision(3);
    gridLayout->addWidget(QVWSetpointEffort, row, 1);
    row++;

    QLabel * pLabel = new QLabel("PGain");
    pLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(pLabel);
    QVWPGain = new vctQtWidgetDynamicVectorDoubleWrite(vctQtWidgetDynamicVectorDoubleWrite::SPINBOX_WIDGET);
    QVWPGain->SetStep(0.01);
    QVWPGain->SetPrecision(3);
    QVWPGain->SetRange(-maximum, maximum);
    gridLayout->addWidget(QVWPGain, row, 1);
    row++;

    QLabel * dLabel = new QLabel("DGain");
    dLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(dLabel);
    QVWDGain = new vctQtWidgetDynamicVectorDoubleWrite(vctQtWidgetDynamicVectorDoubleWrite::SPINBOX_WIDGET);
    QVWDGain->SetStep(0.01);
    QVWDGain->SetPrecision(3);
    QVWDGain->SetRange(-maximum, maximum);
    gridLayout->addWidget(QVWDGain, row, 1);
    row++;

    QLabel * iLabel = new QLabel("IGain");
    iLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(iLabel);
    QVWIGain = new vctQtWidgetDynamicVectorDoubleWrite(vctQtWidgetDynamicVectorDoubleWrite::SPINBOX_WIDGET);
    QVWIGain->SetStep(0.001);
    QVWIGain->SetPrecision(5);
    QVWIGain->SetRange(-maximum, maximum);
    gridLayout->addWidget(QVWIGain, row, 1);
    row++;

    QLabel * dbLabel = new QLabel("Deadband");
    dbLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(dbLabel);
    QVWDeadband = new vctQtWidgetDynamicVectorDoubleWrite(vctQtWidgetDynamicVectorDoubleWrite::SPINBOX_WIDGET);
    QVWDeadband->SetStep(0.001);
    QVWDeadband->SetPrecision(5);
    QVWDeadband->SetRange(-maximum, maximum);
    gridLayout->addWidget(QVWDeadband, row, 1);
    row++;

    QLabel * cLabel = new QLabel("Cutoff");
    cLabel->setAlignment(Qt::AlignRight);
    gridLayout->addWidget(cLabel);
    QVWCutoff = new vctQtWidgetDynamicVectorDoubleWrite(vctQtWidgetDynamicVectorDoubleWrite::SPINBOX_WIDGET);
    QVWCutoff->SetStep(0.000001);
    QVWCutoff->SetPrecision(7);
    QVWCutoff->SetRange(-maximum, maximum);
    gridLayout->addWidget(QVWCutoff, row, 1);
    row++;

    // plot
    QHBoxLayout * plotLayout = new QHBoxLayout;
    // plot control
    QVBoxLayout * plotButtonsLayout = new QVBoxLayout;
    // - pick axis to display
    QLabel * plotIndexLabel = new QLabel("Index");
    plotButtonsLayout->addWidget(plotIndexLabel);
    QSBPlotIndex = new QSpinBox();
    QSBPlotIndex->setRange(0, (m_number_of_joints > 0) ? (m_number_of_joints - 1) : 0);
    plotButtonsLayout->addWidget(QSBPlotIndex);
    // legend
    mtsClickableQLabel * label;
    QPalette palette;
    palette.setColor(QPalette::Window, Qt::black);
    // --
    label = new mtsClickableQLabel("Measured position");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, Qt::red);
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_measured_p->SetVisible(enabled); });
    // --
    label = new mtsClickableQLabel("Setpoint position");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, Qt::green);
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_setpoint_p->SetVisible(enabled); });
    // --
    label = new mtsClickableQLabel("Measured velocity");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, QColor(static_cast<int>(0.7 * 255), 0, 0));
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_measured_v->SetVisible(enabled); });
    // --
    label = new mtsClickableQLabel("Setpoint velocity");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, QColor(0, static_cast<int>(0.7 * 255), 0));
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_setpoint_v->SetVisible(enabled); });
    // --
    label = new mtsClickableQLabel("Measured effort");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, Qt::cyan);
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_measured_f->SetVisible(enabled); });
    // --
    label = new mtsClickableQLabel("Setpoint effort");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, Qt::white);
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_setpoint_f->SetVisible(enabled); });
    // --
    label = new mtsClickableQLabel("Disturbance");
    label->setAutoFillBackground(true);
    palette.setColor(QPalette::WindowText, Qt::magenta);
    label->setPalette(palette);
    plotButtonsLayout->addWidget(label);
    connect(label, &mtsClickableQLabel::clicked, this, [this](bool enabled){ signal_disturbance->SetVisible(enabled); });
    // --
    plotButtonsLayout->addStretch();
    plotLayout->addLayout(plotButtonsLayout);

    // plotting area
    QVPlot = new vctPlot2DOpenGLQtWidget();

    vctPlot2DBase::Scale * scalePosition = QVPlot->AddScale("positions");
    signal_measured_p = scalePosition->AddSignal("measured");
    signal_measured_p->SetColor(vctDouble3(1.0, 0.0, 0.0));
    signal_setpoint_p = scalePosition->AddSignal("setpoint");
    signal_setpoint_p->SetColor(vctDouble3(0.0, 1.0, 0.0));

    vctPlot2DBase::Scale * scaleVelocity = QVPlot->AddScale("velocities");
    signal_measured_v = scaleVelocity->AddSignal("measured");
    signal_measured_v->SetColor(vctDouble3(0.7, 0.0, 0.0));
    signal_setpoint_v = scaleVelocity->AddSignal("setpoint");
    signal_setpoint_v->SetColor(vctDouble3(0.0, 0.7, 0.0));

    vctPlot2DBase::Scale * scaleEffort = QVPlot->AddScale("efforts");
    signal_measured_f = scaleEffort->AddSignal("-measured");
    signal_measured_f->SetColor(vctDouble3(0.0, 1.0, 1.0));
    signal_setpoint_f = scaleEffort->AddSignal("-setpoint");
    signal_setpoint_f->SetColor(vctDouble3(1.0, 1.0, 1.0));
    signal_disturbance = scaleEffort->AddSignal("-disturbance");
    signal_disturbance->SetColor(vctDouble3(1.0, 0.0, 1.0));
    QVPlot->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
    plotLayout->addWidget(QVPlot);

    // control
    QCBEnableDirectControl = new QCheckBox("Direct control");
    QCBEnable = new QCheckBox("Enable PID");
    QCBEnableMeasuredSetpointCheck = new QCheckBox("Measured/setpoint check");
    QCBUseSetpointV = new QCheckBox("Use setpoint_v");
    QCBEnforcePositionLimits = new QCheckBox("Position limits");
    QPBMaintainPosition = new QPushButton("Maintain position");
    QPBSave = new QPushButton("Save configuration");
    QHBoxLayout * controlLayout = new QHBoxLayout;
    controlLayout->setContentsMargins(1, 1, 1, 1);
    controlLayout->addWidget(QCBEnableDirectControl);
    controlLayout->addWidget(QCBEnable);
    controlLayout->addWidget(QCBEnableMeasuredSetpointCheck);
    controlLayout->addWidget(QCBEnforcePositionLimits);
    controlLayout->addWidget(QCBUseSetpointV);
    controlLayout->addWidget(QPBMaintainPosition);
    controlLayout->addWidget(QPBSave);
    QFrame * controlFrame = new QFrame();
    controlFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
    controlFrame->setLayout(controlLayout);

    connect(QCBEnableDirectControl, SIGNAL(toggled(bool)), this, SLOT(SlotEnableDirectControl(bool)));
    connect(QCBEnable, SIGNAL(clicked(bool)), this, SLOT(SlotEnable(bool)));
    connect(this, SIGNAL(SignalEnable(bool)), this, SLOT(SlotEnabledEventHandler(bool)));
    connect(QCBEnableMeasuredSetpointCheck, SIGNAL(clicked(bool)), this, SLOT(SlotEnableMeasuredSetpointCheck(bool)));
    connect(QCBEnforcePositionLimits, SIGNAL(clicked(bool)), this, SLOT(SlotEnforcePositionLimits(bool)));
    connect(QCBUseSetpointV, SIGNAL(clicked(bool)), this, SLOT(SlotUseSetpointV(bool)));
    connect(this, SIGNAL(SignalUseSetpointV(bool)), this, SLOT(SlotSetpointVUsedEventHandler(bool)));
    connect(QPBMaintainPosition, SIGNAL(clicked()), this, SLOT(SlotMaintainPosition()));
    connect(QPBSave, SIGNAL(clicked()), this, SLOT(SlotSave()));
    connect(QSBPlotIndex, SIGNAL(valueChanged(int)), this, SLOT(SlotPlotIndex(int)));

    // main layout
    QVBoxLayout * mainLayout = new QVBoxLayout;
    mainLayout->addLayout(gridLayout);
    mainLayout->addLayout(plotLayout);
    mainLayout->addWidget(controlFrame);

    setLayout(mainLayout);

    setWindowTitle(this->GetName().c_str());
    resize(sizeHint());

    // connect signals & slots
    connect(QVWJointsEnabled, SIGNAL(valueChanged()), this, SLOT(SlotEnabledJointsChanged()));
    connect(QVWSetpointPosition, SIGNAL(valueChanged()), this, SLOT(SlotPositionChanged()));
    connect(QVWPGain, SIGNAL(valueChanged()), this, SLOT(SlotConfigurationChanged()));
    connect(QVWDGain, SIGNAL(valueChanged()), this, SLOT(SlotConfigurationChanged()));
    connect(QVWIGain, SIGNAL(valueChanged()), this, SLOT(SlotConfigurationChanged()));
    connect(QVWDeadband, SIGNAL(valueChanged()), this, SLOT(SlotConfigurationChanged()));
    connect(QVWCutoff, SIGNAL(valueChanged()), this, SLOT(SlotConfigurationChanged()));

    // set initial values
    QCBEnableDirectControl->setChecked(DirectControl);
    SlotEnableDirectControl(DirectControl);
}


void mtsPIDQtWidget::ErrorEventHandler(const mtsMessage & message)
{
    CMN_LOG_CLASS_RUN_VERBOSE << "ErrorEventHandler: " << message << std::endl;
}


void mtsPIDQtWidget::EnabledEventHandler(const bool & enable)
{
    emit SignalEnable(enable);
}

void mtsPIDQtWidget::SetpointVUsedEventHandler(const bool & use)
{
    emit SignalUseSetpointV(use);
}