ZeroTorqueOrVelocityController.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 
8 #include "../utils.h"
9 
11 {
12 
13  void
14  ZeroTorqueOrVelocityController::initialize(const VirtualRobot::RobotNodeSetPtr& rns,
15  const VirtualRobot::RobotNodeSetPtr& rtRns,
16  const std::vector<size_t>& torqueControlledIndex,
17  const std::vector<size_t>& velocityControlledIndex)
18  {
20  numOfJoints = rns->getSize();
21 
22  jointIDTorqueMode = torqueControlledIndex;
23  jointIDVelocityMode = velocityControlledIndex;
24  // ARMARX_CHECK_EQUAL(jointIDTorqueMode.size() + jointIDVelocityMode.size(), numOfJoints);
27 
28  tcp = rns->getTCP();
29  rtTCP = rtRns->getTCP();
31  ik.reset(new VirtualRobot::DifferentialIK(
32  rtRns, rtRns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
33 
34 
36  }
37 
38  void
39  ZeroTorqueOrVelocityController::preactivateInit(const VirtualRobot::RobotNodeSetPtr& rns)
40  {
41  }
42 
43  void
45  {
46  ftsensor.reset();
47  }
48 
49  // bool
50  // ZeroTorqueOrVelocityController::isControlModeValid(
51  // const std::vector<std::string>& nameList,
52  // const std::map<std::string, std::string>& jointControlModeMap)
53  // {
54  // std::set<std::string> validModes = {"velocity", "torque"};
55 
56  // for (const auto& pair : jointControlModeMap)
57  // {
58  // if (std::find(nameList.begin(), nameList.end(), pair.first) == nameList.end())
59  // {
60  // ARMARX_ERROR << "ZeroTorqueOrVelocityController: joint name '" << pair.first
61  // << "' not found in robot node set.";
62  // return false;
63  // }
64 
65  // if (validModes.find(pair.second) == validModes.end())
66  // {
67  // ARMARX_ERROR << "ZeroTorqueOrVelocityController: Invalid control mode for joint '"
68  // << pair.first << "': " << pair.second << std::endl;
69  // return false;
70  // }
71  // }
72 
73  // return true;
74  // }
75 
76  // void
77  // ZeroTorqueOrVelocityController::updateFT(const ft::arondto::FTConfig& c, RtStatus& rtStatus)
78  // {
79  // /// run in rt thread
80  // ftsensor.updateStatus();
81  // if (c.reCalibrate)
82  // {
83  // ftsensor.calibrated.store(false);
84  // /// TODO how to toggle recalibration?
85  // }
86  // ftsensor.compensateTCPGravity(c);
87  // if (!ftsensor.calibrated.load())
88  // {
89  // if (ftsensor.calibrate(c, rtStatus.deltaT))
90  // {
91  // ftsensor.calibrated.store(true);
92  // }
93  // }
94  // else
95  // {
96  // rtStatus.currentForceTorque = ftsensor.getFilteredForceTorque(c);
97  // }
98  // }
99 
100  void
102  {
103  /// ------------------------------ compute jacobi -------------------------------------------------------------
104  rtStatus.jacobi =
105  ik->getJacobianMatrix(rtTCP, VirtualRobot::IKSolver::CartesianSelection::All);
106  rtStatus.jacobi.block(0, 0, 3, rtStatus.numJoints) =
107  0.001 * rtStatus.jacobi.block(0, 0, 3, rtStatus.numJoints);
108  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jacobi.cols());
109  ARMARX_CHECK_EQUAL(6, rtStatus.jacobi.rows());
110 
111  rtStatus.jpinv = ik->computePseudoInverseJacobianMatrix(rtStatus.jacobi, lambda);
112  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.jpinv.rows());
113  ARMARX_CHECK_EQUAL(6, rtStatus.jpinv.cols());
114 
115  /// ------------------------------ update velocity/twist ------------------------------------------------------
116  for (size_t i = 0; i < rtStatus.jointVelocity.size(); ++i)
117  {
118  rtStatus.qpos(i) = rtStatus.jointPosition[i];
119  rtStatus.qvel(i) = rtStatus.jointVelocity[i];
120  }
121  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.qpos.rows());
122  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.qvel.rows());
123  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.qvelFiltered.rows());
124 
125  rtStatus.qvelFiltered =
126  (1 - c.qvelFilter) * rtStatus.qvelFiltered + c.qvelFilter * rtStatus.qvel;
127 
128  // current twist is only modelled w.r.t. to two wrist joint
129  rtStatus.currentTwist.setZero();
130  for (size_t i = 0; i < 6; i++)
131  {
132  for (auto j : jointIDVelocityMode)
133  {
134  rtStatus.currentTwist(i) += rtStatus.jacobi(i, j) * rtStatus.qvelFiltered(j);
135  }
136  }
137 
138  /// ------------------------------------- Velocity control ---------------------------------------------------
139  /// TODO add doc
140  rtStatus.cartesianVelTarget.setZero();
141  if (rtStatus.currentForceTorque.head(3).norm() > c.forceAmplitudeDeadZone)
142  {
143  rtStatus.cartesianVelTarget.head(3) =
144  c.kpCartesianVel.head(3).cwiseProduct(rtStatus.currentForceTorque.head(3)) -
145  c.kdCartesianVel.head(3).cwiseProduct(rtStatus.currentTwist.head(3));
146  }
147  if (rtStatus.currentForceTorque.tail(3).norm() > c.torqueAmplitudeDeadZone)
148  {
149  rtStatus.cartesianVelTarget.tail(3) =
150  c.kpCartesianVel.tail(3).cwiseProduct(rtStatus.currentForceTorque.tail(3)) -
151  c.kdCartesianVel.tail(3).cwiseProduct(rtStatus.currentTwist.tail(3));
152  }
153 
154 
155  /// ----------------------------- Map TS target force/velocity to JS --------------------------------------------------
156  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.desiredJointTorques.rows());
157  ARMARX_CHECK_EQUAL(rtStatus.numJoints, rtStatus.desiredJointVelocity.rows());
158  rtStatus.desiredJointTorques.setZero();
159  rtStatus.desiredJointVelocity = rtStatus.jpinv * rtStatus.cartesianVelTarget;
160 
161  /// ----------------------------- write torque/velocity target --------------------------------------------------
162  for (int i = 0; i < rtStatus.desiredJointVelocity.rows(); ++i)
163  {
164  rtStatus.desiredJointVelocity(i) =
165  std::clamp(rtStatus.desiredJointVelocity(i), -c.velocityLimit, c.velocityLimit);
166  }
167  }
168 } // namespace armarx::control::common::control_law
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::desiredJointTorques
Eigen::VectorXf desiredJointTorques
targets
Definition: ZeroTorqueOrVelocityController.h:79
armarx::control::common::control_law::ZeroTorqueOrVelocityController::jointIDTorqueMode
std::vector< size_t > jointIDTorqueMode
Definition: ZeroTorqueOrVelocityController.h:121
armarx::control::common::control_law::ZeroTorqueOrVelocityController::ftsensor
common::ft::FTSensor ftsensor
Definition: ZeroTorqueOrVelocityController.h:119
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::cartesianVelTarget
Eigen::Vector6f cartesianVelTarget
task space variables
Definition: ZeroTorqueOrVelocityController.h:86
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
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::control_law::ZeroTorqueOrVelocityController::initialize
void initialize(const VirtualRobot::RobotNodeSetPtr &rns, const VirtualRobot::RobotNodeSetPtr &rtRns, const std::vector< size_t > &torqueControlledIndex, const std::vector< size_t > &velocityControlledIndex)
Definition: ZeroTorqueOrVelocityController.cpp:14
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::currentForceTorque
Eigen::Vector6f currentForceTorque
force torque
Definition: ZeroTorqueOrVelocityController.h:83
armarx::control::common::control_law::RobotStatus::jointVelocity
std::vector< float > jointVelocity
Definition: common.h:33
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::jacobi
Eigen::MatrixXf jacobi
others
Definition: ZeroTorqueOrVelocityController.h:95
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:125
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::qvel
Eigen::VectorXf qvel
Definition: ZeroTorqueOrVelocityController.h:90
ZeroTorqueOrVelocityController.h
armarx::control::common::control_law::ZeroTorqueOrVelocityController::jointIDVelocityMode
std::vector< size_t > jointIDVelocityMode
Definition: ZeroTorqueOrVelocityController.h:122
armarx::control::common::control_law::ZeroTorqueOrVelocityController::tcp
VirtualRobot::RobotNodePtr tcp
Definition: ZeroTorqueOrVelocityController.h:117
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::control::common::control_law::ZeroTorqueOrVelocityController::run
void run(Config &c, RtStatus &robotStatus)
Definition: ZeroTorqueOrVelocityController.cpp:101
armarx::control::common::control_law::ZeroTorqueOrVelocityController::preactivateInit
void preactivateInit(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: ZeroTorqueOrVelocityController.cpp:39
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::currentTwist
Eigen::Vector6f currentTwist
Definition: ZeroTorqueOrVelocityController.h:92
armarx::control::common::control_law
This file is part of ArmarX.
Definition: aron_conversions.cpp:68
armarx::control::common::control_law::ZeroTorqueOrVelocityController::nDoFTorque
size_t nDoFTorque
Definition: ZeroTorqueOrVelocityController.h:104
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::qpos
Eigen::VectorXf qpos
current status
Definition: ZeroTorqueOrVelocityController.h:89
armarx::control::common::ft::FTSensor::reset
void reset()
Definition: FTSensor.cpp:87
armarx::control::common::control_law::RobotStatus::jointPosition
std::vector< float > jointPosition
Definition: common.h:32
armarx::control::common::control_law::ZeroTorqueOrVelocityController::Config
arondto::ZeroTorqueOrVelocityControllerConfig Config
you can set the following values from outside of the rt controller via Ice interfaces
Definition: ZeroTorqueOrVelocityController.h:67
armarx::control::common::control_law::ZeroTorqueOrVelocityController::rtTCP
VirtualRobot::RobotNodePtr rtTCP
Definition: ZeroTorqueOrVelocityController.h:118
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::jpinv
Eigen::MatrixXf jpinv
Definition: ZeroTorqueOrVelocityController.h:96
ControlThreadOutputBuffer.h
armarx::control::common::control_law::ZeroTorqueOrVelocityController::firstRun
void firstRun()
Definition: ZeroTorqueOrVelocityController.cpp:44
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::ZeroTorqueOrVelocityController::numOfJoints
unsigned int numOfJoints
Definition: ZeroTorqueOrVelocityController.h:103
armarx::control::common::control_law::RobotStatus::numJoints
unsigned int numJoints
Definition: common.h:39
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::qvelFiltered
Eigen::VectorXf qvelFiltered
Definition: ZeroTorqueOrVelocityController.h:91
armarx::control::common::control_law::ZeroTorqueOrVelocityController::nDoFVelocity
size_t nDoFVelocity
Definition: ZeroTorqueOrVelocityController.h:105
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus
internal status of the controller, containing intermediate variables, mutable targets
Definition: ZeroTorqueOrVelocityController.h:76
armarx::control::common::control_law::ZeroTorqueOrVelocityController::RtStatus::desiredJointVelocity
Eigen::VectorXf desiredJointVelocity
Definition: ZeroTorqueOrVelocityController.h:80