-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatlab_client.cpp
279 lines (227 loc) · 7.55 KB
/
matlab_client.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
MATLAB-MEX wrapper for the CPP client
Written by Emo Todorov
Edited by Anton Sobinov
Copyright (C) 2017 Roboti LLC
Modifications copyright (C) 2017 Anton Sobinov
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.
*/
#include "mex.h"
#include "socket_api.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
static bool basic_init = false;
//------------------------ copy data between MATLAB and Mujoco --------------------------
// check size of numeric field from MATLAB
static void checkNumeric(const mxArray* mx, const char* name, int sz0, int sz1)
{
char msg[100];
if( !mx )
{
sprintf(msg, "%s: missing numeric argument", name);
mexErrMsgTxt(msg);
}
if( mxGetNumberOfDimensions(mx)!=2 )
{
sprintf(msg, "%s: numeric argument has %d dimensions, should be 2",
name, mxGetNumberOfDimensions(mx));
mexErrMsgTxt(msg);
}
if( mxGetClassID(mx)!=mxDOUBLE_CLASS )
{
sprintf(msg, "%s: expected class DOUBLE", name);
mexErrMsgTxt(msg);
}
const mwSize* sz = mxGetDimensions(mx);
if( (sz[0]!=sz0 || sz[1]!=sz1) && (sz[0]!=sz1 || sz[1]!=sz0) )
{
sprintf(msg, "%s: expected %d-by-%d or %d-by-%d, got %d-by-%d",
name, sz0, sz1, sz1, sz0, sz[0], sz[1]);
mexErrMsgTxt(msg);
}
}
// MATLAB => Mujoco: structure field
template <typename T>
void mx2mjc(T* mj, const mxArray* arg, const int nr, const int nc, const char* name)
{
// get field and check
const mxArray* mx = mxGetField(arg, 0, name);
if( !mx )
{
// skip empty array
if( nr==0 || nc==0 )
return;
char msg[200];
sprintf(msg, "missing field '%s'", name);
mexErrMsgTxt(msg);
}
checkNumeric(mx, name, nr, nc);
// copy data
double* mat = mxGetPr(mx);
for( int r=0; r<nr; r++ )
for( int c=0; c<nc; c++ )
mj[c+r*nc] = (T)mat[r+c*nr];
}
// Mujoco => MATLAB: structure field
template <typename T>
void mjc2mx(mxArray* out, const T* mj, const int nr, const int nc, const char* name)
{
// return if no data; empty matrix assigned by default
if( !nr || !nc )
return;
// check field name
if( mxGetFieldNumber(out, name)==-1 )
{
mexPrintf("field name '%s' unrecognized\n", name);
mexErrMsgTxt("error copying data from MuJoCo to Matlab");
}
// create field
mxSetField(out, 0, name, mxCreateDoubleMatrix(nr, nc, mxREAL));
// copy data
double* mat = mxGetPr(mxGetField(out, 0, name));
for( int r=0; r<nr; r++ )
for( int c=0; c<nc; c++ )
mat[r+c*nr] = (double)mj[c+r*nc];
}
// --------------------- API ----------------------------------------------------
// wrapper for mj_request_squares
mxArray* getsquares(const mxArray* pin)
{
mjRequestSquares mjrequestsquares;
memset(&mjrequestsquares, 0, sizeof(mjRequestSquares));
mjReplySquares mjreplysquares;
memset(&mjreplysquares, 0, sizeof(mjReplySquares));
// copy data from Matlab structure to C structure
mx2mjc(&mjrequestsquares.n, pin, 1, 1, "n");
mx2mjc(mjrequestsquares.dat, pin, mjrequestsquares.n, 1, "dat");
// call simulator, make sure call succeeds
if( mj_request_squares(&mjrequestsquares, &mjreplysquares) != mjCOM_OK )
mexErrMsgTxt(sa_last_result());
// create Matlab structure for output
const int size = 2;
const char* name[size] = {
"n",
"dat"
};
mxArray* out = mxCreateStructMatrix(1, 1, size, name);
// fill structure with data
mjc2mx(out, &mjreplysquares.n, 1, 1, "n");
mjc2mx(out, mjreplysquares.dat, mjreplysquares.n, 1, "dat");
return out;
}
// wrapper for mj_request_info
mxArray* getinfo()
{
mjInfo mjinfo;
memset(&mjinfo, 0, sizeof(mjInfo));
// call server, make sure call succeeds
if( mj_request_info(&mjinfo) != mjCOM_OK )
mexErrMsgTxt(sa_last_result());
// create Matlab structure for output
const int size = 1;
const char* name[size] = {
"n"
};
mxArray* out = mxCreateStructMatrix(1, 1, size, name);
// fill structure with data
mjc2mx(out, &mjinfo.n, 1, 1, "n");
return out;
}
//------------------------- main mex API ------------------------------------------------
const char _help[] =
"\nUSAGE: [output] = matlab_client(command, [input])\n\n"
" OUTPUT COMMAND INPUT\n\n"
" 'help'\n"
" 'connect' [host name or ip address [, port]] \n"
" 'close'\n"
" mjReplySquares 'getsquares' mjRequestSquares\n"
" mjInfo 'version'\n"
"\nExamples:\n"
"\tmatlab_client('help') Displays this message\n"
"\tmatlab_client('connect') Connects to localhost on default port\n"
"\tmatlab_client('connect', '10.3.29.233')\n"
"\tmatlab_client('connect', '', '44401') Connects to localhost on port 44401\n";
// exit function
void exitFunction(void)
{
sa_close();
mexUnlock();
}
// entry point
void mexFunction(int nout, mxArray* pout[], int nin, const mxArray* pin[])
{
char command[200], text[200], text2[200];
int res = 0;
// register exit function only once
if( !basic_init )
{
mexAtExit(exitFunction);
}
// no inputs: print help, return
if( !nin )
{
mexPrintf("%s\n", _help);
return;
}
// get command string
if( mxGetClassID(pin[0])!=mxCHAR_CLASS )
mexErrMsgTxt("first argument must be command string");
mxGetString(pin[0], command, 200);
// apply command string
if( !strcmp(command, "connect") )
{
mexLock();
// no host name: NULL
if( nin==1 )
res = sa_connect(0, 0);
else
{
if(mxGetClassID(pin[1])!=mxCHAR_CLASS )
mexErrMsgTxt("string argument expected for host");
else
{
mxGetString(pin[1], text, 200);
if( nin>2 )
if (mxGetClassID(pin[2])!=mxCHAR_CLASS)
mexErrMsgTxt("string arguments expected for port");
else {
mxGetString(pin[2], text2, 200);
if (res = sa_change_port(text2) == saOK)
res = sa_connect(text, 0);
}
else
res = sa_connect(text, 0);
}
}
}
// help
else if( !strcmp(command, "help") )
{
mexPrintf("%s\n", _help);
return;
}
// sa_close or close
else if( !strcmp(command, "close") )
exitFunction();
// getsquares
else if( !strcmp(command, "getsquares"))
pout[0] = getsquares(pin[1]);
// getinfo
else if( !strcmp(command, "version") || !strcmp(command, "info"))
pout[0] = getinfo();
// unknown command
else
mexErrMsgTxt("unknown command");
// return error message text
if( res )
mexErrMsgTxt(sa_last_result());
}