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