-
Notifications
You must be signed in to change notification settings - Fork 11
/
VrepConnector.m
60 lines (53 loc) · 2.44 KB
/
VrepConnector.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
classdef VrepConnector
properties
sim; %Similar to fd
clientID; %Used for server connection and server requests
robot_joints = [] %List of joint handles
step_time_vrep; %Integration step used for simulation
end
methods
function obj = VrepConnector(port, step_time_vrep)
addpath vrep_lib/; %Adding the APIs to the path
obj.step_time_vrep = step_time_vrep;
obj.sim = remApi('remoteApi'); %RemoteAPI object
obj.sim.simxFinish(-1);
obj.clientID = obj.sim.simxStart('127.0.0.1', port, true, true, 5000, 5);
if (obj.clientID > -1)
disp('Connected to simulator');
else
disp('Error in connection');
end
% enable the synchronous mode on the client: (integration step on call)
obj.sim.simxSynchronous(obj.clientID, true);
% start the simulation:
obj.sim.simxStartSimulation(obj.clientID, obj.sim.simx_opmode_blocking);
for i = 1:7
[~,obj.robot_joints(i)] = obj.sim.simxGetObjectHandle(obj.clientID, strcat('LBR_iiwa_14_R820_joint',int2str(i)), obj.sim.simx_opmode_blocking);
end
for i = 1:7
[~, joint_pos] = obj.sim.simxGetJointPosition(obj.clientID, obj.robot_joints(i), obj.sim.simx_opmode_streaming);
end
end
function Close(obj)
obj.sim.simxStopSimulation(obj.clientID, obj.sim.simx_opmode_blocking);
obj.sim.simxFinish(-1);
obj.sim.delete();
end
function ApplyControl(obj, u, delta_t)
for i = 1:7
obj.sim.simxSetJointTargetVelocity(obj.clientID, obj.robot_joints(i), u(i), obj.sim.simx_opmode_oneshot);
end
for i = 1:(delta_t/obj.step_time_vrep) %Number of integrations in delta_t
obj.sim.simxSynchronousTrigger(obj.clientID); %Triggering the integration
% To overcome delay in values according to (Remote API modus operandi) document
end
obj.sim.simxGetPingTime(obj.clientID); %Synchronizing
end
function q = GetState(obj)
q = zeros(7,1);
for i=1:7
[~, q(i)] = obj.sim.simxGetJointPosition(obj.clientID, obj.robot_joints(i), obj.sim.simx_opmode_buffer);
end
end
end
end