31 #include <SimoxUtility/json/eigen_conversion.h>
32 #include <SimoxUtility/json/json.hpp>
33 #include <VirtualRobot/RobotNodeSet.h>
34 #include <VirtualRobot/math/Helpers.h>
47 #include <RobotAPI/interface/units/GraspCandidateProviderInterface.h>
57 Eigen::VectorXf vector(map.size());
59 map.begin(), map.end(), vector.data(), [](
const auto& item) { return item.second; });
67 RapidXmlWriter writer;
68 RapidXmlWriterNode root = writer.createRootNode(
"GraspTrajectory");
71 SimpleJsonLoggerEntry e;
72 e.Add(
"dt", keypoint->dt);
73 e.AddAsArr(
"Pose", keypoint->tcpTarget);
75 for (
const auto& [name,
value] : keypoint->handJointsTarget)
79 e.obj->add(
"HandValues", obj);
80 root.append_string_node(
"Keypoint", e.obj->toJsonString(0,
"",
true));
89 RapidXmlReaderNode
const root = reader->getRoot();
92 for (
const RapidXmlReaderNode& kpNode : root.nodes(
"Keypoint"))
94 StructuralJsonParser p(kpNode.value());
96 JPathNavigator nav(p.parsedJson);
97 float dt = nav.selectSingleNode(
"dt").asFloat();
100 std::vector<JPathNavigator> rows = nav.select(
"Pose/*");
101 for (
int i = 0; i < 4; i++)
103 std::vector<JPathNavigator> cells = rows.at(i).select(
"*");
104 for (
int j = 0; j < 4; j++)
106 pose(i, j) = cells.at(j).asFloat();
111 std::vector<JPathNavigator> cells = nav.select(
"HandValues/*");
112 for (
const auto& cell : cells)
114 handValues[cell.getKey()] = cell.asFloat();
118 const auto shapeNode = nav.selectSingleNode(
"shape");
121 std::optional<std::string> shape;
122 if (shapeNode.isString())
124 shape = nav.selectSingleNode(
"shape").asString();
131 traj = std::make_shared<GraspTrajectory>(pose, handValues, shape);
135 traj->addKeypoint(pose, handValues,
dt, shape);
145 const auto ext = std::filesystem::path(
filename).extension();
166 std::optional<std::string> shape;
168 if (j.contains(
"shape"))
170 if (not j.at(
"shape").is_null())
172 shape = j.at(
"shape");
177 std::optional<std::map<std::string, float>> targetHandValues;
179 if (j.contains(
"fingerValues"))
181 if (not j.at(
"fingerValues").is_null())
183 targetHandValues = j.at(
"fingerValues");
195 tf, targetHandValues.value_or(std::map<std::string, float>{}), shape);
206 const nlohmann::json j = nlohmann::json::parse(ifs);
208 std::vector<GraspTrajectoryKeypoint> traj;
209 traj = j.at(
"keypoints");
217 const float dtSeconds = duration.
toSecondsDouble() / (traj.size() - 1);
220 for (
auto& kp : traj)
225 const std::string frame = j.at(
"frame").at(
"frame");
227 auto graspTrajectory = std::make_shared<GraspTrajectory>(traj);
228 graspTrajectory->setFrame(frame);
230 return graspTrajectory;
242 const std::optional<std::string>& shape)
246 keypointMap[0] = keypoints.size();
247 keypoints.push_back(keypoint);
252 if (keypointz.empty())
258 keypointMap[0] = keypoints.size();
259 keypoints.push_back(std::make_shared<Keypoint>(keypointz.front()));
262 for (std::size_t i = 1; i < keypointz.size(); i++)
264 const auto& kp = keypointz.at(i);
265 addKeypoint(kp.tcpTarget, kp.handJointsTarget, kp.dt, kp.shape);
273 const std::optional<std::string>& shape)
278 keypoint->updateVelocities(prev,
dt);
280 keypointMap[t] = keypoints.size();
281 keypoints.push_back(keypoint);
288 return keypoints.size();
291 const std::vector<GraspTrajectory::KeypointPtr>&
302 const std::optional<std::string>& shape)
305 if (index <= 0 || index > keypoints.size())
311 keypoint->updateVelocities(prev,
dt);
312 if (
index < keypoints.size())
315 next->updateVelocities(keypoint, next->dt);
317 keypoints.insert(keypoints.begin() +
index, keypoint);
325 if (index <= 0 || index >= keypoints.size())
329 keypoints.erase(keypoints.begin() +
index);
330 if (
index < keypoints.size())
335 next->updateVelocities(prev, next->dt);
345 const std::optional<std::string>& shape)
348 if (index <= 0 || index >= keypoints.size())
354 keypoint->updateVelocities(prev,
dt);
355 keypoints.at(
index) = keypoint;
363 if (index <= 0 || index >= keypoints.size())
369 keypoint->updateVelocities(prev,
dt);
377 return keypoints.at(keypoints.size() - 1);
384 return keypoints.at(i);
405 std::map<float, size_t>::const_iterator it, prev;
406 it = keypointMap.upper_bound(t);
407 if (it == keypointMap.end())
409 i1 = keypoints.size() - 1;
410 i2 = keypoints.size() - 1;
414 prev = std::prev(it);
417 f = ::math::Helpers::ILerp(prev->first, it->first, t);
427 return ::math::Helpers::Lerp(
439 if (oriVel.squaredNorm() == 0)
443 Eigen::AngleAxisf aa(oriVel.norm(), oriVel.normalized());
445 return aa.toRotationMatrix() *
getKeypoint(i1)->getTargetOrientation();
455 std::vector<Eigen::Matrix4f>
459 std::vector<Eigen::Matrix4f> res;
462 res.emplace_back(keypoint->getTargetPose());
467 std::vector<Eigen::Vector3f>
471 std::vector<Eigen::Vector3f> res;
474 res.emplace_back(keypoint->getTargetPosition());
479 std::vector<Eigen::Matrix3f>
483 std::vector<Eigen::Matrix3f> res;
486 res.emplace_back(keypoint->getTargetOrientation());
498 auto handTargetsMap =
getKeypoint(i1)->handJointsTarget;
501 const Eigen::VectorXf lerpTargets = handTargets1 * (1 - f) + handTargets2 * f;
502 auto* lerpTargetsIt = lerpTargets.data();
503 for (
auto& [name,
value] : handTargetsMap)
505 value = *(lerpTargetsIt++);
507 return handTargetsMap;
510 std::optional<std::string>
559 return getKeypoint(i2)->feedForwardHandJointsVelocity;
566 return keypointMap.rbegin()->first;
569 GraspTrajectory::Length
574 for (
size_t i = 1; i < keypoints.size(); i++)
579 Eigen::Vector3f pos0 = ::math::Helpers::GetPosition(k0->tcpTarget);
582 Eigen::Vector3f pos1 = ::math::Helpers::GetPosition(k1->tcpTarget);
585 l.pos += (pos1 - pos0).
norm();
586 l.ori += fabs(::math::Helpers::GetAngleAxisFromTo(ori0, ori1).
angle());
598 getKeypoint(0)->getTargetPose(), translation, rotation),
600 for (
size_t i = 1; i < keypoints.size(); i++)
604 ::math::Helpers::TranslateAndRotatePose(kp->getTargetPose(), translation, rotation),
605 kp->handJointsTarget,
618 for (
size_t i = 1; i < keypoints.size(); i++)
622 transform * kp->getTargetPose(), kp->handJointsTarget, kp->dt, kp->shape);
636 const Eigen::Vector3f& handForward)
const
640 Eigen::Vector3f targetHandForward =
641 ::math::Helpers::TransformDirection(
target, handForward);
642 Eigen::Vector3f trajHandForward =
643 ::math::Helpers::TransformDirection(startPose, handForward);
644 Eigen::Vector3f
up(0, 0, 1);
646 float angle = ::math::Helpers::Angle(targetHandForward, trajHandForward,
up);
647 Eigen::AngleAxisf aa(
angle,
up);
650 -::math::Helpers::GetPosition(startPose),
651 aa.toRotationMatrix(),
652 ::math::Helpers::GetPosition(
target));
661 flip_yz(0, 0) *= -1.0;
663 start_pose.block<3, 3>(0, 0) = flip_yz * start_pose.block<3, 3>(0, 0) * flip_yz;
670 target_pose.block<3, 3>(0, 0) = flip_yz * target_pose.block<3, 3>(0, 0) * flip_yz;
671 output_trajectory->addKeypoint(target_pose, kp->handJointsTarget, kp->dt, kp->shape);
673 return output_trajectory;
678 VirtualRobot::RobotNodePtr tcp,
687 GraspTrajectory::updateKeypointMap()
692 for (
size_t i = 0; i < keypoints.size(); i++)
704 Eigen::Vector3f pos0 = ::math::Helpers::GetPosition(prev->tcpTarget);
705 Eigen::Matrix3f ori0 = ::math::Helpers::GetOrientation(prev->tcpTarget);
708 Eigen::Vector3f pos1 = ::math::Helpers::GetPosition(
tcpTarget);
712 Eigen::Vector3f dpos = pos1 - pos0;
713 Eigen::Vector3f dori = ::math::Helpers::GetRotationVector(ori0, ori1);
714 Eigen::VectorXf dhnd = hnd1 - hnd0;
726 return ::math::Helpers::GetPosition(tcpTarget);
733 return ::math::Helpers::GetOrientation(tcpTarget);
747 const Eigen::Vector3f& feedForwardPosVelocity,
748 const Eigen::Vector3f& feedForwardOriVelocity,
749 const Eigen::VectorXf& feedForwardHandJointsVelocity,
750 const std::optional<std::string>& shape) :
751 tcpTarget(tcpTarget),
752 handJointsTarget(handJointsTarget),
755 feedForwardPosVelocity(feedForwardPosVelocity),
756 feedForwardOriVelocity(feedForwardOriVelocity),
757 feedForwardHandJointsVelocity(feedForwardHandJointsVelocity)
763 const std::optional<std::string>& shape) :
764 tcpTarget(tcpTarget),
765 handJointsTarget(handJointsTarget),
768 feedForwardPosVelocity(0, 0, 0),
769 feedForwardOriVelocity(0, 0, 0)
779 const std::optional<std::string>&