TaskspaceVelocityController.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
17  const VirtualRobot::RobotNodeSetPtr& rns,
18  const VirtualRobot::RobotNodeSetPtr& rtRns)
19  {
21  numOfJoints = rns->getSize();
22 
23  tcp = rns->getTCP();
24  rtTCP = rtRns->getTCP();
26  ik.reset(new VirtualRobot::DifferentialIK(
27  rtRns, rtRns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
28 
30  }
31 
32  void
34  const VirtualRobot::RobotNodeSetPtr& rns)
35  {
36  }
37 
38  void
40  {
41  ftsensor.reset();
42  }
43 
44  // bool
45  // TaskspaceVelocityController::isControlModeValid(
46  // const std::vector<std::string>& nameList,
47  // const std::map<std::string, std::string>& jointControlModeMap)
48  // {
49  // std::set<std::string> validModes = {"velocity", "torque"};
50  //
51  // for (const auto& pair : jointControlModeMap)
52  // {
53  // if (std::find(nameList.begin(), nameList.end(), pair.first) == nameList.end())
54  // {
55  // ARMARX_ERROR << "TaskspaceVelocityController: joint name '"
56  // << pair.first << "' not found in robot node set.";
57  // return false;
58  // }
59  //
60  // if (validModes.find(pair.second) == validModes.end())
61  // {
62  // ARMARX_ERROR
63  // << "TaskspaceVelocityController: Invalid control mode for joint '"
64  // << pair.first << "': " << pair.second << std::endl;
65  // return false;
66  // }
67  // }
68  //
69  // return true;
70  // }
71 
72  // void
73  // TaskspaceVelocityController::updateFT(const FTConfig& c, RtStatus& rtStatus)
74  // {
75  // /// run in rt thread
76  // ftsensor.updateStatus();
77  // if (c.reCalibrate)
78  // {
79  // ftsensor.calibrated.store(false);
80  // /// TODO how to toggle recalibration?
81  // }
82  // ftsensor.compensateTCPGravity(c);
83  // if (!ftsensor.calibrated.load())
84  // {
85  // if (ftsensor.calibrate(c, rtStatus.deltaT))
86  // {
87  // ftsensor.calibrated.store(true);
88  // }
89  // }
90  // else
91  // {
92  // rtStatus.currentForceTorque = ftsensor.getFilteredForceTorque(c);
93  // }
94  // }
95 
96  void
98  {
99  /// run in rt thread
100  rtStatus.currentPose = rtTCP->getPoseInRootFrame();
101 
102  /// ---------------------------- safety guard by FT and target pose ------------------------
103  rtStatus.poseDiffMatImp =
104  c.desiredPose.block<3, 3>(0, 0) * rtStatus.currentPose.block<3, 3>(0, 0).transpose();
105  rtStatus.oriDiffAngleAxis.fromRotationMatrix(rtStatus.poseDiffMatImp);
106 
107  /// this should not be moved to non rt, just in case non rt thread dies, rt still functions as safely
108  if (not ftsensor.isSafe(c.ftConfig))
109  {
110  ARMARX_INFO << "FTGuard indicates RT not safe";
111  c.desiredPose = rtStatus.desiredPose;
112  rtStatus.rtSafe = false;
113  rtStatus.rtTargetSafe = true;
114  }
115  else if ((c.desiredPose.block<3, 1>(0, 3) - rtStatus.currentPose.block<3, 1>(0, 3)).norm() >
116  c.safeDistanceMMToGoal or
117  std::fminf(rtStatus.oriDiffAngleAxis.angle(),
118  2 * M_PI - rtStatus.oriDiffAngleAxis.angle()) >
119  VirtualRobot::MathTools::deg2rad(c.safeRotAngleDegreeToGoal))
120  {
121  c.desiredPose = rtStatus.desiredPose;
122  rtStatus.rtSafe = false;
123  rtStatus.rtTargetSafe = false;
124  }
125  else
126  {
127  rtStatus.desiredPose = c.desiredPose;
128  rtStatus.rtSafe = true;
129  rtStatus.rtTargetSafe = true;
130  }
131 
132  /// ------------------------------ compute jacobi ------------------------------------------
133  rtStatus.jacobi =
134  ik->getJacobianMatrix(rtTCP, VirtualRobot::IKSolver::CartesianSelection::All);
135  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jacobi.cols());
136  ARMARX_CHECK_EQUAL(6, rtStatus.jacobi.rows());
137 
138  // rtStatus.jpinv = ik->computePseudoInverseJacobianMatrix(rtStatus.jacobi, lambda);
139  rtStatus.jpinv = ik->computePseudoInverseJacobianMatrix(
140  rtStatus.jacobi, ik->getJacobiRegularization(VirtualRobot::IKSolver::CartesianSelection::All));
141  ARMARX_CHECK_EQUAL(6, rtStatus.jpinv.cols());
142  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jpinv.rows());
143 
144  // rtStatus.jacobi.block(0, 0, 3, rtStatus.numJoints) =
145  // 0.001 * rtStatus.jacobi.block(0, 0, 3, rtStatus.numJoints);
146  // rtStatus.jpinv.block(0, 0, rtStatus.numJoints, 3) =
147  // 0.001 * rtStatus.jpinv.block(0, 0, rtStatus.numJoints, 3);
148 
149  /// ------------------------------ update velocity/twist -----------------------------------
150  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jointPos.rows());
151  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jointVel.rows());
152  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.qvelFiltered.rows());
153 
154  rtStatus.qvelFiltered =
155  (1 - c.qvelFilter) * rtStatus.qvelFiltered + c.qvelFilter * rtStatus.jointVel;
156  rtStatus.currentTwist = rtStatus.jacobi * rtStatus.qvelFiltered; /// in mm/s
157 
158  /// ------------------------------------- TS Error --------------------------------
159  /// calculate pose error between target pose and current pose
160  /// !!! This is very important: you have to keep position 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.head(3) = (rtStatus.desiredPose.block<3, 1>(0, 3) -
168  rtStatus.currentPose.block<3, 1>(0, 3)); /// in mm
169  rtStatus.poseErrorImp.tail(3) =
170  VirtualRobot::MathTools::eigen3f2rpy(rtStatus.poseDiffMatImp);
171 
172  /// ------------------------------------- Velocity control ---------------------------------------------------
173  /// calculate pose error between target pose and current pose
174  /// !!! This is very important: you have to keep position and orientation both
175  /// with UI unit (meter, radian) to calculate desired Cartesian velocity.
176  rtStatus.cartesianVelTarget = c.kpCartesianVel.cwiseProduct(rtStatus.poseErrorImp) -
177  c.kdCartesianVel.cwiseProduct(rtStatus.currentTwist);
178 
179  /// ----------------------------- Nullspace PD Control --------------------------------------------------
180  ARMARX_CHECK_EQUAL(rtStatus.numJoints, c.kpNullspaceVel.rows());
181  ARMARX_CHECK_EQUAL(rtStatus.numJoints, c.kdNullspaceVel.rows());
182  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.nullspaceVelocity.rows());
183  ARMARX_CHECK_EQUAL(rtStatus.numJoints, c.desiredNullspaceJointAngles.value().size());
184 
185  rtStatus.nullspaceVelocity =
186  c.kpNullspaceVel.cwiseProduct(c.desiredNullspaceJointAngles.value() -
187  rtStatus.jointPos) -
188  c.kdNullspaceVel.cwiseProduct(rtStatus.qvelFiltered);
189 
190  /// ----------------------------- Map TS target force/velocity to JS --------------------------------------------------
191  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.desiredJointVelocity.rows());
192  rtStatus.desiredJointVelocity =
193  rtStatus.jpinv * rtStatus.cartesianVelTarget +
194  (I - rtStatus.jpinv * rtStatus.jacobi) * rtStatus.nullspaceVelocity;
195 
196  /// ----------------------------- write torque target -------------------------------------
197  for (int i = 0; i < rtStatus.desiredJointVelocity.rows(); ++i)
198  {
199  rtStatus.desiredJointVelocity(i) =
200  std::clamp(rtStatus.desiredJointVelocity(i), -c.velocityLimit, c.velocityLimit);
201  }
202  }
203 
204  void
206  {
207  /// This function can be called before the RT thread.
208  /// base
209  jointPosition.resize(nDoF, 0.);
210  jointVelocity.resize(nDoF, 0.);
211  jointTorque.resize(nDoF, 0.);
212  jointPos.setZero(nDoF);
213  jointVel.setZero(nDoF);
214  jointTor.setZero(nDoF);
215  deltaT = 0.0;
216  numJoints = nDoF;
217  // nDoFTorque = numDoFTorque;
218  // nDoFVelocity = numDoFVelocity;
219  // ARMARX_CHECK_EQUAL(numJoints, nDoFTorque + nDoFVelocity);
220 
221  /// targets
222  desiredJointVelocity.setZero(nDoF);
223  cartesianVelTarget.setZero();
224  nullspaceVelocity.setZero(nDoF);
225 
226  /// force torque
227  currentForceTorque.setZero();
228 
229  /// current status
230  qvelFiltered.setZero(nDoF);
231  currentPose.setZero();
232  currentTwist.setZero();
233  desiredPose.setZero();
234 
235  /// intermediate results
236  poseDiffMatImp.setZero();
237  poseErrorImp.setZero();
239 
240  /// others
241  jacobi.setZero(6, nDoF);
242  jpinv.setZero(nDoF, 6);
243  rtSafe = false;
244  rtTargetSafe = true;
245  }
246 
247  void
249  const Eigen::Matrix4f& currentTCPPose)
250  {
251  /// !!!! TO BE UPDATED IN PREACTIVATE !!!!
252  /// only then the up-to-date robot joint information is available
253  currentPose = currentTCPPose;
254  desiredPose = currentTCPPose;
255  }
256 
257 } // 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::TaskspaceVelocityController::RtStatus::poseDiffMatImp
Eigen::Matrix3f poseDiffMatImp
intermediate results
Definition: TaskspaceVelocityController.h:74
armarx::control::common::ft::FTSensor::isSafe
bool isSafe(const FTConfig &c)
Definition: FTSensor.cpp:192
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::reset
void reset(const unsigned int nDoF)
Definition: TaskspaceVelocityController.cpp:205
armarx::control::common::control_law::TaskspaceVelocityController::initialize
void initialize(const VirtualRobot::RobotNodeSetPtr &rns, const VirtualRobot::RobotNodeSetPtr &rtRns)
Definition: TaskspaceVelocityController.cpp:16
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::currentPose
Eigen::Matrix4f currentPose
Definition: TaskspaceVelocityController.h:69
armarx::control::common::control_law::RobotStatus::jointTorque
std::vector< float > jointTorque
Definition: common.h:34
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::currentTwist
Eigen::Vector6f currentTwist
Definition: TaskspaceVelocityController.h:70
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::TaskspaceVelocityController::RtStatus::rtPreActivate
void rtPreActivate(const Eigen::Matrix4f &currentPose)
Definition: TaskspaceVelocityController.cpp:248
armarx::control::common::control_law::RobotStatus::jointPos
Eigen::VectorXf jointPos
Definition: common.h:35
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::rtSafe
bool rtSafe
Definition: TaskspaceVelocityController.h:82
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::control_law::TaskspaceVelocityController::firstRun
void firstRun()
Definition: TaskspaceVelocityController.cpp:39
armarx::control::common::control_law::RobotStatus::jointVelocity
std::vector< float > jointVelocity
Definition: common.h:33
device.h
armarx::control::common::control_law::TaskspaceVelocityController::preactivateInit
void preactivateInit(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: TaskspaceVelocityController.cpp:33
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::jacobi
Eigen::MatrixXf jacobi
others
Definition: TaskspaceVelocityController.h:79
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::nullspaceVelocity
Eigen::VectorXf nullspaceVelocity
Definition: TaskspaceVelocityController.h:56
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:125
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::control::common::control_law::TaskspaceVelocityController::rtTCP
VirtualRobot::RobotNodePtr rtTCP
Definition: TaskspaceVelocityController.h:108
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::control::common::control_law::RobotStatus::jointVel
Eigen::VectorXf jointVel
Definition: common.h:36
armarx::control::common::control_law
This file is part of ArmarX.
Definition: aron_conversions.cpp:68
armarx::control::common::control_law::TaskspaceVelocityController::numOfJoints
unsigned int numOfJoints
Definition: TaskspaceVelocityController.h:93
armarx::control::common::ft::FTSensor::reset
void reset()
Definition: FTSensor.cpp:87
armarx::control::common::control_law::TaskspaceVelocityController::tcp
VirtualRobot::RobotNodePtr tcp
Definition: TaskspaceVelocityController.h:107
armarx::control::common::control_law::RobotStatus::jointPosition
std::vector< float > jointPosition
Definition: common.h:32
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::desiredPose
Eigen::Matrix4f desiredPose
Definition: TaskspaceVelocityController.h:71
for
for(;yybottom<=yytop;yybottom++)
Definition: Grammar.cpp:790
armarx::control::common::control_law::TaskspaceVelocityController::run
void run(Config &c, RtStatus &robotStatus)
Definition: TaskspaceVelocityController.cpp:97
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::oriDiffAngleAxis
Eigen::AngleAxisf oriDiffAngleAxis
Definition: TaskspaceVelocityController.h:76
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::qvelFiltered
Eigen::VectorXf qvelFiltered
current status
Definition: TaskspaceVelocityController.h:68
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus
internal status of the controller, containing intermediate variables, mutable targets
Definition: TaskspaceVelocityController.h:50
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::desiredJointVelocity
Eigen::VectorXf desiredJointVelocity
targets
Definition: TaskspaceVelocityController.h:54
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::cartesianVelTarget
Eigen::Vector6f cartesianVelTarget
task space variables
Definition: TaskspaceVelocityController.h:63
TaskspaceVelocityController.h
ControlThreadOutputBuffer.h
armarx::control::common::control_law::TaskspaceVelocityController::ftsensor
common::ft::FTSensor ftsensor
Definition: TaskspaceVelocityController.h:109
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::jpinv
Eigen::MatrixXf jpinv
Definition: TaskspaceVelocityController.h:81
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::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::TaskspaceVelocityController::RtStatus::rtTargetSafe
bool rtTargetSafe
Definition: TaskspaceVelocityController.h:83
armarx::control::common::control_law::TaskspaceVelocityController::Config
common::control_law::arondto::TaskspaceVelocityControllerConfig Config
Definition: TaskspaceVelocityController.h:45
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::poseErrorImp
Eigen::Vector6f poseErrorImp
Definition: TaskspaceVelocityController.h:75
armarx::control::common::control_law::RobotStatus::deltaT
double deltaT
Definition: common.h:38
armarx::control::common::control_law::TaskspaceVelocityController::RtStatus::currentForceTorque
Eigen::Vector6f currentForceTorque
force torque
Definition: TaskspaceVelocityController.h:59