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: Radu Serban
13 // =============================================================================
14 
15 #ifndef CH_LINK_ROTSPRING_CB_H
16 #define CH_LINK_ROTSPRING_CB_H
17 
18 #include "chrono/physics/ChLinkMarkers.h"
19 
20 namespace chrono {
21 
22 /// Class for rotational spring-damper elements with the torque specified through a callback object.
23 /// The torque is applied in the current direction of the relative axis of rotation.
24 /// While a rotational spring-damper can be associated with any pair of bodies in the system, it is
25 /// typically used in cases where the mechanism kinematics are such that the two bodies have a single
26 /// relative rotational degree of freedom (e.g, between two bodies connected through a revolute,
27 /// cylindrical, or screw joint).
28 class ChApi ChLinkRotSpringCB : public ChLinkMarkers {
29 
30   public:
31     ChLinkRotSpringCB();
32     ChLinkRotSpringCB(const ChLinkRotSpringCB& other);
~ChLinkRotSpringCB()33     virtual ~ChLinkRotSpringCB() {}
34 
35     /// "Virtual" copy constructor (covariant return type).
Clone()36     virtual ChLinkRotSpringCB* Clone() const override { return new ChLinkRotSpringCB(*this); }
37 
38     /// Get the current rotation angle about the relative rotation axis.
GetRotSpringAngle()39     double GetRotSpringAngle() const { return relAngle; }
40 
41     /// Get the current relative axis of rotation.
GetRotSpringAxis()42     const ChVector<>& GetRotSpringAxis() const { return relAxis; }
43 
44     /// Get the current relative angular speed about the common rotation axis.
GetRotSpringSpeed()45     double GetRotSpringSpeed() const { return Vdot(relWvel, relAxis); }
46 
47     /// Get the current generated torque.
GetRotSpringTorque()48     double GetRotSpringTorque() const { return m_torque; }
49 
50     /// Class to be used as a callback interface for calculating the general spring-damper torque.
51     /// A derived class must implement the virtual operator().
52     class ChApi TorqueFunctor {
53       public:
~TorqueFunctor()54         virtual ~TorqueFunctor() {}
55 
56         /// Calculate and return the general spring-damper torque at the specified configuration.
57         virtual double operator()(double time,             ///< current time
58                                   double angle,            ///< relative angle of rotation
59                                   double vel,              ///< relative angular speed
60                                   ChLinkRotSpringCB* link  ///< back-pointer to associated link
61                                   ) = 0;
62     };
63 
64     /// Specify the callback object for calculating the torque.
RegisterTorqueFunctor(std::shared_ptr<TorqueFunctor> functor)65     void RegisterTorqueFunctor(std::shared_ptr<TorqueFunctor> functor) { m_torque_fun = functor; }
66 
67     /// Method to allow serialization of transient data to archives.
68     virtual void ArchiveOUT(ChArchiveOut& marchive) override;
69 
70     /// Method to allow deserialization of transient data from archives.
71     virtual void ArchiveIN(ChArchiveIn& marchive) override;
72 
73   protected:
74     /// Include the rotational spring-damper custom torque.
75     virtual void UpdateForces(double time) override;
76 
77     std::shared_ptr<TorqueFunctor> m_torque_fun;  ///< functor for torque calculation
78     double m_torque;                              ///< resulting torque along relative axis of rotation
79 };
80 
81 CH_CLASS_VERSION(ChLinkRotSpringCB, 0)
82 
83 }  // end namespace chrono
84 
85 #endif
86