RTCartesianPositionControl.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package RobotSkillTemplates::RTMotionControlGoup
17* @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18* @date 2019
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
24#include <VirtualRobot/RobotNodeSet.h>
25
26//#include <ArmarXCore/core/time/TimeUtil.h>
27//#include <ArmarXCore/observers/variant/DatafieldRef.h>
28
29
31
32#include <RobotAPI/interface/units/RobotUnit/NJointCartesianVelocityControllerWithRamp.h>
35
36using namespace armarx;
37using namespace RTMotionControlGoup;
38
39// DO NOT EDIT NEXT LINE
40RTCartesianPositionControl::SubClassRegistry
41 RTCartesianPositionControl::Registry(RTCartesianPositionControl::GetName(),
43
44void
46{
47 // put your user code for the enter-point here
48 // execution time should be short (<100ms)
49}
50
51void
53{
54 // put your user code for the execution-phase here
55 // runs in seperate thread, thus can do complex operations
56 // should check constantly whether isRunningTaskStopped() returns true
57 CycleUtil cycle(10);
58 auto robot = getLocalRobot();
59 //auto cameraRobot = getLocalRobot(); // receives new copy
60 auto targetPosition = in.getTargetPosition();
61 auto targetOrientation = in.getTargetOrientation();
62 //ARMARX_CHECK_EXPRESSION(robot.get() != cameraRobot.get());
63
64 auto kinematicChainName = in.getKinematicChainName();
65 auto nodeset = robot->getRobotNodeSet(kinematicChainName);
66 auto tcp = nodeset->getTCP();
67
68 auto ctrlName = "RTCartesianPositionControlTCPCtrl";
69 NJointCartesianVelocityControllerWithRampConfigPtr ctrlCfg =
70 new NJointCartesianVelocityControllerWithRampConfig();
71 ctrlCfg->KpJointLimitAvoidance = 0;
72 ctrlCfg->jointLimitAvoidanceScale = 1;
73 ctrlCfg->mode = CartesianSelectionMode::eAll;
74 ctrlCfg->nodeSetName = kinematicChainName;
75 ctrlCfg->tcpName = tcp->getName();
76 ctrlCfg->maxNullspaceAcceleration = 2;
77 ctrlCfg->maxPositionAcceleration = in.getTCPAcceleration();
78 ctrlCfg->maxOrientationAcceleration = 1;
79
80
81 auto baseCtrl = getRobotUnit()->createNJointController(
82 "NJointCartesianVelocityControllerWithRamp", ctrlName, ctrlCfg);
84 NJointCartesianVelocityControllerWithRampInterfacePrx ctrl =
85 NJointCartesianVelocityControllerWithRampInterfacePrx::checkedCast(baseCtrl);
86 ctrl->activateController();
87
88 CartesianPositionController posController(tcp);
89 posController.maxOriVel = in.getMaxOrientationVelocity();
90 posController.maxPosVel = in.getMaxTCPVelocity();
91 posController.KpOri = in.getKpOrientation();
92 posController.KpPos = in.getKp();
93
94
95 FramedPositionPtr targetPositioninRootFrame = new FramedPosition(*targetPosition);
96 targetPositioninRootFrame->changeFrame(robot, robot->getRootNode()->getName());
97
98 // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
99 while (!isRunningTaskStopped()) // stop run function if returning true
100 {
101 RemoteRobot::synchronizeLocalClone(robot, getRobotStateComponent());
102 auto targetPose =
103 Pose(targetOrientation->toRootEigen(robot), targetPosition->toRootEigen(robot))
104 .toEigen();
105 float distanceToTarget =
106 (tcp->getPositionInRootFrame() - targetPositioninRootFrame->toEigen()).norm();
107 ARMARX_INFO << deactivateSpam(1) << "Distance To Target: " << distanceToTarget;
109 << "Position Error: " << posController.getPositionError(targetPose);
110 ARMARX_INFO << deactivateSpam(1) << "Orientation Error"
111 << posController.getOrientationError(targetPose);
112
113 if (posController.getPositionError(targetPose) < in.getAbsPositionDifference() &&
114 posController.getOrientationError(targetPose) < in.getAbsOrientationDifference())
115 {
116 //if (distanceToTarget < 100){
117 ARMARX_INFO << "TARGET REACHED";
118 emitTargetReached();
119 break;
120 }
121 Eigen::VectorXf cv = posController.calculate(targetPose, VirtualRobot::IKSolver::All);
122 ctrl->setTargetVelocity(cv(0), cv(1), cv(2), cv(3), cv(4), cv(5));
123
124 cycle.waitForCycleDuration();
125 }
126 ctrl->deactivateAndDeleteController();
127}
128
129//void RTCartesianPositionControl::onBreak()
130//{
131// // put your user code for the breaking point here
132// // execution time should be short (<100ms)
133//}
134
135void
137{
138 // put your user code for the exit point here
139 // execution time should be short (<100ms)
140}
141
142// DO NOT EDIT NEXT FUNCTION
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
float getPositionError(const Eigen::Matrix4f &targetPose) const
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
float getOrientationError(const Eigen::Matrix4f &targetPose) const
This util class helps with keeping a cycle time during a control cycle.
Definition CycleUtil.h:41
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition CycleUtil.cpp:53
The FramedPosition class.
Definition FramedPose.h:158
The Pose class.
Definition Pose.h:243
virtual Eigen::Matrix4f toEigen() const
Definition Pose.cpp:334
RTCartesianPositionControl(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
NJointControllerInterfacePrx createNJointController(const std::string &className, const std::string &instanceName, const NJointControllerConfigPtr &config, const Ice::Current &=Ice::emptyCurrent) override
Cretes a NJointControllerBase.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file offers overloads of toIce() and fromIce() functions for STL container types.
RobotUnit * getRobotUnit(RobotUnitModule::ControllerManagement *cmngr)
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64
Definition helper.h:35