File: FLV.m

package info (click to toggle)
mujoco 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 39,800 kB
  • sloc: ansic: 28,947; cpp: 28,897; cs: 14,241; python: 10,465; xml: 5,104; sh: 93; makefile: 34
file content (145 lines) | stat: -rw-r--r-- 3,675 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
% Copyright 2021 DeepMind Technologies Limited
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.


% Force-Length-Velocity function of MuJoCo muscle model
% Defaults: FLV(0.5, 1.6, 1.5, 1.3, 1.2)


function FLV(lmin, lmax, vmax, fpmax, fvmax)

    % derived quantities
  a = 0.5*(lmin+1);
  b = 0.5*(1+lmax);
  c = fvmax-1;

    % length and velocity ranges to plot
    LL = linspace(lmin, lmax, 51);
    VV = linspace(-vmax, vmax, 51);

    % length-passive
    FP = zeros(size(LL));
    for i=1:length(LL)
        L = LL(i);

        if L<=1
            FP(i) = 0;
        elseif L<=b
            x = (L-1)/(b-1);
            FP(i) = 0.25*fpmax*x*x*x;
        else
            x = (L-b)/(b-1);
            FP(i) = 0.25*fpmax*(1+3*x);
        end
    end

    % length-active
    FL = zeros(size(LL));
    for i=1:length(LL)
        L = LL(i);

        FL(i) = bump(L, lmin, 1, lmax) + 0.15*bump(L, lmin, 0.5*(lmin+0.95), 0.95);
    end

    % velocity-active
    FV = zeros(size(VV));
    for i=1:length(VV)
        V = VV(i)/vmax;

        if V<=-1
            FV(i) = 0;
        elseif V<=0
            FV(i) = (V+1)*(V+1);
        elseif V<=c
            FV(i) = fvmax - (c-V)*(c-V)/c;
        else
            FV(i) = fvmax;
        end
    end

    % plot length
    figure(1);
    clf;
    subplot(2,2,1);
    plot(LL, FL, 'r', 'linewidth', 1);
    hold on;
    plot(LL, 0.5*FL, 'b', 'linewidth', 1);
    plot(LL, FP, 'k', 'linewidth', 1);
    axis tight;
    xlabel('length (L0)');
    ylabel('force (F0)');
    text(0.9, 0.85, 'act = 1.0');
    text(0.9, 0.4, 'act = 0.5');
    text(1.3, 1.2, 'passive');
    box off;
    grid on;
    set(gca, 'xtick', [lmin 1 lmax], 'xticklabel', {'lmin', '1', 'lmax'}, ...
    'ytick', [0 1 fpmax], 'yticklabel', {'0', '1', 'fpmax'});

    % plot velocity
    subplot(2,2,2);
    set( plot(VV, FV, 'linewidth', 1), 'color', [.1 .5 .1]);
    axis tight;
    xlabel('velocity (L0/s)');
    ylabel('force (F0)');
    box off;
    grid on;
    set(gca, 'xtick', [-vmax 0 vmax], 'xticklabel', {'-vmax', '0', 'vmax'}, ...
    'ytick', [0 1 fvmax], 'yticklabel', {'0', '1', 'fvmax'});

    % plot full activation
    subplot(2,2,3);
    surf(LL, VV, FV'*FL + ones(size(VV))'*FP);
    axis tight;
    xlabel('length');
    ylabel('velocity');
    zlabel('force');
    title('act = 1.0');
    box off;
    set(gca, 'xtick', [lmin, 1, lmax], 'ytick', [-vmax, 0, vmax], 'ztick', [0, 1]);

    % plot half activation
    subplot(2,2,4);
    surf(LL, VV, 0.5*FV'*FL + ones(size(VV))'*FP);
    axis tight;
    xlabel('length');
    ylabel('velocity');
    zlabel('force');
    title('act = 0.5');
    box off;
    set(gca, 'xtick', [lmin, 1, lmax], 'ytick', [-vmax, 0, vmax], 'ztick', [0, 1]);
end


% skewed bump function: quadratic spline
function y = bump(L, A, mid, B)
    left = 0.5*(A+mid);
    right = 0.5*(mid+B);

    if (L<=A) || (L>=B)
        y = 0;
    elseif L<left
        x = (L-A)/(left-A);
        y = 0.5*x*x;
    elseif L<mid
        x = (mid-L)/(mid-left);
        y = 1-0.5*x*x;
    elseif L<right
        x = (L-mid)/(right-mid);
        y = 1-0.5*x*x;
    else
        x = (B-L)/(B-right);
        y = 0.5*x*x;
    end
end