|
| 1 | +#ifndef OPENSIM_ACTIVATION_COORDINATE_ACTUATOR_H |
| 2 | +#define OPENSIM_ACTIVATION_COORDINATE_ACTUATOR_H |
| 3 | +/* -------------------------------------------------------------------------- * |
| 4 | + * OpenSim: ActivationCoordinateActuator.h * |
| 5 | + * -------------------------------------------------------------------------- * |
| 6 | + * The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * |
| 7 | + * See http://opensim.stanford.edu and the NOTICE file for more information. * |
| 8 | + * OpenSim is developed at Stanford University and supported by the US * |
| 9 | + * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * |
| 10 | + * through the Warrior Web program. * |
| 11 | + * * |
| 12 | + * Copyright (c) 2005-2020 Stanford University and the Authors * |
| 13 | + * Author(s): Christopher Dembia * |
| 14 | + * * |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may * |
| 16 | + * not use this file except in compliance with the License. You may obtain a * |
| 17 | + * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * |
| 18 | + * * |
| 19 | + * Unless required by applicable law or agreed to in writing, software * |
| 20 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 21 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 22 | + * See the License for the specific language governing permissions and * |
| 23 | + * limitations under the License. * |
| 24 | + * -------------------------------------------------------------------------- */ |
| 25 | + |
| 26 | +#include "osimActuatorsDLL.h" |
| 27 | + |
| 28 | +#include "CoordinateActuator.h" |
| 29 | + |
| 30 | +namespace OpenSim { |
| 31 | + |
| 32 | +/// Similar to CoordinateActuator (simply produces a generalized force) but |
| 33 | +/// with first-order linear activation dynamics. This actuator has one state |
| 34 | +/// variable, `activation`, with \f$ \dot{a} = (x - a) / \tau \f$, where |
| 35 | +/// \f$ a \f$ is activation, \f$ x \f$ is excitation, and \f$ \tau \f$ is the |
| 36 | +/// activation time constant (there is no separate deactivation time constant). |
| 37 | +/// The statebounds_activation output is used in Moco to set default values for |
| 38 | +/// the activation state variable. |
| 39 | +/// <b>Default %Property Values</b> |
| 40 | +/// @verbatim |
| 41 | +/// activation_time_constant: 0.01 |
| 42 | +/// default_activation: 0.5 |
| 43 | +/// @endverbatim |
| 44 | +class OSIMACTUATORS_API ActivationCoordinateActuator |
| 45 | + : public CoordinateActuator { |
| 46 | + OpenSim_DECLARE_CONCRETE_OBJECT(ActivationCoordinateActuator, |
| 47 | + CoordinateActuator); |
| 48 | +public: |
| 49 | + OpenSim_DECLARE_PROPERTY(activation_time_constant, double, |
| 50 | + "Larger value means activation can change more rapidly " |
| 51 | + "(units: seconds; default: 0.01 seconds)."); |
| 52 | + |
| 53 | + OpenSim_DECLARE_PROPERTY(default_activation, double, |
| 54 | + "Value of activation in the default state returned by initSystem() " |
| 55 | + "(default: 0.5)."); |
| 56 | + |
| 57 | + OpenSim_DECLARE_OUTPUT(statebounds_activation, SimTK::Vec2, |
| 58 | + getBoundsActivation, SimTK::Stage::Model); |
| 59 | + |
| 60 | + ActivationCoordinateActuator() { |
| 61 | + constructProperties(); |
| 62 | + } |
| 63 | + |
| 64 | + /// Provide the coordinate name. |
| 65 | + explicit ActivationCoordinateActuator(const std::string& coordinateName) |
| 66 | + : ActivationCoordinateActuator() { |
| 67 | + if (!coordinateName.empty()) { |
| 68 | + set_coordinate(coordinateName); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// The lower bound on activation is getMinControl() and the upper bound is |
| 73 | + /// getMaxControl(). |
| 74 | + /// Whether these bounds are enforced is determined by the solver used. |
| 75 | + SimTK::Vec2 getBoundsActivation(const SimTK::State&) const { |
| 76 | + return SimTK::Vec2(getMinControl(), getMaxControl()); |
| 77 | + } |
| 78 | + |
| 79 | +protected: |
| 80 | + void extendAddToSystem(SimTK::MultibodySystem& system) const override { |
| 81 | + Super::extendAddToSystem(system); |
| 82 | + addStateVariable("activation", SimTK::Stage::Dynamics); |
| 83 | + } |
| 84 | + |
| 85 | + void extendInitStateFromProperties(SimTK::State& s) const override { |
| 86 | + Super::extendInitStateFromProperties(s); |
| 87 | + setStateVariableValue(s, "activation", get_default_activation()); |
| 88 | + } |
| 89 | + |
| 90 | + void extendSetPropertiesFromState(const SimTK::State& s) override { |
| 91 | + Super::extendSetPropertiesFromState(s); |
| 92 | + set_default_activation(getStateVariableValue(s, "activation")); |
| 93 | + } |
| 94 | + |
| 95 | + void computeStateVariableDerivatives(const SimTK::State& s) const override { |
| 96 | + // No need to do clamping, etc; CoordinateActuator is bidirectional. |
| 97 | + const auto& tau = get_activation_time_constant(); |
| 98 | + const auto& x = getControl(s); |
| 99 | + const auto& a = getStateVariableValue(s, "activation"); |
| 100 | + const SimTK::Real adot = (x - a) / tau; |
| 101 | + setStateVariableDerivativeValue(s, "activation", adot); |
| 102 | + } |
| 103 | + |
| 104 | + double computeActuation(const SimTK::State& s) const override { |
| 105 | + return getStateVariableValue(s, "activation") * getOptimalForce(); |
| 106 | + } |
| 107 | +private: |
| 108 | + void constructProperties() { |
| 109 | + constructProperty_activation_time_constant(0.010); |
| 110 | + constructProperty_default_activation(0.5); |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +} // namespace OpenSim |
| 115 | + |
| 116 | +#endif // OPENSIM_ACTIVATION_COORDINATE_ACTUATOR_H |
0 commit comments