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
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
/* -*- 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/console.h>

#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsManagerLocal.h>

#include <sawIntuitiveResearchKit/console_configuration.h>
#include <sawIntuitiveResearchKit/system.h>
#include <sawIntuitiveResearchKit/IO_proxy.h>
#include <sawIntuitiveResearchKit/teleop_ECM_proxy.h>
#include <sawIntuitiveResearchKit/teleop_PSM_proxy.h>

#include <sawIntuitiveResearchKit/sawIntuitiveResearchKitConfig.h>
#include <sawIntuitiveResearchKit/mtsDaVinciHeadSensor.h>
#if sawIntuitiveResearchKit_HAS_HID_HEAD_SENSOR
#include <sawIntuitiveResearchKit/mtsHIDHeadSensor.h>
#endif

dvrk::console::console(const std::string & name,
                           dvrk::system * system,
                           dvrk::console_configuration * config):
    m_name(name),
    m_system(system),
    m_config(config)
{
}


void dvrk::console::post_configure(void)
{
    // ECM teleops
    for (auto & proxy_config : m_config->teleop_ECMs) {
        const std::string name = proxy_config.MTML + "_" + proxy_config.MTMR + "_" + proxy_config.ECM;
        auto iter = m_teleop_proxies.find(name);
        if (iter == m_teleop_proxies.end()) {
            // create a new teleop_ecm proxy if needed
            auto teleop_proxy = std::make_shared<teleop_ECM_proxy>(name, this->m_system,
                                                                   this, &proxy_config);
            teleop_proxy->post_configure();
            if (m_system->find_conflicting_teleops(teleop_proxy)) {
                teleop_proxy->m_selected = false;
            } else {
                teleop_proxy->m_selected = true;
            }
            m_teleop_proxies[name] = teleop_proxy;
        } else {
            CMN_LOG_INIT_ERROR << "post_configure: failed to configure teleop_ECMs, "
                               << name << " already exists" << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    // PSM teleops
    std::map <std::string, std::vector<std::shared_ptr<teleop_PSM_proxy>>> teleops_per_MTM;
    for (auto & proxy_config : m_config->teleop_PSMs) {
        const std::string name = proxy_config.MTM + "_" + proxy_config.PSM;
        auto iter = m_teleop_proxies.find(name);
        if (iter == m_teleop_proxies.end()) {
            // create a new teleop_ecm proxy if needed
            auto teleop_proxy = std::make_shared<teleop_PSM_proxy>(name, this->m_system,
                                                                   this, &proxy_config);
            teleop_proxy->post_configure();
            // make sure no conflicting teleops are selected
            if (m_system->find_conflicting_teleops(teleop_proxy)) {
                teleop_proxy->m_selected = false;
            } else {
                teleop_proxy->m_selected = true;
            }
            // add to list of teleops using each MTM
            teleops_per_MTM[proxy_config.MTM].push_back(teleop_proxy);
            m_teleop_proxies[name] = teleop_proxy;
        } else {
            CMN_LOG_INIT_ERROR << "post_configure: failed to configure teleop_PSMs, "
                               << name << " already exists" << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    // find with PSM teleops can be cycled using quick-tap
    bool found_cycle = false;
    for (auto & iter : teleops_per_MTM) {
        if (iter.second.size() == 2) {
            if (!found_cycle) {
                found_cycle = true;
                m_teleop_PSM_cycle_1 = iter.second.at(0);
                m_teleop_PSM_cycle_2 = iter.second.at(1);
            }
        }
    }

    // clutch, camera pedals
    if ((m_config->input_type == console_input_type::PEDALS_ONLY)
        || (m_config->input_type == console_input_type::PEDALS_ISI_HEAD_SENSOR)
        || (m_config->input_type == console_input_type::PEDALS_DVRK_HEAD_SENSOR)
        || (m_config->input_type == console_input_type::PEDALS_GOOVIS_HEAD_SENSOR)) {
        if (m_config->IO_pedals.IO == "") {
            CMN_LOG_INIT_ERROR << "console::post_configure: \"IO_pedals\" must be provided for inputs PEDALS_{}: "
                               << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
        m_config->clutch.component = m_config->IO_pedals.IO;
        m_config->clutch.interface = "clutch";
        m_config->camera.component = m_config->IO_pedals.IO;
        m_config->camera.interface = "camera";
        // if only pedals, use coag for operator present
        if (m_config->input_type == console_input_type::PEDALS_ONLY) {
            m_config->operator_present.component = m_config->IO_pedals.IO;
            m_config->operator_present.interface = "coag";
        }
    } else {
        m_config->IO_pedals.IO = "";
    }

    if (m_config->input_type == console_input_type::PEDALS_ISI_HEAD_SENSOR) {
        if (m_config->IO_head_sensor.IO == "") {
            CMN_LOG_INIT_ERROR << "console::post_configure: \"IO_head_sensor\" must be provided for inputs PEDALS_ISI_HEAD_SENSOR: "
                               << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
        m_config->operator_present.component = m_name + "_daVinci_head_sensor";
        m_config->operator_present.interface = "operator_present";
    }

    if (m_config->input_type == console_input_type::PEDALS_GOOVIS_HEAD_SENSOR) {
#if sawIntuitiveResearchKit_HAS_HID_HEAD_SENSOR
        if (m_config->HID_file == "") {
            CMN_LOG_INIT_ERROR << "console::post_configure: \"HID_file\" must be provided for inputs PEDALS_GOOVIS_HEAD_SENSOR: "
                               << m_name << std::endl;
            exit(EXIT_FAILURE);
        }
        m_config->operator_present.component = m_name + "_goovis_head_sensor";
        m_config->operator_present.interface = "operator_present";
#else
        CMN_LOG_INIT_ERROR << "console::post_configure: can't use HID head sensor." << std::endl
                           << "The code has been compiled with sawIntuitiveResearchKit_HAS_HID_HEAD_SENSOR OFF." << std::endl
                           << "Re-run CMake, re-compile and try again." << std::endl;
        exit(EXIT_FAILURE);
#endif
    } else {
        m_config->HID_file = "";
    }

}


void dvrk::console::create_components(void)
{
    mtsComponentManager * manager = mtsComponentManager::GetInstance();

    // teleops
    for (auto & proxy : m_teleop_proxies) {
        m_system->add_teleop_interfaces(proxy.second);
        proxy.second->create_teleop();
    }

    // inputs
    if (m_config->IO_pedals.IO != "") {
        m_system->configure_IO(m_config->IO_pedals);
    }

    if (m_config->input_type == console_input_type::PEDALS_ISI_HEAD_SENSOR) {
        m_system->configure_IO(m_config->IO_head_sensor);
        m_head_sensor = new mtsDaVinciHeadSensor(m_config->operator_present.component);
        manager->AddComponent(m_head_sensor);
        // schedule connections between IO and head sensor component
        const std::string IO =  m_config->IO_head_sensor.IO;
        m_system->m_connections.Add(m_config->operator_present.component, "head_sensor_turn_off",
                                    IO, "head_sensor_turn_off");
        m_system->m_connections.Add(m_config->operator_present.component, "dv_head_sensor_1",
                                    IO, "dv_head_sensor_1");
        m_system->m_connections.Add(m_config->operator_present.component, "dv_head_sensor_2",
                                    IO, "dv_head_sensor_2");
        m_system->m_connections.Add(m_config->operator_present.component, "dv_head_sensor_3",
                                    IO, "dv_head_sensor_3");
        m_system->m_connections.Add(m_config->operator_present.component, "dv_head_sensor_4",
                                    IO, "dv_head_sensor_4");
    }

    if (m_config->input_type == console_input_type::PEDALS_DVRK_HEAD_SENSOR) {
        m_system->configure_IO(m_config->IO_head_sensor);
        m_config->operator_present.component = m_config->IO_head_sensor.IO;
        m_config->operator_present.interface = "operator_present";
    }

    if (m_config->input_type == console_input_type::PEDALS_GOOVIS_HEAD_SENSOR) {
        CMN_LOG_INIT_VERBOSE << "console::create_component: configuring hid head sensor with \""
                             << m_config->HID_file << "\"" << std::endl;
        const std::string config_file = m_system->find_file(m_config->HID_file);
        if (config_file == "") {
            CMN_LOG_INIT_ERROR << "console::create_component: can't find configuration file "
                               << m_config->HID_file << std::endl;
            exit(EXIT_FAILURE);
        }
#if sawIntuitiveResearchKit_HAS_HID_HEAD_SENSOR
        m_head_sensor = new mtsHIDHeadSensor(m_config->operator_present.component);
        m_head_sensor->Configure(config_file);
        manager->AddComponent(m_head_sensor);
#endif
    }
}


void dvrk::console::Startup(void)
{
    // emit all events to initialize GUI and ROs
    emit_teleop_state_events();
    events.scale(mtsIntuitiveResearchKit::TeleOperationPSM::Scale);
}


void dvrk::console::emit_teleop_state_events(void)
{
    for (auto & iter : m_teleop_proxies) {
        if (iter.second->m_selected) {
            events.teleop_selected(iter.first);
        } else {
            events.teleop_unselected(iter.first);
        }
    }
}


void dvrk::console::teleop_enable(const bool & _enable)
{
    // m_teleop_enabled = _enable;
    // //     // if we have an SUJ, make sure it's ready
    // //     if (enable && m_SUJ) {
    // //         const auto sujState = ArmStates.find("SUJ");
    // //         if ((sujState == ArmStates.end())
    // //             || (sujState->second.State() != prmOperatingState::ENABLED)) {
    // //             m_teleop_enabled = false;
    // //         }
    // //     }
    m_teleop_wanted = _enable; // for recovery after errors
    // event
    events.teleop_enabled(m_teleop_wanted);
    m_system->m_interface->SendStatus(m_name + ": tele operation "
                                      + (m_teleop_enabled ? "enabled" : "disabled"));
    // event
    update_teleop_state();
}


void dvrk::console::set_scale(const double & _scale)
{
    // PSM teleops
    for (auto & proxy : m_teleop_proxies) {
        proxy.second->set_scale(_scale);
    }
    events.scale(_scale);
}


void dvrk::console::select_teleop(const std::string & _teleop)
{
    auto iter = m_teleop_proxies.find(_teleop);
    if (iter != m_teleop_proxies.end()) {
        m_system->find_conflicting_teleops(iter->second, true /* to unselect */);
        iter->second->m_selected = true;
        events.teleop_selected(_teleop);
        update_teleop_state();
        return;
    }
    m_interface_provided->SendStatus(m_name + ": select_teleop can't find " + _teleop);
}


void dvrk::console::unselect_teleop(const std::string & _teleop)
{
    auto iter = m_teleop_proxies.find(_teleop);
    if (iter != m_teleop_proxies.end()) {
        iter->second->m_selected = false;
        events.teleop_unselected(_teleop);
        update_teleop_state();
        return;
    }
    m_interface_provided->SendStatus(m_name + ": unselect_teleop can't find " + _teleop);
}


bool dvrk::console::find_conflicting_teleops(std::shared_ptr<teleop_proxy> _teleop, const bool _unselect)
{
    bool result = false;
    for (auto & teleop : m_teleop_proxies) {
        if ((teleop.first != _teleop->m_name)
            && (teleop.second->type() == _teleop->type())
            && (teleop.second->m_selected)) {
            std::list<std::string> conflicting_arms;
            std::set_intersection(_teleop->m_arms_used.begin(), _teleop->m_arms_used.end(),
                                  teleop.second->m_arms_used.begin(), teleop.second->m_arms_used.end(),
                                  std::back_inserter(conflicting_arms));
            for (auto & arm : conflicting_arms) {
                result |= true;
                if (_unselect) {
                    unselect_teleop(teleop.first);
                    m_interface_provided->SendStatus(m_name + ": " + teleop.first + " unselected to free " + arm);
                }
            }
        }
    }
    return result;
}


void dvrk::console::clutch_event_handler(const prmEventButton & _button)
{
    switch (_button.Type()) {
    case prmEventButton::PRESSED:
        m_interface_provided->SendStatus(m_name + ": clutch pressed");
        m_system->audio.beep(vct3(0.1, 700.0, m_system->m_audio_volume));
        break;
    case prmEventButton::RELEASED:
        m_interface_provided->SendStatus(m_name + ": clutch released");
        m_system->audio.beep(vct3(0.1, 700.0, m_system->m_audio_volume));
        break;
    case prmEventButton::CLICKED:
        m_interface_provided->SendStatus(m_name + ": clutch quick tap");
        m_system->audio.beep(vct3(0.05, 2000.0, m_system->m_audio_volume));
        m_system->audio.beep(vct3(0.05, 2000.0, m_system->m_audio_volume));
        if (m_teleop_PSM_cycle_1 != nullptr) {
            if (m_teleop_PSM_cycle_1->m_selected) {
                select_teleop(m_teleop_PSM_cycle_2->m_name);
            } else {
                select_teleop(m_teleop_PSM_cycle_1->m_name);
            }
        }
        break;
    default:
        break;
    }
    events.clutch(_button);
}


void dvrk::console::camera_event_handler(const prmEventButton & _button)
{
    switch (_button.Type()) {
    case prmEventButton::PRESSED:
        m_camera = true;
        m_interface_provided->SendStatus(m_name + ": camera pressed");
        m_system->audio.beep(vct3(0.1, 1000.0, m_system->m_audio_volume));
        break;
    case prmEventButton::RELEASED:
        m_camera = false;
        m_interface_provided->SendStatus(m_name + ": camera released");
        m_system->audio.beep(vct3(0.1, 1000.0, m_system->m_audio_volume));
        break;
    case prmEventButton::CLICKED:
        m_interface_provided->SendStatus(m_name + ": camera quick tap");
        m_system->audio.beep(vct3(0.05, 2500.0, m_system->m_audio_volume));
        m_system->audio.beep(vct3(0.05, 2500.0, m_system->m_audio_volume));
        break;
    default:
        break;
    }
    update_teleop_state();
    events.camera(_button);
}


void dvrk::console::operator_present_event_handler(const prmEventButton & _button)
{
    switch (_button.Type()) {
    case prmEventButton::PRESSED:
        m_operator_present = true;
        m_interface_provided->SendStatus(m_name + ": operator present");
        m_system->audio.beep(vct3(0.3, 1500.0, m_system->m_audio_volume));
        break;
    case prmEventButton::RELEASED:
        m_operator_present = false;
        m_interface_provided->SendStatus(m_name + ": operator not present");
        m_system->audio.beep(vct3(0.3, 1200.0, m_system->m_audio_volume));
        break;
    default:
        break;
    }
    update_teleop_state();
    events.operator_present(_button);
}


void dvrk::console::update_teleop_state(void)
{
    // overall teleop
    if (m_teleop_wanted != m_teleop_enabled) {
        m_teleop_enabled = m_teleop_wanted;
    }

    // per teleop pair
    for (auto & iter : m_teleop_proxies) {
        auto & teleop_proxy = iter.second;
        if (m_teleop_enabled
            && !teleop_proxy->m_enabled
            && teleop_proxy->m_selected
            && (((teleop_proxy->type() == teleop_proxy::ECM)
                 && m_camera)
                || ((teleop_proxy->type() == teleop_proxy::PSM)
                    && !m_camera)
                )
            ) {
            if (m_operator_present
                || (teleop_proxy->type() == dvrk::teleop_proxy::ECM)
                ) {
                teleop_proxy->m_enabled = true;
                teleop_proxy->state_command(std::string("enable"));
            } else {
                teleop_proxy->state_command(std::string("align_MTM"));
            }
        } else if (!m_teleop_enabled
                   || !m_operator_present
                   || teleop_proxy->m_enabled
                   ) {
            teleop_proxy->m_enabled = false;
            teleop_proxy->state_command(std::string("disable"));
        }
    }
}