File size: 6,186 Bytes
ef17c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- 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-07

  (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 <iostream>
// cisst/saw
#include <cisstCommon/cmnPath.h>
#include <cisstCommon/cmnCommandLineOptions.h>
#include <cisstOSAbstraction/osaSleep.h>
#include <cisstMultiTask/mtsManagerLocal.h>
#include <sawRobotIO1394/mtsRobotIO1394.h>
#include <sawRobotIO1394/mtsRobotIO1394QtWidgetFactory.h>
#include <sawControllers/mtsPID.h>
#include <sawControllers/mtsPIDQtWidget.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKit.h>

#include <QApplication>
#include <QMainWindow>

int main(int argc, char ** argv)
{
    // period used for IO and PID
    const double periodIOPID = mtsIntuitiveResearchKit::IOPeriod;

    // log configuration
    cmnLogger::SetMask(CMN_LOG_ALLOW_ALL);
    cmnLogger::SetMaskDefaultLog(CMN_LOG_ALLOW_ALL);
    cmnLogger::SetMaskFunction(CMN_LOG_ALLOW_ALL);
    cmnLogger::SetMaskClass("mtsPID", CMN_LOG_ALLOW_ALL);
    cmnLogger::SetMaskClassMatching("mtsIntuitiveResearchKit", CMN_LOG_ALLOW_ALL);
    cmnLogger::AddChannel(std::cerr, CMN_LOG_ALLOW_ERRORS_AND_WARNINGS);

    // parse options
    cmnCommandLineOptions options;
    std::string portName = mtsRobotIO1394::DefaultPort();
    std::string ioConfigFile, pidConfigFile;
    std::string robotName;

    options.AddOptionOneValue("i", "io",
                              "configuration file for robot IO (see sawRobotIO1394)",
                              cmnCommandLineOptions::REQUIRED_OPTION, &ioConfigFile);
    options.AddOptionOneValue("g", "pid",
                              "configuration file for PID controller (see sawControllers, mtsPID)",
                              cmnCommandLineOptions::REQUIRED_OPTION, &pidConfigFile);
    options.AddOptionOneValue("P", "port",
                              "controller port (X, fw:X, udp, udp:XX.XX.XX.XX)",
                              cmnCommandLineOptions::OPTIONAL_OPTION, &portName);
    options.AddOptionOneValue("n", "robot-name",
                              "robot name (i.e. PSM1, PSM2, MTML or MTMR) as defined in the sawRobotIO1394 file",
                              cmnCommandLineOptions::REQUIRED_OPTION, &robotName);

    if (!options.Parse(argc, argv, std::cerr)) {
        return -1;
    }

    if (!cmnPath::Exists(ioConfigFile)) {
        std::cerr << "File not found: " << ioConfigFile << std::endl;
        return -1;
    }
    if (!cmnPath::Exists(pidConfigFile)) {
        std::cerr << "File not found: " << pidConfigFile << std::endl;
        return -1;
    }

    unsigned int numberOfJoints;
    if ((robotName == "PSM1") || (robotName == "PSM2") || (robotName == "PSM3")) {
        numberOfJoints = 7;
    } else if ((robotName == "MTML") || (robotName == "MTMR")) {
        numberOfJoints = 7;
    } else if (robotName == "ECM") {
        numberOfJoints = 4;
    } else if (robotName == "Focus") {
        numberOfJoints = 1;
    } else {
        std::cerr << "Unknown robot name: " << robotName << ", should be one one PSM1, PSM2, PSM3, MTML, MTMR, ECM or Focus" << std::endl;
        return -1;
    }

    std::cout << "Configuration file for IO: " << ioConfigFile << std::endl
              << "Configuration file for PID: " << pidConfigFile << std::endl
              << "Robot name: " << robotName << std::endl
              << "Number of joints: " << numberOfJoints << std::endl
              << "FirewirePort: " << portName << std::endl;

    mtsManagerLocal * componentManager = mtsManagerLocal::GetInstance();

    // create a Qt user interface
    QApplication application(argc, argv);
    application.setWindowIcon(QIcon(":/dVRK.png"));

    // organize all widgets in a tab widget
    QTabWidget * tabWidget = new QTabWidget;

    // IO
    mtsRobotIO1394 * io = new mtsRobotIO1394("io", periodIOPID, portName);
    io->Configure(ioConfigFile);
    componentManager->AddComponent(io);

    mtsRobotIO1394QtWidgetFactory * robotWidgetFactory = new mtsRobotIO1394QtWidgetFactory("robotWidgetFactory");
    componentManager->AddComponent(robotWidgetFactory);
    componentManager->Connect("robotWidgetFactory", "RobotConfiguration", "io", "Configuration");
    robotWidgetFactory->Configure();

    for (const auto & w : robotWidgetFactory->Widgets()) {
        tabWidget->addTab(w.second, w.first.c_str());
    }
    if (robotWidgetFactory->ButtonsWidget()) {
        tabWidget->addTab(robotWidgetFactory->ButtonsWidget(), "Buttons");
    }


    // Qt PID Controller GUI
    mtsPIDQtWidget * pidGUI = new mtsPIDQtWidget("pidGUI", numberOfJoints);
    pidGUI->Configure();
    componentManager->AddComponent(pidGUI);
    tabWidget->addTab(pidGUI, "PID");

    mtsPID * pid = new mtsPID("pid", periodIOPID);
    pid->Configure(pidConfigFile);
    componentManager->AddComponent(pid);
    // connect pidGUI to pid
    componentManager->Connect("pidGUI", "Controller", "pid", "Controller");

    // tie pid execution to io
    componentManager->Connect("pid", "ExecIn", "io", "ExecOut");
    // connect pid to io
    componentManager->Connect("pid", "RobotJointTorqueInterface", "io", robotName);  // see const std::string defined before main()

    //-------------- create the components ------------------
    componentManager->CreateAll();
    componentManager->WaitForStateAll(mtsComponentState::READY, 2.0 * cmn_s);

    // start the periodic Run
    componentManager->StartAll();

    // run Qt user interface
    tabWidget->show();
    application.exec();

    componentManager->KillAllAndWait(5.0 * cmn_s);
    componentManager->Cleanup();

    // delete dvgc robot
    delete pid;
    delete pidGUI;
    delete io;
    delete robotWidgetFactory;

    // stop all logs
    cmnLogger::Kill();

    return 0;
}