1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  *   https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  *   Redistribution and use in source and binary forms, with or
10  *   without modification, are permitted provided that the following
11  *   conditions are met:
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above
15  *     copyright notice, this list of conditions and the following
16  *     disclaimer in the documentation and/or other materials provided
17  *     with the distribution.
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  *   POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_UTILS_MJCF_DETAIL_JOINT_HPP_
34 #define DART_UTILS_MJCF_DETAIL_JOINT_HPP_
35 
36 #include <tinyxml2.h>
37 
38 #include "dart/utils/mjcf/detail/Compiler.hpp"
39 #include "dart/utils/mjcf/detail/Default.hpp"
40 #include "dart/utils/mjcf/detail/Error.hpp"
41 #include "dart/utils/mjcf/detail/JointAttributes.hpp"
42 #include "dart/utils/mjcf/detail/Types.hpp"
43 
44 namespace dart {
45 namespace utils {
46 namespace MjcfParser {
47 namespace detail {
48 
49 class Body;
50 
51 class Joint final
52 {
53 public:
54   Joint() = default;
55 
56   const std::string& getName() const;
57   JointType getType() const;
58   const Eigen::Vector3d& getPos() const;
59   const Eigen::Vector3d& getAxis() const;
60   bool isLimited() const;
61   const Eigen::Vector2d& getRange() const;
62   double getDamping() const;
63   double getSpringRef() const;
64 
65 private:
66   // Private members used by Body class
67   friend class Body;
68   Errors read(
69       tinyxml2::XMLElement* element,
70       const Defaults& defaults,
71       const JointAttributes& defaultAttributes);
72 
73   /// Updates attributes and elements that doesn't require any other elements.
74   Errors preprocess(const Compiler& compiler);
75 
76   /// Updates attributes and elements that require the preprocessed child
77   /// elements of this <joint>.
78   Errors compile(const Compiler& compiler);
79 
80   /// Updates attributes and elements that require the compiled parent element.
81   Errors postprocess(const Body* body, const Compiler& compiler);
82 
83 private:
84   JointAttributes mAttributes;
85 
86   std::string mName{""};
87 
88   JointType mType{JointType::HINGE};
89 
90   int mGroup{0};
91 
92   /// Position of the joint, specified in local or global coordinates as
93   /// determined by the coordinate attribute of compiler. For free joints this
94   /// attribute is ignored.
95   Eigen::Vector3d mPos{Eigen::Vector3d::Zero()};
96 
97   /// This attribute specifies the axis of rotation for hinge joints and the
98   /// direction of translation for slide joints. It is ignored for free and ball
99   /// joints.
100   Eigen::Vector3d mAxis{Eigen::Vector3d::UnitZ()};
101 
102   Eigen::Vector2d mSpringDamper{Eigen::Vector2d::Zero()};
103 
104   /// This attribute specifies if the joint has limits.
105   bool mLimited{false};
106 
107   double mStiffness{0};
108 
109   /// The joint limits.
110   Eigen::Vector2d mRange{Eigen::Vector2d::Zero()};
111 
112   double mMargin{0};
113 
114   double mRef{0};
115 
116   double mSpringRef{0};
117 
118   double mArmature{0};
119 
120   double mDamping{0};
121 
122   double mFrictionLoss{0};
123 
124   Eigen::VectorXd mUser;
125 };
126 
127 } // namespace detail
128 } // namespace MjcfParser
129 } // namespace utils
130 } // namespace dart
131 
132 #endif // #ifndef DART_UTILS_MJCF_DETAIL_JOINT_HPP_
133