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