-
Notifications
You must be signed in to change notification settings - Fork 1
/
MBconstraint.m
93 lines (82 loc) · 3 KB
/
MBconstraint.m
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
classdef MBconstraint < matlab.mixin.Heterogeneous
%MBconstraint Base class for constraints
% Detailed explanation goes here
properties
id
start = 0;
neq
bodyI
bodyJ = [];
fun
funD
funDD
end
methods
function obj = MBconstraint(id, neq, bodyI, bodyJ, funStr)
if nargin > 0
obj.id = id;
obj.neq = neq;
obj.bodyI = bodyI;
obj.bodyJ = bodyJ;
% Create functions.
% NOTE: if funStr represents a constant function (i.e. no
% explicit dependency on 't'), eval(funStr) would result
% in a double, and not a sym variable (since eval is an
% overloaded function). To address this, we force the
% result from eval(funStr) to *always* be a sym. In
% addition, we explicitly specify the variable as 't'.
if ~strcmp(funStr, 'NONE')
syms t;
f = sym(eval(funStr));
obj.fun = matlabFunction(f, 'vars', t);
obj.funD = matlabFunction(diff(f), 'vars', t);
obj.funDD = matlabFunction(diff(diff(f)), 'vars', t);
end
end
end
function obj = set.start(obj, start)
obj.start = start;
end
function obj = setStartIndex(obj, start)
obj.start = start;
end
function [F,T] = calcReaction(obj, t, which, P, qi, qj, lam)
% Evaluate constraint Jacobian and extract Phi_r and Phi_phi.
if isempty(obj.bodyJ)
[~,Phi_q, ~, ~] = obj.eval(t, qi, [], [0,1,0,0]);
[~,B] = MBbody.rotMat(qi(3));
else
[~,Phi_q,~,~] = obj.eval(t, qi, qj, [], [], [0,1,0,0]);
if which == 1
Phi_q(:,4:6) = [];
[~,B] = MBbody.rotMat(qi(3));
else
Phi_q(:,1:3) = [];
[~,B] = MBbody.rotMat(qj(3));
end
end
F = -Phi_q(:,1:2)' * lam;
T = -P' * B' * F - Phi_q(:,3)' * lam;
end
function print(obj)
fprintf('Constraint %i type: %s\n', obj.id, obj.getType);
fprintf(' start_eq: %i neq: %i\n', obj.start, obj.neq);
if isempty(obj.bodyJ)
fprintf(' body: %i\n', obj.bodyI);
else
fprintf(' bodyI: %i bodyJ: %i\n', obj.bodyI, obj.bodyJ);
end
% Print the 'fun' member of this constraint
if isempty(obj.fun)
fprintf(' function: NONE\n');
else
fprintf(' function: %s\n', func2str(obj.fun));
end
end
end
methods(Static)
function type = getType()
type = 'Generic';
end
end
end