utils.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 #include <Eigen/Geometry>
23 
24 #include <SimoxUtility/json.h>
25 
28 
30 
31 #include <armarx/control/common/mp/aron/MPConfig.aron.generated.h>
33 
34 #include <mplib/core/types.h>
35 
37 {
38 
39  Eigen::VectorXf getEigenVec(nlohmann::json& userConfig,
40  nlohmann::json& defaultConfig,
41  const std::string& entryName,
42  int size = 0,
43  int value = 0);
44 
45  template <typename T>
46  void
47  getEigenVec(nlohmann::json& userConfig,
48  nlohmann::json& defaultConfig,
49  const std::string& entryName,
50  T& vec)
51  {
52  if (userConfig.find(entryName) != userConfig.end())
53  {
54  vec = userConfig[entryName];
55  }
56  else
57  {
58  vec = defaultConfig[entryName];
59  }
60  }
61 
62  void
63  getEigenMatrix(nlohmann::json& userConfig, const std::string& entryName, Eigen::MatrixXf& mat);
64 
65  Eigen::VectorXf getEigenVecWithDefault(nlohmann::json& userConfig,
66  Eigen::VectorXf defaultValue,
67  const std::string& entryName);
68 
69  template <typename T>
70  T
71  getValue(nlohmann::json& userConfig,
72  nlohmann::json& defaultConfig,
73  const std::string& entryName)
74  {
75  if (userConfig.find(entryName) != userConfig.end())
76  {
77  return userConfig[entryName].get<T>();
78  }
79  else
80  {
81  return defaultConfig[entryName].get<T>();
82  }
83  }
84 
85  template <typename T>
86  T
87  getValueWithDefault(nlohmann::json& userConfig, T defaultValue, const std::string& entryName)
88  {
89  if (userConfig.find(entryName) != userConfig.end())
90  {
91  return userConfig[entryName].get<T>();
92  }
93  else
94  {
95  return defaultValue;
96  }
97  }
98 
99  void
100  debugEigenPose(StringVariantBaseMap& datafields, const std::string& name, Eigen::Matrix4f pose);
101  void
102  debugEigenVec(StringVariantBaseMap& datafields, const std::string& name, Eigen::VectorXf vec);
103 
104  void
105  debugStdVec(StringVariantBaseMap& datafields, const std::string& name, std::vector<float> vec);
106 
107  void getError(Eigen::Matrix4f& currentPose,
108  Eigen::Vector3d& targetPosition,
109  Eigen::Quaterniond& targetQuaternion,
110  double& posiError,
111  double& oriError);
112 
113  void to_json(nlohmann::json& j, const Pose& fp);
114 
115  void from_json(const nlohmann::json& j, Pose& fp);
116 
117  void to_json(nlohmann::json& j, const PosePtr& fp);
118 
119  void from_json(const nlohmann::json& j, PosePtr& fp);
120 
121  void to_json(nlohmann::json& j, const PoseBasePtr& fp);
122 
123  void from_json(const nlohmann::json& j, PoseBasePtr& fp);
124 
125  Eigen::Block<Eigen::Matrix4f, 3, 3> getOri(Eigen::Matrix4f& matrix);
126 
127  Eigen::Block<Eigen::Matrix4f, 3, 1> getPos(Eigen::Matrix4f& matrix);
128 
129 
130  PosePtr dVecToPose(const mplib::core::DVec& dvec);
131 
132  /**
133  * @brief create Eigen:Matrix4f from 7D double vector.
134  * @param dvec [x, y, z, qw, qx, qy, qz] format
135  * @return Eigen::Matrix4f \in SE(3)
136  *
137  * Note that, the order of the quaternion elements follows w, x, y, z format
138  */
140 
142 
143  /**
144  * @brief convert Eigen:Matrix4f to 7D double vector.
145  * @param mat Eigen::Matrix4f \in SE(3)
146  * @return vector of format [x, y, z, qw, qx, qy, qz]
147  *
148  * Note that, the order of the quaternion elements follows w, x, y, z format
149  */
151 
152  std::vector<float> poseToFVec(PosePtr& pose);
153 
154  std::vector<float> mat4ToFVec(const Eigen::Matrix4f& mat);
155 
156  std::string dVecToString(const mplib::core::DVec& dvec);
157 
158  Eigen::VectorXf vecToEigen(std::vector<float>& vec);
159 
160  std::string sVecToString(const std::vector<std::string>& vec,
161  const std::string& delimiter = ", ");
162 
163  armarx::control::common::mp::arondto::MPTraj
164  mpTrajFromFile(std::string filename, bool containsHeader = true, bool noTimeStamp = false);
165 
166  /**
167  * @brief create a dummy trajectory that only has zeros for every dimension.
168  * @param dimension the dimension of the trajectory, e.g. 7d for taskspace and
169  * Nd for N-joint kinematic chain.
170  * @param timesteps the total number of timesteps in the trajectory
171  * @return the MPTraj instance
172  *
173  * Note that, we you use this to configure MPs and execute it after, you'll have to set
174  * a proper start and goal for it, either set them in the MPListConfig for the corresponding
175  * MP and call startAll(), or use start("MPName", start, goal, duration_second).
176  * Otherwise, the MP will assume the first and last timestep being the start and goal, which
177  * are all zeros and therefore wrong for your task.
178  */
179  mp::arondto::MPTraj createDummyTraj(const int dimension, const int timesteps = 100);
180 
181  mp::arondto::MPConfig getDefaultMPConfig(
182  MPType mpType,
183  const std::string& name,
184  const std::string& nodeSetName,
185  const double& durationSec = -1.0,
186  const std::vector<mp::arondto::MPTraj>& mpTraj = std::vector<mp::arondto::MPTraj>(),
187  const std::vector<mp::arondto::ListViaPoint>& viaPointList =
188  std::vector<mp::arondto::ListViaPoint>());
189 
190  mp::arondto::MPConfig
191  getDefaultMPConfig(MPType mpType,
192  const std::string& name,
193  const std::string& nodeSetName,
194  const double& durationSec,
195  const std::vector<std::string>& mpTraj = std::vector<std::string>(),
196  const std::vector<mp::arondto::ListViaPoint>& viaPointList =
197  std::vector<mp::arondto::ListViaPoint>());
198 
199  void addViaPoint(mp::arondto::MPConfig& cfg,
200  const double& canValue,
201  const std::vector<double>& viaPoint);
202  void addViaPoint(mp::arondto::MPConfig& cfg,
203  const double& canValue,
204  const std::vector<float>& viaPoint);
205  void addViaPoint(mp::arondto::MPConfig& cfg,
206  const double& canValue,
207  const Eigen::VectorXf& viaPoint);
208  void
209  addViaPoint(mp::arondto::MPConfig& cfg, const double& canValue, const Eigen::Matrix4f& viaPose);
210 
211  void appendToViapointList(std::vector<mp::arondto::ListViaPoint>& vpList,
212  const double canonicalValue,
213  const std::vector<double>& viaPoint);
214  void appendToViapointList(std::vector<mp::arondto::ListViaPoint>& vpList,
215  const double canonicalValue,
216  const std::vector<float>& viaPoint);
217  void appendToViapointList(std::vector<mp::arondto::ListViaPoint>& vpList,
218  const double canonicalValue,
219  const Eigen::VectorXf& viaPoint);
220  void appendToViapointList(std::vector<mp::arondto::ListViaPoint>& vpList,
221  const double canonicalValue,
222  const Eigen::Matrix4f& viaPose);
223 
224  float getDeltaAngleBetweenRotMat(const Eigen::Matrix3f& rotMat1, const Eigen::Matrix3f& rotMat2);
225  float getDeltaAngleBetweenPose(const Eigen::Matrix4f& pose1, const Eigen::Matrix4f& pose2);
226 
227  bool poseDeviationTooLarge(const Eigen::Matrix4f& currentPose,
228  const Eigen::Matrix4f& desiredPose,
229  float positionThrMM,
230  float angleThrDeg);
231  void reportPoseDeviationWarning(const std::string& who,
232  const Eigen::Matrix4f& pose1,
233  const Eigen::Matrix4f& pose2,
234  const std::string& namePose1,
235  const std::string& namePose2);
237  const Eigen::Matrix4f& pose1,
238  const Eigen::Matrix4f& pose2,
239  const std::string& namePose1,
240  const std::string& namePose2,
241  float positionThrMM,
242  float angleThrDeg,
243  const std::string& who);
244 
248 } // namespace armarx::control::common
armarx::control::common::mirrorLeftRightPose
Eigen::Matrix4f mirrorLeftRightPose(Eigen::Matrix4f &sourcePose)
Definition: utils.cpp:621
armarx::control::common::debugEigenPose
void debugEigenPose(StringVariantBaseMap &datafields, const std::string &name, Eigen::Matrix4f pose)
Definition: utils.cpp:113
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::control::common::getOri
Eigen::Block< Eigen::Matrix4f, 3, 3 > getOri(Eigen::Matrix4f &matrix)
Definition: utils.cpp:200
armarx::control::common::createDummyTraj
mp::arondto::MPTraj createDummyTraj(const int dimension, const int timesteps)
create a dummy trajectory that only has zeros for every dimension.
Definition: utils.cpp:414
armarx::control::common::getValueWithDefault
T getValueWithDefault(nlohmann::json &userConfig, T defaultValue, const std::string &entryName)
Definition: utils.h:87
armarx::control::common::getEigenVecWithDefault
Eigen::VectorXf getEigenVecWithDefault(nlohmann::json &userConfig, Eigen::VectorXf defaultValue, const std::string &entryName)
Definition: utils.cpp:76
armarx::control::common::detectAndReportPoseDeviationWarning
bool detectAndReportPoseDeviationWarning(const Eigen::Matrix4f &pose1, const Eigen::Matrix4f &pose2, const std::string &namePose1, const std::string &namePose2, float positionThrMM, float angleThrDeg, const std::string &who)
Definition: utils.cpp:600
Pose.h
armarx::control::common::debugStdVec
void debugStdVec(StringVariantBaseMap &datafields, const std::string &name, std::vector< float > vec)
Definition: utils.cpp:134
armarx::control::common::mirrorLeftRightOri
Eigen::Matrix4f mirrorLeftRightOri(Eigen::Matrix4f &sourcePose)
Definition: utils.cpp:628
armarx::control::common::getError
void getError(Eigen::Matrix4f &currentPose, Eigen::Vector3d &targetPosition, Eigen::Quaterniond &targetQuaternion, double &posiError, double &oriError)
Definition: utils.cpp:94
armarx::control::common::from_json
void from_json(const nlohmann::json &j, Pose &fp)
Definition: utils.cpp:155
armarx::control::common::reportPoseDeviationWarning
void reportPoseDeviationWarning(const std::string &who, const Eigen::Matrix4f &pose1, const Eigen::Matrix4f &pose2, const std::string &namePose1, const std::string &namePose2)
Definition: utils.cpp:581
armarx::control::common
This file is part of ArmarX.
Definition: aron_conversions.cpp:29
type.h
IceInternal::Handle< Pose >
armarx::control::common::poseToFVec
std::vector< float > poseToFVec(PosePtr &pose)
Definition: utils.cpp:261
armarx::control::common::dVecToString
std::string dVecToString(const mplib::core::DVec &dvec)
Definition: utils.cpp:289
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ManagedIceObject.h
armarx::control::common::poseDeviationTooLarge
bool poseDeviationTooLarge(const Eigen::Matrix4f &currentPose, const Eigen::Matrix4f &desiredPose, float positionThrMM, float angleThrDeg)
Definition: utils.cpp:570
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:46
armarx::control::common::MPType
MPType
Definition: type.h:59
armarx::control::common::getPos
Eigen::Block< Eigen::Matrix4f, 3, 1 > getPos(Eigen::Matrix4f &matrix)
Definition: utils.cpp:206
armarx::control::common::getDeltaAngleBetweenPose
float getDeltaAngleBetweenPose(const Eigen::Matrix4f &pose1, const Eigen::Matrix4f &pose2)
Definition: utils.cpp:563
armarx::control::common::poseToDVec
mplib::core::DVec poseToDVec(PosePtr &pose)
Definition: utils.cpp:233
armarx::control::common::dVecToMat4
Eigen::Matrix4f dVecToMat4(const mplib::core::DVec &dvec)
create Eigen:Matrix4f from 7D double vector.
Definition: utils.cpp:218
filename
std::string filename
Definition: VisualizationRobot.cpp:83
armarx::control::common::dVecToPose
PosePtr dVecToPose(const mplib::core::DVec &dvec)
Definition: utils.cpp:212
armarx::control::common::debugEigenVec
void debugEigenVec(StringVariantBaseMap &datafields, const std::string &name, Eigen::VectorXf vec)
Definition: utils.cpp:125
armarx::control::common::vecToEigen
Eigen::VectorXf vecToEigen(std::vector< float > &vec)
Definition: utils.cpp:300
armarx::control::common::getDeltaAngleBetweenRotMat
float getDeltaAngleBetweenRotMat(const Eigen::Matrix3f &rotMat1, const Eigen::Matrix3f &rotMat2)
Definition: utils.cpp:551
armarx::control::common::getValue
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition: utils.h:71
armarx::control::common::mpTrajFromFile
mp::arondto::MPTraj mpTrajFromFile(std::string filepath, bool containsHeader, bool noTimeStamp)
Definition: utils.cpp:321
armarx::Pose
The Pose class.
Definition: Pose.h:242
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::control::common::mat4ToFVec
std::vector< float > mat4ToFVec(const Eigen::Matrix4f &mat)
Definition: utils.cpp:268
armarx::control::common::appendToViapointList
void appendToViapointList(std::vector< mp::arondto::ListViaPoint > &vpList, const double canonicalValue, const std::vector< double > &viaPoint)
Definition: utils.cpp:505
armarx::control::common::mat4ToDVec
mplib::core::DVec mat4ToDVec(const Eigen::Matrix4f &mat)
convert Eigen:Matrix4f to 7D double vector.
Definition: utils.cpp:240
armarx::control::common::to_json
void to_json(nlohmann::json &j, const Pose &fp)
Definition: utils.cpp:143
Eigen::Quaterniond
Quaternion< double, 0 > Quaterniond
Definition: EigenForwardDeclarations.h:62
armarx::control::common::addViaPoint
void addViaPoint(mp::arondto::MPConfig &cfg, const double &canValue, const std::vector< double > &viaPoint)
Definition: utils.cpp:463
armarx::control::common::getEigenVec
Eigen::VectorXf getEigenVec(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName, int size, int value)
Definition: utils.cpp:23
Logging.h
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::control::common::sVecToString
std::string sVecToString(const std::vector< std::string > &vec, const std::string &delimiter)
Definition: utils.cpp:306
armarx::control::common::getEigenMatrix
void getEigenMatrix(nlohmann::json &userConfig, const std::string &entryName, Eigen::MatrixXf &mat)
Definition: utils.cpp:49
armarx::control::common::getDefaultMPConfig
mp::arondto::MPConfig getDefaultMPConfig(MPType mpType, const std::string &name, const std::string &nodeSetName, const double &durationSec, const std::vector< mp::arondto::MPTraj > &mpTraj, const std::vector< mp::arondto::ListViaPoint > &viaPointList)
Definition: utils.cpp:423