TaskspaceAdmittanceController.cpp
Go to the documentation of this file.
2 
3 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
4 #include <VirtualRobot/MathTools.h>
5 
7 
9 
10 #include "../utils.h"
11 
13 {
14 
15  void
16  TaskspaceAdmittanceController::initialize(const VirtualRobot::RobotNodeSetPtr& rns,
17  const VirtualRobot::RobotNodeSetPtr& rtRns)
18  {
20  tcp = rns->getTCP();
21  rtTCP = rtRns->getTCP();
23  ik.reset(new VirtualRobot::DifferentialIK(
24  rtRns, rtRns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
25 
26  numOfJoints = rns->getSize();
28  }
29 
30  void
31  TaskspaceAdmittanceController::preactivateInit(const VirtualRobot::RobotNodeSetPtr& rns)
32  {
33  }
34 
35  void
37  {
38  ftsensor.reset();
39  }
40 
41  // void
42  // TaskspaceAdmittanceController::updateFT(const FTConfig& c, RtStatus& rtStatus)
43  // {
44  // /// run in rt thread
45  // ftsensor.updateStatus();
46  // if (c.reCalibrate)
47  // {
48  // ftsensor.calibrated.store(false);
49  // /// TODO how to toggle recalibration?
50  // }
51  // ftsensor.compensateTCPGravity(c);
52  // if (!ftsensor.calibrated.load())
53  // {
54  // if (ftsensor.calibrate(c, rtStatus.deltaT))
55  // {
56  // ftsensor.calibrated.store(true);
57  // }
58  // }
59  // else
60  // {
61  // rtStatus.currentForceTorque = ftsensor.getFilteredForceTorque(c);
62  // }
63  // }
64 
65  void
67  {
68  /// run in rt thread
69  rtStatus.currentPose = rtTCP->getPoseInRootFrame();
70 
71  /// ---------------------------- safety guard by FT and target pose ------------------------
72  rtStatus.poseDiffMatImp =
73  c.desiredPose.block<3, 3>(0, 0) * rtStatus.currentPose.block<3, 3>(0, 0).transpose();
74  rtStatus.oriDiffAngleAxis.fromRotationMatrix(rtStatus.poseDiffMatImp);
75 
76  /// this should not be moved to non rt, just in case non rt thread dies, rt still functions as safely
77  if (not ftsensor.isSafe(c.ftConfig))
78  {
79  ARMARX_INFO << "FTGuard indicates RT not safe";
80  c.desiredPose = rtStatus.desiredPose;
81  rtStatus.rtSafe = false;
82  rtStatus.rtTargetSafe = true;
83  }
84  else if (ftsensor.calibrated.load())
85  {
86  if ((c.desiredPose.block<3, 1>(0, 3) - rtStatus.currentPose.block<3, 1>(0, 3)).norm() >
87  c.safeDistanceMMToGoal or
88  std::fminf(rtStatus.oriDiffAngleAxis.angle(),
89  2 * M_PI - rtStatus.oriDiffAngleAxis.angle()) >
90  VirtualRobot::MathTools::deg2rad(c.safeRotAngleDegreeToGoal))
91  {
92  c.desiredPose = rtStatus.desiredPose;
93  rtStatus.rtSafe = false;
94  rtStatus.rtTargetSafe = false;
95  }
96  else
97  {
98  rtStatus.desiredPose = c.desiredPose;
99  rtStatus.rtSafe = true;
100  rtStatus.rtTargetSafe = true;
101  }
102  }
103  else
104  {
105  ARMARX_RT_LOGF_WARN("not initialized or ft sensor not calibrated!");
106  c.desiredTwist.setZero();
107  c.kmAdmittance.setZero();
108  }
109 
110  /// ------------------------------ compute jacobi ------------------------------------------
111  rtStatus.jacobi =
112  ik->getJacobianMatrix(rtTCP, VirtualRobot::IKSolver::CartesianSelection::All);
113  rtStatus.jacobi.block(0, 0, 3, rtStatus.numJoints) =
114  0.001 * rtStatus.jacobi.block(0, 0, 3, rtStatus.numJoints);
115  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jacobi.cols());
116  ARMARX_CHECK_EQUAL(6, rtStatus.jacobi.rows());
117 
118  rtStatus.jtpinv =
119  ik->computePseudoInverseJacobianMatrix(rtStatus.jacobi.transpose(), lambda);
120  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jtpinv.cols());
121  ARMARX_CHECK_EQUAL(6, rtStatus.jtpinv.rows());
122 
123  /// ------------------------------ update velocity/twist -----------------------------------
124  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jointPos.rows());
125  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jointVel.rows());
126  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.qvelFiltered.rows());
127 
128  rtStatus.qvelFiltered =
129  (1 - c.qvelFilter) * rtStatus.qvelFiltered + c.qvelFilter * rtStatus.jointVel;
130  rtStatus.currentTwist = rtStatus.jacobi * rtStatus.qvelFiltered;
131 
132  /// ---------------------------- admittance control ---------------------------------------------
133  /// calculate pose error between the virtual pose and the target pose
134  rtStatus.poseDiffMatAdm = rtStatus.virtualPose.block<3, 3>(0, 0) *
135  rtStatus.desiredPose.block<3, 3>(0, 0).transpose();
136  rtStatus.poseErrorAdm.head(3) =
137  rtStatus.virtualPose.block<3, 1>(0, 3) - rtStatus.desiredPose.block<3, 1>(0, 3);
138  rtStatus.poseErrorAdm.tail(3) =
139  VirtualRobot::MathTools::eigen3f2rpy(rtStatus.poseDiffMatAdm);
140 
141  /// admittance control law and Euler Integration -> virtual pose
142  rtStatus.acc = c.kmAdmittance.cwiseProduct(rtStatus.currentForceTorque) -
143  c.kpAdmittance.cwiseProduct(rtStatus.poseErrorAdm) -
144  c.kdAdmittance.cwiseProduct(rtStatus.virtualVel);
145 
146  rtStatus.vel =
147  rtStatus.virtualVel + 0.5 * rtStatus.deltaT * (rtStatus.acc + rtStatus.virtualAcc);
148  rtStatus.pos = 0.5 * rtStatus.deltaT * (rtStatus.vel + rtStatus.virtualVel);
149  rtStatus.virtualAcc = rtStatus.acc;
150  rtStatus.virtualVel = rtStatus.vel;
151 
152  rtStatus.virtualPose.block<3, 1>(0, 3) += rtStatus.pos.head(3);
153  rtStatus.virtualPose.block<3, 3>(0, 0) =
154  VirtualRobot::MathTools::rpy2eigen3f(
155  rtStatus.pos(3), rtStatus.pos(4), rtStatus.pos(5)) *
156  rtStatus.virtualPose.block<3, 3>(0, 0);
157 
158  /// ------------------------------------- Impedance control --------------------------------
159  /// calculate pose error between target pose and current pose
160  /// !!! This is very important: you have to keep postion and orientation both
161  /// with UI unit (meter, radian) to calculate impedance force.
162 
163  rtStatus.poseDiffMatImp = rtStatus.desiredPose.block<3, 3>(0, 0) *
164  rtStatus.currentPose.block<3, 3>(0, 0).transpose();
165  rtStatus.poseErrorImp.head(3) = 0.001 * (rtStatus.desiredPose.block<3, 1>(0, 3) -
166  rtStatus.currentPose.block<3, 1>(0, 3));
167  rtStatus.poseErrorImp.tail(3) =
168  VirtualRobot::MathTools::eigen3f2rpy(rtStatus.poseDiffMatImp);
169 
170  rtStatus.forceImpedance = c.kpImpedance.cwiseProduct(rtStatus.poseErrorImp) -
171  c.kdImpedance.cwiseProduct(rtStatus.currentTwist);
172 
173  /// ----------------------------- Nullspace PD Control -------------------------------------
174  ARMARX_CHECK_EQUAL(rtStatus.numJoints, c.kpNullspace.rows());
175  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.nullspaceTorque.rows());
176  ARMARX_CHECK_EQUAL(rtStatus.numJoints, c.desiredNullspaceJointAngles.value().size());
177  rtStatus.nullspaceTorque =
178  c.kpNullspace.cwiseProduct(c.desiredNullspaceJointAngles.value() - rtStatus.jointPos) -
179  c.kdNullspace.cwiseProduct(rtStatus.qvelFiltered);
180 
181  /// ----------------------------- Map TS target force to JS --------------------------------
182  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.desiredJointTorques.rows());
183  rtStatus.desiredJointTorques =
184  rtStatus.jacobi.transpose() * rtStatus.forceImpedance +
185  (I - rtStatus.jacobi.transpose() * rtStatus.jtpinv) * rtStatus.nullspaceTorque;
186 
187  /// ----------------------------- write torque target -------------------------------------
188  for (int i = 0; i < rtStatus.desiredJointTorques.rows(); ++i)
189  {
190  rtStatus.desiredJointTorques(i) =
191  std::clamp(rtStatus.desiredJointTorques(i), -c.torqueLimit, c.torqueLimit);
192  }
193  }
194 
195  void
197  {
198  /// This function can be called before the RT thread.
199  /// base
200  jointPosition.resize(nDoF, 0.);
201  jointVelocity.resize(nDoF, 0.);
202  jointTorque.resize(nDoF, 0.);
203  jointPos.setZero(nDoF);
204  jointVel.setZero(nDoF);
205  jointTor.setZero(nDoF);
206  deltaT = 0.0;
207  numJoints = nDoF;
208 
209  /// targets
210  desiredJointTorques.setZero(nDoF);
211  nullspaceTorque.setZero(nDoF);
212 
213  /// force torque
214  currentForceTorque.setZero();
215 
216  /// task space variables
217  forceImpedance.setZero();
218 
219  /// current status
220  qvelFiltered.setZero(nDoF);
221  currentPose.setZero();
222  currentTwist.setZero();
223  desiredPose.setZero();
224 
225  /// virtual status
226  virtualPose.setZero();
227  virtualVel.setZero();
228  virtualAcc.setZero();
229 
230  /// intermediate results
231  poseDiffMatImp.setZero();
232  poseDiffMatAdm.setZero();
233  poseErrorImp.setZero();
234  poseErrorAdm.setZero();
236  ARMARX_INFO << "------ " << VAROUT(oriDiffAngleAxis.angle());
237 
238  /// others
239  jacobi.setZero(6, nDoF);
240  jtpinv.setZero(6, nDoF);
241  rtSafe = false;
242  rtTargetSafe = true;
243  }
244 
245  void
247  {
248  /// !!!! TO BE UPDATED IN PREACTIVATE !!!!
249  /// only then the up-to-date robot joint information is available
250  currentPose = currentTCPPose;
251  desiredPose = currentTCPPose;
252  virtualPose = currentTCPPose;
253  }
254 
255 } // namespace armarx::control::common::control_law
armarx::control::common::control_law::RobotStatus::jointTor
Eigen::VectorXf jointTor
Definition: common.h:37
armarx::control::common::control_law::TaskspaceAdmittanceController::Config
common::control_law::arondto::TaskspaceAdmittanceControllerConfig Config
Definition: TaskspaceAdmittanceController.h:44
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus
internal status of the controller, containing intermediate variables, mutable targets
Definition: TaskspaceAdmittanceController.h:48
armarx::control::common::control_law::TaskspaceAdmittanceController::rtTCP
VirtualRobot::RobotNodePtr rtTCP
Definition: TaskspaceAdmittanceController.h:106
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::rtPreActivate
void rtPreActivate(const Eigen::Matrix4f &currentPose)
Definition: TaskspaceAdmittanceController.cpp:246
armarx::control::common::control_law::TaskspaceAdmittanceController::firstRun
void firstRun()
Definition: TaskspaceAdmittanceController.cpp:36
armarx::control::common::ft::FTSensor::isSafe
bool isSafe(const FTConfig &c)
Definition: FTSensor.cpp:192
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::virtualVel
Eigen::Vector6f virtualVel
Definition: TaskspaceAdmittanceController.h:56
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::nullspaceTorque
Eigen::VectorXf nullspaceTorque
Definition: TaskspaceAdmittanceController.h:52
armarx::control::common::control_law::RobotStatus::jointTorque
std::vector< float > jointTorque
Definition: common.h:34
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::reset
void reset(const unsigned int nDoF)
Definition: TaskspaceAdmittanceController.cpp:196
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::control::common::control_law::RobotStatus::jointPos
Eigen::VectorXf jointPos
Definition: common.h:35
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::poseErrorAdm
Eigen::Vector6f poseErrorAdm
Definition: TaskspaceAdmittanceController.h:60
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::control_law::RobotStatus::jointVelocity
std::vector< float > jointVelocity
Definition: common.h:33
armarx::control::common::control_law::TaskspaceAdmittanceController::ftsensor
common::ft::FTSensor ftsensor
Definition: TaskspaceAdmittanceController.h:107
device.h
armarx::control::common::control_law::TaskspaceAdmittanceController::numOfJoints
unsigned int numOfJoints
Definition: TaskspaceAdmittanceController.h:94
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::currentPose
Eigen::Matrix4f currentPose
Definition: TaskspaceAdmittanceController.h:75
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:125
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::forceImpedance
Eigen::Vector6f forceImpedance
task space variables
Definition: TaskspaceAdmittanceController.h:69
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::currentTwist
Eigen::Vector6f currentTwist
Definition: TaskspaceAdmittanceController.h:76
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::jtpinv
Eigen::MatrixXf jtpinv
Definition: TaskspaceAdmittanceController.h:86
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::oriDiffAngleAxis
Eigen::AngleAxisf oriDiffAngleAxis
Definition: TaskspaceAdmittanceController.h:82
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::control::common::control_law::TaskspaceAdmittanceController::preactivateInit
void preactivateInit(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: TaskspaceAdmittanceController.cpp:31
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::virtualPose
Eigen::Matrix4f virtualPose
variables that need to be kept locally by the rt thread
Definition: TaskspaceAdmittanceController.h:55
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::poseErrorImp
Eigen::Vector6f poseErrorImp
Definition: TaskspaceAdmittanceController.h:81
armarx::control::common::control_law::RobotStatus::jointVel
Eigen::VectorXf jointVel
Definition: common.h:36
armarx::control::common::control_law::TaskspaceAdmittanceController::run
void run(Config &c, RtStatus &robotStatus)
Definition: TaskspaceAdmittanceController.cpp:66
armarx::control::common::ft::FTSensor::calibrated
std::atomic_bool calibrated
Definition: FTSensor.h:70
armarx::control::common::control_law
This file is part of ArmarX.
Definition: aron_conversions.cpp:68
armarx::control::common::ft::FTSensor::reset
void reset()
Definition: FTSensor.cpp:87
TaskspaceAdmittanceController.h
armarx::control::common::control_law::RobotStatus::jointPosition
std::vector< float > jointPosition
Definition: common.h:32
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::acc
Eigen::Vector6f acc
Definition: TaskspaceAdmittanceController.h:61
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::qvelFiltered
Eigen::VectorXf qvelFiltered
Definition: TaskspaceAdmittanceController.h:74
for
for(;yybottom<=yytop;yybottom++)
Definition: Grammar.cpp:790
armarx::control::common::control_law::TaskspaceAdmittanceController::tcp
VirtualRobot::RobotNodePtr tcp
Definition: TaskspaceAdmittanceController.h:105
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::rtSafe
bool rtSafe
Definition: TaskspaceAdmittanceController.h:87
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::currentForceTorque
Eigen::Vector6f currentForceTorque
force torque
Definition: TaskspaceAdmittanceController.h:66
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::virtualAcc
Eigen::Vector6f virtualAcc
Definition: TaskspaceAdmittanceController.h:57
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::poseDiffMatAdm
Eigen::Matrix3f poseDiffMatAdm
Definition: TaskspaceAdmittanceController.h:59
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::pos
Eigen::Vector6f pos
Definition: TaskspaceAdmittanceController.h:63
ControlThreadOutputBuffer.h
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::poseDiffMatImp
Eigen::Matrix3f poseDiffMatImp
intermediate results
Definition: TaskspaceAdmittanceController.h:80
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::desiredJointTorques
Eigen::VectorXf desiredJointTorques
targets
Definition: TaskspaceAdmittanceController.h:51
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
ARMARX_RT_LOGF_WARN
#define ARMARX_RT_LOGF_WARN(...)
Definition: ControlThreadOutputBuffer.h:345
armarx::transpose
std::vector< std::vector< T > > transpose(const std::vector< std::vector< T >> &src, Thrower thrower)
Definition: SimoxCSpace.cpp:706
armarx::control::common::control_law::RobotStatus::numJoints
unsigned int numJoints
Definition: common.h:39
armarx::control::common::control_law::TaskspaceAdmittanceController::initialize
void initialize(const VirtualRobot::RobotNodeSetPtr &rns, const VirtualRobot::RobotNodeSetPtr &rtRns)
Definition: TaskspaceAdmittanceController.cpp:16
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::desiredPose
Eigen::Matrix4f desiredPose
Definition: TaskspaceAdmittanceController.h:77
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::rtTargetSafe
bool rtTargetSafe
Definition: TaskspaceAdmittanceController.h:88
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::vel
Eigen::Vector6f vel
Definition: TaskspaceAdmittanceController.h:62
armarx::control::common::control_law::RobotStatus::deltaT
double deltaT
Definition: common.h:38
armarx::control::common::control_law::TaskspaceAdmittanceController::RtStatus::jacobi
Eigen::MatrixXf jacobi
others
Definition: TaskspaceAdmittanceController.h:85