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 
25 //#include <ArmarXCore/core/time/TimeUtil.h>
26 //#include <ArmarXCore/observers/variant/DatafieldRef.h>
27 
28 
31 #include <RobotAPI/interface/units/RobotUnit/NJointCartesianVelocityControllerWithRamp.h>
33 
34 using namespace armarx;
35 using namespace RTMotionControlGoup;
36 
37 // DO NOT EDIT NEXT LINE
38 RTCartesianPositionControl::SubClassRegistry RTCartesianPositionControl::Registry(RTCartesianPositionControl::GetName(), &RTCartesianPositionControl::CreateInstance);
39 
40 
41 
43 {
44  // put your user code for the enter-point here
45  // execution time should be short (<100ms)
46 }
47 
49 {
50  // put your user code for the execution-phase here
51  // runs in seperate thread, thus can do complex operations
52  // should check constantly whether isRunningTaskStopped() returns true
53  CycleUtil cycle(10);
54  auto robot = getLocalRobot();
55  //auto cameraRobot = getLocalRobot(); // receives new copy
56  auto targetPosition = in.getTargetPosition();
57  auto targetOrientation = in.getTargetOrientation();
58  //ARMARX_CHECK_EXPRESSION(robot.get() != cameraRobot.get());
59 
60  auto kinematicChainName = in.getKinematicChainName();
61  auto nodeset = robot->getRobotNodeSet(kinematicChainName);
62  auto tcp = nodeset->getTCP();
63 
64  auto ctrlName = "RTCartesianPositionControlTCPCtrl";
65  NJointCartesianVelocityControllerWithRampConfigPtr ctrlCfg = new NJointCartesianVelocityControllerWithRampConfig();
66  ctrlCfg->KpJointLimitAvoidance = 0;
67  ctrlCfg->jointLimitAvoidanceScale = 1;
68  ctrlCfg->mode = CartesianSelectionMode::eAll;
69  ctrlCfg->nodeSetName = kinematicChainName;
70  ctrlCfg->tcpName = tcp->getName();
71  ctrlCfg->maxNullspaceAcceleration = 2;
72  ctrlCfg->maxPositionAcceleration = in.getTCPAcceleration();
73  ctrlCfg->maxOrientationAcceleration = 1;
74 
75 
76  auto baseCtrl = getRobotUnit()->createNJointController("NJointCartesianVelocityControllerWithRamp", ctrlName, ctrlCfg);
77  ARMARX_CHECK_EXPRESSION(baseCtrl);
78  NJointCartesianVelocityControllerWithRampInterfacePrx ctrl = NJointCartesianVelocityControllerWithRampInterfacePrx::checkedCast(baseCtrl);
79  ctrl->activateController();
80 
81  CartesianPositionController posController(tcp);
82  posController.maxOriVel = in.getMaxOrientationVelocity();
83  posController.maxPosVel = in.getMaxTCPVelocity();
84  posController.KpOri = in.getKpOrientation();
85  posController.KpPos = in.getKp();
86 
87 
88  FramedPositionPtr targetPositioninRootFrame = new FramedPosition(*targetPosition);
89  targetPositioninRootFrame->changeFrame(robot, robot->getRootNode()->getName());
90 
91  // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
92  while (!isRunningTaskStopped()) // stop run function if returning true
93  {
94  RemoteRobot::synchronizeLocalClone(robot, getRobotStateComponent());
95  auto targetPose = Pose(targetOrientation->toRootEigen(robot), targetPosition->toRootEigen(robot)).toEigen();
96  float distanceToTarget = (tcp->getPositionInRootFrame() - targetPositioninRootFrame->toEigen()).norm();
97  ARMARX_INFO << deactivateSpam(1) << "Distance To Target: " << distanceToTarget;
98  ARMARX_INFO << deactivateSpam(1) << "Position Error: " << posController.getPositionError(targetPose);
99  ARMARX_INFO << deactivateSpam(1) << "Orientation Error" << posController.getOrientationError(targetPose);
100 
101  if (posController.getPositionError(targetPose) < in.getAbsPositionDifference() && posController.getOrientationError(targetPose) < in.getAbsOrientationDifference())
102  {
103  //if (distanceToTarget < 100){
104  ARMARX_INFO << "TARGET REACHED";
105  emitTargetReached();
106  break;
107  }
108  Eigen::VectorXf cv = posController.calculate(targetPose, VirtualRobot::IKSolver::All);
109  ctrl->setTargetVelocity(cv(0), cv(1), cv(2), cv(3), cv(4), cv(5));
110 
111  cycle.waitForCycleDuration();
112  }
113  ctrl->deactivateAndDeleteController();
114 }
115 
116 //void RTCartesianPositionControl::onBreak()
117 //{
118 // // put your user code for the breaking point here
119 // // execution time should be short (<100ms)
120 //}
121 
123 {
124  // put your user code for the exit point here
125  // execution time should be short (<100ms)
126 }
127 
128 
129 // DO NOT EDIT NEXT FUNCTION
131 {
132  return XMLStateFactoryBasePtr(new RTCartesianPositionControl(stateData));
133 }
134 
armarx::RTMotionControlGoup::RTCartesianPositionControl::Registry
static SubClassRegistry Registry
Definition: RTCartesianPositionControl.h:45
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::RTMotionControlGoup::RTCartesianPositionControl::RTCartesianPositionControl
RTCartesianPositionControl(const XMLStateConstructorParams &stateData)
Definition: RTCartesianPositionControl.h:32
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:448
Pose.h
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:76
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
armarx::RTMotionControlGoup::RTCartesianPositionControl::onEnter
void onEnter() override
Definition: RTCartesianPositionControl.cpp:42
armarx::RTMotionControlGoup::RTCartesianPositionControl::onExit
void onExit() override
Definition: RTCartesianPositionControl.cpp:122
armarx::RobotUnitModule::ControllerManagement::createNJointController
NJointControllerInterfacePrx createNJointController(const std::string &className, const std::string &instanceName, const NJointControllerConfigPtr &config, const Ice::Current &=Ice::emptyCurrent) override
Cretes a NJointControllerBase.
Definition: RobotUnitModuleControllerManagement.cpp:176
armarx::RTMotionControlGoup::RTCartesianPositionControl::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: RTCartesianPositionControl.cpp:130
armarx::getRobotUnit
RobotUnit * getRobotUnit(RobotUnitModule::ControllerManagement *cmngr)
Definition: NJointController.cpp:35
IceInternal::Handle< FramedPosition >
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:93
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:75
armarx::CycleUtil::waitForCycleDuration
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition: CycleUtil.cpp:53
armarx::RTMotionControlGoup::RTCartesianPositionControl::run
void run() override
Definition: RTCartesianPositionControl.cpp:48
CartesianPositionController.h
CycleUtil.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::NJointTaskSpaceDMPControllerMode::eAll
@ eAll
Definition: ControllerInterface.ice:38
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::CartesianPositionController
Definition: CartesianPositionController.h:41
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:77
cv
Definition: helper.h:35
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:74
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
armarx::CartesianPositionController::getOrientationError
float getOrientationError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:98
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
RTCartesianPositionControl.h
norm
double norm(const Point &a)
Definition: point.hpp:94
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:37