common.h
Go to the documentation of this file.
1 /**
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @author Jianfeng Gao ( jianfeng dot gao at kit dot edu )
17  * @date 2022
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #pragma once
23 
24 #include <VirtualRobot/Robot.h>
25 
26 
28 {
29 
30  struct RobotStatus
31  {
32  std::vector<float> jointPosition;
33  std::vector<float> jointVelocity;
34  std::vector<float> jointTorque;
35  double deltaT = 0;
36  };
37 
39  {
41  std::vector<float> jointValuesFullRobot;
42  };
43 
45  {
46  std::vector<float> targets;
47  };
48 
49  inline Eigen::MatrixXf
50  computeJointLimitWeightMatrix(const Eigen::Ref<const Eigen::VectorXf>& jointAngles,
51  const Eigen::Ref<const Eigen::VectorXf>& jointLimitsLow,
52  const Eigen::Ref<const Eigen::VectorXf>& jointLimitsHigh,
53  Eigen::VectorXf& jointAngleLimitGradient)
54  {
55  // temporally copied from simox control
56  // Based on "A weighted least-norm solution based scheme for avoiding joint limits for redundant joint manipulators", T.F.Chan (1995)
57  int nbJoints = jointAngles.size();
58  Eigen::VectorXf weights(nbJoints);
59  Eigen::VectorXf gradient = Eigen::VectorXf::Zero(nbJoints);
60 
61  if (jointAngleLimitGradient.rows() == 0)
62  {
63  jointAngleLimitGradient = Eigen::VectorXf::Zero(nbJoints);
64  }
65 
66  for (int i = 0; i < nbJoints; i++)
67  {
68  if ((jointLimitsLow(i) == 0 && jointLimitsHigh(i) == 0) || jointLimitsHigh(i) > 1e8)
69  {
70  // Joint is limitless
71  weights(i) = 1.;
72  }
73  else
74  {
75  gradient(i) = std::pow(jointLimitsHigh(i) - jointLimitsLow(i), 2) *
76  (2 * jointAngles(i) - jointLimitsHigh(i) - jointLimitsLow(i)) /
77  (4 * std::pow(jointLimitsHigh(i) - jointAngles(i), 2) *
78  std::pow(jointAngles(i) - jointLimitsLow(i), 2));
79 
80  if ((std::abs(gradient(i)) - std::abs(jointAngleLimitGradient(i))) >= 0.)
81  {
82  // Joint going towards limits
83  weights(i) = 1. / std::sqrt((1. + std::abs(gradient(i))));
84  }
85  else
86  {
87  // Joint going towards the center
88  weights(i) = 1.;
89  }
90  }
91  }
92  jointAngleLimitGradient = gradient;
93  return weights.asDiagonal();
94  }
95 
96 
97 } // namespace armarx::control::common::control_law
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::control::common::control_law::ControlTarget::targets
std::vector< float > targets
Definition: common.h:46
armarx::control::common::control_law::RobotStatus::jointTorque
std::vector< float > jointTorque
Definition: common.h:34
armarx::control::common::control_law::RobotStatus::jointVelocity
std::vector< float > jointVelocity
Definition: common.h:33
armarx::control::common::control_law::VirtualRobotStatus::globalPose
Eigen::Matrix4f globalPose
Definition: common.h:40
armarx::control::common::control_law::VirtualRobotStatus::jointValuesFullRobot
std::vector< float > jointValuesFullRobot
Definition: common.h:41
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::control::common::control_law::RobotStatus
Definition: common.h:30
armarx::control::common::control_law::VirtualRobotStatus
Definition: common.h:38
armarx::control::common::control_law
namespace armarx::control::common::ft
Definition: aron_conversions.cpp:62
armarx::control::common::control_law::RobotStatus::jointPosition
std::vector< float > jointPosition
Definition: common.h:32
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::control::common::control_law::computeJointLimitWeightMatrix
Eigen::MatrixXf computeJointLimitWeightMatrix(const Eigen::Ref< const Eigen::VectorXf > &jointAngles, const Eigen::Ref< const Eigen::VectorXf > &jointLimitsLow, const Eigen::Ref< const Eigen::VectorXf > &jointLimitsHigh, Eigen::VectorXf &jointAngleLimitGradient)
Definition: common.h:50
armarx::control::common::control_law::ControlTarget
Definition: common.h:44
armarx::control::common::control_law::RobotStatus::deltaT
double deltaT
Definition: common.h:35