Biorbd Release_1.13.0
Loading...
Searching...
No Matches
Muscle.cpp
1#define BIORBD_API_EXPORTS
2
3#include "InternalForces/Muscles/Muscle.h"
4
5#include "InternalForces/Compound.h"
6#include "InternalForces/Muscles/Characteristics.h"
7#include "InternalForces/Muscles/MuscleGeometry.h"
8#include "InternalForces/Muscles/MusclesEnums.h"
9#include "InternalForces/Muscles/StateDynamics.h"
10#include "InternalForces/Muscles/StateDynamicsBuchanan.h"
11#include "InternalForces/Muscles/StateDynamicsDeGroote.h"
12#include "InternalForces/PathModifiers.h"
13#include "RigidBody/GeneralizedCoordinates.h"
14#include "RigidBody/GeneralizedVelocity.h"
15#include "RigidBody/Joints.h"
16#include "Utils/Error.h"
17
18using namespace BIORBD_NAMESPACE;
20 : internal_forces::Compound(),
21 m_position(std::make_shared<internal_forces::muscles::MuscleGeometry>()),
22 m_type(
23 std::make_shared<internal_forces::muscles::MUSCLE_TYPE>(
24 internal_forces::muscles::MUSCLE_TYPE::NO_MUSCLE_TYPE)),
26 std::make_shared<internal_forces::muscles::Characteristics>()),
27 m_state(std::make_shared<internal_forces::muscles::State>()) {
28 setType();
29}
30
32 const utils::String &name,
35 : internal_forces::Compound(name),
37 std::make_shared<internal_forces::muscles::MuscleGeometry>(position)),
38 m_type(
39 std::make_shared<internal_forces::muscles::MUSCLE_TYPE>(
40 internal_forces::muscles::MUSCLE_TYPE::NO_MUSCLE_TYPE)),
42 std::make_shared<internal_forces::muscles::Characteristics>(
44 m_state(std::make_shared<internal_forces::muscles::State>()) {}
45
47 const utils::String &name,
50 const internal_forces::muscles::State &dynamicState)
51 : internal_forces::Compound(name),
53 std::make_shared<internal_forces::muscles::MuscleGeometry>(position)),
54 m_type(
55 std::make_shared<internal_forces::muscles::MUSCLE_TYPE>(
56 internal_forces::muscles::MUSCLE_TYPE::NO_MUSCLE_TYPE)),
58 std::make_shared<internal_forces::muscles::Characteristics>(
60 m_state(std::make_shared<internal_forces::muscles::State>(dynamicState)) {
61
62}
63
65 const utils::String &name,
68 const internal_forces::PathModifiers &pathModifiers)
69 : internal_forces::Compound(name, pathModifiers),
71 std::make_shared<internal_forces::muscles::MuscleGeometry>(position)),
72 m_type(
73 std::make_shared<internal_forces::muscles::MUSCLE_TYPE>(
74 internal_forces::muscles::MUSCLE_TYPE::NO_MUSCLE_TYPE)),
76 std::make_shared<internal_forces::muscles::Characteristics>(
78 m_state(std::make_shared<internal_forces::muscles::State>()) {}
79
87
88internal_forces::muscles::Muscle::Muscle(const std::shared_ptr<Muscle> other)
89 : internal_forces::Compound(other),
90 m_position(other->m_position),
91 m_type(other->m_type),
93 m_state(other->m_state) {}
94
96 const utils::String &name,
99 const internal_forces::PathModifiers &pathModifiers,
101 : internal_forces::Compound(name, pathModifiers),
102 m_position(std::make_shared<internal_forces::muscles::MuscleGeometry>(g)),
103 m_type(
104 std::make_shared<internal_forces::muscles::MUSCLE_TYPE>(
105 internal_forces::muscles::MUSCLE_TYPE::NO_MUSCLE_TYPE)),
107 std::make_shared<internal_forces::muscles::Characteristics>(c)),
108 m_state(std::make_shared<internal_forces::muscles::State>()) {
109 setState(emg);
110
112 pathModifiers.nbWraps() <= 1,
113 "Multiple wrapping objects is not implemented yet");
114}
115
119
123 *m_position = other.m_position->DeepCopy();
124 *m_type = *other.m_type;
125 *m_characteristics = other.m_characteristics->DeepCopy();
126 *m_state = other.m_state->DeepCopy();
127}
128
129internal_forces::muscles::MUSCLE_TYPE internal_forces::muscles::Muscle::type()
130 const {
131 return *m_type;
132}
133
135 *m_type = internal_forces::muscles::MUSCLE_TYPE::NO_MUSCLE_TYPE;
136}
137
139 rigidbody::Joints &updatedModel,
141 // Update de la position des insertions et origines
142 m_position->updateKinematics(
143 updatedModel, *m_characteristics, *m_pathChanger, &Q, nullptr);
144}
146 rigidbody::Joints &updatedModel,
148 const rigidbody::GeneralizedVelocity &Qdot) {
149 // Update de la position des insertions et origines
150 m_position->updateKinematics(
151 updatedModel, *m_characteristics, *m_pathChanger, &Q, &Qdot);
152}
154 std::vector<utils::Vector3d> &musclePointsInGlobal,
155 utils::Matrix &jacoPointsInGlobal) {
156 // Update de la position des insertions et origines
157 m_position->updateKinematics(
158 musclePointsInGlobal, jacoPointsInGlobal, *m_characteristics, nullptr);
159}
161 std::vector<utils::Vector3d> &musclePointsInGlobal,
162 utils::Matrix &jacoPointsInGlobal,
163 const rigidbody::GeneralizedVelocity &Qdot) {
164 // Update de la position des insertions et origines
165 m_position->updateKinematics(
166 musclePointsInGlobal, jacoPointsInGlobal, *m_characteristics, &Qdot);
167}
168
177
179 rigidbody::Joints &updatedModel,
181 bool updateMuscleParameters) {
182 if (updateMuscleParameters)
184 updatedModel, *m_characteristics, *m_pathChanger, &Q, nullptr);
185 return position().length();
186}
187
189 rigidbody::Joints &updatedModel,
191 bool updateMuscleParameters) {
192 if (updateMuscleParameters)
193 m_position->updateKinematics(
194 updatedModel, *m_characteristics, *m_pathChanger, &Q, nullptr);
195 return position().musculoTendonLength();
196}
197
199 rigidbody::Joints &updatedModel,
202 bool updateMuscleParameters) {
203 if (updateMuscleParameters)
204 m_position->updateKinematics(
205 updatedModel, *m_characteristics, *m_pathChanger, &Q, &Qdot);
206 return m_position->velocity();
207}
208
211 bool alreadyNormalized) const {
212 std::shared_ptr<internal_forces::muscles::StateDynamics> state_copy =
213 std::dynamic_pointer_cast<internal_forces::muscles::StateDynamics>(
214 m_state);
216 state_copy != nullptr,
217 "The muscle " + name() + " is not a dynamic muscle");
218 return state_copy->timeDerivativeActivation(
219 state, characteristics(), alreadyNormalized);
220}
221
226
227const std::vector<utils::Vector3d> &
229 rigidbody::Joints &updatedModel,
231 bool updateMuscleParameters) {
232 if (updateMuscleParameters)
233 m_position->updateKinematics(
234 updatedModel, *m_characteristics, *m_pathChanger, &Q, nullptr);
235 return musclesPointsInGlobal();
236}
237
238const std::vector<utils::Vector3d> &
240 return m_position->pointsInGlobal();
241}
242
244 const utils::Scalar &forceMax) {
245 m_characteristics->setForceIsoMax(forceMax);
246}
247
252
257
258// Get and set
261 if (emg.type() == internal_forces::muscles::STATE_TYPE::BUCHANAN) {
262 m_state = std::make_shared<internal_forces::muscles::StateDynamicsBuchanan>(
264 } else if (emg.type() == internal_forces::muscles::STATE_TYPE::DE_GROOTE) {
265 m_state = std::make_shared<internal_forces::muscles::StateDynamicsDeGroote>(
267 } else if (emg.type() == internal_forces::muscles::STATE_TYPE::DYNAMIC) {
268 m_state = std::make_shared<internal_forces::muscles::StateDynamics>(
270 } else {
272 utils::String(internal_forces::muscles::STATE_TYPE_toStr(emg.type())) +
273 " is not a valid type for setState");
274 }
275 *m_state = emg;
276}
const utils::String & name() const
Return the name of the muscle.
Definition Compound.cpp:48
void DeepCopy(const Compound &other)
Deep copy of a compound.
Definition Compound.cpp:41
std::shared_ptr< utils::Scalar > m_force
The last computed force.
Definition Compound.h:108
Compound()
Construct muscle compound.
Definition Compound.cpp:11
std::shared_ptr< internal_forces::PathModifiers > m_pathChanger
The set of path modifiers.
Definition Compound.h:107
Holder of all the path modifiers of a muscle.
size_t nbWraps() const
Return the total number of wrapping objects in the set.
void updateKinematics(rigidbody::Joints &updatedModel, const Characteristics &characteristics, const rigidbody::GeneralizedCoordinates *Q=nullptr, const rigidbody::GeneralizedVelocity *Qdot=nullptr)
Updates the position and dynamic elements of the muscles.
void setState(const State &emg)
Set the dynamic state.
Definition Muscle.cpp:259
std::shared_ptr< Characteristics > m_characteristics
The muscle characteristics.
Definition Muscle.h:347
Characteristics & characteristics() const
Return the muscle characteristics.
Definition Muscle.cpp:254
virtual ~Muscle()
Destroy class properly.
Definition Muscle.cpp:116
std::shared_ptr< State > m_state
The dynamic state.
Definition Muscle.h:348
const utils::Scalar & activationDot(const State &state, bool alreadyNormalized=false) const
Return the activation time derivative.
Definition Muscle.cpp:209
void setCharacteristics(const Characteristics &characteristics)
Set the muscle characteristics.
Definition Muscle.cpp:248
virtual void setType()
Set the type of muscle.
Definition Muscle.cpp:134
virtual void computeForce(const State &emg)
Computer the forces from a specific emg.
Definition Muscle.cpp:222
std::shared_ptr< internal_forces::muscles::MuscleGeometry > m_position
The geometry (position of the nodes) of the muscle.
Definition Muscle.h:345
const utils::Scalar & musculoTendonLength(rigidbody::Joints &updatedModel, const rigidbody::GeneralizedCoordinates &Q, bool updateMuscleParameters=true)
Return the musculo tendon length.
Definition Muscle.cpp:188
const utils::Scalar & length(rigidbody::Joints &updatedModel, const rigidbody::GeneralizedCoordinates &Q, bool updateMuscleParameters=true)
Get the length of the muscle.
Definition Muscle.cpp:178
std::shared_ptr< MUSCLE_TYPE > m_type
The type of the muscle.
Definition Muscle.h:343
const internal_forces::muscles::MuscleGeometry & position() const
Return the position of all the points attached to the muscle (0 being the origin).
Definition Muscle.cpp:174
const std::vector< utils::Vector3d > & musclesPointsInGlobal() const
Return the previously computed muscle points in global reference frame.
Definition Muscle.cpp:239
const State & state() const
Return the dynamic state.
Definition Muscle.cpp:277
virtual utils::Scalar getForceFromActivation(const State &emg)=0
Function allowing modification of the way the multiplication is done in computeForce(EMG).
void setForceIsoMax(const utils::Scalar &forceMax)
Set the maximal isometric force.
Definition Muscle.cpp:243
MUSCLE_TYPE type() const
Return the type of the muscle.
Definition Muscle.cpp:129
const std::vector< utils::Vector3d > & musclesPointsInGlobal(rigidbody::Joints &updatedModel, const rigidbody::GeneralizedCoordinates &Q, bool updateMuscleParameters=true)
Return the muscle points in global reference frame.
Definition Muscle.cpp:228
const utils::Scalar & velocity(rigidbody::Joints &updatedModel, const rigidbody::GeneralizedCoordinates &Q, const rigidbody::GeneralizedVelocity &Qdot, bool updateMuscleParameters=true)
Return the velocity of the muscle.
Definition Muscle.cpp:198
void DeepCopy(const Muscle &other)
Deep copy of a muscle in new muscle.
Definition Muscle.cpp:120
void setPosition(const internal_forces::muscles::MuscleGeometry &positions)
Set the position of all the points attached to the muscle (0 being the origin).
Definition Muscle.cpp:169
void updateOrientations(rigidbody::Joints &updatedModel, const rigidbody::GeneralizedCoordinates &Q)
Update the position of the origin and insertion positions of the muscle.
Definition Muscle.cpp:138
Time derivative of activation as described by Buchanan (https://www.sciencedirect....
EMG with the capability to compute the time derivative.
EMG with the capability to compute the time derivative.
EMG holder to interact with the muscle.
Definition State.h:17
STATE_TYPE type() const
Return the state type.
Definition State.cpp:121
This is the core of the musculoskeletal model in biorbd.
Definition Joints.h:48
static void raise(const String &message)
Throw an error message.
Definition Error.cpp:6
static void check(bool cond, const String &message)
Assert that raises the error message if false.
Definition Error.cpp:10
A wrapper for the Eigen::MatrixXd.
Definition Matrix.h:18
Wrapper around the std::string class with augmented functionality.
Definition String.h:18