26#include <VirtualRobot/math/AbstractFunctionR1R6.h>
27#include <VirtualRobot/math/Helpers.h>
33GraspTrajectory::Keypoint::Keypoint(
const Eigen::Matrix4f&
tcpTarget,
44GraspTrajectory::Keypoint::Keypoint(
const Eigen::Matrix4f&
tcpTarget,
62 return ::math::Helpers::GetPosition(
tcpTarget);
68 return ::math::Helpers::GetOrientation(
tcpTarget);
80 Eigen::Vector3f pos0 = ::math::Helpers::GetPosition(prev->tcpTarget);
81 Eigen::Matrix3f ori0 = ::math::Helpers::GetOrientation(prev->tcpTarget);
82 Eigen::VectorXf hnd0 = prev->handJointsTarget;
84 Eigen::Vector3f pos1 = ::math::Helpers::GetPosition(
tcpTarget);
85 Eigen::Matrix3f ori1 = ::math::Helpers::GetOrientation(
tcpTarget);
88 Eigen::Vector3f dpos = pos1 - pos0;
89 Eigen::Vector3f dori = ::math::Helpers::GetRotationVector(ori0, ori1);
90 Eigen::VectorXf dhnd = hnd1 - hnd0;
99 const Eigen::VectorXf& handJointsStart)
102 keypointMap[0] = keypoints.size();
103 keypoints.push_back(keypoint);
108 const Eigen::VectorXf& handJointsTarget,
114 keypoint->updateVelocities(prev,
dt);
116 keypointMap[t] = keypoints.size();
117 keypoints.push_back(keypoint);
123 return keypoints.size();
128 const Eigen::Matrix4f& tcpTarget,
129 const Eigen::VectorXf& handJointsTarget,
135 throw LocalException(
"Index out of range" + std::to_string(
index));
139 keypoint->updateVelocities(prev,
dt);
140 if (
index < keypoints.size())
143 next->updateVelocities(keypoint, next->dt);
145 keypoints.insert(keypoints.begin() +
index, keypoint);
154 throw LocalException(
"Index out of range" + std::to_string(
index));
156 keypoints.erase(keypoints.begin() +
index);
157 if (
index < keypoints.size())
161 next->updateVelocities(prev, next->dt);
168 const Eigen::Matrix4f& tcpTarget,
169 const Eigen::VectorXf& handJointsTarget,
175 throw LocalException(
"Index out of range" + std::to_string(
index));
179 keypoint->updateVelocities(prev,
dt);
180 keypoints.at(
index) = keypoint;
189 throw LocalException(
"Index out of range" + std::to_string(
index));
193 keypoint->updateVelocities(prev,
dt);
200 return keypoints.at(keypoints.size() - 1);
206 return keypoints.at(i);
225 std::map<float, size_t>::const_iterator it, prev;
226 it = keypointMap.upper_bound(t);
227 if (it == keypointMap.end())
229 i1 = keypoints.size() - 1;
230 i2 = keypoints.size() - 1;
234 prev = std::prev(it);
237 f = ::math::Helpers::ILerp(prev->first, it->first, t);
246 return ::math::Helpers::Lerp(
257 if (oriVel.squaredNorm() == 0)
261 Eigen::AngleAxisf aa(oriVel.norm(), oriVel.normalized());
263 return aa.toRotationMatrix() *
getKeypoint(i1)->getTargetOrientation();
272std::vector<Eigen::Matrix4f>
275 std::vector<Eigen::Matrix4f> res;
278 res.emplace_back(keypoint->getTargetPose());
283std::vector<Eigen::Vector3f>
286 std::vector<Eigen::Vector3f> res;
289 res.emplace_back(keypoint->getTargetPosition());
294std::vector<Eigen::Matrix3f>
297 std::vector<Eigen::Matrix3f> res;
300 res.emplace_back(keypoint->getTargetOrientation());
348 return getKeypoint(i2)->feedForwardHandJointsVelocity;
354 return keypointMap.rbegin()->first;
361 for (
size_t i = 1; i < keypoints.size(); i++)
366 Eigen::Vector3f pos0 = ::math::Helpers::GetPosition(k0->tcpTarget);
367 Eigen::Matrix3f ori0 = ::math::Helpers::GetOrientation(k0->tcpTarget);
369 Eigen::Vector3f pos1 = ::math::Helpers::GetPosition(k1->tcpTarget);
370 Eigen::Matrix3f ori1 = ::math::Helpers::GetOrientation(k1->tcpTarget);
373 l.
ori += fabs(::math::Helpers::GetAngleAxisFromTo(ori0, ori1).
angle());
381 return keypoints.at(0)->handJointsTarget.rows();
386 const Eigen::Matrix3f& rotation)
390 getKeypoint(0)->getTargetPose(), translation, rotation),
392 for (
size_t i = 1; i < keypoints.size(); i++)
396 ::math::Helpers::TranslateAndRotatePose(kp->getTargetPose(), translation, rotation),
397 kp->handJointsTarget,
408 for (
size_t i = 1; i < keypoints.size(); i++)
411 traj->addKeypoint(
transform * kp->getTargetPose(), kp->handJointsTarget, kp->dt);
424 const Eigen::Vector3f& handForward)
427 Eigen::Vector3f targetHandForward = ::math::Helpers::TransformDirection(target, handForward);
428 Eigen::Vector3f trajHandForward = ::math::Helpers::TransformDirection(startPose, handForward);
429 Eigen::Vector3f up(0, 0, 1);
431 float angle = ::math::Helpers::Angle(targetHandForward, trajHandForward, up);
432 Eigen::AngleAxisf aa(
angle, up);
434 Eigen::Matrix4f
transform = ::math::Helpers::CreateTranslationRotationTranslationPose(
435 -::math::Helpers::GetPosition(startPose),
436 aa.toRotationMatrix(),
437 ::math::Helpers::GetPosition(target));
443 VirtualRobot::RobotNodePtr tcp,
458 e.
Add(
"dt", keypoint->dt);
459 e.
AddAsArr(
"Pose", keypoint->tcpTarget);
460 e.
AddAsArr(
"HandValues", keypoint->handJointsTarget);
469 std::string packageName =
"armar6_skills";
471 std::string dataDir = finder.
getDataDir() +
"/" + packageName;
473 cnd->executionHints->graspTrajectoryName +
".xml");
486 float dt =
nav.selectSingleNode(
"dt").asFloat();
488 Eigen::Matrix4f pose;
489 std::vector<JPathNavigator> rows =
nav.select(
"Pose/*");
490 for (
int i = 0; i < 4; i++)
492 std::vector<JPathNavigator> cells = rows.at(i).select(
"*");
493 for (
int j = 0; j < 4; j++)
495 pose(i, j) = cells.at(j).asFloat();
499 Eigen::Vector3f handValues;
500 std::vector<JPathNavigator> cells =
nav.select(
"HandValues/*");
501 for (
int j = 0; j < 3; j++)
503 handValues(j) = cells.at(j).asFloat();
512 traj->addKeypoint(pose, handValues,
dt);
531GraspTrajectory::updateKeypointMap()
535 for (
size_t i = 0; i < keypoints.size(); i++)
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
std::string getDataDir() const
Eigen::Matrix3f getTargetOrientation() const
void updateVelocities(const GraspTrajectoryKeypointPtr &prev, float deltat)
Eigen::Vector3f getTargetPosition() const
Eigen::Matrix4f getTargetPose() const
Eigen::VectorXf handJointsTarget
Eigen::VectorXf feedForwardHandJointsVelocity
Eigen::Vector3f feedForwardPosVelocity
Eigen::Vector3f feedForwardOriVelocity
Eigen::Matrix4f tcpTarget
Eigen::VectorXf GetHandValues(float t)
void removeKeypoint(size_t index)
float getDuration() const
Eigen::Vector3f GetOrientationDerivative(float t)
Eigen::Matrix4f getStartPose()
Eigen::Matrix4f GetPose(float t)
SimpleDiffIK::Reachability calculateReachability(VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), SimpleDiffIK::Parameters params=SimpleDiffIK::Parameters())
Eigen::Vector3f GetPositionDerivative(float t)
void addKeypoint(const Eigen::Matrix4f &tcpTarget, const Eigen::VectorXf &handJointsTarget, float dt)
GraspTrajectoryPtr getClone()
GraspTrajectoryPtr getTranslatedAndRotated(const Eigen::Vector3f &translation, const Eigen::Matrix3f &rotation)
static GraspTrajectoryPtr ReadFromString(const std::string &xml)
size_t getKeypointCount() const
std::vector< Eigen::Vector3f > getAllKeypointPositions()
Eigen::Vector3f GetPosition(float t)
GraspTrajectoryKeypoint Keypoint
void setKeypointDt(size_t index, float dt)
std::vector< Eigen::Matrix3f > getAllKeypointOrientations()
std::shared_ptr< Keypoint > KeypointPtr
void replaceKeypoint(size_t index, const Eigen::Matrix4f &tcpTarget, const Eigen::VectorXf &handJointsTarget, float dt)
GraspTrajectory()=default
Eigen::VectorXf GetHandJointsDerivative(float t)
GraspTrajectoryPtr getTransformed(const Eigen::Matrix4f &transform)
int GetHandJointCount() const
KeypointPtr & getKeypoint(int i)
void getIndex(float t, int &i1, int &i2, float &f)
KeypointPtr & lastKeypoint()
Length calculateLength() const
std::vector< Eigen::Matrix4f > getAllKeypointPoses()
Eigen::Matrix3f GetOrientation(float t)
void insertKeypoint(size_t index, const Eigen::Matrix4f &tcpTarget, const Eigen::VectorXf &handJointsTarget, float dt)
GraspTrajectoryPtr getTransformedToGraspPose(const Eigen::Matrix4f &target, const Eigen::Vector3f &handForward=Eigen::Vector3f::UnitZ())
static GraspTrajectoryPtr ReadFromReader(const RapidXmlReaderPtr &reader)
Eigen::Vector6f GetTcpDerivative(float t)
static GraspTrajectoryPtr ReadFromFile(const grasping::GraspCandidatePtr &cnd)
void writeToFile(const std::string &filename)
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
static RapidXmlReaderPtr FromFile(const std::string &path)
static RapidXmlReaderPtr FromXmlString(const std::string &xml)
RapidXmlWriterNode & append_string_node(const std::string &name, const std::string &value)
void saveToFile(const std::string &path, bool indent)
RapidXmlWriterNode createRootNode(const std::string &name)
static Reachability CalculateReachability(const std::vector< Eigen::Matrix4f > targets, const Eigen::VectorXf &initialJV, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), Parameters params=Parameters())
Use this to check a trajectory of waypoints.
void Add(const std::string &key, const std::string &value)
void AddAsArr(const std::string &key, const Eigen::Vector3f &vec)
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Matrix< float, 6, 1 > Vector6f
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
std::shared_ptr< class GraspTrajectory > GraspTrajectoryPtr
double norm(const Point &a)
double angle(const Point &a, const Point &b, const Point &c)