File size: 30,046 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 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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 | /* -*- 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, Nicole Ortega
Created on: 2016-01-21
(C) Copyright 2016-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>
// cisst
#include <sawIntuitiveResearchKit/mtsTeleOperationECM.h>
#include <cisstCommon/cmnUnits.h>
#include <cisstVector/vctBoundingBox3.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstParameterTypes/prmOperatingState.h>
#include <cisstParameterTypes/prmForceCartesianSet.h>
CMN_IMPLEMENT_SERVICES_DERIVED_ONEARG(mtsTeleOperationECM, mtsTaskPeriodic, mtsTaskPeriodicConstructorArg);
mtsTeleOperationECM::mtsTeleOperationECM(const std::string & componentName, const double periodInSeconds):
mtsTaskPeriodic(componentName, periodInSeconds),
mTeleopState(componentName, "DISABLED")
{
Init();
}
mtsTeleOperationECM::mtsTeleOperationECM(const mtsTaskPeriodicConstructorArg & arg):
mtsTaskPeriodic(arg),
mTeleopState(arg.Name, "DISABLED")
{
Init();
}
mtsTeleOperationECM::~mtsTeleOperationECM()
{
}
void mtsTeleOperationECM::Init(void)
{
// configure state machine
mTeleopState.AddState("SETTING_ARMS_STATE");
mTeleopState.AddState("ENABLED");
mTeleopState.AddAllowedDesiredState("DISABLED");
mTeleopState.AddAllowedDesiredState("ENABLED");
// state change, to convert to string events for users (Qt, ROS)
mTeleopState.SetStateChangedCallback(&mtsTeleOperationECM::StateChanged,
this);
// run for all states
mTeleopState.SetRunCallback(&mtsTeleOperationECM::RunAllStates,
this);
// disabled
mTeleopState.SetTransitionCallback("DISABLED",
&mtsTeleOperationECM::TransitionDisabled,
this);
// setting arms state
mTeleopState.SetEnterCallback("SETTING_ARMS_STATE",
&mtsTeleOperationECM::EnterSettingArmsState,
this);
mTeleopState.SetTransitionCallback("SETTING_ARMS_STATE",
&mtsTeleOperationECM::TransitionSettingArmsState,
this);
// enabled
mTeleopState.SetEnterCallback("ENABLED",
&mtsTeleOperationECM::EnterEnabled,
this);
mTeleopState.SetRunCallback("ENABLED",
&mtsTeleOperationECM::RunEnabled,
this);
mTeleopState.SetTransitionCallback("ENABLED",
&mtsTeleOperationECM::TransitionEnabled,
this);
m_clutched = false;
StateTable.AddData(mMTML.m_measured_cp, "MTML/measured_cp");
StateTable.AddData(mMTMR.m_measured_cp, "MTMR/measured_cp");
StateTable.AddData(mECM.m_measured_cp, "ECM/measured_cp");
mConfigurationStateTable = new mtsStateTable(100, "Configuration");
mConfigurationStateTable->SetAutomaticAdvance(false);
AddStateTable(mConfigurationStateTable);
mConfigurationStateTable->AddData(m_config.scale, "scale");
mConfigurationStateTable->AddData(m_config.camera_roll_offset, "camera_roll_offset");
mtsInterfaceRequired * interfaceRequired = AddInterfaceRequired("MTML");
if (interfaceRequired) {
interfaceRequired->AddFunction("measured_cp",
mMTML.measured_cp);
interfaceRequired->AddFunction("measured_cv",
mMTML.measured_cv);
interfaceRequired->AddFunction("lock_orientation",
mMTML.lock_orientation);
interfaceRequired->AddFunction("body/servo_cf",
mMTML.body_servo_cf);
interfaceRequired->AddFunction("body/set_cf_orientation_absolute",
mMTML.body_set_cf_orientation_absolute);
interfaceRequired->AddFunction("use_gravity_compensation",
mMTML.use_gravity_compensation);
interfaceRequired->AddFunction("operating_state",
mMTML.operating_state);
interfaceRequired->AddFunction("state_command",
mMTML.state_command);
interfaceRequired->AddEventHandlerWrite(&mtsTeleOperationECM::arm_error_event_handler,
this, "error");
}
interfaceRequired = AddInterfaceRequired("MTMR");
if (interfaceRequired) {
interfaceRequired->AddFunction("measured_cp",
mMTMR.measured_cp);
interfaceRequired->AddFunction("measured_cv",
mMTMR.measured_cv);
interfaceRequired->AddFunction("lock_orientation",
mMTMR.lock_orientation);
interfaceRequired->AddFunction("body/servo_cf",
mMTMR.body_servo_cf);
interfaceRequired->AddFunction("body/set_cf_orientation_absolute",
mMTMR.body_set_cf_orientation_absolute);
interfaceRequired->AddFunction("use_gravity_compensation",
mMTMR.use_gravity_compensation);
interfaceRequired->AddEventHandlerWrite(&mtsTeleOperationECM::arm_error_event_handler,
this, "error");
interfaceRequired->AddFunction("operating_state",
mMTMR.operating_state);
interfaceRequired->AddFunction("state_command",
mMTMR.state_command);
}
interfaceRequired = AddInterfaceRequired("ECM");
if (interfaceRequired) {
// ECM, use PID desired position to make sure there is no jump when engaging
interfaceRequired->AddFunction("setpoint_cp",
mECM.measured_cp);
interfaceRequired->AddFunction("setpoint_js",
mECM.setpoint_js);
interfaceRequired->AddFunction("servo_jp",
mECM.servo_jp);
interfaceRequired->AddFunction("operating_state",
mECM.operating_state);
interfaceRequired->AddFunction("state_command",
mECM.state_command);
interfaceRequired->AddEventHandlerWrite(&mtsTeleOperationECM::arm_error_event_handler,
this, "error");
}
// footpedal events
interfaceRequired = AddInterfaceRequired("clutch");
if (interfaceRequired) {
interfaceRequired->AddEventHandlerWrite(&mtsTeleOperationECM::clutch_event_handler, this, "Button");
}
mInterface = AddInterfaceProvided("Setting");
if (mInterface) {
mInterface->AddMessageEvents();
// commands
mInterface->AddCommandReadState(StateTable, StateTable.PeriodStats,
"period_statistics"); // mtsIntervalStatistics
mInterface->AddCommandWrite(&mtsTeleOperationECM::state_command, this,
"state_command", std::string("DISABLED"));
mInterface->AddCommandWrite(&mtsTeleOperationECM::set_scale, this,
"set_scale", 0.5);
mInterface->AddCommandReadState(*mConfigurationStateTable,
m_config.scale,
"scale");
mInterface->AddCommandReadState(StateTable,
mMTML.m_measured_cp,
"MTML/measured_cp");
mInterface->AddCommandReadState(StateTable,
mMTMR.m_measured_cp,
"MTMR/measured_cp");
mInterface->AddCommandReadState(StateTable,
mECM.m_measured_cp,
"ECM/measured_cp");
// events
mInterface->AddEventWrite(MessageEvents.desired_state,
"desired_state", std::string(""));
mInterface->AddEventWrite(MessageEvents.current_state,
"current_state", std::string(""));
mInterface->AddEventWrite(MessageEvents.following,
"following", false);
// configuration
mInterface->AddEventWrite(ConfigurationEvents.scale,
"scale", 0.5);
}
}
void mtsTeleOperationECM::Configure(const std::string & filename)
{
std::ifstream jsonStream;
Json::Value jsonConfig;
Json::Reader jsonReader;
if (filename == "") {
return;
}
jsonStream.open(filename.c_str());
if (!jsonReader.parse(jsonStream, jsonConfig)) {
CMN_LOG_CLASS_INIT_ERROR << "Configure " << this->GetName()
<< ": failed to parse configuration file \""
<< filename << "\"\n"
<< jsonReader.getFormattedErrorMessages();
exit(EXIT_FAILURE);
}
CMN_LOG_CLASS_INIT_VERBOSE << "Configure: " << this->GetName()
<< " using file \"" << filename << "\"" << std::endl
<< "----> content of configuration file: " << std::endl
<< jsonConfig << std::endl
<< "<----" << std::endl;
// base component configuration
mtsComponent::ConfigureJSON(jsonConfig);
// JSON part
mtsTeleOperationECM::Configure(jsonConfig);
}
void mtsTeleOperationECM::Configure(const Json::Value & _json_config)
{
cmnDataDeSerializeTextJSON(m_config, _json_config);
CMN_LOG_CLASS_INIT_VERBOSE << "Configure, loaded:" << std::endl
<< "------>" << std::endl
<< cmnDataSerializeTextJSON(m_config) << std::endl
<< "<------" << std::endl;
if (m_config.scale <= 0.0) {
CMN_LOG_CLASS_INIT_ERROR << "Configure " << this->GetName()
<< ": \"scale\" must be a strictly positive number" << std::endl;
exit(EXIT_FAILURE);
}
}
void mtsTeleOperationECM::Startup(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Startup" << std::endl;
set_scale(m_config.scale);
set_following(false);
}
void mtsTeleOperationECM::Run(void)
{
ProcessQueuedCommands();
ProcessQueuedEvents();
// run based on state
mTeleopState.Run();
}
void mtsTeleOperationECM::Cleanup(void)
{
CMN_LOG_CLASS_INIT_VERBOSE << "Cleanup" << std::endl;
}
void mtsTeleOperationECM::StateChanged(void)
{
const std::string newState = mTeleopState.CurrentState();
MessageEvents.current_state(newState);
mInterface->SendStatus(this->GetName() + ": current state is " + newState);
}
void mtsTeleOperationECM::RunAllStates(void)
{
mtsExecutionResult executionResult;
// get MTML Cartesian position/velocity
executionResult = mMTML.measured_cp(mMTML.m_measured_cp);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "Run: call to MTML.measured_cp failed \""
<< executionResult << "\"" << std::endl;
mInterface->SendError(this->GetName() + ": unable to get cartesian position from MTML");
mTeleopState.SetDesiredState("DISABLED");
}
executionResult = mMTML.measured_cv(mMTML.m_measured_cv);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "Run: call to MTML.measured_cv failed \""
<< executionResult << "\"" << std::endl;
mInterface->SendError(this->GetName() + ": unable to get cartesian velocity from MTML");
mTeleopState.SetDesiredState("DISABLED");
}
// get MTMR Cartesian position
executionResult = mMTMR.measured_cp(mMTMR.m_measured_cp);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "Run: call to MTMR.measured_cp failed \""
<< executionResult << "\"" << std::endl;
mInterface->SendError(this->GetName() + ": unable to get cartesian position from MTMR");
mTeleopState.SetDesiredState("DISABLED");
}
executionResult = mMTMR.measured_cv(mMTMR.m_measured_cv);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "Run: call to MTMR.measured_cv failed \""
<< executionResult << "\"" << std::endl;
mInterface->SendError(this->GetName() + ": unable to get cartesian velocity from MTMR");
mTeleopState.SetDesiredState("DISABLED");
}
// get ECM Cartesian position for GUI
executionResult = mECM.measured_cp(mECM.m_measured_cp);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "Run: call to ECM.measured_cp failed \""
<< executionResult << "\"" << std::endl;
mInterface->SendError(this->GetName() + ": unable to get cartesian position from ECM");
mTeleopState.SetDesiredState("DISABLED");
}
// for motion computation
executionResult = mECM.setpoint_js(mECM.m_setpoint_js);
if (!executionResult.IsOK()) {
CMN_LOG_CLASS_RUN_ERROR << "Run: call to ECM.setpoint_js failed \""
<< executionResult << "\"" << std::endl;
mInterface->SendError(this->GetName() + ": unable to get joint state from ECM");
mTeleopState.SetDesiredState("DISABLED");
}
// check if anyone wanted to disable anyway
if ((mTeleopState.DesiredState() == "DISABLED")
&& (mTeleopState.CurrentState() != "DISABLED")) {
set_following(false);
mTeleopState.SetCurrentState("DISABLED");
return;
}
// monitor state of arms if needed
if ((mTeleopState.CurrentState() != "DISABLED")
&& (mTeleopState.CurrentState() != "SETTING_ARMS_STATE")) {
prmOperatingState state;
mECM.operating_state(state);
if ((state.State() != prmOperatingState::ENABLED)
|| !state.IsHomed()) {
mTeleopState.SetDesiredState("DISABLED");
mInterface->SendError(this->GetName() + ": ECM is not in state \"READY\" anymore");
}
mMTML.operating_state(state);
if ((state.State() != prmOperatingState::ENABLED)
|| !state.IsHomed()) {
mTeleopState.SetDesiredState("DISABLED");
mInterface->SendError(this->GetName() + ": MTML is not in state \"READY\" anymore");
}
mMTMR.operating_state(state);
if ((state.State() != prmOperatingState::ENABLED)
|| !state.IsHomed()) {
mTeleopState.SetDesiredState("DISABLED");
mInterface->SendError(this->GetName() + ": MTMR is not in state \"READY\" anymore");
}
}
}
void mtsTeleOperationECM::TransitionDisabled(void)
{
if (mTeleopState.DesiredState() == "ENABLED") {
mTeleopState.SetCurrentState("SETTING_ARMS_STATE");
}
}
void mtsTeleOperationECM::EnterSettingArmsState(void)
{
// reset timer
mInStateTimer = StateTable.GetTic();
// request state if needed
prmOperatingState state;
mECM.operating_state(state);
if (state.State() != prmOperatingState::ENABLED) {
mECM.state_command(std::string("enable"));
}
if (!state.IsHomed()) {
mECM.state_command(std::string("home"));
}
mMTML.operating_state(state);
if (state.State() != prmOperatingState::ENABLED) {
mMTML.state_command(std::string("enable"));
}
if (!state.IsHomed()) {
mMTML.state_command(std::string("home"));
}
mMTMR.operating_state(state);
if (state.State() != prmOperatingState::ENABLED) {
mMTMR.state_command(std::string("enable"));
}
if (!state.IsHomed()) {
mMTMR.state_command(std::string("home"));
}
}
void mtsTeleOperationECM::TransitionSettingArmsState(void)
{
// check state
prmOperatingState ecmState, mtmlState, mtmrState;
mECM.operating_state(ecmState);
mMTML.operating_state(mtmlState);
mMTMR.operating_state(mtmrState);
if ((ecmState.State() == prmOperatingState::ENABLED) && ecmState.IsHomed()
&& (mtmlState.State() == prmOperatingState::ENABLED) && mtmlState.IsHomed()
&& (mtmrState.State() == prmOperatingState::ENABLED) && mtmrState.IsHomed()) {
// make sure the coordinate systems make sense, i.e. did the
// user set a base-frame that matches ISI coordinate system
// for the console side
vctBoundingBox3 workArea(vct3(-400.0 * cmn_mm,
-200.0 * cmn_mm,
10.0 * cmn_mm),
vct3( 400.0 * cmn_mm,
200.0 * cmn_mm,
800.0 * cmn_mm));
if (!workArea.Includes(mMTML.m_measured_cp.Position().Translation())) {
mInterface->SendError(this->GetName() + ": MTML position doesn't seem to be in the work area. Make sure your \"base-frame\" is set correctly in your console JSON configuration file.");
mTeleopState.SetDesiredState("DISABLED");
return;
}
if (!workArea.Includes(mMTMR.m_measured_cp.Position().Translation())) {
mInterface->SendError(this->GetName() + ": MTMR position doesn't seem to be in the work area. Make sure your \"base-frame\" is set correctly in your console JSON configuration file.");
mTeleopState.SetDesiredState("DISABLED");
return;
}
// we should be good to go
mTeleopState.SetCurrentState("ENABLED");
return;
}
// check timer
if ((StateTable.GetTic() - mInStateTimer) > 60.0 * cmn_s) {
mInterface->SendError(this->GetName() + ": timed out while setting up arms state");
mTeleopState.SetDesiredState("DISABLED");
}
}
void mtsTeleOperationECM::EnterEnabled(void)
{
// set cartesian effort parameters
mMTML.use_gravity_compensation(true);
mMTML.body_set_cf_orientation_absolute(true);
mMTML.lock_orientation(mMTML.m_measured_cp.Position().Rotation());
mMTMR.use_gravity_compensation(true);
mMTMR.body_set_cf_orientation_absolute(true);
mMTMR.lock_orientation(mMTMR.m_measured_cp.Position().Rotation());
// initial state for MTM force feedback
// -1- initial distance between MTMs
vct3 vectorLR;
vectorLR.DifferenceOf(mMTMR.m_measured_cp.Position().Translation(),
mMTML.m_measured_cp.Position().Translation());
// -2- mid-point, aka center of image
mInitial.C.SumOf(mMTMR.m_measured_cp.Position().Translation(),
mMTML.m_measured_cp.Position().Translation());
mInitial.C.Multiply(0.5);
// -3- image up vector
mInitial.Up.CrossProductOf(vectorLR, mInitial.C);
mInitial.Up.NormalizedSelf();
// -4- width of image, depth of arms wrt image plan
vct3 side;
side.CrossProductOf(mInitial.C, mInitial.Up);
side.NormalizedSelf();
mInitial.w = 0.5 * vctDotProduct(side, vectorLR);
mInitial.d = 0.5 * vctDotProduct(mInitial.C.Normalized(), vectorLR);
// projections
mInitial.Lr.Assign(mInitial.C[0], 0, mInitial.C[2]);
mInitial.Lr.NormalizedSelf();
mInitial.Ud.Assign(0, mInitial.C[1], mInitial.C[2]);
mInitial.Ud.NormalizedSelf();
mInitial.Cw.Assign(mInitial.Up[0], mInitial.Up[1], 0);
mInitial.Cw.NormalizedSelf();
mInitial.ECMPositionJoint = mECM.m_setpoint_js.Position();
// -5- store current rotation matrix for MTML, MTMR, and ECM
vctEulerZYXRotation3 eulerAngles;
eulerAngles.Assign(mInitial.ECMPositionJoint[3], mInitial.ECMPositionJoint[0], mInitial.ECMPositionJoint[1]);
vctEulerToMatrixRotation3(eulerAngles, mInitial.ECMRotEuler);
mInitial.MTMLRot = mMTML.m_measured_cp.Position().Rotation();
mInitial.MTMRRot = mMTMR.m_measured_cp.Position().Rotation();
#if 0
std::cerr << CMN_LOG_DETAILS << std::endl
<< "L: " << mMTML.m_measured_cp.Position().Translation() << std::endl
<< "R: " << mMTMR.m_measured_cp.Position().Translation() << std::endl
<< "C: " << mInitial.C << std::endl
<< "Up: " << mInitial.Up << std::endl
<< "w: " << mInitial.w << std::endl
<< "d: " << mInitial.d << std::endl
<< "Si: " << side << std::endl;
#endif
mInitial.ECMPositionJoint = mECM.m_setpoint_js.Position();
// check if by any chance the clutch pedal is pressed
if (m_clutched) {
Clutch(true);
} else {
set_following(true);
}
}
void mtsTeleOperationECM::RunEnabled(void)
{
if (m_clutched) {
return;
}
/* --- Forces on MTMs --- */
const vct3 frictionForceCoeff(-10.0, -10.0, -40.0);
const double distanceForceCoeff = 150.0;
//-1- vector between MTMs
vct3 vectorLR;
vectorLR.DifferenceOf(mMTMR.m_measured_cp.Position().Translation(),
mMTML.m_measured_cp.Position().Translation());
// -2- mid-point, aka center of image
vct3 c;
c.SumOf(mMTMR.m_measured_cp.Position().Translation(),
mMTML.m_measured_cp.Position().Translation());
c.Multiply(0.5);
vct3 directionC = c.Normalized();
// -3- image up vector
vct3 up;
up.CrossProductOf(vectorLR, c);
up.NormalizedSelf();
// -4- Width of image
vct3 side;
side.CrossProductOf(c, up);
side.NormalizedSelf();
// -5- find desired position for L and R
vct3 goalL(c);
goalL.AddProductOf(-mInitial.w, side);
goalL.AddProductOf(-mInitial.d, directionC);
vct3 goalR(c);
goalR.AddProductOf(mInitial.w, side);
goalR.AddProductOf(mInitial.d, directionC);
// compute forces on L and R based on error in position
vct3 forceFriction;
vct3 force;
prmForceCartesianSet wrenchR, wrenchL;
// MTMR
// apply force
force.DifferenceOf(goalR,
mMTMR.m_measured_cp.Position().Translation());
force.Multiply(distanceForceCoeff);
wrenchR.Force().Ref<3>(0).Assign(force);
// add friction force
forceFriction.ElementwiseProductOf(frictionForceCoeff,
mMTMR.m_measured_cv.VelocityLinear());
wrenchR.Force().Ref<3>(0).Add(forceFriction);
// apply
mMTMR.body_servo_cf(wrenchR);
// MTML
// apply force
force.DifferenceOf(goalL,
mMTML.m_measured_cp.Position().Translation());
force.Multiply(distanceForceCoeff);
wrenchL.Force().Ref<3>(0).Assign(force);
// add friction force
forceFriction.ElementwiseProductOf(frictionForceCoeff,
mMTML.m_measured_cv.VelocityLinear());
wrenchL.Force().Ref<3>(0).Add(forceFriction);
// apply
mMTML.body_servo_cf(wrenchL);
/* --- Joint Control --- */
static const vct3 normXZ(0.0, 1.0, 0.0);
static const vct3 normYZ(1.0, 0.0, 0.0);
static const vct3 normXY(0.0, 0.0, 1.0);
// Initial ECM joints
vctVec goalJoints(mInitial.ECMPositionJoint);
// Change in directions and joints
vctVec changeJoints(4);
vctVec changeDir(4);
vct3 crossN; // normal to direction of motion
// - Direction 0 - left/right, movement in the XZ plane
vct3 lr(c[0], 0.0, c[2]);
lr.NormalizedSelf();
if (mInitial.Lr.AlmostEqual(lr)) {
changeDir[0] = 0.0;
} else {
changeDir[0] = -acos(vctDotProduct(mInitial.Lr, lr));
crossN = vctCrossProduct(mInitial.Lr, lr);
if (vctDotProduct(normXZ, crossN) < 0.0) {
changeDir[0] = -changeDir[0];
}
}
// - Direction 1 - up/down, movement in the YZ plane
vct3 ud(0.0, c[1], c[2]);
ud.NormalizedSelf();
if (mInitial.Ud.AlmostEqual(ud)) {
changeDir[1] = 0.0;
} else {
changeDir[1] = acos(vctDotProduct(mInitial.Ud, ud));
crossN = vctCrossProduct(mInitial.Ud, ud);
if (vctDotProduct(normYZ, crossN) < 0.0) {
changeDir[1] = -changeDir[1];
}
}
// - Direction 2 - in/out
changeDir[2] = m_config.scale * (mInitial.C.Norm() - c.Norm());
// - Direction 3 - cc/ccw, movement in the XY plane
vct3 cw(up[0], up[1], 0);
cw.NormalizedSelf();
if (mInitial.Cw.AlmostEqual(cw)) {
changeDir[3] = 0.0;
} else {
changeDir[3] = -acos(vctDotProduct(mInitial.Cw, cw));
crossN = vctCrossProduct(mInitial.Cw, cw);
if (vctDotProduct(normXY, crossN) < 0) {
changeDir[3] = -changeDir[3];
}
}
// adjusting movement for camera orientation (plus offset on camera roll if present between ECM and endoscope)
double totalChangeJoint3 = changeDir[3] + mInitial.ECMPositionJoint[3] + m_config.camera_roll_offset;
changeJoints[0] = changeDir[0] * cos(totalChangeJoint3) - changeDir[1] * sin(totalChangeJoint3);
changeJoints[1] = changeDir[1] * cos(totalChangeJoint3) + changeDir[0] * sin(totalChangeJoint3);
changeJoints[2] = changeDir[2];
changeJoints[3] = changeDir[3];
goalJoints.Add(changeJoints);
mECM.m_servo_jp.Goal().ForceAssign(goalJoints);
mECM.servo_jp(mECM.m_servo_jp);
/* --- Lock Orientation --- */
//Calculate new rotations of MTMs
vctMatRot3 currMTMLRot;
vctMatRot3 currMTMRRot;
// Current ECM Rotation
vctEulerZYXRotation3 finalEulerAngles;
vctMatrixRotation3<double> currECMRot;
vctMatrixRotation3<double> finalECMRot;
finalEulerAngles.Assign(goalJoints[3], goalJoints[0], goalJoints[1]);
vctEulerToMatrixRotation3(finalEulerAngles, finalECMRot);
currECMRot = finalECMRot * mInitial.ECMRotEuler.Inverse();
// Set MTM Orientation
currMTMLRot = currECMRot.Inverse() * mInitial.MTMLRot;
currMTMRRot = currECMRot.Inverse() * mInitial.MTMRRot;
// set cartesian effort parameters
mMTML.body_set_cf_orientation_absolute(true);
mMTML.lock_orientation(currMTMLRot);
mMTMR.body_set_cf_orientation_absolute(true);
mMTMR.lock_orientation(currMTMRRot);
}
void mtsTeleOperationECM::TransitionEnabled(void)
{
if (mTeleopState.DesiredStateIsNotCurrent()) {
set_following(false);
mTeleopState.SetCurrentState(mTeleopState.DesiredState());
}
}
void mtsTeleOperationECM::set_following(const bool following)
{
MessageEvents.following(following);
m_following = following;
}
void mtsTeleOperationECM::arm_error_event_handler(const mtsMessage & message)
{
mTeleopState.SetDesiredState("DISABLED");
mInterface->SendError(this->GetName() + ": received from arm [" + message.Message + "]");
}
void mtsTeleOperationECM::clutch_event_handler(const prmEventButton & button)
{
switch (button.Type()) {
case prmEventButton::PRESSED:
m_clutched = true;
break;
case prmEventButton::RELEASED:
m_clutched = false;
break;
default:
break;
}
// if the teleoperation is activated
if (mTeleopState.DesiredState() == "ENABLED") {
Clutch(m_clutched);
}
}
void mtsTeleOperationECM::Clutch(const bool & clutch)
{
// if the teleoperation is activated
if (clutch) {
set_following(false);
mInterface->SendStatus(this->GetName() + ": console clutch pressed");
// set MTMs in effort mode, no force applied but gravity and locked orientation
prmForceCartesianSet wrench;
mMTML.body_servo_cf(wrench);
mMTML.use_gravity_compensation(true);
mMTML.lock_orientation(mMTML.m_measured_cp.Position().Rotation());
mMTMR.body_servo_cf(wrench);
mMTMR.use_gravity_compensation(true);
mMTMR.lock_orientation(mMTMR.m_measured_cp.Position().Rotation());
} else {
m_clutched = false;
mInterface->SendStatus(this->GetName() + ": console clutch released");
mTeleopState.SetCurrentState("SETTING_ARMS_STATE");
}
}
void mtsTeleOperationECM::state_command(const std::string & command)
{
if (command == "enable") {
SetDesiredState("ENABLED");
return;
}
if (command == "disable") {
SetDesiredState("DISABLED");
return;
}
mInterface->SendWarning(this->GetName() + ": " + command + " doesn't seem to be a valid state_command");
}
void mtsTeleOperationECM::SetDesiredState(const std::string & state)
{
// try to find the state in state machine
if (!mTeleopState.StateExists(state)) {
mInterface->SendError(this->GetName() + ": unsupported state " + state);
return;
}
// return is already the desired state
if (mTeleopState.DesiredState() == state) {
MessageEvents.desired_state(state);
return;
}
// try to set the desired state
try {
mTeleopState.SetDesiredState(state);
} catch (...) {
mInterface->SendError(this->GetName() + ": " + state + " is not an allowed desired state");
return;
}
// messages and events
MessageEvents.desired_state(state);
mInterface->SendStatus(this->GetName() + ": set desired state to " + state);
}
void mtsTeleOperationECM::set_scale(const double & scale)
{
mConfigurationStateTable->Start();
m_config.scale = scale;
mConfigurationStateTable->Advance();
ConfigurationEvents.scale(m_config.scale);
}
|