File size: 10,615 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
/* -*- 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):  Brendan Burkhart
  Created on: 2025-01-23

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

// header
#include "mtsBilateralTeleOperationPSM.h"

// cisst includes
#include <cisstMultiTask/mtsManagerLocal.h>

CMN_IMPLEMENT_SERVICES_DERIVED_ONEARG(mtsBilateralTeleOperationPSM,
                                      mtsTeleOperationPSM,
                                      mtsTaskPeriodicConstructorArg);

void mtsBilateralTeleOperationPSM::ForceSource::Configure(mtsBilateralTeleOperationPSM* teleop, const Json::Value & jsonConfig) {
    this->teleop = teleop;

    Json::Value value;
    value = jsonConfig["component"];
    if (!value.empty()) {
        component_name = value.asString();
    }

    value = jsonConfig["interface"];
    if (!value.empty()) {
        provided_interface_name = value.asString();
    }

    value = jsonConfig["function"];
    if (!value.empty()) {
        function_name = value.asString();
    }

    std::string required_interface_name = component_name + "_" + provided_interface_name + "_force_source";
    mtsInterfaceRequired* interface = teleop->AddInterfaceRequired(required_interface_name);
    if (interface) {
        interface->AddFunction(function_name, measured_cf);
    }
}

void mtsBilateralTeleOperationPSM::Arm::populateInterface(mtsInterfaceRequired* interface)
{
    interface->AddFunction("servo_cpvf", servo_cpvf, MTS_OPTIONAL);
    interface->AddFunction("measured_cs", measured_cs, MTS_OPTIONAL);
}

prmStateCartesian mtsBilateralTeleOperationPSM::Arm::computeGoal(Arm* target, double scale)
{
    prmStateCartesian goal;
    prmStateCartesian target_state = target->state();

    vct3 target_translation = target_state.Position().Translation() - target->ClutchOrigin().Translation();
    vct3 goal_translation = scale * target_translation + ClutchOrigin().Translation();

    auto align = vctMatRot3(target->ClutchOrigin().Rotation().TransposeRef() * ClutchOrigin().Rotation());

    if (target_state.PositionIsValid()) {
        goal.Position().Translation() = goal_translation;
        goal.Position().Rotation() = target_state.Position().Rotation() * align;
    }
    goal.PositionIsValid() = target_state.PositionIsValid();

    if (target_state.VelocityIsValid()) {
        goal.Velocity().Ref<3>(0) = scale * target_state.Velocity().Ref<3>(0);
        goal.Velocity().Ref<3>(3) = target_state.Velocity().Ref<3>(3);
    }
    goal.VelocityIsValid() = target_state.VelocityIsValid();

    prmStateCartesian current_state = state();
    if (target_state.ForceIsValid() && current_state.ForceIsValid()) {
        goal.Force() = -target_state.Force() - current_state.Force();
    }
    goal.ForceIsValid() = target_state.ForceIsValid() && current_state.ForceIsValid();

    return goal;
}

prmStateCartesian mtsBilateralTeleOperationPSM::Arm::state()
{
    prmStateCartesian measured_state;
    measured_cs(measured_state);

    if (force_source) {
        force_source->measured_cf(force_source->m_measured_cf);
        measured_state.Force() = force_source->m_measured_cf.Force();
        measured_state.ForceIsValid() = force_source->m_measured_cf.Valid();
    }

    return measured_state;
}

void mtsBilateralTeleOperationPSM::Arm::servo(prmStateCartesian goal)
{
    servo_cpvf(goal);
}

vctFrm4x4& mtsBilateralTeleOperationPSM::ArmMTM::ClutchOrigin() { return teleop->mMTM.CartesianInitial; }

prmStateCartesian mtsBilateralTeleOperationPSM::ArmMTM::state()
{
    if (measured_cs.IsValid()) {
        return Arm::state();
    }

    // measured_cs not available, fall back to measured_cp/measured_cv
    prmStateCartesian state;
    state.Position() = teleop->mMTM.m_measured_cp.Position();
    state.PositionIsValid() = teleop->mMTM.m_measured_cp.Valid();

    if (teleop->m_config.use_MTM_linear_velocity || teleop->m_config.use_MTM_angular_velocity) {
        auto mtm_velocity = teleop->mMTM.m_measured_cv;
        state.Velocity().Ref<3>(0) = mtm_velocity.VelocityLinear();
        state.Velocity().Ref<3>(3) = mtm_velocity.VelocityAngular();
        state.VelocityIsValid() = mtm_velocity.Valid();
    } else {
        state.VelocityIsValid() = false;
    }

    if (force_source) {
        force_source->measured_cf(force_source->m_measured_cf);
        state.Force() = force_source->m_measured_cf.Force();
        state.ForceIsValid() = force_source->m_measured_cf.Valid();
    } else {
        state.ForceIsValid() = false;
    }

    return state;
}

void mtsBilateralTeleOperationPSM::ArmMTM::servo(prmStateCartesian goal)
{
    // Use servo_cpvf if available, otherwise fall back to servo_cp
    if (servo_cpvf.IsValid()) {
        Arm::servo(goal);
    } else {
        prmPositionCartesianSet& servo = teleop->mArmMTM.m_servo_cp;
        servo.Goal() = goal.Position();

        if (goal.VelocityIsValid()) {
            servo.Velocity() = goal.Velocity().Ref<3>(0);
            servo.VelocityAngular() = goal.Velocity().Ref<3>(3);
        } else {
            servo.Velocity().Assign(vct3(0));
            servo.VelocityAngular().Assign(vct3(0));
        }

        teleop->mArmMTM.servo_cp(servo);
    }
}

vctFrm4x4& mtsBilateralTeleOperationPSM::ArmPSM::ClutchOrigin() { return teleop->mPSM.CartesianInitial; };

prmStateCartesian mtsBilateralTeleOperationPSM::ArmPSM::state()
{
    if (measured_cs.IsValid()) {
        return Arm::state();
    }

    // measured_cs not available, fall back to measured_cp/measured_cv
    
    prmStateCartesian state;
    teleop->mArmPSM.measured_cp(teleop->mArmPSM.m_measured_cp);
    state.Position() = teleop->mArmPSM.m_measured_cp.Position();
    state.PositionIsValid() = teleop->mArmPSM.m_measured_cp.Valid();

    teleop->mArmPSM.measured_cv(teleop->mArmPSM.m_measured_cv);
    auto psm_velocity = teleop->mArmPSM.m_measured_cv;
    state.Velocity().Ref<3>(0) = psm_velocity.VelocityLinear();
    state.Velocity().Ref<3>(3) = psm_velocity.VelocityAngular();
    state.VelocityIsValid() = psm_velocity.Valid();

    if (force_source) {
        force_source->measured_cf(force_source->m_measured_cf);
        state.Force() = force_source->m_measured_cf.Force();
        state.ForceIsValid() = force_source->m_measured_cf.Valid();
    } else {
        state.ForceIsValid() = false;
    }

    return state;
}

void mtsBilateralTeleOperationPSM::ArmPSM::servo(prmStateCartesian goal)
{
    // Use servo_cpvf if available, otherwise fall back to servo_cp
    if (servo_cpvf.IsValid()) {
        Arm::servo(goal);
    } else {
        prmPositionCartesianSet& servo = teleop->mPSM.m_servo_cp;
        servo.Goal() = goal.Position();

        if (goal.VelocityIsValid()) {
            servo.Velocity() = goal.Velocity().Ref<3>(0);
            servo.VelocityAngular() = goal.Velocity().Ref<3>(3);
        } else {
            servo.Velocity().Assign(vct3(0));
            servo.VelocityAngular().Assign(vct3(0));
        }

        teleop->mPSM.servo_cp(servo);
    }
}

mtsBilateralTeleOperationPSM::mtsBilateralTeleOperationPSM(const std::string & componentName,
                                                       const double periodInSeconds) :
    mtsTeleOperationPSM(componentName, periodInSeconds), mArmMTM(this), mArmPSM(this) { Init(); }

mtsBilateralTeleOperationPSM::mtsBilateralTeleOperationPSM(const mtsTaskPeriodicConstructorArg & arg) :
    mtsTeleOperationPSM(arg), mArmMTM(this), mArmPSM(this) { Init(); }

void mtsBilateralTeleOperationPSM::Init() {
    m_bilateral_enabled = true;

    mtsInterfaceRequired* interface;

    interface = GetInterfaceRequired("MTM");
    if (interface) {
        interface->AddFunction("servo_cp", mArmMTM.servo_cp);
        mArmMTM.populateInterface(interface);
    }

    interface = GetInterfaceRequired("PSM");
    if (interface) {
        interface->AddFunction("measured_cp", mArmPSM.measured_cp);
        mArmPSM.populateInterface(interface);
    }

    mConfigurationStateTable->AddData(m_bilateral_enabled, "bilateral_enabled");

    mtsInterfaceProvided* setting_interface = GetInterfaceProvided("Setting");
    if (setting_interface) {
        setting_interface->AddCommandWrite(&mtsBilateralTeleOperationPSM::set_bilateral_enabled, this,
                                        "set_bilateral_enabled", m_bilateral_enabled);
        setting_interface->AddCommandReadState(*(mConfigurationStateTable),
                                        m_bilateral_enabled, "bilateral_enabled");
        setting_interface->AddEventWrite(bilateral_enabled_event,
                                    "bilateral_enabled", m_bilateral_enabled);
    }
}

void mtsBilateralTeleOperationPSM::Configure(const Json::Value & jsonConfig)
{
    mtsTeleOperationPSM::Configure(jsonConfig);
    Json::Value jsonValue;

    jsonValue = jsonConfig["psm_force_source"];
    if (!jsonValue.empty()) {
        auto source = std::make_unique<ForceSource>();
        source->Configure(this, jsonValue);
        mArmPSM.add_force_source(std::move(source));
    }

    jsonValue = jsonConfig["mtm_force_source"];
    if (!jsonValue.empty()) {
        auto source = std::make_unique<ForceSource>();
        source->Configure(this, jsonValue);
        mArmMTM.add_force_source(std::move(source));
    }

    jsonValue = jsonConfig["mtm_torque_gain"];
    if (!jsonValue.empty()) {
        m_mtm_torque_gain = jsonValue.asDouble();
    }
}

void mtsBilateralTeleOperationPSM::set_bilateral_enabled(const bool & enabled)
{
    mConfigurationStateTable->Start();
    m_bilateral_enabled = enabled;
    mConfigurationStateTable->Advance();
    bilateral_enabled_event(m_bilateral_enabled);
}

void mtsBilateralTeleOperationPSM::RunCartesianTeleop()
{
    // fall back to default behavior when in unilateral mode
    if (!m_bilateral_enabled) {
        mtsTeleOperationPSM::RunCartesianTeleop();
        return;
    }

    if (m_clutched) {
        return;
    }

    auto psm_goal = mArmPSM.computeGoal(&mArmMTM, m_config.scale);
    mArmPSM.servo(psm_goal);

    auto mtm_goal = mArmMTM.computeGoal(&mArmPSM, 1.0 / m_config.scale);
    // scale MTM torque goal to reduce oscillations
    mtm_goal.Force().Ref<3>(3) = m_mtm_torque_gain * mtm_goal.Force().Ref<3>(3);
    mArmMTM.servo(mtm_goal);
}