Skip to content
Snippets Groups Projects
Commit 9846e1e3 authored by Patrick Dormanns's avatar Patrick Dormanns
Browse files

make aborting while computing trajectory possible

parent f61e0abf
No related branches found
No related tags found
1 merge request!53Implement 'PointAt'-skill
...@@ -84,20 +84,28 @@ namespace armarx::control::pointing::core ...@@ -84,20 +84,28 @@ namespace armarx::control::pointing::core
Pointing::computTrajectory(VirtualRobot::RobotPtr robot, Side side, Eigen::Vector3f target) Pointing::computTrajectory(VirtualRobot::RobotPtr robot, Side side, Eigen::Vector3f target)
{ {
ARMARX_VERBOSE << "Computing trajectory..."; ARMARX_VERBOSE << "Computing trajectory...";
auto ik = PointingIK(robot, side); auto ik = PointingIK(robot, side, target);
armarx::TrajectoryPtr traj = ik.computeTrajectory(target);
if (not traj)
{
ARMARX_WARNING << "IK didn't find a solution";
throw PointingFailedException();
}
ARMARX_VERBOSE << "Done computing trajectory";
if (traj->size() == 1) // forearm already aligned with target if (ik.isTargetReached()) // forearm already aligned with target
{ {
return armarx::TrajectoryPtr(new Trajectory); return armarx::TrajectoryPtr(new Trajectory);
} }
return traj;
do
{
if (ik.optimize() < 1e-6)
{
ARMARX_WARNING << "PointingIK didn't find a solution";
throw PointingFailedException();
}
if (aborted)
{
ARMARX_INFO << "Pointing aborted.";
throw PointingAbortedException();
}
} while (not ik.isTargetReached());
return ik.getTrajectory();
} }
void void
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
namespace armarx::control::pointing::core namespace armarx::control::pointing::core
{ {
PointingIK::PointingIK(VirtualRobot::RobotPtr robot, Side side) PointingIK::PointingIK(VirtualRobot::RobotPtr robot, Side side, Eigen::Vector3f& target)
{ {
auto humanMapping = robot->getHumanMapping(); auto humanMapping = robot->getHumanMapping();
ARMARX_CHECK(humanMapping) << "Robot '" << robot->getName() << "' has no human mapping"; ARMARX_CHECK(humanMapping) << "Robot '" << robot->getName() << "' has no human mapping";
...@@ -31,39 +31,41 @@ namespace armarx::control::pointing::core ...@@ -31,39 +31,41 @@ namespace armarx::control::pointing::core
*reducedRobot_->getNode(elbow), *reducedRobot_->getNode(elbow),
side == LEFT ? reducedRobot_->humanMapping()->leftArm side == LEFT ? reducedRobot_->humanMapping()->leftArm
: reducedRobot_->humanMapping()->rightArm); : reducedRobot_->humanMapping()->rightArm);
}
TrajectoryPtr
PointingIK::computeTrajectory(Eigen::Vector3f& target)
{
ik_->setPointingTarget(target.cast<double>() / 1000); // simox uses meters ik_->setPointingTarget(target.cast<double>() / 1000); // simox uses meters
// actuated joints x timesteps trajData.assign(ik_->getNodeNames().size(), std::vector<double>());
std::vector<std::vector<double>> configs(ik_->getNodeNames().size()); appendTrajData(ik_->getJointValues());
auto appendConfig = [&configs](Eigen::VectorXd config) }
{
for (size_t i = 0; i < configs.size(); i++)
{
configs[i].push_back(config[i]);
}
};
appendConfig(ik_->getJointValues()); bool
PointingIK::isTargetReached()
{
return ik_->isTargetReached();
}
double
PointingIK::optimize()
{
Eigen::VectorXd prevJointValues = ik_->getJointValues(); Eigen::VectorXd prevJointValues = ik_->getJointValues();
while (not ik_->isTargetReached()) ik_->optimize();
{ appendTrajData(ik_->getJointValues());
ik_->optimize(); return (ik_->getJointValues() - prevJointValues).norm();
if ((ik_->getJointValues() - prevJointValues).norm() < 1e-6) }
{
return nullptr;
}
appendConfig(ik_->getJointValues()); TrajectoryPtr
prevJointValues = ik_->getJointValues(); PointingIK::getTrajectory()
} {
return new Trajectory(trajData, Ice::DoubleSeq(), ik_->getNodeNames());
}
return new Trajectory(configs, Ice::DoubleSeq(), ik_->getNodeNames()); void
PointingIK::appendTrajData(const Eigen::VectorXd& jointValues)
{
for (size_t i = 0; i < trajData.size(); i++)
{
trajData[i].push_back(jointValues[i]);
}
} }
} // namespace armarx::control::pointing::core } // namespace armarx::control::pointing::core
...@@ -10,11 +10,29 @@ namespace armarx::control::pointing::core ...@@ -10,11 +10,29 @@ namespace armarx::control::pointing::core
{ {
/** /**
* @brief IK to compute joint configurations for a pointing gesture. * @brief IK to compute trajectory for a pointing gesture.
* *
* Wrapper for simox-control's method::example::PointingIK. Computes pointing gesture joint * Wrapper for simox-control's method::example::PointingIK. Computes a trajectory for a
* configurations that line up the forearm with the target. Uses the robot's human mapping to * pointing gesture that lines up the forearm with a target. Uses the robot's human mapping to
* obtain relevant joints: All arm-joints until (exluding) the wrist are beeing used. * obtain relevant joints: All arm-joints until (excluding) the wrist are being used.
*
* This class keeps an internal robot model. Each call to @ref optimize() optimizes the joint
* configuration regarding pointing towards the target. @ref isTargetReached() returns whether
* the robot in the latest configuration already points at the target. @ref getTrajectory()
* returns the sequence of generated configurations as Trajectory.
*
* Example usage:
*
* @code
* ik = PointingIK(...);
*
* while (not ik.isTargetReached()) {
* if (ik.optimize) < 1e-6 {
* return nullptr;
* }
* }
* return ik.getTrajectory();
* @endcode
* *
* @ingroup Library-pointing * @ingroup Library-pointing
*/ */
...@@ -26,35 +44,45 @@ namespace armarx::control::pointing::core ...@@ -26,35 +44,45 @@ namespace armarx::control::pointing::core
* @param robot Robot model with current robot configuration to be used by the IK. Must * @param robot Robot model with current robot configuration to be used by the IK. Must
* provide a human mapping. * provide a human mapping.
* @param side Side of the arm to point with. * @param side Side of the arm to point with.
* @param target Target position to point at in the global coordinate system (mm).
*/ */
PointingIK(VirtualRobot::RobotPtr robot, Side side); PointingIK(VirtualRobot::RobotPtr robot, Side side, Eigen::Vector3f& target);
/**
* @return Whether the robot in the latest joint configuration (from @ref optimize())
* already points at the target.
*/
bool isTargetReached();
/** /**
* @brief Compute a joint trajectory ending in a configuration in which the robot points in * @brief Optimizes the joint configuration of the internal robot model regarding pointing
* the direction of the passed target position. * towards the target.
* * @return Difference between the previous and new joint configurations. May be used to
* The initial timestep of the trajectory contains the current joint configuration of the * determine if the IK is unable to find a solution.
* robot passed to the constructor. The following timesteps contain the step-by-step */
* optimized configurations until the end configuration is reached. The trajectory is double optimize();
* constructed without timestamps, i.e., has a duration of 1s with equidistant timesteps.
/**
* @brief Constructs a trajectory containing the sequence of joint configurations from the
* calls to @ref optimize().
* *
* Neither the generated trajectory nor the end configuration are guaranteed to be self- * Neither the trajectory nor the end configuration are guaranteed to be self-
* collision-free. Furthermore, generated trajectories aren't efficient in reaching the end * collision free. Furthermore, the trajectories aren't efficient in reaching the end
* configuration. * configuration.
* *
* This method may only be called once to ensure a well-defined initial robot configuration. * `getTrajectory()->size()` will equal the number of calls to @ref optimize() plus one for
* the initial joint configuration
* *
* @param target Target position to point at in the global coordinate system (mm). * @return The constructed trajectory.
* @return The computed trajectory. Null if no solution was found. If the robot has already
* pointed at the target, the trajectory only contains the initial timestep (i.e.
* size()==1).
*/ */
TrajectoryPtr computeTrajectory(Eigen::Vector3f& target); armarx::TrajectoryPtr getTrajectory();
private: private:
std::unique_ptr<simox::control::simox::robot::Robot> reducedRobot_; std::unique_ptr<simox::control::simox::robot::Robot> reducedRobot_;
std::unique_ptr<simox::control::method::example::PointingIK> ik_; std::unique_ptr<simox::control::method::example::PointingIK> ik_;
std::vector<std::vector<double>> trajData; // actuated joints x timesteps
void appendTrajData(const Eigen::VectorXd& jointValues);
}; };
} // namespace armarx::control::pointing::core } // namespace armarx::control::pointing::core
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment