File size: 20,854 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 | /* -*- 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, Zihan Chen
Created on: 2013-05-15
(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
#include <iostream>
#include <time.h>
// cisst
#include <cisstCommon/cmnPath.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstParameterTypes/prmEventButton.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKitECM.h>
#include <sawIntuitiveResearchKit/robManipulatorECM.h>
// ECM-specific GC - accesses ECM's Manipulator so knows current endoscope mass
class GravityCompensationECM : public robGravityCompensation {
public:
bool configure(std::string physical_dh_file);
std::string error(void);
void set_endoscope_mass(double mass);
vctVec compute(const prmStateJoint& state, vct3 gravity) override;
private:
robManipulator physical_model;
std::string error_message;
};
bool GravityCompensationECM::configure(std::string physical_dh_file)
{
robManipulator::Errno err = physical_model.LoadRobot(physical_dh_file);
if (err != robManipulator::ESUCCESS) {
error_message = physical_model.mLastError;
return false;
}
// make sure we have expected number of links so we know which link
// to put the variable endoscope/camera mass
if (physical_model.links.size() == 6) {
return true; // 6 links, ECM Si physical DH
} else if (physical_model.links.size() == 4) {
return true; // 4 links, ECM Classic virtual DH used as physical DH
} else {
error_message = "ECM GC kinematics does not match Classic or Si number of links";
return false; // unknown physical DH
}
}
std::string GravityCompensationECM::error(void)
{
return error_message;
}
void GravityCompensationECM::set_endoscope_mass(double mass)
{
if (physical_model.links.size() == 6) {
physical_model.links.at(5).MassData().Mass() = mass;
} else {
physical_model.links.at(2).MassData().Mass() = mass;
}
}
vctVec GravityCompensationECM::compute(const prmStateJoint& state, vct3 gravity)
{
size_t n_joints = physical_model.links.size();
auto j = state.Position();
vctDoubleVec qd(n_joints, 0.0);
vctDoubleVec efforts(state.Position().size(), 0.0);
if (n_joints == 6) {
// convert virtual joint positions to physical
vctDoubleVec q(6, j[0], 0.0, j[1], -j[1], j[1], j[2]);
vctDoubleVec predicted_efforts = physical_model.CCG_MDH(q, qd, gravity);
efforts[0] = predicted_efforts[0];
efforts[1] = predicted_efforts[2] - predicted_efforts[3] + predicted_efforts[4];
efforts[2] = predicted_efforts[5];
} else if (n_joints == 4) {
vctDoubleVec q(4, j[0], j[1], j[2], 0.0);
efforts = physical_model.CCG_MDH(j, qd, gravity);
}
return efforts;
}
CMN_IMPLEMENT_SERVICES_DERIVED_ONEARG(mtsIntuitiveResearchKitECM, mtsTaskPeriodic, mtsTaskPeriodicConstructorArg);
mtsIntuitiveResearchKitECM::mtsIntuitiveResearchKitECM(const std::string & componentName, const double periodInSeconds):
mtsIntuitiveResearchKitArm(componentName, periodInSeconds)
{
Init();
}
mtsIntuitiveResearchKitECM::mtsIntuitiveResearchKitECM(const mtsTaskPeriodicConstructorArg & arg):
mtsIntuitiveResearchKitArm(arg)
{
Init();
}
// need to define destructor after definition of GravityCompensationECM is available
mtsIntuitiveResearchKitECM::~mtsIntuitiveResearchKitECM() = default;
void mtsIntuitiveResearchKitECM::set_simulated(void)
{
mtsIntuitiveResearchKitArm::set_simulated();
// in simulation mode, we don't need clutch IO
RemoveInterfaceRequired("arm_clutch");
// for Si systems, remove a few more interfaces
if (m_generation == dvrk::generation::Si) {
RemoveInterfaceRequired("SUJ_clutch");
RemoveInterfaceRequired("SUJ_clutch_2");
RemoveInterfaceRequired("SUJ_brake");
}
}
void mtsIntuitiveResearchKitECM::set_generation(const dvrk::generation generation)
{
mtsIntuitiveResearchKitArm::set_generation(generation);
// for S/si, add SUJClutch interface
if ((generation == dvrk::generation::Si)
&& !m_simulated) {
auto interfaceRequired = AddInterfaceRequired("SUJ_clutch");
if (interfaceRequired) {
interfaceRequired->AddEventHandlerWrite(&mtsIntuitiveResearchKitECM::EventHandlerSUJClutch, this, "Button");
}
interfaceRequired = AddInterfaceRequired("SUJ_clutch_2");
if (interfaceRequired) {
interfaceRequired->AddEventHandlerWrite(&mtsIntuitiveResearchKitECM::EventHandlerSUJClutch, this, "Button");
}
interfaceRequired = AddInterfaceRequired("SUJ_brake");
if (interfaceRequired) {
interfaceRequired->AddFunction("SetValue", SUJClutch.Brake);
}
} else {
if (GetInterfaceProvided("SUJ_clutch")) {
RemoveInterfaceRequired("SUJ_clutch");
}
}
}
void mtsIntuitiveResearchKitECM::PostConfigure(const Json::Value & jsonConfig,
const cmnPath & CMN_UNUSED(configPath),
const std::string & filename)
{
// load tool tip transform if any (for up/down endoscopes)
// future work: add UP/DOWN, _HD and set mass for GC, also create separate method with ROS topic + GUI to set the endoscope
const Json::Value jsonEndoscope = jsonConfig["endoscope"];
if (!jsonEndoscope.isNull()) {
std::string endoscope = jsonEndoscope.asString();
set_endoscope_type(endoscope);
} else {
CMN_LOG_CLASS_INIT_ERROR << "Configure " << this->GetName()
<< ": \"endoscope\" must be defined (from file \""
<< filename << "\")" << std::endl;
exit(EXIT_FAILURE);
}
if (!m_endoscope_configured) {
exit(EXIT_FAILURE);
}
// ask to set pitch if not already defined
if (m_mounting_pitch > std::numeric_limits<double>::max()) {
if (generation() == dvrk::generation::Classic) {
m_mounting_pitch = -45.0 * cmnPI_180;
} else {
m_mounting_pitch = -70.0 * cmnPI_180;
}
CMN_LOG_CLASS_INIT_ERROR << "Configure: " << this->GetName() << std::endl
<< "\"mounting_pitch\" was not defined in \"" << filename << "\"" << std::endl
<< "If your ECM is mounted on the SUJ you should add it using: " << std::endl
<< " \"mounting_pitch\": " << std::to_string(m_mounting_pitch)
<< " // " << std::to_string(m_mounting_pitch * cmn180_PI) << " degrees" << std::endl;
exit(EXIT_FAILURE);
}
}
void mtsIntuitiveResearchKitECM::ConfigureGC(const Json::Value & jsonConfig,
const cmnPath & configPath,
const std::string & filename)
{
std::string physical_dh_name;
const auto jsonPhysicalDH = jsonConfig["kinematic_gc"];
if (!jsonPhysicalDH.isNull()) {
physical_dh_name = jsonPhysicalDH.asString();
} else if (m_generation == dvrk::generation::Si) {
CMN_LOG_CLASS_INIT_VERBOSE << "Configure" << GetName() << ": no GC kinematics specified, using default for ECM Si" << std::endl;
physical_dh_name = "kinematic/ECM_Si_physical.json";
} else if (m_generation == dvrk::generation::Classic) {
CMN_LOG_CLASS_INIT_VERBOSE << "Configure" << GetName() << ": no GC kinematics specified, using default for ECM Classic" << std::endl;
physical_dh_name = "kinematic/ECM.json";
} else {
CMN_LOG_CLASS_INIT_VERBOSE << "Configure" << GetName() << ": no GC kinematics specified, so gravity compensation is not available" << std::endl;
return;
}
const auto physical_dh = configPath.Find(physical_dh_name);
if (physical_dh == "") {
CMN_LOG_CLASS_INIT_ERROR << "Configure: " << this->GetName()
<< " using file \"" << filename << "\", can't find GC kinematics file \""
<< physical_dh << "\"" << std::endl;
exit(EXIT_FAILURE);
}
m_gc = std::make_unique<GravityCompensationECM>();
bool ok = m_gc->configure(physical_dh);
if (ok) {
m_rob_gravity_compensation = m_gc.get();
} else {
CMN_LOG_CLASS_INIT_ERROR << "ConfigureGC: " << this->GetName()
<< " using GC kinematics file \"" << physical_dh << "\", got error \""
<< m_gc->error() << "\"" << std::endl;
exit(EXIT_FAILURE);
}
}
robManipulator::Errno mtsIntuitiveResearchKitECM::InverseKinematics(vctDoubleVec & jointSet,
const vctFrm4x4 & cartesianGoal) const
{
// solve IK
if (Manipulator->InverseKinematics(jointSet, cartesianGoal) == robManipulator::ESUCCESS) {
// find closest solution mod 2 pi
const double difference = m_kin_measured_js.Position()[3] - jointSet[3];
const double differenceInTurns = nearbyint(difference / (2.0 * cmnPI));
jointSet[3] = jointSet[3] + differenceInTurns * 2.0 * cmnPI;
// make sure we are away from RCM point, this test is
// simplistic
if (jointSet[2] < 40.0 * cmn_mm) {
jointSet[2] = 40.0 * cmn_mm;
}
#if 0
vctFrm4x4 forward = Manipulator->ForwardKinematics(jointSet);
vctDouble3 diff;
diff.DifferenceOf(forward.Translation(), cartesianGoal.Translation());
std::cerr << cmnInternalTo_mm(diff.Norm()) << "mm ";
#endif
return robManipulator::ESUCCESS;
}
return robManipulator::EFAILURE;
}
void mtsIntuitiveResearchKitECM::CreateManipulator(void)
{
if (Manipulator) {
delete Manipulator;
}
Manipulator = new robManipulatorECM();
}
void mtsIntuitiveResearchKitECM::Init(void)
{
// main initialization from base type
mtsIntuitiveResearchKitArm::Init();
ToolOffset = 0;
// state machine specific to ECM, see base class for other states
mArmState.AddState("MANUAL");
// after arm homed
mArmState.SetEnterCallback("HOMED",
&mtsIntuitiveResearchKitECM::EnterHomed,
this);
mArmState.SetEnterCallback("MANUAL",
&mtsIntuitiveResearchKitECM::EnterManual,
this);
mArmState.SetRunCallback("MANUAL",
&mtsIntuitiveResearchKitECM::RunManual,
this);
mArmState.SetLeaveCallback("MANUAL",
&mtsIntuitiveResearchKitECM::LeaveManual,
this);
// initialize trajectory data
m_trajectory_j.v_max.Assign(30.0 * cmnPI_180, // degrees per second
30.0 * cmnPI_180,
60.0 * cmn_mm, // mm per second
30.0 * cmnPI_180);
m_trajectory_j.a_max.Assign(90.0 * cmnPI_180,
90.0 * cmnPI_180,
60.0 * cmn_mm,
90.0 * cmnPI_180);
m_trajectory_j.goal_tolerance.SetAll(3.0 * cmnPI / 180.0); // hard coded to 3 degrees
mtsInterfaceRequired * interfaceRequired;
// Main interface should have been created by base class init
CMN_ASSERT(m_arm_interface);
m_arm_interface->AddEventWrite(ClutchEvents.ManipClutch, "arm_clutch", prmEventButton());
// endoscope commands and events
m_arm_interface->AddCommandWrite(&mtsIntuitiveResearchKitECM::set_endoscope_type, this, "set_endoscope_type");
m_arm_interface->AddEventWrite(EndoscopeEvents.endoscope_type, "endoscope_type", std::string());
// ManipClutch: digital input button event from ECM
interfaceRequired = AddInterfaceRequired("arm_clutch");
if (interfaceRequired) {
interfaceRequired->AddEventHandlerWrite(&mtsIntuitiveResearchKitECM::EventHandlerManipClutch, this, "Button");
}
}
bool mtsIntuitiveResearchKitECM::is_homed(void) const
{
return m_powered && m_encoders_biased_from_pots;
}
void mtsIntuitiveResearchKitECM::unhome(void)
{
m_encoders_biased_from_pots = false;
}
bool mtsIntuitiveResearchKitECM::is_joint_ready(void) const
{
return m_powered && m_encoders_biased_from_pots;
}
bool mtsIntuitiveResearchKitECM::is_cartesian_ready(void) const
{
return m_powered && m_encoders_biased_from_pots;
}
void mtsIntuitiveResearchKitECM::SetGoalHomingArm(void)
{
// if simulated, start at zero but insert endoscope so it can be used in cartesian mode
if (m_simulated) {
m_trajectory_j.goal.SetAll(0.0);
m_trajectory_j.goal.at(2) = 12.0 * cmn_cm;
return;
}
// make sure tracking error is set
PID.enable_measured_setpoint_check(true);
// compute joint goal position
m_trajectory_j.goal.SetSize(number_of_joints());
if (m_homing_goes_to_zero) {
// move to zero position
m_trajectory_j.goal.SetAll(0.0);
} else {
// stay at current position by default
m_trajectory_j.goal.Assign(m_pid_setpoint_js.Position(), number_of_joints());
}
}
void mtsIntuitiveResearchKitECM::EnterHomed(void)
{
mtsIntuitiveResearchKitArm::EnterHomed();
// event to propagate endoscope type based on configuration file
EndoscopeEvents.endoscope_type(mtsIntuitiveResearchKitEndoscopeTypes::TypeToString(m_endoscope_type));
}
void mtsIntuitiveResearchKitECM::EnterManual(void)
{
UpdateOperatingStateAndBusy(prmOperatingState::ENABLED, true);
free();
}
void mtsIntuitiveResearchKitECM::RunManual(void)
{
if (mControlCallback) {
mControlCallback->Execute();
}
}
void mtsIntuitiveResearchKitECM::LeaveManual(void)
{
UpdateOperatingStateAndBusy(prmOperatingState::ENABLED, false);
hold();
}
void mtsIntuitiveResearchKitECM::EventHandlerTrackingError(void)
{
m_arm_interface->SendError(this->GetName() + ": PID tracking error");
SetDesiredState("FAULT");
}
void mtsIntuitiveResearchKitECM::EventHandlerManipClutch(const prmEventButton & button)
{
// Pass events
ClutchEvents.ManipClutch(button);
// Start manual mode but save the previous state
switch (button.Type()) {
case prmEventButton::PRESSED:
if (ArmIsReady("Clutch", mtsIntuitiveResearchKitControlTypes::JOINT_SPACE)) {
ClutchEvents.ManipClutchPreviousState = mArmState.CurrentState();
mArmState.SetCurrentState("MANUAL");
set_LED_pattern(mtsIntuitiveResearchKit::Blue200,
mtsIntuitiveResearchKit::Green200,
true, false);
} else {
m_arm_interface->SendWarning(this->GetName() + ": arm not ready yet, manipulator clutch ignored");
}
break;
case prmEventButton::RELEASED:
if (mArmState.CurrentState() == "MANUAL") {
set_LED_pattern(mtsIntuitiveResearchKit::Green200,
mtsIntuitiveResearchKit::Green200,
false, false);
// go back to state before clutching
mArmState.SetCurrentState(ClutchEvents.ManipClutchPreviousState);
}
break;
default:
break;
}
}
void mtsIntuitiveResearchKitECM::EventHandlerSUJClutch(const prmEventButton & button)
{
bool value = (button.Type() == prmEventButton::PRESSED);
if (value
&& (m_operating_state.State() != prmOperatingState::ENABLED)) {
m_arm_interface->SendWarning(this->GetName() + ": arm needs to be enabled to release the SUJ brakes");
} else {
SUJClutch.Brake(value);
}
}
void mtsIntuitiveResearchKitECM::set_endoscope_type(const std::string & endoscopeType)
{
// initialize configured flag
m_endoscope_configured = false;
m_arm_interface->SendStatus(this->GetName() + ": setting up for endoscope type \"" + endoscopeType + "\"");
// check if the endoscope is in the supported list
auto found =
std::find(mtsIntuitiveResearchKitEndoscopeTypes::TypeVectorString().begin(),
mtsIntuitiveResearchKitEndoscopeTypes::TypeVectorString().end(),
endoscopeType);
if (found == mtsIntuitiveResearchKitEndoscopeTypes::TypeVectorString().end()) {
m_arm_interface->SendError(this->GetName() + ": endoscope type \"" + endoscopeType + "\" is not supported");
EndoscopeEvents.endoscope_type(std::string("ERROR"));
return;
}
// supported endoscopes
m_endoscope_type = mtsIntuitiveResearchKitEndoscopeTypes::TypeFromString(endoscopeType);
// update tool tip offset
switch (m_endoscope_type) {
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_STRAIGHT:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_DOWN:
ToolOffsetTransformation.Assign( 0.0, 1.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.18,
0.0, 0.0, 0.0, 1.0);
break;
default:
ToolOffsetTransformation.Assign( 0.0, 1.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
break;
}
vctFrm4x4 tip;
tip.Translation().Assign(vct3(0.0, 0.0, 0.0));
switch (m_endoscope_type) {
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_SD_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_HD_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Si_HD_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_UP:
// -30 degree rotation along X axis
tip.Rotation().From(vctAxAnRot3(vct3(1.0, 0.0, 0.0), -30.0 * cmnPI_180));
ToolOffsetTransformation = ToolOffsetTransformation * tip;
break;
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_SD_DOWN:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_HD_DOWN:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Si_HD_DOWN:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_DOWN:
// 30 degree rotation along X axis
tip.Rotation().From(vctAxAnRot3(vct3(1.0, 0.0, 0.0), 30.0 * cmnPI_180));
ToolOffsetTransformation = ToolOffsetTransformation * tip;
break;
default:
break;
}
// remove old tip and replace by new one
Manipulator->DeleteTools();
ToolOffset = new robManipulator(ToolOffsetTransformation);
Manipulator->Attach(ToolOffset);
// update estimated mass for gravity compensation
double mass;
switch (m_endoscope_type) {
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_SD_STRAIGHT:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_SD_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_SD_DOWN:
mass = mtsIntuitiveResearchKit::ECM::ClassicSDMass;
break;
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_HD_STRAIGHT:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_HD_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Classic_HD_DOWN:
mass = mtsIntuitiveResearchKit::ECM::ClassicHDMass;
break;
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Si_HD_STRAIGHT:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Si_HD_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Si_HD_DOWN:
mass = mtsIntuitiveResearchKit::ECM::SiHDMass;
break;
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_STRAIGHT:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_UP:
case mtsIntuitiveResearchKitEndoscopeTypes::SCOPE_Custom_DOWN:
mass = mtsIntuitiveResearchKit::ECM::CustomMass;
break;
default:
mass = mtsIntuitiveResearchKit::ECM::EmptyMass;
break;
}
m_gc->set_endoscope_mass(mass);
// set configured flag
m_endoscope_configured = true;
// event to inform other components (GUI/ROS)
EndoscopeEvents.endoscope_type(endoscopeType);
}
|