-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVREP.m
2638 lines (2152 loc) · 86.1 KB
/
VREP.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% VREP Simulator Class
% NOTE: All VREP API Functions use SI Units!
% This is a class that wraps the VREP Remote API. It manages the Remote API
% connection. By default will attempt to connect to '127.0.0.1:19997'
% (Localhost).
%
% Constructor Arguments.
%
% 'IP' % String that containes IP address of V-REP server.
% Default is '127.0.0.1'
% 'PORT' % Port number of V-REP server. Default is 19997.
% 'timeout' % Connection timeout in ms. Default is 2000ms
% 'getter_mode' % Opmode used by commands that return a reply.
% Default is obj.vrep.simx_opmode_oneshot_wait
% 'setter_mode' % Opmode used by commands that do not return a
% reply. Default is obj.vrep.simx_opmode_oneshot
% 'cycle' % Cycle time in ms. Default is 5ms
% 'wait' % Wait for reconnect. Logical value. Default is true
% 'reconect' % Reconnect on error. Logical value. Default is true
% 'path' % Optional if API Files are on MATLAB path. This is
% the path to V-REP API files.
%
% Properties
%
% clientID
% sim
% PORT
% IP
% clientID
% path
% libpath
% getter_mode
% setter_mode
% blocking_mode
%
%
% Methods:
%
% Simulator managemet
%
% display(obj)
% delete(obj)
% pauseComms(obj,status) % Pause communication with V-REP
% pauseSim(obj) % Pause the simulation
% startSim(obj) % Start the simulation
% stopSim(obj) % Stop the simulation
% checkcomms(obj) % Check communication with V-REP
% loadScene(obj,scene,varargin) % Load a V-REP scene
% loadObject(obj,model,varargin) % Load a model into a V-REP scene
% deleteObject(obj,handle) % Delete a model from a V-REP scene
% closeScene(obj) % Close current V-REP scene
% pingSim(obj,n) % Ping V-REP
%
% Generic object management
%
% getObjects(type) % Gets all object. Optionally a
% type of object can be specified
% getHandle(in,varargin) % Gets the Object ID of a model
% with a specified name
% getName(objhandle) % Get the name of an object with a
% specified Object ID
% getType(objhandle,str_out) % Get the type of an object
% getChildren(handle) % Get all the child objects
% associated to a specified object
% getOrientation(handle,rel2) % Get the orientation of an object
% getPosition(handle,rel2) % Get the position of an object
% setPosition(handle,new,rel2) % Set the position of an object
% setOrientation(handle,orient,rel2)% Set the orientation of an object
%
% Joint Specific Methods
%
% getJointPosition(handle) % Gets a joint's postion
% getJointMatrix(handle) % Gets a spherical joint's position
% getJointForce(handle) % Gets the forces acting on a joint
% setJointMatrix(handle,matrix) % Sets a spherical joint's position
% setJointForce(handle,force) % Sets the forces acting on a joint
% setJointPosition(handle,pos) % Sets a joint's position
% setJointTargetPosition(handle,pos)% Sets a joint's target position
% setJointTargetVelocity(handle,vel)% Sets a joint's target velocity
%
% Object Parameters
%
% setObjIntParam(handle,param,new) % Sets an object's integer parameter
% setObjFloatParam(handle,param,new)% Sets an object's float parameter
% getObjIntParam(handle,param) % Gets an object's integer
% parameter
% getObjFloatParam(handle,param) % Gets an object's float parameter
%
% Parameter Management
%
% getIntegerParam(param)
% getFloatParam(param) % Gets a float parameter
% getBooleanParam(param) % Gets a boolean parameter
% getStringParam(param) % Gets a string parameter
% setIntegerParam(param,new) % Sets an integer paramter
% setFloatParam(param, new) % Sets a float parameter
% setBooleanParam(param, new) % Sets a boolean parameter
% setStringParam(param, new) % Sets a string parameter
%
% Signal management
%
% getIntegerSignal(signal) % Gets an integer signal
% getFloatSignal(signal) % Gets a float signal
% getBooleanSignal(signal) % Gets a boolean signal
% getStringSignal(signal) % Gets a string signal
% setIntegerSignal(signal,new) % Sets an integer signal
% setFloatSignal(signal,new) % Sets a float signal
% setBooleanSignal(signal,new) % Sets a boolean signal
% setStringSignal(signal,new) % Sets a string signal
% clearIntegerSignal(signal) % Removes an integer signal from
% server
% clearFloatSignal(signalName) % Removes a float signal from
% server
% clearBooleanSignal(signalName) % Removes a boolean signal from
% server
% clearStringSignal(signalName) % Removes a string signal from
% server
%
% Image Sensor Handling
%
% readVisionSensor(handle,grey) % Gets a RGB or greyscale image
% from a vision sensor.
% readPointVisionSensor(handle) % Gets raw data packets from a
% vision sensor.
% readVisionSensorDepth(handle) % Gets a greyscale depth image from
% a vision sensor.
%
% Other sensors
%
% readForceSensor(handle) % Reads a force sensor
% readProximitySensor(handle) % Reads a proximity sensor
%
% Simulation Object Factories.
%
% entity(handle) % Creates a sim_entity object
% joint(handle) % Creates a sim_joint object
% sphericalJoint(handle) % Creates a sim_spherical_joint
% object
% visionSensor(handle) % Creates a sim_vision_sensor
% object
% camera(handle) % Creates a sim_camera object
% depthCamera(handle) % Creates a sim_depth_camera object
% xyz_sensor(handle) % Creates a sim_xyz_sensor object
% xy_sensor(handle) % Creates a sim_xy_sensor object
% rgbdCamera(handle) % Creates a sim_cameraRGBD object
% forceSensor(handle) % Creates a sim_force_sensor object
% proximitySensor(handle) % Creates a sim_proximity_sensor
% object
% hokuyo(handle,ref) % Creates a sim_fast_hokuyo object
% arm(base_handle,joint_handle,fmt) % Creates a sim_arm object
% youBot(handle) % Creates a sim_youBot object
% youBotTRS(handle) % Creates a sim_youBot_TRS object
% diffBot(handle) % Creates a sim_diffBot object
%
% Helpers
%
% armhelper(name, fmt) % Helper function that discovers
% all joints that exist in a scene
% with the specified naming
% convention.
%
%% Opmodes
%
%
% simx_opmode_oneshot
%
% Non-blocking - Command send and a previous reply to the same
% command returned. The function does not wait for the actual reply.
%
% simx_opmode_blocking
%
% Blocking - The command is sent, and the function will wait for the
% actual reply and return that as long as the command doesn't time
% out. The recieved reply is removed from the inbox buffer.
%
% simx_opmode_streaming + alpha
%
% Non-blocking - The command is sent and any previous reply to the
% same command is returned. This command will be continuously
% executed on the server side and the function does not wait for the
% actual reply. Alpha is a value between 100 and 65535 representing
% the delay between function executions in ms.
%
% simx_opmode_oneshot_split + beta
%
% Non-blocking - NOT RECOMMENDED - The command is sent in small
% chunks and any previous reply for the same command is returned. The
% command is executed server side and the reply will also be returned
% in small chunks. The function does not wait for the actual reply.
% Beta is a value between 100 and 65535 representing the maximum
% chunk size in bytes to send. Small values won't slow down the
% communication framework but it will take longer for the full
% command to be transferred. Larger values will result in commands
% being transferred faster but the communication framework might
% appear frozen while chunks are being trasferred.
%
% simx_opmode_streaming_split + beta
%
% Non-blocking - NOT RECOMMENDED - The command is sent in small
% chunks and any previous reply to the same command is returned. The
% command will be continuously executed on the server side, and
% replys will be sent in small chunks. The function doesn't wait for
% the actual reply. Beta is a value between 100 and 65535
% representing the maximum chunk size in bytes to send. Small values
% won't slow down the communication framework, but it will take more
% time until the full commmand has been transferred. With large
% values, command are transferred faster, but the communication
% framework might appear frozen while the chunks are being
% transferred.
%
% simx_opmode_discontinue
%
% Non-blocking - The command is sent and any previous reply to the
% same command returned. A same command will be erased from the
% server side if the command is of streaming or continuous type. The
% same will happen on the client's input buffer. The function doesn't
% wait for the actual reply.
%
% simx_opmode_buffer
%
% Non-blocking - A previous reply to the same command is returned if
% avaliable. The command is not sent nor does the function wait for
% the actual reply.
%
% simx_opmode_remove
%
% Non-blocking - A previous reply to the same command is cleared from
% the input buffer if present. The command is not sent and the
% function doesn't return any values aside from the return code. Can
% be used to free memory client side.
%
%% V-REP Parameters
% See http://www.coppeliarobotics.com/helpFiles/en/objectParameterIDs.htm
% for a complete list of parameters.
classdef VREP < handle
properties
vrep
PORT
IP
clientID
path
libpath
getter_mode
setter_mode
blocking_mode
end
methods
function obj = VREP(varargin)
%vrep = remApi('remoteApi','extApi.h'); % This option requires a compiler
obj.vrep = remApi('remoteApi');
obj.vrep.simxFinish(-1);
p = inputParser;
%% input parsing
defaultIP = '127.0.0.1';
defaultPORT = 19997;
defaulttimeout = 2000;
defaultcycle = 5;
defaultwaitforconnect = true;
defaultreconnect = true;
defaultpath = getenv('VREP');
defaultgettermode = obj.vrep.simx_opmode_oneshot_wait;
defaultsettermode = obj.vrep.simx_opmode_oneshot;
addParameter(p,'IP',defaultIP,@isstring);
addParameter(p,'PORT',defaultPORT,@isnumeric);
addParameter(p,'timeout',defaulttimeout,@isnumeric);
addParameter(p,'getter_mode',defaultgettermode,@isstring);
addParameter(p,'setter_mode',defaultsettermode,@isstring);
addParameter(p,'cycle',defaultcycle,@isnumeric);
addParameter(p,'wait',defaultwaitforconnect,@islogical); %wait for connect
addParameter(p,'reconnect',defaultreconnect,@islogical); %reconnect on error
addParameter(p,'path',defaultpath,@isstring);
parse(p,varargin{:});
obj.blocking_mode = obj.vrep.simx_opmode_blocking;
obj.getter_mode = p.Results.getter_mode;
obj.setter_mode = p.Results.setter_mode;
obj.IP = p.Results.IP;
obj.PORT = p.Results.PORT;
path = p.Results.path;
% Check if all the VREP API files are already on path
if ispc == true
file1 = exist('remoteApi.dll');
elseif isunix == true
file1 = exist('remoteApi.so');
elseif ismac == true
file1 = exist('remoteApi.dylib');
else
error('RTB-SIM:VREP:', 'Operating system not identified');
end
file2 = exist('remApi.m');
file3 = exist('remoteApiProto.m');
% Prioritize specified path over any files found on path,
% but if no install folder found, fall back to files already found
% on MATLAB path (if any).
if isempty(path)
if file1 ~= 2 || file2 ~= 2 || file3 ~= 2
error('RTB-SIM:VREP:API Files not Found','No or invalid VREP path given');
end
obj.path = 'None';
else
if ~exist(path,'dir')
error('RTB-SIM:VREP:Invalid Path','VREP Folder %s not found', path);
end
obj.path = path;
libpath = { fullfile(obj.path, 'programming', 'remoteApi')
fullfile(obj.path, 'programming', 'remoteApiBindings', 'matlab', 'matlab')
fullfile(obj.path, 'programming', 'remoteApiBindings', 'lib', 'lib')
};
obj.libpath = libpath;
addpath( libpath{:} );
end
% Initialize the simulator.
obj.clientID = obj.vrep.simxStart(obj.IP,obj.PORT,p.Results.wait, ...
p.Results.reconnect,p.Results.timeout,p.Results.cycle);
if obj.clientID < 0
error('RTB-SIM:VREP:Connection Failed','Connection to VREP failed!');
end
end
function display(obj)
% VREP.display Display parameters
%
% V.display() displays the VREP parameters in compact format.
%
% Notes::
% - This method is invoked implicitly at the command line when the result
% of an expression is a VREP object and the command has no trailing
% semicolon.
%
loose = strcmp( get(0, 'FormatSpacing'), 'loose');
if loose
disp(' ');
end
disp([inputname(1), ' = '])
disp( char(obj) );
end
function s = char(obj)
% VREP.char Convert to string
%
% V.char() is a string representation the VREP parameters in human
% readable foramt.
%
s = sprintf('V-REP robotic simulator interface (active=%d)', obj.checkcomms() );
s = strvcat(s, sprintf('path: %s ', obj.path));
switch obj.getter_mode
case obj.vrep.simx_opmode_oneshot
s = strvcat(s, 'Getter mode: simx_opmode_oneshot (non-blocking)');
case obj.vrep.simx_opmode_oneshot_wait
s = strvcat(s, 'Getter mode: simx_opmode_oneshot_wait (blocking)');
case obj.vrep.simx_opmode_streaming
s = strvcat(s, 'Getter mode: simx_opmode_streaming (non-blocking)');
case obj.vrep.simx_opmode_buffer
s = strvcat(s, 'Getter mode: simx_opmode_buffer (non-blocking)');
end
switch obj.setter_mode
case obj.vrep.simx_opmode_oneshot
s = strvcat(s, 'Setter mode: simx_opmode_oneshot (non-blocking)');
case obj.vrep.simx_opmode_oneshot_wait
s = strvcat(s, 'Setter mode: simx_opmode_oneshot_wait (blocking)');
case obj.vrep.simx_opmode_streaming
s = strvcat(s, 'Setter mode: simx_opmode_streaming (non-blocking)');
case obj.vrep.simx_opmode_buffer
s = strvcat(s, 'Setter mode: simx_opmode_buffer (non-blocking)');
end
switch obj.blocking_mode
case obj.vrep.simx_opmode_oneshot
s = strvcat(s, 'Blocking mode: simx_opmode_oneshot (non-blocking)');
case obj.vrep.simx_opmode_oneshot_wait
s = strvcat(s, 'Blocking mode: simx_opmode_oneshot_wait (blocking)');
case obj.vrep.simx_opmode_streaming
s = strvcat(s, 'Blocking mode: simx_opmode_streaming (non-blocking)');
case obj.vrep.simx_opmode_buffer
s = strvcat(s, 'Blocking mode: simx_opmode_buffer (non-blocking)');
end
end
function delete(obj)
% VREP.delete
%
% Destroy the simulator object and cleanup.
%
obj.vrep.simxFinish(obj.clientID);
obj.vrep.simxFinish(-1);
obj.vrep.delete();
end
%% Generic simulation management
function pauseComms(obj,status)
% VREP.pauseComms
%
% Pauses the communication thread, preventing it from sending
% and receiving data. Useful for sending multiple commands that
% are to be recieved and evaluated simultaniously.
%
% Arguments
%
% status % 1 or true to pause, 0 or false to resume
%
r = obj.vrep.simxPauseCommunication(obj.clientID,status);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function pauseSim(obj)
% VREP.pauseSim
%
% Pauses the current simulation.
%
r = obj.vrep.simxPauseSimulation(obj.clientID,obj.setter_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function startSim(obj)
% VREP.startSim
%
% Starts the simulation. This must be run before any other commands
% are called.
%
r = obj.vrep.simxStartSimulation(obj.clientID,obj.setter_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function stopSim(obj)
% VREP.stopSim
%
% Stops the simulation. V-REP will automatically reset the scene to
% the state it was in before VREP.startSim was called.
%
r = obj.vrep.simxStopSimulation(obj.clientID,obj.setter_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function r = checkcomms(obj)
% VREP.checkcomms
%
% Retruns the VREP connection ID if a valid connection exists.
%
r = obj.vrep.simxGetConnectionId(obj.clientID);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function loadScene(obj,scene,varargin)
% VREP.loadScene
%
% There are three ways to use this function.
%
% First: To load a server side scene from V-REP scene folder.
%
% To do this: Ignore all parameters, and just specify the
% arguement scene as the filename.
% You can include the /scenes directory and the
% .ttt file extension, however it is not necessary.
%
% Example: loadScene('e-puckDemo')
%
% Second: To load a server side scene that is not in the V-REP
% scene folder.
%
% To do this: Argumet scene is the file name (with or without the
% file extension) and specify the full directory to the file using
% parameter 'fn'.
%
% Example: loadScene('e-puckDemo','fn','Drive:/full/path/to/scene/directory')
%
% Third: To load a client side scene.
%
% To do this: Argument scene is the file name (with or without the file
% extension) and then specify parameter
% 'fn','Drive:/full/path/to/scene/directory'). Also specify
% parameter 'clientside',true.
%
% Example:
% loadScene('e-puckDemo','fn','Drive:/full/path/to/scene/directory','clientside',true)
%
% Arguments:
%
% scene % A charater array containing the file name of a scene.
%
% Parameters:
%
% 'clientside' % Whether or not scene is on the same system as
% the V-REP server. If running MATLAB and V-REP
% on the same machine, the model is always considered
% serverside.
%
% 'fn' % a character array containing the full directory
% path to the directory that contains the scene.
%
p = inputParser;
defaultFN = 'None';
defaultLoc = false;
addRequired(p,'scene',@ischar);
addParameter(p,'fn',defaultFN,@ischar);
addParameter(p,'clientside',defaultLoc,@islogical);
parse(p,scene,varargin{:});
cs_path = p.Results.fn;
clientside = p.Results.clientside;
nocspath = strcmp(cs_path,'None');
nopath = strcmp(obj.path,'None');
if clientside
if nocspath
error('RTB-SIM:VREP:','A path must be given if model is Client side!');
elseif ~nocspath
scene = fullfile(cs_path, [scene '.ttt']);
end
elseif ~clientside && nocspath
if ~nopath
scene = fullfile(obj.path, 'scenes', [scene '.ttt']);
elseif nopath
if ~startsWith(scene,"scenes/")
scene = sprintf('scenes/%s',scene);
end
if ~endsWith(scene,".ttt")
scene = sprintf('%s.ttt',scene);
end
end
elseif ~clientside && ~nocspath
scene = fullfile(cs_path, [scene '.ttt']);
end
disp(scene)
obj.stopSim();
pause(1);
r = obj.vrep.simxLoadScene(obj.clientID,scene,clientside,obj.blocking_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function id = loadObject(obj,model,varargin)
% VREP.loadObject
%
% There are three ways to use this function.
%
% First: To load a server side model from V-REP model library.
%
% To do this: Ignore all parameters, and just specify the
% arguement model as the path and filename as seen in the V-REP
% model browser. You can include the /models directory and the
% .ttm file extension, however it is not necessary.
%
% Example: loadObject('furniture/chairs/sofa')
%
% Second: To load a server side model that is not in the V-REP
% model library.
%
% To do this: Argumet model is the file name (with or without the
% file extension) and specify the full directory to the file using
% parameter 'fn'.
%
% Example: loadObject('sofa','fn','Drive:/full/path/to/model/directory')
%
% Third: To load a client side model.
%
% To do this: Argument model is the file name (with or without the file
% extension) and then specify parameter
% 'fn','Drive:/full/path/to/model/directory'). Also specify
% parameter 'clientside',true.
%
% Example:
% loadObject('sofa','fn','Drive:/full/path/to/model/directory','clientside',true)
%
%
%
%
% Arguments:
%
% model % A charater array containing the file name of a model.
%
% Parameters:
%
% 'clientside' % Whether or not model is on the same system as
% the V-REP server. If running MATLAB and V-REP
% on the same machine, the model is always considered
% serverside.
%
% 'fn' % The full directory path to the directory that
% contains the model as a character array.
%
p = inputParser;
defaultFN = 'None';
defaultLoc = false;
addRequired(p,'model',@ischar);
addParameter(p,'fn',defaultFN,@ischar);
addParameter(p,'clientside',defaultLoc,@islogical);
parse(p,model,varargin{:});
cs_path = p.Results.fn;
clientside = p.Results.clientside;
nocspath = strcmp(cs_path,'None');
nopath = strcmp(obj.path,'None');
if clientside
if nocspath
error('RTB-SIM:VREP:','A path must be given if model is Client side!');
elseif ~nocspath
model = fullfile(cs_path, [model '.ttm']);
end
elseif ~clientside && nocspath
if ~nopath
model = fullfile(obj.path, 'models', [model '.ttm']);
elseif nopath
if ~startsWith(model,"models/")
model = sprintf('models/%s',model);
end
if ~endsWith(model,".ttm")
model = sprintf('%s.ttm',model);
end
end
elseif ~clientside && ~nocspath
model = fullfile(cs_path, [model '.ttm']);
end
[r,id] = obj.vrep.simxLoadModel(obj.clientID,model,clientside,obj.blocking_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
pause(0.5)
end
function deleteObject(obj,handle)
% VREP.deleteSimObject
%
% Deletes the specified object with a given handle from currently
% active V-REP scene.
%
% Note: Objects deleted while a simulation is
% running will be reinstated when the scene resets (after
% VREP.stopSim is called or the simulation is stopped using V-REP's
% GUI.
%
% Arguments:
%
% handle % A VREP object ID
%
r = obj.vrep.simxRemoveModel(obj.clientID,handle,obj.setter_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function closeScene(obj)
% VREP.closeScene
%
% Closes the currently open scene and then switches to the next
% open scene. If no other scenes are open, a new scene will be
% created.
%
obj.stopSim();
pause(1);
r = obj.vrep.simxCloseScene(obj.clientID,obj.blocking_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function time = pingSim(obj,n)
% VREP.pingSim
%
% Ping the VREP API n times.
% Returns a matrix of all resulting ping times.
%
% Arguments:
%
% n % Number of pings to send.
%
%
% Returns:
%
% time % An n-length vector containing the time of each
% ping.
%
temp = [];
for i = 0:n
[r,temp(n)] = obj.vrep.simxGetPingTime(obj.clientID);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
time = temp;
end
%% Generic object management
function [objid,name] = getObjects(obj,type)
% VREP.getObjects
%
% Show a list of all objects in the scene. Optionally specify a
% particular type of object to return only objects of that type.
%
% -----------------
% Major Object Types:
% -----------------
% shape = sim_object_shape_type
% joint = sim_object_joint_type
% graph = sim_object_graph_type
% camera = sim_object_camera_type
% light = sim_object_light_type
% dummy = sim_object_dummy_type
% proximity_sensor = sim_object_proximitysensor_type
% path = sim_object_path_type
% vision_sensor = sim_object_visionsensor_type
% mill = sim_object_mill_type
% force_sensor = sim_object_forcesensor_type
% mirror = sim_object_mirror_type
% -----------------
% Object Subtypes
% -----------------
% omni_light = sim_light_omnidirectional_subtype
% spot_light = sim_light_spot_subtype
% directional_light = sim_light_directional_subtype
% revolute_joint = sim_joint_revolute_subtype
% prismatic_joint = sim_joint_prismatic_subtype
% spherical_joint = sim_joint_spherical_subtype
% simple_shape = sim_shape_simpleshape_subtype
% multi_shape = sim_shape_multishape_subtype
% ray_proximity_sensor = sim_proximitysensor_ray_subtype
% pyramid_proximity_sensor = sim_proximitysensor_pyramid_subtype
% cylinder_proximity_sensor = sim_proximitysensor_cylinder_subtype
% disc_proximity_sensor = sim_proximitysensor_disc_subtype
% cone_proximity_sensor = sim_proximitysensor_cone_subtype
% pyramid_mill = sim_mill_pyramid_subtype
% cylinder_mill = sim_mill_cylinder_subtype
% disc_mill = sim_mill_disc_subtype
% cone_mill = sim_mill_cone_subtype
%
% Arguments:
%
% type % Optionally specifies a type of object to
% return.
%
% Returns:
%
% objid % A list of VREP object IDs.
% name % A list of the object names.
%
if nargin < 2
stype = obj.vrep.sim_appobj_object_type;
else
switch (type)
case 'shape'
stype = obj.vrep.sim_object_shape_type;
case 'joint'
stype = obj.vrep.sim_object_joint_type;
case 'graph'
stype = obj.vrep.sim_object_graph_type;
case 'camera'
stype = obj.vrep.sim_object_camera_type;
case 'light'
stype = obj.vrep.sim_object_light_type;
case 'dummy'
stype = obj.vrep.sim_object_dummy_type;
case 'proximity_sensor'
stype = obj.vrep.sim_object_proximitysensor_type;
case 'path'
stype = obj.vrep.sim_object_path_type;
case 'vision_sensor'
stype = obj.vrep.sim_object_visionsensor_type;
case 'mill'
stype = obj.vrep.sim_object_mill_type;
case 'force_sensor'
stype = obj.vrep.sim_object_forcesensor_type;
case 'mirror'
stype = obj.vrep.sim_object_mirror_type;
case 'omni_light'
stype = obj.vrep.sim_light_omnidirectional_subtype;
case 'spot_light'
stype = obj.vrep.sim_light_spot_subtype;
case 'directional_light'
stype = obj.vrep.sim_light_directional_subtype;
case 'revolute_joint'
stype = obj.vrep.sim_joint_revolute_subtype;
case 'prismatic_joint'
stype = obj.vrep.sim_joint_prismatic_subtype;
case 'spherical_joint'
stype = obj.vrep.sim_joint_spherical_subtype;
case 'simple_shape'
stype = obj.vrep.sim_shape_simpleshape_subtype;
case 'multi_shape'
stype = obj.vrep.sim_shape_multishape_subtype;
case 'ray_proximity_sensor'
stype = obj.vrep.sim_proximitysensor_ray_subtype;
case 'pyramid_proximity_sensor'
stype = obj.vrep.sim_proximitysensor_pyramid_subtype;
case 'cylinder_proximity_sensor'
stype = obj.vrep.sim_proximitysensor_cylinder_subtype;
case 'disc_proximity_sensor'
stype = obj.vrep.sim_proximitysensor_disc_subtype;
case 'cone_proximity_sensor'
stype = obj.vrep.sim_proximitysensor_cone_subtype;
case 'pyramid_mill'
stype = obj.vrep.sim_mill_pyramid_subtype;
case 'cylinder_mill'
stype = obj.vrep.sim_mill_cylinder_subtype;
case 'disc_mill'
stype = obj.vrep.sim_mill_disc_subtype;
case 'cone_mill'
stype = obj.vrep.sim_mill_cone_subtype;
otherwise
error("VREP.getObjects: An invalid/unknown type was specified! (Type: %d)",types);
end
end
[r,objid,~,~,name] = obj.vrep.simxGetObjectGroupData(obj.clientID, stype, 0, obj.blocking_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end
end
function [handle] = getHandle(obj, in, varargin)
% VREP.getHandle
%
% Retrieves the V-REP identifier of an object given its string
% name.
%
% Arguments:
%
% in % The string name
%
% Optional Arguments:
%
% fmt % A format specifier. %% TODO Fill out the rest of
% this
%
% Returns:
%
% handle % The V-REP object ID of the object with a name
% matching the one specified.
%
if nargin < 3
name = in;
else
name = sprintf(in, varargin{:});
end
[r,handle] = obj.vrep.simxGetObjectHandle(obj.clientID,name,obj.blocking_mode);
if r ~= 0 && r ~= 1
throw(obj.errcheck(r))
end