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 
36 using namespace armarx;
37 using namespace RTMotionControlGoup;
38 
39 // DO NOT EDIT NEXT LINE
40 RTCartesianPositionControl::SubClassRegistry
41  RTCartesianPositionControl::Registry(RTCartesianPositionControl::GetName(),
43 
44 void
46 {
47  // put your user code for the enter-point here
48  // execution time should be short (<100ms)
49 }
50 
51 void
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);
83  ARMARX_CHECK_EXPRESSION(baseCtrl);
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 
135 void
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
145 {
146  return XMLStateFactoryBasePtr(new RTCartesianPositionControl(stateData));
147 }
armarx::RTMotionControlGoup::RTCartesianPositionControl::Registry
static SubClassRegistry Registry
Definition: RTCartesianPositionControl.h:46
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:522
Pose.h
armarx::CartesianPositionController::maxPosVel
float maxPosVel
Definition: CartesianPositionController.h:82
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
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:45
armarx::RTMotionControlGoup::RTCartesianPositionControl::onExit
void onExit() override
Definition: RTCartesianPositionControl.cpp:136
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:144
armarx::getRobotUnit
RobotUnit * getRobotUnit(RobotUnitModule::ControllerManagement *cmngr)
Definition: NJointController.cpp:35
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< FramedPosition >
armarx::CartesianPositionController::getPositionError
float getPositionError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:104
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::CartesianPositionController::KpOri
float KpOri
Definition: CartesianPositionController.h:81
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:52
CartesianPositionController.h
CycleUtil.h
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:181
armarx::CartesianPositionController
Definition: CartesianPositionController.h:42
armarx::CartesianPositionController::maxOriVel
float maxOriVel
Definition: CartesianPositionController.h:83
cv
Definition: helper.h:34
armarx::CartesianPositionController::KpPos
float KpPos
Definition: CartesianPositionController.h:80
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:38
armarx::CartesianPositionController::getOrientationError
float getOrientationError(const Eigen::Matrix4f &targetPose) const
Definition: CartesianPositionController.cpp:110
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
RTCartesianPositionControl.h
norm
double norm(const Point &a)
Definition: point.hpp:102
armarx::CartesianPositionController::calculate
Eigen::VectorXf calculate(const Eigen::Matrix4f &targetPose, VirtualRobot::IKSolver::CartesianSelection mode) const
Definition: CartesianPositionController.cpp:45