File size: 22,223 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
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
/* -*- 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-05-17

  (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 ---
*/

#include <sawIntuitiveResearchKit/arm_proxy.h>

#include <cisstMultiTask/mtsManagerLocal.h>

#include <sawControllers/mtsPID.h>

#include <sawRobotIO1394/mtsRobotIO1394.h>

#include <sawIntuitiveResearchKit/sawIntuitiveResearchKitConfig.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitMTM.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitPSM.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitECM.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitSUJ.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitSUJFixed.h>
#if sawIntuitiveResearchKit_HAS_SUJ_Si
  #include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitSUJSi.h>
#endif
#include <sawIntuitiveResearchKit/system.h>
#include <sawIntuitiveResearchKit/IO_proxy.h>

dvrk::arm_proxy::arm_proxy(const std::string & name,
                           dvrk::system * system,
                           dvrk::arm_proxy_configuration * config):
    m_name(name),
    m_system(system),
    m_config(config),
    m_arm_component_name(name),
    m_arm_interface_name("Arm")
{
}


void dvrk::arm_proxy::post_configure(void)
{
    // check if user has multiple IOs
    if (m_config->name_on_IO == "") {
        m_config->name_on_IO = m_config->name;
    }

    // make sure IO component is provided
    if (m_config->expects_IO()) {
        // valid string
        if (m_config->IO == "") {
            CMN_LOG_INIT_ERROR << "arm_proxy::post_configure: \"IO\" must be provided for native or derived arm: "
                               << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
        // registered in component manager?
        auto iter = m_system->m_IO_proxies.find(m_config->IO);
        if (iter == m_system->m_IO_proxies.end()) {
            CMN_LOG_INIT_ERROR << "arm_proxy::post_configure: IO \""
                               << m_config->IO << "\" is not defined in \"IOs\"" << std::endl;
            exit(EXIT_FAILURE);
        }
        m_IO_component_name = m_config->IO;
        m_IO_interface_name = m_config->name_on_IO;
    }

    // for generic and derived arms, component name must be provided
    if (m_config->generic_or_derived()) {
        if ((m_config->component != "") && (m_config->interface != "")) {
            m_arm_component_name = m_config->component;
            m_arm_interface_name = m_config->interface;
        } else {
            CMN_LOG_INIT_ERROR << "arm_proxy::post_configure: \"component\" and \"interface\" must be provided for generic or derived arm: "
                               << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    // check base frame settings
    if (!m_config->base_frame.valid()) {
        CMN_LOG_INIT_ERROR << "arm_proxy::post_configure, base_frame must be defined by either \"reference_frame\" name and \"transform\" OR \"component\" and \"interface\" for "
                           << m_name << std::endl;
        exit(EXIT_FAILURE);
    } else {
        if ((m_config->base_frame.component != "") && (m_config->base_frame.interface != "")) {
            m_base_frame_component_name = m_config->base_frame.component;
            m_base_frame_interface_name = m_config->base_frame.interface;
        }
    }
}


void dvrk::arm_proxy::create_arm(void)
{
    if (!m_config->native_or_derived()) {
        return;
    }

    // infer arm configuration file
    // -1- provided by user
    if (m_config->arm_file != "") {
        m_arm_configuration_file = m_system->find_file(m_config->arm_file);
        if (m_arm_configuration_file == "") {
            CMN_LOG_INIT_ERROR << "arm_proxy::create_arm: can't find configuration file " << m_config->arm_file
                               << " for arm " << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
    } else {
        // -2- using serial number
        if ((m_config->type != dvrk::arm_type::FOCUS_CONTROLLER)
            && (m_config->simulation != dvrk::simulation::SIMULATION_KINEMATIC)) {
            if (m_config->serial == "") {
                CMN_LOG_INIT_ERROR << "arm_proxy::create_arm: serial number required for arm "
                                   << m_name << std::endl;
                exit(EXIT_FAILURE);
            }
            const auto default_file = m_config->name_on_IO + "-" + m_config->serial + ".json";
            m_arm_configuration_file = m_system->find_file(default_file);
            if (m_arm_configuration_file == "") {
                CMN_LOG_INIT_ERROR << "arm_proxy::create_arm: can't find \"arm\" setting for arm "
                                   << m_name << ". \"arm\" is not set and the default file "
                                   << default_file << " doesn't seem to exist either." << std::endl;
                exit(EXIT_FAILURE);
            } else {
                CMN_LOG_INIT_VERBOSE << "arm_proxy::create_arm: using the default file "
                                     << default_file << " for arm " << m_name << std::endl;

            }
        }
    }


    mtsManagerLocal * component_manager = mtsManagerLocal::GetInstance();
    // for research kit arms, create, add to manager and connect to
    // extra IO, PID, etc.  For generic arms, do nothing.
    switch (m_config->type) {
    case dvrk::arm_type::MTM:
        {
            m_arm = std::make_shared<mtsIntuitiveResearchKitMTM>(m_name, m_config->period);
        }
        break;
    case dvrk::arm_type::PSM:
        {
            m_arm = std::make_shared<mtsIntuitiveResearchKitPSM>(m_name, m_config->period);
        }
        break;
    case dvrk::arm_type::ECM:
        {
            m_arm = std::make_shared<mtsIntuitiveResearchKitECM>(m_name, m_config->period);
        }
        break;
    case dvrk::arm_type::SUJ_Classic:
        {
            mtsIntuitiveResearchKitSUJ * suj = new mtsIntuitiveResearchKitSUJ(m_name, m_config->period);
            if (m_config->simulation == dvrk::simulation::SIMULATION_KINEMATIC) {
                suj->set_simulated();
            } else if (m_config->simulation == dvrk::simulation::SIMULATION_NONE) {
                m_system->m_connections.Add(m_name, "no_mux_reset",
                                            m_IO_component_name, "no_mux_reset");
                m_system->m_connections.Add(m_name, "mux_increment",
                                            m_IO_component_name, "mux_increment");
                m_system->m_connections.Add(m_name, "control_PWM",
                                            m_IO_component_name, "control_PWM");
                m_system->m_connections.Add(m_name, "disable_PWM",
                                            m_IO_component_name, "disable_PWM");
                m_system->m_connections.Add(m_name, "motor_up",
                                            m_IO_component_name, "motor_up");
                m_system->m_connections.Add(m_name, "motor_down",
                                            m_IO_component_name, "motor_down");
                m_system->m_connections.Add(m_name, "SUJ_clutch_1",
                                            m_IO_component_name, "SUJ_clutch_1");
                m_system->m_connections.Add(m_name, "SUJ_clutch_2",
                                            m_IO_component_name, "SUJ_clutch_2");
                m_system->m_connections.Add(m_name, "SUJ_clutch_3",
                                            m_IO_component_name, "SUJ_clutch_3");
                m_system->m_connections.Add(m_name, "SUJ_clutch_4",
                                            m_IO_component_name, "SUJ_clutch_4");
            }
            suj->Configure(m_arm_configuration_file);
            component_manager->AddComponent(suj);
        }
        break;
    case dvrk::arm_type::SUJ_Si:
        {
#if sawIntuitiveResearchKit_HAS_SUJ_Si
            mtsIntuitiveResearchKitSUJSi * suj = new mtsIntuitiveResearchKitSUJSi(m_name, m_config->period);
            if (m_config->simulation == dvrk::simulation::SIMULATION_KINEMATIC) {
                suj->set_simulated();
            }
            suj->Configure(m_arm_configuration_file);
            component_manager->AddComponent(suj);
#else
            CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: can't create an arm of type SUJ_Si because sawIntuitiveResearchKit_HAS_SUJ_Si is set to OFF in CMake"
                               << std::endl;
            exit(EXIT_FAILURE);
#endif
        }
        break;
    case dvrk::arm_type::SUJ_Fixed:
        {
            mtsIntuitiveResearchKitSUJFixed * suj = new mtsIntuitiveResearchKitSUJFixed(m_name, m_config->period);
            suj->Configure(m_arm_configuration_file);
            component_manager->AddComponent(suj);
        }
        break;
    case dvrk::arm_type::MTM_DERIVED:
        {
            mtsComponent * component;
            component = component_manager->GetComponent(m_name);
            if (component) {
                mtsIntuitiveResearchKitMTM * mtm = dynamic_cast<mtsIntuitiveResearchKitMTM *>(component);
                if (mtm) {
                    std::cerr << CMN_LOG_DETAILS << " is this risky?" << std::endl;
                    m_arm = std::shared_ptr<mtsIntuitiveResearchKitMTM>(mtm, [](mtsIntuitiveResearchKitMTM * p){ ; });
                } else {
                    CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: component \""
                                       << m_name << "\" doesn't seem to be derived from mtsIntuitiveResearchKitMTM."
                                       << std::endl;
                    exit(EXIT_FAILURE);
                }
            } else {
                CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: component \""
                                   << m_name << "\" not found."
                                   << std::endl;
                exit(EXIT_FAILURE);
            }
        }
        break;
    case dvrk::arm_type::PSM_DERIVED:
        {
            mtsComponent * component;
            component = component_manager->GetComponent(m_name);
            if (component) {
                mtsIntuitiveResearchKitPSM * psm = dynamic_cast<mtsIntuitiveResearchKitPSM *>(component);
                if (psm) {
                    std::cerr << CMN_LOG_DETAILS << " is this risky?" << std::endl;
                    m_arm = std::shared_ptr<mtsIntuitiveResearchKitArm>(psm);
                } else {
                    CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: component \""
                                       << m_name << "\" doesn't seem to be derived from mtsIntuitiveResearchKitPSM."
                                       << std::endl;
                    exit(EXIT_FAILURE);
                }
            } else {
                CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: component \""
                                   << m_name << "\" not found."
                                   << std::endl;
                exit(EXIT_FAILURE);
            }
        }
        break;
    case dvrk::arm_type::ECM_DERIVED:
        {
            mtsComponent * component;
            component = component_manager->GetComponent(m_name);
            if (component) {
                mtsIntuitiveResearchKitECM * ecm = dynamic_cast<mtsIntuitiveResearchKitECM *>(component);
                if (ecm) {
                    std::cerr << CMN_LOG_DETAILS << " is this risky?" << std::endl;
                    m_arm = std::shared_ptr<mtsIntuitiveResearchKitArm>(ecm);
                } else {
                    CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: component \""
                                       << m_name << "\" doesn't seem to be derived from mtsIntuitiveResearchKitECM."
                                       << std::endl;
                    exit(EXIT_FAILURE);
                }
            } else {
                CMN_LOG_INIT_ERROR << "dvrk::arm_proxy::ConfigureArm: component \""
                                   << m_name << "\" not found."
                                   << std::endl;
                exit(EXIT_FAILURE);
            }
        }
        break;

    default:
        break;
    }

    // if native or derived active arms
    if (m_config->native_or_derived()
        && !m_config->SUJ()) {
        CMN_ASSERT(m_arm != nullptr);
        if (m_config->simulation == dvrk::simulation::SIMULATION_KINEMATIC) {
            m_arm->set_simulated();
        }
        m_arm->set_calibration_mode(m_calibration_mode);
        m_arm->Configure(m_arm_configuration_file);
        set_base_frame_if_needed();
        component_manager->AddComponent(m_arm.get());

        // for all native arms not simulated, connect a few IOS
        if (m_config->simulation == dvrk::simulation::SIMULATION_NONE) {

            if (m_config->PSM()) {
                std::vector<std::string> itfs = {"adapter", "tool", "arm_clutch", "dallas"};
                for (const auto & itf : itfs) {
                    m_system->m_connections.Add(m_name, itf,
                                                m_IO_component_name, m_config->name_on_IO + "_" + itf);
                }
            }

            if (m_config->ECM()) {
                m_system->m_connections.Add(m_name, "arm_clutch",
                                            m_IO_component_name, m_config->name_on_IO + "_arm_clutch");
            }

            // for Si patient side, connect the SUJ brakes to buttons on arm
            if ((m_config->PSM() || m_config->ECM())
                && (m_arm->generation() == dvrk::generation::Si)) {
                std::vector<std::string> itfs = {"SUJ_clutch", "SUJ_clutch_2", "SUJ_brake"};
                for (const auto & itf : itfs) {
                    m_system->m_connections.Add(m_name, itf,
                                                m_IO_component_name, m_config->name_on_IO + "_" + itf);
                }
            }
        }
    }
}


void dvrk::arm_proxy::configure_IO(void)
{
    if (!m_config->expects_IO()) {
        return;
    }

    // search for the config file
    std::string config_file;
    if (m_config->IO_file != "") {
        config_file = m_config->IO_file;
    } else {
        if (m_config->serial != "") {
            config_file = "sawRobotIO1394-" + m_config->name_on_IO + "-" + m_config->serial + ".json";
        } else {
            CMN_LOG_INIT_ERROR << "arm_proxy::configure_IO: can't find \"serial\" nor \"IO_file\" setting for arm \""
                               << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
    }
    m_IO_configuration_file = m_system->find_file(config_file);
    if (m_IO_configuration_file == "") {
        CMN_LOG_INIT_ERROR << "arm_proxy::configure_IO: can't find IO file " << config_file << std::endl;
        exit(EXIT_FAILURE);
    }

    // search for the IO component
    auto iter_IO = m_system->m_IO_proxies.find(m_config->IO);
    CMN_LOG_INIT_VERBOSE << "arm_proxy::configure_IO: configuring IO \""
                         << m_config->IO << "\" for arm " << m_name
                         << " using configuration file: " << m_IO_configuration_file << std::endl;
    // configure_IO should only happen if IO is valid
    CMN_ASSERT(iter_IO != m_system->m_IO_proxies.end());
    iter_IO->second->m_IO->Configure(m_IO_configuration_file);


    // search for the gripper config file
    if (m_config->MTM()) {
        std::string config_file;
        if (m_config->IO_gripper_file != "") {
            config_file = m_config->IO_gripper_file;
        } else {
            if (m_config->serial != "") {
                config_file = "sawRobotIO1394-" + m_config->name_on_IO + "-gripper-" + m_config->serial + ".json";
            } else {
                CMN_LOG_INIT_ERROR << "arm_proxy::configure_IO: can't find \"serial\" nor \"IO_gripper_file\" setting for arm \""
                                   << m_name << std::endl;
                exit(EXIT_FAILURE);
            }
        }
        m_IO_gripper_configuration_file = m_system->find_file(config_file);
        if (m_IO_gripper_configuration_file == "") {
            CMN_LOG_INIT_ERROR << "arm_proxy::configure_IO: can't find IO gripper file " << config_file << std::endl;
            exit(EXIT_FAILURE);
        }

        // re-use IO component
        iter_IO->second->m_IO->Configure(m_IO_gripper_configuration_file);
    }
}


void dvrk::arm_proxy::create_PID(void)
{
    if (!m_config->expects_PID()) {
        return;
    }

    // infer pid config file name
    std::string pid_config;
    if (m_config->PID_file !=  "") {
        m_PID_configuration_file = m_config->PID_file;
        CMN_LOG_INIT_VERBOSE << "ConfigurePID: using \"PID\" file name for arm \""
                             << m_name << "\": \""
                             << m_PID_configuration_file << "\"" << std::endl;
    } else {
        // not user defined, try to find the default
        auto generation = this->generation();
        if (m_config->native_or_derived_MTM()) {
            m_PID_configuration_file = "pid/sawControllersPID-MTM.json";
        } else if (m_config->native_or_derived_PSM()) {
            if (generation == dvrk::generation::Classic) {
                m_PID_configuration_file = "pid/sawControllersPID-PSM.json";
            } else {
                m_PID_configuration_file = "pid/sawControllersPID-PSM-Si.json";
            }
        } else if (m_config->native_or_derived_ECM()) {
            if (generation == dvrk::generation::Classic) {
                m_PID_configuration_file = "pid/sawControllersPID-ECM.json";
            } else  {
                m_PID_configuration_file = "pid/sawControllersPID-ECM-Si.json";
            }
        } else {
            m_PID_configuration_file = "pid/sawControllersPID-" + m_name + ".json";
        }
        CMN_LOG_INIT_VERBOSE << "ConfigurePID: no \"PID\" file name for arm \""
                             << m_name << "\", using default based on type and generation: \""
                             << m_PID_configuration_file << "\"" << std::endl;
    }

    auto configuration_file = m_system->find_file(m_PID_configuration_file);
    if (configuration_file == "") {
        CMN_LOG_INIT_ERROR << "configure_PID: can't find PID file " << m_PID_configuration_file << std::endl;
        exit(EXIT_FAILURE);
    }

    CMN_LOG_INIT_VERBOSE << "configure_PID: found file " << configuration_file << std::endl;
    m_PID_configuration_file = configuration_file;
    m_PID_component_name = m_name + "_PID";

    mtsManagerLocal * component_manager = mtsManagerLocal::GetInstance();
    mtsPID * pid = new mtsPID(m_PID_component_name,
                              (m_config->PID_period != 0.0) ? m_config->PID_period : mtsIntuitiveResearchKit::IOPeriod);
    bool hasIO = true;
    pid->Configure(m_PID_configuration_file);
    if (m_config->simulation == dvrk::simulation::SIMULATION_KINEMATIC) {
        pid->SetSimulated();
        hasIO = false;
    }
    component_manager->AddComponent(pid);
    if (hasIO) {
        m_system->m_connections.Add(m_PID_component_name, "RobotJointTorqueInterface",
                                    m_IO_component_name, m_IO_interface_name);
        if (m_config->PID_period == 0.0) {
            m_system->m_connections.Add(m_PID_component_name, "ExecIn",
                                        m_IO_component_name, "ExecOut");
        }
    }
}


void dvrk::arm_proxy::set_base_frame_if_needed(void)
{
    // use reference_name to determine if we're using a fixed base frame
    if (m_config->base_frame.reference_frame != "") {
        m_base_frame.Goal().From(m_config->base_frame.transform);
        m_base_frame.ReferenceFrame() = m_config->base_frame.reference_frame;
        m_base_frame.Valid() = true;

        if (m_arm == nullptr) {
            CMN_LOG_INIT_ERROR << "arm_proxy::set_base_frame_if_needed failed, arm needs to be configured first" << std::endl;
            exit(EXIT_FAILURE);
        }
        m_arm->set_base_frame(m_base_frame);
    }
}


bool dvrk::arm_proxy::connect(void)
{
    mtsManagerLocal * component_manager = mtsManagerLocal::GetInstance();
    // if the arm is a research kit arm
    if (m_config->native_or_derived()) {
        // Connect arm to IO if not simulated
        if (m_config->expects_IO()) {
            component_manager->Connect(m_name, "RobotIO",
                                       m_IO_component_name, m_config->name_on_IO);
        }
        // connect MTM gripper to IO
        if (m_config->native_or_derived_MTM()
            && !m_config->simulated()) {
            component_manager->Connect(m_name, "GripperIO",
                                       m_IO_component_name, m_config->name_on_IO + "_gripper");
        }
        // connect PID
        if (m_config->expects_PID()) {
            component_manager->Connect(m_name, "PID",
                                       m_PID_component_name, "Controller");
        }
        // connect m_base_frame if needed
        if ((m_base_frame_component_name != "") && (m_base_frame_interface_name != "")) {
            component_manager->Connect(m_base_frame_component_name, m_base_frame_interface_name,
                                       m_name, "Arm");
        }
    }
    return true;
}


dvrk::generation dvrk::arm_proxy::generation(void) const
{
    if (m_arm != nullptr) {
        return m_arm->generation();
    } else {
        CMN_LOG_INIT_ERROR << "arm_proxy::generation failed, arm needs to be configured first" << std::endl;
        exit(EXIT_FAILURE);
    }
    return dvrk::generation::GENERATION_UNDEFINED;
}


void dvrk::arm_proxy::current_state_event_handler(const prmOperatingState & _current_state)
{
    m_system->set_arm_current_state(m_name, _current_state);
}