-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpolar_sfunc.m
163 lines (133 loc) · 4.84 KB
/
polar_sfunc.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
% Copyright 2022-2023 Peter Corke, Witold Jachimczyk, Remo Pillat
function polar_sfunc(block)
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).Dimensions = 3;
block.InputPort(1).DirectFeedthrough = true;
% Override output port properties
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).Dimensions = 4;
block.NumDialogPrms = 0;
% Specify if Accelerator should use TLC or call back into
% M-file
block.SetAccelRunOnTLC(false);
block.SimStateCompliance = 'DefaultSimState';
%block.RegBlockMethod('CheckParameters', @CheckPrms);
%block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
%block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);
%block.RegBlockMethod('SetOutputPortDimensions', @SetOutPortDims);
%block.RegBlockMethod('SetInputPortDataType', @SetInpPortDataType);
%block.RegBlockMethod('SetOutputPortDataType', @SetOutPortDataType);
%block.RegBlockMethod('SetInputPortComplexSignal', @SetInpPortComplexSig);
%block.RegBlockMethod('SetOutputPortComplexSignal', @SetOutPortComplexSig);
%% PostPropagationSetup:
%% Functionality : Setup work areas and state variables. Can
%% also register run-time methods here
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
%% Register methods called at run-time
%% -----------------------------------------------------------------
%%
%% ProcessParameters:
%% Functionality : Called in order to allow update of run-time
%% parameters
%block.RegBlockMethod('ProcessParameters', @ProcessPrms);
%%
%% InitializeConditions:
%% Functionality : Called in order to initialize state and work
%% area values
%block.RegBlockMethod('InitializeConditions', @InitializeConditions);
%%
%% Start:
%% Functionality : Called in order to initialize state and work
%% area values
block.RegBlockMethod('Start', @Start);
%%
%% Outputs:
%% Functionality : Called to generate block outputs in
%% simulation step
block.RegBlockMethod('Outputs', @Outputs);
%%
%% Update:
%% Functionality : Called to update discrete states
%% during simulation step
%% C-Mex counterpart: mdlUpdate
%%
%block.RegBlockMethod('Update', @Update);
%%
%% Derivatives:
%% Functionality : Called to update derivatives of
%% continuous states during simulation step
%% C-Mex counterpart: mdlDerivatives
%%
%block.RegBlockMethod('Derivatives', @Derivatives);
%%
%% Projection:
%% Functionality : Called to update projections during
%% simulation step
%% C-Mex counterpart: mdlProjections
%%
%block.RegBlockMethod('Projection', @Projection);
%%
%% SimStatusChange:
%% Functionality : Called when simulation goes to pause mode
%% or continnues from pause mode
%% C-Mex counterpart: mdlSimStatusChange
%%
%block.RegBlockMethod('SimStatusChange', @SimStatusChange);
%%
%% Terminate:
%% Functionality : Called at the end of simulation for cleanup
%% C-Mex counterpart: mdlTerminate
%%
%block.RegBlockMethod('Terminate', @Terminate);
%block.RegBlockMethod('WriteRTW', @WriteRTW);
end
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'direction';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = false;
end
function Start(block)
block.Dwork(1).Data = 0;
end
function Outputs(block)
X = block.InputPort(1).Data;
x = X(1); y = X(2); theta = X(3);
d = sqrt(x^2 + y^2);
if block.Dwork(1).Data == 0
beta = -atan2(-y, -x);
alpha = -theta - beta;
fprintf('alpha %f, beta %f\n', alpha, beta);
% first time in simulation, choose the direction of travel
if (alpha > pi/2) || (alpha < -pi/2)
fprintf('going backwards\n');
block.Dwork(1).Data = -1;
else
fprintf('going forwards\n');
block.Dwork(1).Data = 1;
end
elseif block.Dwork(1).Data == -1
beta = -atan2(y, x);
alpha = -theta - beta;
else
beta = -atan2(-y, -x);
alpha = -theta - beta;
end
if alpha > pi/2
alpha = pi/2;
end
if alpha < -pi/2
alpha = -pi/2;
end
block.OutputPort(1).Data = [d alpha beta block.Dwork(1).Data];
end