2 #include <dart/common/Console.hpp>
3 #include <dart/dynamics/dynamics.hpp>
18 template <ExecutorType T>
20 ::dart::dynamics::BodyNode* eeNode,
24 executor ? executor->getDofs() :
checkNull(eeNode)->getDependentDofs(),
25 executor ? executor->getTypes()
38 mExecutor = std::make_shared<KinematicSimulationJointCommandExecutor<T>>(
39 mEENode->getDependentDofs());
43 mExecutor->releaseDofs();
47 template <ExecutorType T>
51 std::lock_guard<std::mutex> lock(mMutex);
57 mPromise->set_exception(
58 std::make_exception_ptr(std::runtime_error(
"Command canceled.")));
66 template <ExecutorType T>
68 const Eigen::Vector6d command,
69 const std::chrono::duration<double>& timeout,
70 const std::chrono::system_clock::time_point& timepoint)
74 std::lock_guard<std::mutex> lock(mMutex);
80 mCommand = Eigen::Vector6d::Zero();
81 mPromise->set_exception(
82 std::make_exception_ptr(std::runtime_error(
"Command canceled.")));
85 mPromise.reset(
new std::promise<int>());
88 mExecutionStartTime = timepoint;
90 mFuture = mExecutor->execute(SE3ToJoint(mCommand), mTimeout, timepoint);
94 return mPromise->get_future();
98 template <ExecutorType T>
100 const Eigen::Vector6d command)
104 Eigen::Vector6d refCmd = command;
106 Eigen::VectorXd jointCmd(this->mDofs.size());
109 if (refCmd.norm() != 0)
112 Eigen::MatrixXd fullJ = mEENode->getWorldJacobian();
113 Eigen::MatrixXd J(SE3_SIZE, this->mDofs.size());
114 auto fullDofs = mEENode->getDependentDofs();
115 for (
size_t fullIndex = 0; fullIndex < fullDofs.size(); fullIndex++)
118 this->mDofs.begin(), this->mDofs.end(), fullDofs[fullIndex]);
119 if (it != this->mDofs.end())
121 int index = it - this->mDofs.begin();
122 J.col(index) = fullJ.col(fullIndex);
127 Eigen::MatrixXd JtJ = J.transpose() * J;
128 JtJ += mLambda * mLambda
129 * Eigen::MatrixXd::Identity(JtJ.rows(), JtJ.cols());
130 jointCmd = JtJ.inverse() * J.transpose() * refCmd;
133 return std::vector<double>(
134 jointCmd.data(), jointCmd.data() + jointCmd.size());
138 template <ExecutorType T>
140 const std::chrono::system_clock::time_point& timepoint)
142 std::lock_guard<std::mutex> lock(mMutex);
143 mExecutor->step(timepoint);
149 auto status = mFuture.wait_for(std::chrono::milliseconds(1));
150 if (status == std::future_status::ready)
155 int result = mFuture.get();
159 mPromise->set_value(result);
162 catch (
const std::exception& e)
165 mPromise->set_exception(std::current_exception());
169 mCommand = Eigen::Vector6d::Zero();
176 const auto timeSinceBeginning = timepoint - mExecutionStartTime;
177 const auto executionTime
178 = std::chrono::duration<double>(timeSinceBeginning).count();
182 if (executionTime < 0)
186 if (mTimeout.count() > 0.0 && executionTime >= mTimeout.count())
188 mCommand = Eigen::Vector6d::Zero();
191 mPromise->set_value(0);
195 mFuture = mExecutor->execute(SE3ToJoint(mCommand), mTimeout, timepoint);
196 mExecutor->step(timepoint);
200 template <ExecutorType T>
202 const std::vector<double>& command,
203 const std::chrono::duration<double>& timeout,
204 const std::chrono::system_clock::time_point& timepoint)
211 return mExecutor->execute(command, timeout, timepoint);
215 template <ExecutorType T>
218 std::lock_guard<std::mutex> lock(mMutex);
223 mCommand = Eigen::Vector6d::Zero();
226 mPromise->set_exception(
227 std::make_exception_ptr(std::runtime_error(
"Command canceled.")));
231 dtwarn <<
"[JacobianExecutor::cancel] Attempting to "
232 <<
"cancel command, but no command in progress.\n";