-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjMPC_Install.m
250 lines (217 loc) · 7.5 KB
/
jMPC_Install.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
function jMPC_Install(savePath,runTests,openBrowser)
% jMPC Toolbox Installation File
%
% jMPC_Install(savePath, runTests, openBrowser)
%
% savePath: Save the paths added by jMPC to the MATLAB path
% runTests: Run the post-installation tests
% openBrowser: Whether to open the jMPC Toolbox Website after installation
%
% All arguments are optional and if not supplied, the user will be prompted
% to enter their selection in the MATLAB Command Window. True is the
% default option for each argument.
%
% You MUST be in the current directory of this file!
%
% Copyright (C) 2023 Jonathan Currie (Control Engineering)
% https://controlengineering.co.nz/Wikis/jMPC/
% Handle missing input args
if (nargin < 3), openBrowser = []; end
if (nargin < 2), runTests = []; end
if (nargin < 1), savePath = []; end
cpath = cd;
try
cd('Utilities');
catch %#ok<CTCH>
error('You don''t appear to be in the jMPC Toolbox directory');
end
cver = jMPCver();
fprintf('\n------------------------------------------------\n')
fprintf([' INSTALLING jMPC TOOLBOX ver ' sprintf('%1.2f',cver) '\n\n'])
cd(cpath);
%Perform pre-req check
if(~preReqChecks(cpath))
return;
end
fprintf('\n- Checking for previous versions of jMPC Toolbox...\n');
no = jMPC_Uninstall('jMPC_Install.m',0);
if(no < 1)
fprintf('Could not find a previous installation of jMPC Toolbox\n');
else
fprintf('Successfully uninstalled previous version(s) of jMPC Toolbox\n');
end
fprintf('\n- Adding jMPC Paths to MATLAB Search Path...');
genp = genpath(cd);
genp = regexp(genp,';','split');
%Folders to exclude from adding to Matlab path
remInd1 = strfind(genp,'.git');
ind = [];
%Can't seem a way to check if cells are empty as a vector?
for i = 1:length(remInd1)
if(any(remInd1{i}))
ind = [ind i]; %#ok<AGROW>
end
end
%Remove paths from above and add to matlab path
genp(ind) = [];
addpath(genp{:});
rehash
fprintf('Done\n\n');
if (isempty(savePath))
in = input('- Would You Like To Save the Path Changes? (Recommended) (y/n): ','s');
else
in = bool2yn(savePath);
end
if(strcmpi(in,'y'))
try
savepath;
catch
warning(['It appears you do not have administrator rights on your computer to save the Matlab path. '...
'In order to run jMPC Toolbox you will need to install it each time you wish to use it. To fix '...
'this please contact your system administrator to obtain administrator rights.']);
end
end
fprintf('\n');
if (isempty(runTests))
in = input('- Would You Like To Run Post Installation Tests? (Recommended) (y/n): ','s');
else
in = bool2yn(runTests);
end
if(strcmpi(in,'y'))
jMPC_Install_Test(1);
end
fprintf('\n');
%Launch Examples page
if (isempty(openBrowser) || (openBrowser == true))
web('https://controlengineering.co.nz/Wikis/jMPC/index.php/Examples/Examples','-browser');
end
fprintf('\njMPC Toolbox Installation Complete!\n');
disp('------------------------------------------------')
function no = jMPC_Uninstall(token,del)
no = 0;
%Check nargin in, default don't delete and jmpc mode
if(nargin < 2 || isempty(del))
del = 0;
end
%Check if we have anything to remove
paths = which(token,'-all');
len = length(paths);
%If mode is opti, should always be at least 1 if we are in correct directory
if(~len)
error('Expected to find "%s" in the current directory - please ensure you are in the jMPC Toolbox directory',token);
%If mode is opti, and there is one entry
elseif(len == 1)
%if len == 1, either we are in the correct folder with nothing to remove, or we are in the
%wrong folder and there are files to remove, check CD
if(any(strfind(paths{1},cd)))
no = 0;
return;
else
error('Expected to find "%s" in the current directory - please ensure you are in the jMPC Toolbox directory',token);
end
else %old ones to remove
%Remove each folder found, and all subdirs under
for n = 2:len
%Absolute path to remove
removeP = paths{n};
%Search backwards for first file separator (we don't want the filename)
for j = length(removeP):-1:1
if(removeP(j) == filesep)
break;
end
end
removeP = removeP(1:max(j-1,1));
%Everything is lowercase to aid matching
lrpath = lower(removeP);
opath = regexp(lower(path),';','split');
%Find & Remove Matching Paths
no = 0;
for i = 1:length(opath)
%If we find it in the current path string, remove it
fnd = strfind(opath{i},lrpath);
if(~isempty(fnd))
rmpath(opath{i});
no = no + 1;
end
end
%Check we aren't removing our development version
rehash;
if(isdir([removeP filesep 'Testing'])) %is this robust enough?
fprintf('Found development version in "%s", skipping.\n',removeP);
return;
end
%If delete is specified, also delete the directory
if(del)
stat = recycle; recycle('on'); %turn on recycling
rmdir(removeP,'s'); %not sure if we dont have permissions here
recycle(stat); %restore to original
end
end
end
function OK = preReqChecks(cpath)
%Search for each required prereq
% Note we no longer search the registry, simply check if we can load a mex
% file which requires each runtime
if(~isempty(strfind(computer,'64')))
arch = 'x64';
else
arch = 'x86';
end
mver = ver('MATLAB');
fprintf('- Checking MATLAB version and operating system...\n');
vv = regexp(mver.Version,'\.','split');
if(str2double(vv{1}) <= 9)
if(str2double(vv{2}) <= 4)
fprintf(2,'jMPC is designed for MATLAB 2018b or above.\nIt will install into lower versions, but you may experience reliability problems.\nPlease upgrade to R2018b or later.\n');
end
end
switch(mexext)
case 'mexw32'
warning('jMPC Toolbox contains MEX files compiled for 64bit Windows PCs only. Only MATLAB MPC simulations will be available.');
OK = 1; return;
case 'mexw64'
fprintf('MATLAB %s 64bit (Windows x64) detected\n',mver.Release);
otherwise
warning('jMPC Toolbox contains MEX files compiled for Windows PCs only. Only MATLAB MPC simulations will be available.');
OK = 1; return;
end
fprintf('\n- Checking for required pre-requisites...\n');
havVC = true;
missing = false;
%Check for VC++ 2019
cd 'Solvers'
try
a = mquad_wright; %#ok<NASGU>
catch
havVC = false;
end
cd ../
cd(cpath);
%See if missing anything
if(~havVC)
missing = true;
end
%Print Missing PreReqs
if(~havVC)
fprintf(2,'Cannot find the Microsoft VC++ 2019 %s Redistributable!\n',arch);
else
fprintf('Found the Microsoft VC++ 2019 %s Redistributable\n',arch);
end
%Install Instructions for each Package
if(missing)
fprintf(2,'\nYou are missing one or more pre-requisites. Please read the instructions below carefully to install them:\n\n');
if(~havVC)
fprintf(2,' Microsoft VC++ 2019:\n - Download from: https://aka.ms/vs/17/release/vc_redist.x64.exe\n');
fprintf(2,' - When prompted, select the ''%s'' package. Once downloaded, install it.\n\n',arch);
end
fprintf(2,'\nOnce you have downloaded AND installed all the above packages, you MUST restart MATLAB.\n\nIf this message appears again after installing the above packages, try restarting your computer.\n\n\n');
OK = false;
else
OK = true;
end
function in = bool2yn(val)
if (isempty(val) || val == true)
in = 'y';
else
in = 'n';
end