Biorbd
State.cpp
1 #define BIORBD_API_EXPORTS
2 #include "Muscles/State.h"
3 
4 #include "Utils/Error.h"
5 
7  const biorbd::utils::Scalar& excitation,
8  const biorbd::utils::Scalar& activation) :
9  m_stateType(std::make_shared<biorbd::muscles::STATE_TYPE>()),
10  m_excitation(std::make_shared<biorbd::utils::Scalar>(excitation)),
11  m_excitationNorm(std::make_shared<biorbd::utils::Scalar>(0)),
12  m_activation(std::make_shared<biorbd::utils::Scalar>(activation))
13 {
14  setType();
15 }
16 
18  const biorbd::muscles::State &other) :
19  m_stateType(other.m_stateType),
20  m_excitation(other.m_excitation),
21  m_excitationNorm(std::make_shared<biorbd::utils::Scalar>(0)),
22  m_activation(other.m_activation)
23 {
24 
25 }
26 
28 {
29  //dtor
30 }
31 
33 {
35  copy.DeepCopy(*this);
36  return copy;
37 }
38 
40 {
41  *m_stateType = *other.m_stateType;
42  *m_excitation = *other.m_excitation;
43  *m_excitationNorm = *other.m_excitationNorm;
44  *m_activation = *other.m_activation;
45 }
46 
48  const biorbd::utils::Scalar& val,
49  bool turnOffWarnings) {
50 
51 #ifdef BIORBD_USE_CASADI_MATH
52  *m_excitation = casadi::MX::if_else_zero(casadi::MX::gt(val, 0), val);
53 #else
54  if (val<0){
55  if (!turnOffWarnings) {
57  0, "Excitation can't be lower than 0, 0 is used then");
58  }
59  *m_excitation = 0;
60  }
61  else
62  *m_excitation = val;
63 #endif
64 }
65 
66 const biorbd::utils::Scalar& biorbd::muscles::State::excitation() const
67 {
68  return *m_excitation;
69 }
70 
71 const biorbd::utils::Scalar& biorbd::muscles::State::normalizeExcitation(
72  const biorbd::muscles::State &emgMax,
73  bool turnOffWarnings) {
74 
75 #ifndef BIORBD_USE_CASADI_MATH
76  if (!turnOffWarnings) {
78  *m_excitation < emgMax.excitation(),
79  "Excitation is higher than maximal excitation.");
80  }
81 #endif
82  *m_excitationNorm = *m_excitation / emgMax.excitation();
83 
84  return *m_excitationNorm;
85 }
86 
88  const biorbd::utils::Scalar& val)
89 {
90  *m_excitationNorm = val;
91 }
92 
93 const biorbd::utils::Scalar& biorbd::muscles::State::excitationNorm() const
94 {
95  return *m_excitationNorm;
96 }
97 
99  const biorbd::utils::Scalar& val,
100  bool turnOffWarnings){
101 
102 #ifdef BIORBD_USE_CASADI_MATH
103  *m_activation = casadi::MX::if_else_zero(casadi::MX::gt(val, 0), val);
104  *m_activation = casadi::MX::if_else(casadi::MX::lt(val, 1), val, 1);
105 // *m_activation = val;
106 #else
107  if (val <= 0) {
108  if (!turnOffWarnings){
110  0, "Activation is " + biorbd::utils::String::to_string(val) + " but can't be lower than 0, 0 is used then");
111  }
112  *m_activation = 0;
113  }
114  else if (val >= 1) {
115  if (!turnOffWarnings){
117  0, "Activation " + biorbd::utils::String::to_string(val) + " but can't be higher than 1, 1 is used then");
118  }
119  *m_activation = 1;
120  }
121  else {
122  *m_activation = val;
123  }
124 #endif
125 }
126 
127 const biorbd::utils::Scalar& biorbd::muscles::State::activation() const
128 {
129  return *m_activation;
130 }
131 
132 biorbd::muscles::STATE_TYPE biorbd::muscles::State::type() const
133 {
134  return *m_stateType;
135 }
136 
138 {
139  *m_stateType = biorbd::muscles::STATE_TYPE::SIMPLE_STATE;
140 }
biorbd::muscles::State::State
State(const biorbd::utils::Scalar &excitation=0, const biorbd::utils::Scalar &activation=0)
Construct a state.
Definition: State.cpp:6
biorbd::muscles::State::m_excitationNorm
std::shared_ptr< biorbd::utils::Scalar > m_excitationNorm
The normalized excitation.
Definition: State.h:129
biorbd::muscles::State::m_stateType
std::shared_ptr< biorbd::muscles::STATE_TYPE > m_stateType
The state type.
Definition: State.h:127
biorbd::muscles::State::setExcitationNorm
void setExcitationNorm(const biorbd::utils::Scalar &val)
Force set the normalized excitation.
Definition: State.cpp:87
biorbd::muscles::State::m_excitation
std::shared_ptr< biorbd::utils::Scalar > m_excitation
The muscle excitation.
Definition: State.h:128
biorbd::muscles::State
EMG holder to interact with the muscle.
Definition: State.h:16
biorbd::muscles::State::excitationNorm
const biorbd::utils::Scalar & excitationNorm() const
Return the previously normalized excitation.
Definition: State.cpp:93
biorbd::muscles::State::DeepCopy
biorbd::muscles::State DeepCopy() const
Deep copy of state.
Definition: State.cpp:32
biorbd::muscles::State::activation
const biorbd::utils::Scalar & activation() const
Return the muscle activation.
Definition: State.cpp:127
biorbd::muscles::State::type
biorbd::muscles::STATE_TYPE type() const
Return the state type.
Definition: State.cpp:132
biorbd::utils::Error::warning
static void warning(bool cond, const biorbd::utils::String &message)
Non-blocking assert that displays the error message if false.
Definition: Error.cpp:19
biorbd::muscles::State::excitation
const biorbd::utils::Scalar & excitation() const
Return the muscle excitation.
Definition: State.cpp:66
biorbd::muscles::State::normalizeExcitation
const biorbd::utils::Scalar & normalizeExcitation(const biorbd::muscles::State &emgMax, bool turnOffWarnings=false)
Compute and return the normalized excitation.
Definition: State.cpp:71
biorbd::muscles::State::~State
virtual ~State()
Destroy class properly.
Definition: State.cpp:27
biorbd::muscles::State::setType
virtual void setType()
Set the type to simple_state.
Definition: State.cpp:137
biorbd::utils::String::to_string
static biorbd::utils::String to_string(double val)
Overload of the to_string C++11 function to allow for X-digits precision.
Definition: String.cpp:117
biorbd::muscles::State::setActivation
virtual void setActivation(const biorbd::utils::Scalar &val, bool turnOffWarnings=false)
Set the muscle activation.
Definition: State.cpp:98
biorbd::muscles::State::setExcitation
virtual void setExcitation(const biorbd::utils::Scalar &val, bool turnOffWarnings=false)
Set the muscle excitation.
Definition: State.cpp:47
biorbd::muscles::State::m_activation
std::shared_ptr< biorbd::utils::Scalar > m_activation
The muscle activation.
Definition: State.h:130