-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVrep_sink.m
109 lines (86 loc) · 3.31 KB
/
Vrep_sink.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
%% This S-function implements the Simulink callback functions necessary to
% communicate with a VRep scene. In particular, the Simulink block that uses this
% S-function is intended to communicate synchronously with a
% VRep scene that runs in real-time mode.
% Matlab uses its discrete time steps to command the VRep simulation. In
% order to synchronize the simulations in Vrep and Matlab, the
% communication must be properly initialized (see the initialization
% script) and, before sending any command to the server, the Matlab client
% needs to send a trigger signal, which is a blocking operation, with the
% function call vrep.simxSynchronousTrigger(clientID);
%
% The following initialization script is inserted in the InitFcn callback of
% the Simulink block (File->Model Properties->Model Properties->Callbacks):
%
% %------ START OF THE INITIALIZATION SCRIPT ------
% clc
% global clientID
% global vrep
% global joint4Handle
%
% addpath('C:\Program Files (x86)\V-REP3\V-REP_PRO_EDU\programming\remoteApiBindings\lib\lib\32Bit')
% disp('Program started');
% vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
% vrep.simxFinish(-1); % just in case, close all opened connections
% clientID=vrep.simxStart('127.0.0.1',19997,true,true,5000,5);
%
% vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot_wait);
% vrep.simxSynchronous(clientID,true)
%
% if (clientID<=-1)
% disp('stop simulation')
% vrep.delete();
% set_param(gcs, 'SimulationCommand', 'stop')
% end
%
% [res,joint4Handle]=vrep.simxGetObjectHandle (clientID,'LBR4p_joint1',vrep.simx_opmode_oneshot_wait);
%
% %------ END OF THE INITIALIZATION SCRIPT ------
%
% Similarly, the termination script is inserted in the StopFnc callback:
%
%
% %------ START OF THE TERMINATION SCRIPT ------
% global clientID
% vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot_wait);
%
% %------ END OF THE TERMINATION SCRIPT ------
function Vrep_sink(block)
% Level-2 MATLAB file S-Function for inherited sample time demo.
% Copyright 1990-2009 The MathWorks, Inc.
% $Revision: 1.1.6.2 $
setup(block);
%endfunction
function setup(block)
%% Register number of input and output ports
block.NumInputPorts = 6;
block.NumOutputPorts = 0;
%% Setup functional port properties to dynamically
%% inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
for i=1:6
block.InputPort(i).SamplingMode = 0;
block.InputPort(i).Dimensions = 1;
block.InputPort(i).DirectFeedthrough = false;
end
%% Set block sample time to inherited
block.SampleTimes = [-1 0];
%% Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%% Register methods
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Terminate', @Terminate); % Required
%endfunction
function Outputs(block)
global clientID vrep vecJoints nJoints
%[cmdTime] = vrep.simxGetLastCmdTime(clientID);
for i=1:nJoints
if vecJoints(i) ~= -1
[res] = vrep.simxSetJointTargetPosition(clientID,vecJoints(i),block.InputPort(i).Data,vrep.simx_opmode_streaming);
end
end
%vrep.simxSynchronousTrigger(clientID);
%endfunction
function Terminate(block)
%end Terminate