demo_testDampingRatio01.m 2.94 KB
Newer Older
Sylvain Calinon's avatar
Sylvain Calinon committed
1
function demo_testDampingRatio01
2 3 4 5 6 7 8
% Behaviors of critically damped systems and ideal underdamped systems.
%
% Writing code takes time. Polishing it and making it available to others takes longer! 
% If some parts of the code were useful for your research of for a better understanding 
% of the algorithms, please reward the authors by citing the related publications, 
% and consider making your own research available in this way.
%
9
% @article{Calinon16JIST,
10 11 12
%   author="Calinon, S.",
%   title="A Tutorial on Task-Parameterized Movement Learning and Retrieval",
%   journal="Intelligent Service Robotics",
13 14 15 16 17 18
%		publisher="Springer Berlin Heidelberg",
%		doi="10.1007/s11370-015-0187-9",
%		year="2016",
%		volume="9",
%		number="1",
%		pages="1--29"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
% }
% 
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
% 
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
% 
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
% 
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
Sylvain Calinon's avatar
Sylvain Calinon committed
37

38 39 40

%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sylvain Calinon's avatar
Sylvain Calinon committed
41 42 43 44 45 46 47
nbData = 100; %Number of datapoints
kp = 200; %Stiffness
kv = 2*kp^.5; %Damping for critically damped system
kv2 = (2*kp)^.5; %Damping for ideal underdamped system
xhat = 1; %Desired target
dt = 0.01; %Time step

48 49 50

%% Simulation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sylvain Calinon's avatar
Sylvain Calinon committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
%Computation of tracking error for critically damped system
x=0; dx=0; e=0;
for t=1:nbData
	Data(:,t) = [x; dx]; %Log data
	ddx = kp*(xhat-x) - kv * dx; %Spring-damper system
	dx = dx + ddx*dt; %Update velocity
	x = x + dx*dt; %Update position
	e = e + (xhat-x); %Cumulated tracking error
end
disp(['Cumulated tracking error for critically damped system: ' num2str(e)]);

%Computation of tracking error for ideal underdamped system
x=0; dx=0; e=0;
for t=1:nbData
	Data2(:,t) = [x; dx]; %Log data
	ddx = kp*(xhat-x) - kv2 * dx; %Spring-damper system
	dx = dx + ddx*dt; %Update velocity
	x = x + dx*dt; %Update position
	e = e + (xhat-x); %Cumulated tracking error
end
disp(['Cumulated tracking error for ideal underdamped system: ' num2str(e)]);

73 74
%% Plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sylvain Calinon's avatar
Sylvain Calinon committed
75 76 77 78 79 80
figure; hold on;
plot([1:nbData]*dt, Data(1,:), 'k-');
plot([1:nbData]*dt, Data2(1,:), 'k:');
legend('critically damped', 'ideal underdamped');
plot([1,nbData]*dt, [xhat,xhat], 'r-');
xlabel('t'); ylabel('x');
81 82 83 84

%print('-dpng','graphs/demo_testDampingRatio01.png');
%pause;
%close all;
Sylvain Calinon's avatar
Sylvain Calinon committed
85 86 87 88