PointingIK.cpp
Go to the documentation of this file.
1#include "PointingIK.h"
2
3#include <VirtualRobot/Robot.h>
4#include <VirtualRobot/RobotNodeSet.h>
5
7{
8
9 PointingIK::PointingIK(VirtualRobot::RobotPtr robot, Side side, Eigen::Vector3f& target)
10 {
11 auto humanMapping = robot->getHumanMapping();
12 ARMARX_CHECK(humanMapping) << "Robot '" << robot->getName() << "' has no human mapping";
13
14 auto armDescription = (side == LEFT) ? humanMapping->leftArm : humanMapping->rightArm;
15 auto elbow = armDescription.segments.elbow.nodeName;
16 auto wrist = armDescription.segments.wrist.nodeName;
17 auto palm = armDescription.segments.palm.nodeName;
18
19 // actuated joints = all arm joints until (excluding) wrist
20 auto nodeSetArm = robot->getRobotNodeSet(armDescription.nodeSet);
21 auto actuatedJoints = nodeSetArm->getNodeNames();
22 actuatedJoints.resize(nodeSetArm->getRobotNodeIndex(wrist));
23
24 // reduced robot must contain shoulder, elbow, wrist and palm for IK to work
25 reducedRobot_ = std::make_unique<simox::control::simox::robot::Robot>(
26 robot, actuatedJoints, std::vector({wrist, palm}), true);
27
28 ik_ = std::make_unique<simox::control::method::example::PointingIK>(
29 *reducedRobot_,
30 *reducedRobot_->getNode(wrist),
31 *reducedRobot_->getNode(elbow),
32 side == LEFT ? reducedRobot_->humanMapping()->leftArm
33 : reducedRobot_->humanMapping()->rightArm);
34
35 ik_->setPointingTarget(target.cast<double>() / 1000); // simox uses meters
36
37 trajData.assign(ik_->getNodeNames().size(), std::vector<double>());
38 appendTrajData(ik_->getJointValues());
39 }
40
41 bool
43 {
44 return ik_->isTargetReached();
45 }
46
47 double
49 {
50 Eigen::VectorXd prevJointValues = ik_->getJointValues();
51 ik_->optimize();
52 appendTrajData(ik_->getJointValues());
53 return (ik_->getJointValues() - prevJointValues).norm();
54 }
55
58 {
59 return new Trajectory(trajData, Ice::DoubleSeq(), ik_->getNodeNames());
60 }
61
62 void
63 PointingIK::appendTrajData(const Eigen::VectorXd& jointValues)
64 {
65 for (size_t i = 0; i < trajData.size(); i++)
66 {
67 trajData[i].push_back(jointValues[i]);
68 }
69 }
70
71} // namespace armarx::control::pointing::core
The Trajectory class represents n-dimensional sampled trajectories.
Definition Trajectory.h:77
PointingIK(VirtualRobot::RobotPtr robot, Side side, Eigen::Vector3f &target)
Initialize a new PointingIK.
Definition PointingIK.cpp:9
double optimize()
Optimizes the joint configuration of the internal robot model regarding pointing towards the target.
armarx::TrajectoryPtr getTrajectory()
Constructs a trajectory containing the sequence of joint configurations from the calls to optimize().
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Side
Describes the side of an arm.
Definition Side.h:11
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition Trajectory.h:52