Skip to content

Commit 71b036b

Browse files
authored
This PR adds force/torque actuation/reading methods to the class 'DQ_VrepInterface' (#104)
* Added the class FrankaEmikaPandaRobot(). * Added the method 'get_force_sensor_readings' to DQ_VrepInterface(). It allows reading the force/torques of a force sensor in V-REP/CoppeliaSim. * Added the methods 'get_joint_torques()' and 'set_joint_torques()' to the class DQ_VrepInterface. * Added the class 'FrankaEmikaPandaVrepRobot'. * Removed method 'get_force_sensor_readings()' from class 'DQ_VrepInterface'. * Removed the class 'FrankaEmikaPandaVrepRobot'. Added the methods 'send_tau_to_vrep()' and 'get_tau_from_vrep()' to class 'LBR4pVrepRobot'. * Improved documentation of the methods 'get_joint_torques' and 'set_joint_torques' in class 'DQ_VrepInterface'. * [DQ_VrepInterface] Removed 'return_code' from method 'get_joint_torques'. * [LBR4pVrepRobot] Removed methods 'send_tau_to_vrep()' and 'get_tau_from_vrep()'. * [LBR4pVrepRobot.m] Reverted modification on the comments. * [DQ_VrepInterface.m] Changed 'handles' by 'jointnames' in method 'get_joint_torques'. * [DQ_VrepInterface.m] Changed 'joint_names' by 'jointnames' in method 'set_joint_torques'.
1 parent 1eb3459 commit 71b036b

File tree

1 file changed

+129
-24
lines changed

1 file changed

+129
-24
lines changed

interfaces/vrep/DQ_VrepInterface.m

Lines changed: 129 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
% set_joint_target_velocities - Set the joint target velocities of a
5858
% robot
5959
% get_joint_velocities - Get the joint velocities of a robot
60+
% get_joint_torques - Get the joint torques of a robot
61+
% set_joint_torques - Set the joint torques of a robot
6062
%
6163
% DQ_VrepInterface Methods (For advanced users)
6264
% get_handle - Get the handle of a V-REP object
@@ -96,6 +98,9 @@
9698
% - Improved the documentation of the class
9799
%
98100
% 3. Frederico Fernandes Afonso Silva ([email protected])
101+
% - Added the following methods:
102+
% - get_joint_torques() (see https://github.com/dqrobotics/matlab/pull/104)
103+
% - set_joint_torques() (see https://github.com/dqrobotics/matlab/pull/104)
99104
% - Altered the following properties from 'private' to 'protected'
100105
% (see discussions in https://github.com/dqrobotics/matlab/pull/101
101106
% to further details):
@@ -181,22 +186,19 @@ function connect(obj,ip,port)
181186
error('Failed to connect to remote API server');
182187
end
183188
end
184-
185189

186190
function disconnect(obj)
187191
% This method ends the communication between the client and
188192
% the CoppeliaSim scene. This should be the very last method called.
189193
obj.vrep.simxFinish(obj.clientID);
190194
end
191195

192-
193196
function disconnect_all(obj)
194197
% This method ends all running communication threads with the
195198
% CoppeliaSim scene. This should be the very last method called.
196199
obj.vrep.simxFinish(-1);
197200
end
198201

199-
200202
function set_synchronous(obj,flag)
201203
% This method enables or disables the stepped (synchronous) mode
202204
% for the remote API server service that the client is connected to.
@@ -209,33 +211,28 @@ function set_synchronous(obj,flag)
209211
obj.vrep.simxSynchronous(obj.clientID,flag);
210212
end
211213

212-
213214
function trigger_next_simulation_step(obj)
214215
% This method sends a synchronization trigger signal to the CoppeliaSim scene,
215216
% which performs a simulation step when the synchronous mode is used.
216217
obj.vrep.simxSynchronousTrigger(obj.clientID);
217218
end
218219

219-
220220
function ping_time = wait_for_simulation_step_to_end(obj)
221221
% This method returns the time needed for a command to be sent
222222
% to the CoppeliaSim scene, executed, and sent back.
223223
[~, ping_time] = obj.vrep.simxGetPingTime(obj.clientID);
224224
end
225225

226-
227226
function start_simulation(obj)
228227
% This method starts the CoppeliaSim simulation.
229228
obj.vrep.simxStartSimulation(obj.clientID,obj.vrep.simx_opmode_oneshot);
230229
end
231230

232-
233231
function stop_simulation(obj)
234232
% This method stops the CoppeliaSim simulation.
235233
obj.vrep.simxStopSimulation(obj.clientID,obj.vrep.simx_opmode_blocking);
236234
end
237235

238-
239236
function handles = get_handles(obj,names)
240237
% This method gets the handles for a cell array of
241238
% object names in the the CoppeliaSim scene.
@@ -259,7 +256,6 @@ function stop_simulation(obj)
259256
end
260257
end
261258

262-
263259
function handle = get_handle(obj,name)
264260
% This method gets the handle for a given object in the CoppeliaSim scene.
265261
%
@@ -275,7 +271,6 @@ function stop_simulation(obj)
275271
obj.vrep.simx_opmode_blocking);
276272
end
277273

278-
279274
function t = get_object_translation(obj,objectname,reference_frame,opmode)
280275
% This method gets the translation of an object in the CoppeliaSim scene.
281276
%
@@ -353,7 +348,6 @@ function stop_simulation(obj)
353348
t = DQ([0,double(object_position)]);
354349
end
355350

356-
357351
function set_object_translation(obj,objectname,translation,reference_frame,opmode)
358352
% This method sets the translation of an object in the CoppeliaSim scene.
359353
% Usage:
@@ -408,7 +402,6 @@ function set_object_translation(obj,objectname,translation,reference_frame,opmod
408402
end
409403
end
410404

411-
412405
function r = get_object_rotation(obj, objectname, reference_frame, opmode)
413406
% This method gets the rotation of an object in the CoppeliaSim scene.
414407
%
@@ -493,7 +486,6 @@ function set_object_translation(obj,objectname,translation,reference_frame,opmod
493486
object_rotation_double(3)]));
494487
end
495488

496-
497489
function set_object_rotation(obj,objectname,rotation,reference_frame,opmode)
498490
% This method sets the rotation of an object in the CoppeliaSim scene.
499491
% Usage:
@@ -550,7 +542,6 @@ function set_object_rotation(obj,objectname,rotation,reference_frame,opmode)
550542
end
551543
end
552544

553-
554545
function x = get_object_pose(obj,objectname,reference_frame,opmode)
555546
% This method gets the pose of an object in the CoppeliaSim scene.
556547
%
@@ -606,7 +597,6 @@ function set_object_rotation(obj,objectname,rotation,reference_frame,opmode)
606597
x = r + 0.5*DQ.E*t*r;
607598
end
608599

609-
610600
function set_object_pose(obj,objectname,pose,reference_frame,opmode)
611601
% This method sets the pose of an object in the CoppeliaSim scene.
612602
% Usage:
@@ -678,8 +668,7 @@ function set_object_pose(obj,objectname,pose,reference_frame,opmode)
678668
obj.handle_from_string_or_handle(relative_to_handle),...
679669
opmode);
680670
end
681-
end
682-
671+
end
683672

684673
function set_joint_positions(obj,jointnames,joint_positions,opmode)
685674
% This method sets the joint positions of a robot in the CoppeliaSim scene.
@@ -742,8 +731,7 @@ function set_joint_positions(obj,jointnames,joint_positions,opmode)
742731
end
743732
end
744733
end
745-
746-
734+
747735
function set_joint_target_positions(obj,jointnames,joint_target_positions,opmode)
748736
% This method sets the joint target positions of a robot in the CoppeliaSim scene.
749737
% It is required a dynamics enabled scene, and joints in dynamic mode
@@ -807,7 +795,6 @@ function set_joint_target_positions(obj,jointnames,joint_target_positions,opmode
807795
end
808796
end
809797

810-
811798
function [joint_positions,retval]=get_joint_positions(obj,jointnames,opmode)
812799
% This method gets the joint positions of a robot in the CoppeliaSim scene.
813800
% Usage:
@@ -898,8 +885,7 @@ function set_joint_target_positions(obj,jointnames,joint_target_positions,opmode
898885
joint_positions = thetas;
899886
end
900887

901-
902-
function joint_velocities = get_joint_velocities(obj,jointnames,opmode)
888+
function joint_velocities = get_joint_velocities(obj,jointnames,opmode)
903889
% This method gets the joint velocities of a robot in the CoppeliaSim scene.
904890
% Usage:
905891
% Recommended:
@@ -930,7 +916,7 @@ function set_joint_target_positions(obj,jointnames,joint_target_positions,opmode
930916
%
931917
% % Advanced usage:
932918
% joint_velocities = get_joint_velocities(jointnames, OP_ONESHOT);
933-
919+
934920
joint_velocities = zeros(length(jointnames),1);
935921
for joint_index=1:length(jointnames)
936922
% First approach to the auto-management using
@@ -1034,7 +1020,126 @@ function set_joint_target_velocities(obj,jointnames,joint_target_velocities,opmo
10341020
opmode);
10351021
end
10361022
end
1037-
end
1023+
end
1024+
1025+
function joint_torques = get_joint_torques(obj,jointnames,opmode)
1026+
% This method gets the joint torques of a robot in the CoppeliaSim scene.
1027+
% Usage:
1028+
% Recommended:
1029+
% joint_torques = get_joint_torques(jointnames);
1030+
%
1031+
% Advanced:
1032+
% joint_torques = get_joint_torques(jointnames, opmode)
1033+
%
1034+
% jointnames: The joint names.
1035+
% (optional) opmode: The operation mode. If not
1036+
% specified, the opmode will be set automatically.
1037+
%
1038+
% You can use the following modes:
1039+
% OP_BLOCKING
1040+
% OP_STREAMING
1041+
% OP_ONESHOT
1042+
% OP_BUFFER;
1043+
%
1044+
% Check this link for more details: https://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsMatlab.htm#simxGetJointForce
1045+
%
1046+
% Example:
1047+
% jointnames={'LBR4p_joint1','LBR4p_joint2','LBR4p_joint3','LBR4p_joint4',...
1048+
% 'LBR4p_joint5','LBR4p_joint6','LBR4p_joint7'};
1049+
%
1050+
% % Recommended:
1051+
% joint_torques = get_joint_torques(jointnames);
1052+
%
1053+
% % Advanced usage:
1054+
% joint_torques = get_joint_torques(jointnames, OP_STREAMING);
1055+
1056+
joint_torques = zeros(length(jointnames),1);
1057+
1058+
% If the user does not specify the opmode, it is chosen first
1059+
% as STREAMING and then as BUFFER, as specified by the remote
1060+
% API documentation.
1061+
for joint_index=1:length(jointnames)
1062+
% First approach to the auto-management using
1063+
% DQ_VrepInterfaceMapElements. If the user does not specify the
1064+
% opmode, it is chosen first as STREAMING and then as BUFFER,
1065+
% as specified by the remote API documentation
1066+
if nargin <= 2
1067+
if isa(jointnames,'cell')
1068+
element = obj.element_from_string(jointnames{joint_index});
1069+
else
1070+
element = obj.element_from_string(jointnames);
1071+
end
1072+
if(~element.state_from_function_signature('get_joint_torques'))
1073+
[~,tmp] = obj.vrep.simxGetJointForce(obj.clientID, element.handle, obj.OP_STREAMING);
1074+
return_code = 1;
1075+
while(return_code == 1)
1076+
[return_code,tmp] = obj.vrep.simxGetJointForce(obj.clientID, element.handle, obj.OP_BUFFER);
1077+
end
1078+
else
1079+
[~,tmp] = obj.vrep.simxGetJointForce(obj.clientID, element.handle, obj.OP_BUFFER);
1080+
end
1081+
else
1082+
[~,tmp] = obj.vrep.simxGetJointForce(obj.clientID, ...
1083+
obj.handle_from_string_or_handle(jointnames{joint_index}), opmode);
1084+
end
1085+
joint_torques(joint_index) = double(-tmp); % V-REP returns joint torques with an inverse sign
1086+
end
1087+
end
1088+
1089+
function set_joint_torques(obj,jointnames,torques,opmode)
1090+
% This method sets the joint torques of a robot in the CoppeliaSim scene.
1091+
% Usage:
1092+
% Recommended:
1093+
% set_joint_torques(jointnames, torques);
1094+
%
1095+
% Advanced:
1096+
% set_joint_torques(jointnames, torques, opmode);
1097+
%
1098+
% jointnames: The joint names.
1099+
% torques: The joint torques.
1100+
% (optional) opmode: The operation mode. If not
1101+
% specified, the opmode will be set automatically.
1102+
%
1103+
% You can use the following modes:
1104+
% OP_BLOCKING
1105+
% OP_STREAMING
1106+
% OP_ONESHOT
1107+
% OP_BUFFER;
1108+
%
1109+
% Check this link for more details: https://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsMatlab.htm#simxSetJointTargetVelocity
1110+
%
1111+
% Example:
1112+
% jointnames={'LBR4p_joint1','LBR4p_joint2','LBR4p_joint3','LBR4p_joint4',...
1113+
% 'LBR4p_joint5','LBR4p_joint6','LBR4p_joint7'};
1114+
% u = [0.1 0.1 0.1 0.1 0.1 0.1 0.1];
1115+
%
1116+
% % Recommended:
1117+
% set_joint_torques(jointnames, u);
1118+
%
1119+
% % Advanced usage:
1120+
% set_joint_torques(jointnames, u, opmode);
1121+
1122+
% If the user does not specify the opmode, it is chosen first
1123+
% as STREAMING and then as OP_ONESHOT, as specified by the
1124+
% remote API documentation.
1125+
if nargin == 3
1126+
% The recommended mode is OP_ONESHOT
1127+
for joint_index=1:length(jointnames)
1128+
obj.vrep.simxSetJointTargetVelocity(obj.clientID, obj.handle_from_string_or_handle(jointnames{joint_index}), ...
1129+
sign(torques(joint_index))*10e10, obj.OP_ONESHOT);
1130+
obj.vrep.simxSetJointForce(obj.clientID, obj.handle_from_string_or_handle(jointnames{joint_index}), ...
1131+
abs(torques(joint_index)), obj.OP_ONESHOT);
1132+
end
1133+
else
1134+
for joint_index=1:length(jointnames)
1135+
obj.vrep.simxSetJointTargetVelocity(obj.clientID, obj.handle_from_string_or_handle(jointnames{joint_index}),...
1136+
sign(torques(joint_index))*10e10, opmode);
1137+
obj.vrep.simxSetJointForce(obj.clientID,obj.handle_from_string_or_handle(jointnames{joint_index}),abs(torques(joint_index)),...
1138+
opmode);
1139+
end
1140+
end
1141+
end
1142+
10381143
end
10391144

10401145
end

0 commit comments

Comments
 (0)