File size: 21,847 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 | /* -*- 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: 2023-06-16
(C) Copyright 2023-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 <sawIntuitiveResearchKit/mtsIntuitiveResearchKitSUJFixed.h>
#include <sawIntuitiveResearchKit/mtsIntuitiveResearchKit.h>
#include <cisstMultiTask/mtsInterfaceProvided.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstParameterTypes/prmPositionCartesianGet.h>
#include <cisstParameterTypes/prmPositionCartesianSet.h>
#include <cisstParameterTypes/prmStateJoint.h>
#include <cisstParameterTypes/prmConfigurationJoint.h>
CMN_IMPLEMENT_SERVICES_DERIVED_ONEARG(mtsIntuitiveResearchKitSUJFixed, mtsTaskPeriodic, mtsTaskPeriodicConstructorArg);
class mtsIntuitiveResearchKitSUJFixedArmData
{
public:
typedef enum {SUJ_UNDEFINED, SUJ_PSM, SUJ_ECM, SUJ_MOTORIZED_PSM} SujType;
inline mtsIntuitiveResearchKitSUJFixedArmData(const std::string & name,
mtsInterfaceProvided * interface_provided,
mtsInterfaceRequired * interface_required):
m_name(name),
m_state_table(500, name)
{
m_measured_cp.SetReferenceFrame("Cart");
m_measured_cp.SetMovingFrame(name + "_base");
m_state_table.AddData(m_measured_cp, "measured_cp");
m_local_measured_cp.SetReferenceFrame("Cart");
m_local_measured_cp.SetMovingFrame(name + "_base");
m_state_table.AddData(m_local_measured_cp, "local/measured_cp");
CMN_ASSERT(interface_provided);
m_interface = interface_provided;
// set position
m_interface->AddCommandWrite(&mtsIntuitiveResearchKitSUJFixedArmData::local_servo_cp,
this, "local/servo_cp");
m_interface->AddCommandReadState(m_state_table, m_measured_cp,
"measured_cp");
m_interface->AddCommandReadState(m_state_table, m_local_measured_cp,
"local/measured_cp");
// dummy joint stuff for GUI
m_interface->AddCommandRead(&mtsIntuitiveResearchKitSUJFixedArmData::measured_js,
this, "measured_js");
m_interface->AddCommandRead(&mtsIntuitiveResearchKitSUJFixedArmData::configuration_js,
this, "configuration_js");
// Events
m_interface->AddEventWrite(state_events.operating_state, "operating_state", prmOperatingState());
m_interface->AddMessageEvents();
// Stats
m_interface->AddCommandReadState(m_state_table, m_state_table.PeriodStats,
"period_statistics");
CMN_ASSERT(interface_required);
m_interface_required = interface_required;
m_interface_required->AddFunction("set_base_frame", m_arm_set_base_frame);
m_interface_required->AddFunction("local/measured_cp", m_get_local_measured_cp);
}
inline void local_servo_cp(const prmPositionCartesianSet & cp) {
m_local_measured_cp.Position().Assign(cp.Goal());
m_local_measured_cp.Timestamp() = cp.Timestamp();
m_local_measured_cp.Valid() = cp.Valid();
}
inline void measured_js(prmStateJoint & js) const {
js = prmStateJoint();
}
inline void configuration_js(prmConfigurationJoint & jc) const {
jc = prmConfigurationJoint();
}
// name of this SUJ arm (ECM, PSM1, ...)
std::string m_name;
// interfaces
mtsInterfaceProvided * m_interface = nullptr;
mtsInterfaceRequired * m_interface_required = nullptr;
// state of this SUJ arm
mtsStateTable m_state_table; // for positions, fairly slow, i.e 12 * delay for a2d
prmPositionCartesianGet m_measured_cp;
prmPositionCartesianGet m_local_measured_cp;
mtsFunctionWrite m_arm_set_base_frame;
// for reference arm only, get current position
mtsFunctionRead m_get_local_measured_cp;
struct {
mtsFunctionWrite current_state;
mtsFunctionWrite desired_state;
mtsFunctionWrite operating_state;
} state_events;
};
mtsIntuitiveResearchKitSUJFixed::mtsIntuitiveResearchKitSUJFixed(const std::string & componentName, const double periodInSeconds):
mtsTaskPeriodic(componentName, periodInSeconds),
m_state_machine(componentName, "DISABLED")
{
init();
}
mtsIntuitiveResearchKitSUJFixed::mtsIntuitiveResearchKitSUJFixed(const mtsTaskPeriodicConstructorArg & arg):
mtsTaskPeriodic(arg),
m_state_machine(arg.Name, "DISABLED")
{
init();
}
void mtsIntuitiveResearchKitSUJFixed::init(void)
{
// initialize arm pointers
m_sarms.SetAll(nullptr);
// configure state machine common to all arms (ECM/MTM/PSM)
// possible states
m_state_machine.AddState("ENABLED");
// possible desired states
m_state_machine.AddAllowedDesiredState("DISABLED");
m_state_machine.AddAllowedDesiredState("ENABLED");
// state change, to convert to string events for users (Qt, ROS)
m_state_machine.SetStateChangedCallback(&mtsIntuitiveResearchKitSUJFixed::state_changed,
this);
// run for all states
m_state_machine.SetRunCallback(&mtsIntuitiveResearchKitSUJFixed::run_all_states,
this);
// disabled
m_state_machine.SetEnterCallback("DISABLED",
&mtsIntuitiveResearchKitSUJFixed::enter_DISABLED,
this);
// enabled
m_state_machine.SetEnterCallback("ENABLED",
&mtsIntuitiveResearchKitSUJFixed::enter_ENABLED,
this);
m_operating_state.SetValid(true);
m_operating_state.SetState(prmOperatingState::DISABLED);
m_operating_state.SetIsHomed(true);
StateTable.AddData(m_operating_state, "operating_state");
m_interface = AddInterfaceProvided("Arm");
if (m_interface) {
m_interface->AddCommandWrite(&mtsIntuitiveResearchKitSUJFixed::set_reference_arm,
this, "set_reference_arm", std::string(""));
// Arm State
m_interface->AddCommandWrite(&mtsIntuitiveResearchKitSUJFixed::state_command,
this, "state_command", std::string(""));
m_interface->AddCommandReadState(StateTable,
m_operating_state, "operating_state");
// Events
m_interface->AddMessageEvents();
m_interface->AddEventWrite(state_events.operating_state, "operating_state", prmOperatingState());
// Stats
m_interface->AddCommandReadState(StateTable, StateTable.PeriodStats,
"period_statistics");
}
}
void mtsIntuitiveResearchKitSUJFixed::Configure(const std::string & filename)
{
std::ifstream jsonStream;
jsonStream.open(filename.c_str());
Json::Value jsonConfig;
Json::Reader jsonReader;
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);
// get the reference arm if defined. By default ECM and should be
// used only and only if user want to use a PSM to hold a camera
std::string reference_sarm_name = "ECM";
const Json::Value jsonReferenceArm = jsonConfig["reference-arm"];
if (!jsonReferenceArm.isNull()) {
reference_sarm_name = jsonReferenceArm.asString();
CMN_LOG_CLASS_INIT_WARNING << "Configure: \"reference-arm\" is user defined. This should only happen if you are using a PSM to hold a camera. Most users shouldn't define \"reference-arm\". If undefined, all arm cartesian positions will be defined with respect to the ECM" << std::endl;
}
// find all arms, there should be 4 of them
const Json::Value jsonArms = jsonConfig["arms"];
if (jsonArms.size() != 4) {
CMN_LOG_CLASS_INIT_ERROR << "Configure: failed to find 4 SUJ arms" << std::endl;
exit(EXIT_FAILURE);
}
mtsIntuitiveResearchKitSUJFixedArmData * sarm;
for (unsigned int index = 0; index < jsonArms.size(); ++index) {
// name
Json::Value jsonArm = jsonArms[index];
std::string name = jsonArm["name"].asString();
if (!((name == "ECM") || (name == "PSM1") || (name == "PSM2") || (name == "PSM3"))) {
CMN_LOG_CLASS_INIT_ERROR << "Configure: incorrect arm name for SUJ \""
<< name << "\", must be one of \"PSM1\", \"PSM2\", \"PSM3\" or \"ECM\""
<< std::endl;
exit(EXIT_FAILURE);
}
// add interfaces, one is provided so users can find the SUJ
// info, the other is required to the SUJ can get position of
// ECM and change base frame on attached arms
mtsInterfaceProvided * interfaceProvided = this->AddInterfaceProvided(name);
mtsInterfaceRequired * interfaceRequired = this->AddInterfaceRequired(name, MTS_OPTIONAL);
sarm = new mtsIntuitiveResearchKitSUJFixedArmData(name,
interfaceProvided,
interfaceRequired);
m_sarms[index] = sarm;
AddStateTable(&(sarm->m_state_table));
// save which arm is the Reference Arm
if (name == reference_sarm_name) {
m_reference_arm_index = index;
}
// Arm State so GUI widget for each arm can set/get state
interfaceProvided->AddCommandWrite(&mtsIntuitiveResearchKitSUJFixed::state_command,
this, "state_command", std::string(""));
// look for hard coded position if available - users can always push new joint values using ROS
Json::Value json_cp = jsonArm["measured_cp"];
if (!json_cp.empty()) {
vctFrm4x4 transform;
try {
cmnDataJSON<vctFrm4x4>::DeSerializeText(transform, json_cp);
} catch (std::exception & e) {
CMN_LOG_CLASS_INIT_ERROR << "Configure " << this->GetName()
<< ": caught exception \"" << e.what() << " for "
<< name << " measured_cp" << std::endl;
exit(EXIT_FAILURE);
}
sarm->m_local_measured_cp.Position().From(transform);
sarm->m_local_measured_cp.SetValid(true);
} else {
CMN_LOG_CLASS_INIT_WARNING << "Configure: failed to load \"measured_cp\" for \""
<< name << "\"" << std::endl;
sarm->m_local_measured_cp.SetValid(false);
}
}
}
void mtsIntuitiveResearchKitSUJFixed::update_operating_state_and_busy(const prmOperatingState::StateType & state,
const bool isBusy)
{
m_operating_state.State() = state;
m_operating_state.IsBusy() = isBusy;
dispatch_operating_state();
}
void mtsIntuitiveResearchKitSUJFixed::state_changed(void)
{
dispatch_status(this->GetName() + ": current state " + m_state_machine.CurrentState());
dispatch_operating_state();
}
void mtsIntuitiveResearchKitSUJFixed::run_all_states(void)
{
// states are fairly meaningless so just change to desired state
// is different
if (m_state_machine.DesiredStateIsNotCurrent()) {
m_state_machine.SetCurrentState(m_state_machine.DesiredState());
}
// get robot data, i.e. process mux/pots
get_robot_data();
// update all forward kinematics
update_forward_kinematics();
}
void mtsIntuitiveResearchKitSUJFixed::enter_DISABLED(void)
{
update_operating_state_and_busy(prmOperatingState::DISABLED, false);
}
void mtsIntuitiveResearchKitSUJFixed::enter_ENABLED(void)
{
update_operating_state_and_busy(prmOperatingState::ENABLED, false);
}
void mtsIntuitiveResearchKitSUJFixed::Startup(void)
{
set_desired_state("DISABLED");
}
void mtsIntuitiveResearchKitSUJFixed::Run(void)
{
// collect data from required interfaces
ProcessQueuedEvents();
try {
m_state_machine.Run();
} catch (std::exception & e) {
dispatch_error(this->GetName() + ": in state " + m_state_machine.CurrentState()
+ ", caught exception \"" + e.what() + "\"");
set_desired_state("DISABLED");
}
// trigger ExecOut event
RunEvent();
ProcessQueuedCommands();
}
void mtsIntuitiveResearchKitSUJFixed::Cleanup(void)
{
}
void mtsIntuitiveResearchKitSUJFixed::set_simulated(void)
{
dispatch_warning(this->GetName() + ": simulated mode doesn't make sense on fixed SUJ");
}
void mtsIntuitiveResearchKitSUJFixed::get_robot_data(void)
{
// for fixed arm, no data from robot
}
void mtsIntuitiveResearchKitSUJFixed::update_forward_kinematics(void)
{
// find the reference arm (usually ECM)
prmPositionCartesianGet reference_arm_local_cp;
prmPositionCartesianGet reference_arm_to_cart_cp;
auto * reference_sarm = m_sarms[m_reference_arm_index];
if (! (reference_sarm->m_get_local_measured_cp(reference_arm_local_cp))) {
// interface not connected, reporting wrt cart
reference_arm_to_cart_cp.Position().Assign(vctFrm3::Identity());
reference_arm_to_cart_cp.SetValid(true);
reference_arm_to_cart_cp.SetReferenceFrame("Cart");
} else {
// get position from reference arm and convert to useful type
vctFrm3 cart_to_reference_arm_cp = reference_sarm->m_local_measured_cp.Position() * reference_arm_local_cp.Position();
// compute and send new base frame for all SUJs (SUJ will handle BaseFrameArm differently)
reference_arm_to_cart_cp.Position().From(cart_to_reference_arm_cp.Inverse());
// it's an inverse, swap moving and reference frames
reference_arm_to_cart_cp.SetReferenceFrame(reference_arm_local_cp.MovingFrame());
reference_arm_to_cart_cp.SetMovingFrame(reference_sarm->m_local_measured_cp.ReferenceFrame());
// valid only if both are valid
reference_arm_to_cart_cp.SetValid(reference_sarm->m_local_measured_cp.Valid()
&& reference_arm_local_cp.Valid());
// take most recent timestamp
reference_arm_to_cart_cp.SetTimestamp(std::max(reference_sarm->m_local_measured_cp.Timestamp(),
reference_arm_local_cp.Timestamp()));
}
// reference sarm measured_cp is always with respect to cart, same as local
reference_sarm->m_measured_cp.Position().Assign(reference_sarm->m_local_measured_cp.Position());
reference_sarm->m_measured_cp.SetValid(reference_sarm->m_local_measured_cp.Valid());
reference_sarm->m_measured_cp.SetTimestamp(reference_sarm->m_local_measured_cp.Timestamp());
// update other arms
vctFrm4x4 reference_frame(reference_arm_to_cart_cp.Position());
vctFrm4x4 local_cp, cp;
for (size_t arm_index = 0; arm_index < 4; ++arm_index) {
auto * sarm = m_sarms[arm_index];
// update positions with base frame, local positions are only
// updated from FK when joints are ready
if (arm_index != m_reference_arm_index) {
sarm->m_measured_cp.SetReferenceFrame(reference_arm_to_cart_cp.ReferenceFrame());
local_cp.From(sarm->m_local_measured_cp.Position());
cp = reference_frame * local_cp;
// - with base frame
sarm->m_measured_cp.Position().From(cp);
sarm->m_measured_cp.SetValid(sarm->m_local_measured_cp.Valid()
&& reference_arm_to_cart_cp.Valid());
sarm->m_measured_cp.SetTimestamp(std::max(sarm->m_local_measured_cp.Timestamp(),
reference_arm_to_cart_cp.Timestamp()));
} else {
// for reference arm, measured_cp is local_measured_cp
sarm->m_measured_cp = sarm->m_local_measured_cp;
}
// convert from prmPositionCartesianGet to prmPositionCartesianSet
prmPositionCartesianSet base_frame;
base_frame.Goal().Assign(sarm->m_measured_cp.Position());
base_frame.SetValid(sarm->m_measured_cp.Valid());
base_frame.SetTimestamp(sarm->m_measured_cp.Timestamp());
base_frame.SetReferenceFrame(sarm->m_measured_cp.ReferenceFrame());
base_frame.SetMovingFrame(sarm->m_measured_cp.MovingFrame());
// and finally set the base frame
sarm->m_arm_set_base_frame(base_frame);
}
}
void mtsIntuitiveResearchKitSUJFixed::set_desired_state(const std::string & state)
{
// setting desired state triggers a new event so user nows which state is current
dispatch_operating_state();
// try to find the state in state machine
if (!m_state_machine.StateExists(state)) {
dispatch_error(this->GetName() + ": unsupported state " + state);
return;
}
// try to set the desired state
try {
m_state_machine.SetDesiredState(state);
} catch (...) {
dispatch_error(this->GetName() + ": " + state + " is not an allowed desired state");
return;
}
dispatch_operating_state();
dispatch_status(this->GetName() + ": desired state " + state);
// state transitions with direct transitions
if (state == "DISABLED") {
m_state_machine.SetCurrentState(state);
}
}
void mtsIntuitiveResearchKitSUJFixed::state_command(const std::string & command)
{
std::string humanReadableMessage;
prmOperatingState::StateType newOperatingState;
try {
if (m_operating_state.ValidCommand(prmOperatingState::CommandTypeFromString(command),
newOperatingState, humanReadableMessage)) {
if (command == "enable") {
set_desired_state("ENABLED");
return;
}
if (command == "disable") {
set_desired_state("DISABLED");
return;
}
if (command == "home") {
set_desired_state("ENABLED");
set_homed(true);
return;
}
if (command == "unhome") {
set_homed(false);
return;
}
if (command == "pause") {
std::cerr << CMN_LOG_DETAILS << " not implemented yet" << std::endl;
return;
}
if (command == "resume") {
std::cerr << CMN_LOG_DETAILS << " not implemented yet" << std::endl;
return;
}
} else {
dispatch_warning(this->GetName() + ": " + humanReadableMessage);
}
} catch (std::runtime_error & e) {
dispatch_warning(this->GetName() + ": " + command + " doesn't seem to be a valid state_command (" + e.what() + ")");
}
}
void mtsIntuitiveResearchKitSUJFixed::set_homed(const bool homed)
{
if (homed != m_operating_state.IsHomed()) {
m_operating_state.IsHomed() = homed;
dispatch_operating_state();
}
}
void mtsIntuitiveResearchKitSUJFixed::set_reference_arm(const std::string & arm_name)
{
for (size_t arm_index = 0; arm_index < 4; ++arm_index) {
if (m_sarms[arm_index]->m_name == arm_name) {
m_reference_arm_index = arm_index;
return;
}
}
m_interface->SendError(this->GetName() + ": set_reference_arm, " + arm_name + " not found");
}
void mtsIntuitiveResearchKitSUJFixed::dispatch_error(const std::string & message)
{
m_interface->SendError(message);
for (auto sarm : m_sarms) {
sarm->m_interface->SendError(sarm->m_name + " " + message);
}
}
void mtsIntuitiveResearchKitSUJFixed::dispatch_warning(const std::string & message)
{
m_interface->SendWarning(message);
for (auto sarm : m_sarms) {
sarm->m_interface->SendWarning(sarm->m_name + " " + message);
}
}
void mtsIntuitiveResearchKitSUJFixed::dispatch_status(const std::string & message)
{
m_interface->SendStatus(message);
for (auto sarm : m_sarms) {
sarm->m_interface->SendStatus(sarm->m_name + " " + message);
}
}
void mtsIntuitiveResearchKitSUJFixed::dispatch_operating_state(void)
{
state_events.operating_state(m_operating_state);
for (auto sarm : m_sarms) {
sarm->state_events.operating_state(m_operating_state);
sarm->state_events.current_state(m_state_machine.CurrentState());
sarm->state_events.desired_state(m_state_machine.DesiredState());
}
}
|