1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora, Radu Serban
13 // =============================================================================
14 
15 #ifndef CHSHAFTSBODY_H
16 #define CHSHAFTSBODY_H
17 
18 #include "chrono/physics/ChBodyFrame.h"
19 #include "chrono/physics/ChShaft.h"
20 #include "chrono/solver/ChConstraintTwoGeneric.h"
21 
22 namespace chrono {
23 
24 // Forward references (for parent hierarchy pointer)
25 class ChShaft;
26 class ChBodyFrame;
27 
28 /// Class for creating a constraint between a 3D ChBody object and a 1D ChShaft object.
29 /// A rotation axis must be specified (to tell along which direction the shaft inertia
30 /// and rotation affects the body).
31 /// This constraint is useful, for example, when you have modeled a 3D car using ChBody
32 /// items and a 1D powertrain (gears, differential, etc.) using ChShaft objects: you
33 /// can connect the former (at least, the wheels) to the latter using this constraint.
34 
35 class ChApi ChShaftsBody : public ChPhysicsItem {
36 
37   private:
38     double torque_react;                ///< reaction torque
39     ChConstraintTwoGeneric constraint;  ///< used as an interface to the solver
40     ChShaft* shaft;                     ///< connected shaft
41     ChBodyFrame* body;                  ///< connected body
42     ChVector<> shaft_dir;               ///< shaft direction
43 
44   public:
45     ChShaftsBody();
46     ChShaftsBody(const ChShaftsBody& other);
~ChShaftsBody()47     ~ChShaftsBody() {}
48 
49     /// "Virtual" copy constructor (covariant return type).
Clone()50     virtual ChShaftsBody* Clone() const override { return new ChShaftsBody(*this); }
51 
52     /// Get the number of scalar variables affected by constraints in this link
GetNumCoords()53     virtual int GetNumCoords() const { return 6 + 1; }
54 
55     /// Number of scalar constraints
GetDOC_c()56     virtual int GetDOC_c() override { return 1; }
57 
58     // Override/implement interfaces for global state vectors, see ChPhysicsItem for comments.
59 
60     virtual void IntStateGatherReactions(const unsigned int off_L, ChVectorDynamic<>& L) override;
61     virtual void IntStateScatterReactions(const unsigned int off_L, const ChVectorDynamic<>& L) override;
62     virtual void IntLoadResidual_CqL(const unsigned int off_L,
63                                      ChVectorDynamic<>& R,
64                                      const ChVectorDynamic<>& L,
65                                      const double c) override;
66     virtual void IntLoadConstraint_C(const unsigned int off,
67                                      ChVectorDynamic<>& Qc,
68                                      const double c,
69                                      bool do_clamp,
70                                      double recovery_clamp) override;
IntLoadConstraint_Ct(const unsigned int off,ChVectorDynamic<> & Qc,const double c)71     virtual void IntLoadConstraint_Ct(const unsigned int off, ChVectorDynamic<>& Qc, const double c) override {}
72     virtual void IntToDescriptor(const unsigned int off_v,
73                                  const ChStateDelta& v,
74                                  const ChVectorDynamic<>& R,
75                                  const unsigned int off_L,
76                                  const ChVectorDynamic<>& L,
77                                  const ChVectorDynamic<>& Qc) override;
78     virtual void IntFromDescriptor(const unsigned int off_v,
79                                    ChStateDelta& v,
80                                    const unsigned int off_L,
81                                    ChVectorDynamic<>& L) override;
82 
83     // Override/implement system functions of ChPhysicsItem
84     // (to assemble/manage data for system solver)
85 
86     virtual void InjectConstraints(ChSystemDescriptor& mdescriptor) override;
87     virtual void ConstraintsBiReset() override;
88     virtual void ConstraintsBiLoad_C(double factor = 1, double recovery_clamp = 0.1, bool do_clamp = false) override;
89     virtual void ConstraintsBiLoad_Ct(double factor = 1) override;
90     virtual void ConstraintsLoadJacobians() override;
91     virtual void ConstraintsFetch_react(double factor = 1) override;
92 
93     /// Use this function after object creation, to initialize it, given
94     /// the 1D shaft and 3D body to join.
95     /// Each item must belong to the same ChSystem.
96     /// Direction is expressed in the local coordinates of the body.
97     bool Initialize(std::shared_ptr<ChShaft> mshaft,  ///< shaft to join
98                     std::shared_ptr<ChBodyFrame>
99                         mbody,              ///< body to join
100                     const ChVector<>& mdir  ///< the direction of the shaft on 3D body (applied on COG: pure torque)
101                     );
102 
103     /// Get the shaft
GetShaft()104     ChShaft* GetShaft() { return shaft; }
105     /// Get the body
GetBody()106     ChBodyFrame* GetBody() { return body; }
107 
108     /// Set the direction of the shaft respect to 3D body, as a
109     /// normalized vector expressed in the coordinates of the body.
110     /// The shaft applies only torque, about this axis.
SetShaftDirection(ChVector<> md)111     void SetShaftDirection(ChVector<> md) { shaft_dir = Vnorm(md); }
112 
113     /// Get the direction of the shaft respect to 3D body, as a
114     /// normalized vector expressed in the coordinates of the body.
GetShaftDirection()115     const ChVector<>& GetShaftDirection() const { return shaft_dir; }
116 
117     /// Get the reaction torque considered as applied to ChShaft.
GetTorqueReactionOnShaft()118     double GetTorqueReactionOnShaft() const { return -(torque_react); }
119 
120     /// Get the reaction torque considered as applied to ChBody,
121     /// expressed in the coordinates of the body.
GetTorqueReactionOnBody()122     ChVector<> GetTorqueReactionOnBody() const { return (shaft_dir * torque_react); }
123 
124     /// Update all auxiliary data of the gear transmission at given time
125     virtual void Update(double mytime, bool update_assets = true) override;
126 
127     //
128     // SERIALIZATION
129     //
130 
131     /// Method to allow serialization of transient data to archives.
132     virtual void ArchiveOUT(ChArchiveOut& marchive) override;
133 
134     /// Method to allow deserialization of transient data from archives.
135     virtual void ArchiveIN(ChArchiveIn& marchive) override;
136 };
137 
138 CH_CLASS_VERSION(ChShaftsBody,0)
139 
140 
141 
142 
143 /// Class for creating a constraint between a 3D ChBody object and a 1D ChShaft object
144 /// that represents a 1D translational DOF (differently from the ChShaftsBody constraint
145 /// that connects to a rotational DOF)
146 /// A translation axis must be specified (to tell along which direction the 1D shaft inertia
147 /// rotation affects the body).
148 
149 class ChApi ChShaftsBodyTranslation : public ChPhysicsItem {
150 
151   private:
152     double force_react;                 ///< reaction force
153     ChConstraintTwoGeneric constraint;  ///< used as an interface to the solver
154     ChShaft* shaft;                     ///< connected shaft (translation dof)
155     ChBodyFrame* body;                  ///< connected body
156     ChVector<> shaft_dir;               ///< shaft direction
157     ChVector<> shaft_pos;               ///< shaft anchor to body
158 
159   public:
160     ChShaftsBodyTranslation();
161     ChShaftsBodyTranslation(const ChShaftsBodyTranslation& other);
~ChShaftsBodyTranslation()162     ~ChShaftsBodyTranslation() {}
163 
164     /// "Virtual" copy constructor (covariant return type).
Clone()165     virtual ChShaftsBodyTranslation* Clone() const override { return new ChShaftsBodyTranslation(*this); }
166 
167     /// Get the number of scalar variables affected by constraints in this link
GetNumCoords()168     virtual int GetNumCoords() const { return 6 + 1; }
169 
170     /// Number of scalar constraints
GetDOC_c()171     virtual int GetDOC_c() override { return 1; }
172 
173     // Override/implement interfaces for global state vectors, see ChPhysicsItem for comments.
174 
175     virtual void IntStateGatherReactions(const unsigned int off_L, ChVectorDynamic<>& L) override;
176     virtual void IntStateScatterReactions(const unsigned int off_L, const ChVectorDynamic<>& L) override;
177     virtual void IntLoadResidual_CqL(const unsigned int off_L,
178                                      ChVectorDynamic<>& R,
179                                      const ChVectorDynamic<>& L,
180                                      const double c) override;
181     virtual void IntLoadConstraint_C(const unsigned int off,
182                                      ChVectorDynamic<>& Qc,
183                                      const double c,
184                                      bool do_clamp,
185                                      double recovery_clamp) override;
IntLoadConstraint_Ct(const unsigned int off,ChVectorDynamic<> & Qc,const double c)186     virtual void IntLoadConstraint_Ct(const unsigned int off, ChVectorDynamic<>& Qc, const double c) override {}
187     virtual void IntToDescriptor(const unsigned int off_v,
188                                  const ChStateDelta& v,
189                                  const ChVectorDynamic<>& R,
190                                  const unsigned int off_L,
191                                  const ChVectorDynamic<>& L,
192                                  const ChVectorDynamic<>& Qc) override;
193     virtual void IntFromDescriptor(const unsigned int off_v,
194                                    ChStateDelta& v,
195                                    const unsigned int off_L,
196                                    ChVectorDynamic<>& L) override;
197 
198     // Override/implement system functions of ChPhysicsItem
199     // (to assemble/manage data for system solver)
200 
201     virtual void InjectConstraints(ChSystemDescriptor& mdescriptor) override;
202     virtual void ConstraintsBiReset() override;
203     virtual void ConstraintsBiLoad_C(double factor = 1, double recovery_clamp = 0.1, bool do_clamp = false) override;
204     virtual void ConstraintsBiLoad_Ct(double factor = 1) override;
205     virtual void ConstraintsLoadJacobians() override;
206     virtual void ConstraintsFetch_react(double factor = 1) override;
207 
208     /// Use this function after object creation, to initialize it, given
209     /// the 1D shaft and 3D body to join.
210     /// Each item must belong to the same ChSystem.
211     /// Direction is expressed in the local coordinates of the body.
212     bool Initialize(std::shared_ptr<ChShaft> mshaft,  ///< shaft to join, representing translational dof
213                     std::shared_ptr<ChBodyFrame> mbody, ///< body to join
214                     const ChVector<>& mdir,  ///< the direction of the shaft on 3D body, in body coords
215                     const ChVector<>& mpos  ///< the anchro position of the shaft on 3D body, in body coords
216                     );
217 
218     /// Get the shaft
GetShaft()219     ChShaft* GetShaft() { return shaft; }
220     /// Get the body
GetBody()221     ChBodyFrame* GetBody() { return body; }
222 
223     /// Set the direction of the shaft respect to 3D body, as a
224     /// normalized vector expressed in the coordinates of the body.
SetShaftDirection(ChVector<> md)225     void SetShaftDirection(ChVector<> md) { shaft_dir = Vnorm(md); }
226 
227     /// Get the direction of the shaft respect to 3D body, as a
228     /// normalized vector expressed in the coordinates of the body.
GetShaftDirection()229     const ChVector<>& GetShaftDirection() const { return shaft_dir; }
230 
231     /// Set the anchor point of the shaft respect to 3D body, as a
232     /// vector expressed in the coordinates of the body.
SetShaftPos(ChVector<> md)233     void SetShaftPos(ChVector<> md) { shaft_pos = md; }
234 
235     /// Get the anchor point of the shaft respect to 3D body, as a
236     /// vector expressed in the coordinates of the body.
GetShaftPos()237     const ChVector<>& GetShaftPos() const { return shaft_pos; }
238 
239 
240     /// Get the reaction force considered as applied to ChShaft.
GetForceReactionOnShaft()241     double GetForceReactionOnShaft() const { return -(force_react); }
242 
243     /// Get the reaction torque considered as applied to ChBody,
244     /// expressed in the coordinates of the body.
GetForceReactionOnBody()245     ChVector<> GetForceReactionOnBody() const { return (shaft_dir * force_react); }
246 
247     /// Update all auxiliary data of the gear transmission at given time
248     virtual void Update(double mytime, bool update_assets = true) override;
249 
250     //
251     // SERIALIZATION
252     //
253 
254     /// Method to allow serialization of transient data to archives.
255     virtual void ArchiveOUT(ChArchiveOut& marchive) override;
256 
257     /// Method to allow deserialization of transient data from archives.
258     virtual void ArchiveIN(ChArchiveIn& marchive) override;
259 };
260 
261 CH_CLASS_VERSION(ChShaftsBodyTranslation,0)
262 
263 
264 
265 
266 
267 }  // end namespace chrono
268 
269 #endif
270