31 #include <SimoxUtility/json/eigen_conversion.h>
32 #include <SimoxUtility/json/json.hpp>
33 #include <VirtualRobot/math/Helpers.h>
46 #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);
204 const nlohmann::json j = nlohmann::json::parse(ifs);
206 std::vector<GraspTrajectoryKeypoint> traj;
207 traj = j.at(
"keypoints");
215 const float dtSeconds = duration.
toSecondsDouble() / (traj.size() - 1);
217 for (
auto& kp : traj)
222 const std::string frame = j.at(
"frame").at(
"frame");
224 auto graspTrajectory = std::make_shared<GraspTrajectory>(traj);
225 graspTrajectory->setFrame(frame);
227 return graspTrajectory;
239 const std::optional<std::string>& shape)
243 keypointMap[0] = keypoints.size();
244 keypoints.push_back(keypoint);
249 if (keypointz.empty())
255 keypointMap[0] = keypoints.size();
256 keypoints.push_back(std::make_shared<Keypoint>(keypointz.front()));
259 for (std::size_t i = 1; i < keypointz.size(); i++)
261 const auto& kp = keypointz.at(i);
262 addKeypoint(kp.tcpTarget, kp.handJointsTarget, kp.dt, kp.shape);
270 const std::optional<std::string>& shape)
275 keypoint->updateVelocities(prev,
dt);
277 keypointMap[t] = keypoints.size();
278 keypoints.push_back(keypoint);
285 return keypoints.size();
288 const std::vector<GraspTrajectory::KeypointPtr>&
299 const std::optional<std::string>& shape)
302 if (index <= 0 || index > keypoints.size())
308 keypoint->updateVelocities(prev,
dt);
309 if (
index < keypoints.size())
312 next->updateVelocities(keypoint, next->dt);
314 keypoints.insert(keypoints.begin() +
index, keypoint);
322 if (index <= 0 || index >= keypoints.size())
326 keypoints.erase(keypoints.begin() +
index);
327 if (
index < keypoints.size())
332 next->updateVelocities(prev, next->dt);
342 const std::optional<std::string>& shape)
345 if (index <= 0 || index >= keypoints.size())
351 keypoint->updateVelocities(prev,
dt);
352 keypoints.at(
index) = keypoint;
360 if (index <= 0 || index >= keypoints.size())
366 keypoint->updateVelocities(prev,
dt);
374 return keypoints.at(keypoints.size() - 1);
381 return keypoints.at(i);
402 std::map<float, size_t>::const_iterator it, prev;
403 it = keypointMap.upper_bound(t);
404 if (it == keypointMap.end())
406 i1 = keypoints.size() - 1;
407 i2 = keypoints.size() - 1;
411 prev = std::prev(it);
414 f = ::math::Helpers::ILerp(prev->first, it->first, t);
424 return ::math::Helpers::Lerp(
436 if (oriVel.squaredNorm() == 0)
440 Eigen::AngleAxisf aa(oriVel.norm(), oriVel.normalized());
442 return aa.toRotationMatrix() *
getKeypoint(i1)->getTargetOrientation();
452 std::vector<Eigen::Matrix4f>
456 std::vector<Eigen::Matrix4f> res;
459 res.emplace_back(keypoint->getTargetPose());
464 std::vector<Eigen::Vector3f>
468 std::vector<Eigen::Vector3f> res;
471 res.emplace_back(keypoint->getTargetPosition());
476 std::vector<Eigen::Matrix3f>
480 std::vector<Eigen::Matrix3f> res;
483 res.emplace_back(keypoint->getTargetOrientation());
495 auto handTargetsMap =
getKeypoint(i1)->handJointsTarget;
498 const Eigen::VectorXf lerpTargets = handTargets1 * (1 - f) + handTargets2 * f;
499 auto* lerpTargetsIt = lerpTargets.data();
500 for (
auto& [name,
value] : handTargetsMap)
502 value = *(lerpTargetsIt++);
504 return handTargetsMap;
507 std::optional<std::string>
556 return getKeypoint(i2)->feedForwardHandJointsVelocity;
563 return keypointMap.rbegin()->first;
566 GraspTrajectory::Length
571 for (
size_t i = 1; i < keypoints.size(); i++)
576 Eigen::Vector3f pos0 = ::math::Helpers::GetPosition(k0->tcpTarget);
579 Eigen::Vector3f pos1 = ::math::Helpers::GetPosition(k1->tcpTarget);
582 l.pos += (pos1 - pos0).
norm();
583 l.ori += fabs(::math::Helpers::GetAngleAxisFromTo(ori0, ori1).
angle());
595 getKeypoint(0)->getTargetPose(), translation, rotation),
597 for (
size_t i = 1; i < keypoints.size(); i++)
601 ::math::Helpers::TranslateAndRotatePose(kp->getTargetPose(), translation, rotation),
602 kp->handJointsTarget,
615 for (
size_t i = 1; i < keypoints.size(); i++)
619 transform * kp->getTargetPose(), kp->handJointsTarget, kp->dt, kp->shape);
633 const Eigen::Vector3f& handForward)
const
637 Eigen::Vector3f targetHandForward =
638 ::math::Helpers::TransformDirection(
target, handForward);
639 Eigen::Vector3f trajHandForward =
640 ::math::Helpers::TransformDirection(startPose, handForward);
641 Eigen::Vector3f up(0, 0, 1);
643 float angle = ::math::Helpers::Angle(targetHandForward, trajHandForward, up);
644 Eigen::AngleAxisf aa(
angle, up);
647 -::math::Helpers::GetPosition(startPose),
648 aa.toRotationMatrix(),
649 ::math::Helpers::GetPosition(
target));
658 flip_yz(0, 0) *= -1.0;
660 start_pose.block<3, 3>(0, 0) = flip_yz * start_pose.block<3, 3>(0, 0) * flip_yz;
667 target_pose.block<3, 3>(0, 0) = flip_yz * target_pose.block<3, 3>(0, 0) * flip_yz;
668 output_trajectory->addKeypoint(target_pose, kp->handJointsTarget, kp->dt, kp->shape);
670 return output_trajectory;
675 VirtualRobot::RobotNodePtr tcp,
684 GraspTrajectory::updateKeypointMap()
689 for (
size_t i = 0; i < keypoints.size(); i++)
701 Eigen::Vector3f pos0 = ::math::Helpers::GetPosition(prev->tcpTarget);
702 Eigen::Matrix3f ori0 = ::math::Helpers::GetOrientation(prev->tcpTarget);
705 Eigen::Vector3f pos1 = ::math::Helpers::GetPosition(
tcpTarget);
709 Eigen::Vector3f dpos = pos1 - pos0;
710 Eigen::Vector3f dori = ::math::Helpers::GetRotationVector(ori0, ori1);
711 Eigen::VectorXf dhnd = hnd1 - hnd0;
723 return ::math::Helpers::GetPosition(tcpTarget);
730 return ::math::Helpers::GetOrientation(tcpTarget);
744 const Eigen::Vector3f& feedForwardPosVelocity,
745 const Eigen::Vector3f& feedForwardOriVelocity,
746 const Eigen::VectorXf& feedForwardHandJointsVelocity,
747 const std::optional<std::string>& shape) :
748 tcpTarget(tcpTarget),
749 handJointsTarget(handJointsTarget),
752 feedForwardPosVelocity(feedForwardPosVelocity),
753 feedForwardOriVelocity(feedForwardOriVelocity),
754 feedForwardHandJointsVelocity(feedForwardHandJointsVelocity)
760 const std::optional<std::string>& shape) :
761 tcpTarget(tcpTarget),
762 handJointsTarget(handJointsTarget),
765 feedForwardPosVelocity(0, 0, 0),
766 feedForwardOriVelocity(0, 0, 0)
776 const std::optional<std::string>&