forked from friend0/vrepMatlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbleRobClient.m
154 lines (119 loc) · 5.76 KB
/
bubbleRobClient.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
function bubbleRobClient(actionFunction, functionInput)
% Connection parameters
IPADDRESS = '127.0.0.1';
PORT = 19999;
WAIT_UNTIL_CONNECTED = true;
DO_NOT_RECONNECT_ONCE_DISCONNECTED = true;
TIME_OUT_IN_MSEC = 5000;
COMM_THREAD_CYCLE_IN_MS = 1;
% Arbitrary speed multiplier for motors
MOTOR_SPEED = 1;
% Bozo filter for input args
if nargin < 1
fprintf('Usage: bubbleRobClient(actionFunction)\n')
fprintf('Example: bubbleRobClient(@actionFromKeyboard)\n')
return
end
% Read WAV file for reporting success
%[success_sound,success_fs] = audioread('success.wav');
% Use this to build the library the first time through
%loadlibrary('remoteApi','extApi.h','mfilename','remoteApiProto')
%vrep = remApi('remoteApi', 'extApi.h');
% Load the V-REP remote API
vrep = remApi('remoteApi');
% Start the simulation on the server
clientID = vrep.simxStart(IPADDRESS, PORT, ...
WAIT_UNTIL_CONNECTED, DO_NOT_RECONNECT_ONCE_DISCONNECTED, ...
TIME_OUT_IN_MSEC, COMM_THREAD_CYCLE_IN_MS);
% We use try/catch to avoid fatal errors messing up the server
try
if (clientID > -1)
fprintf('Connected to remote API server as client %d\n', clientID);
% Grab handles from V-REP server
leftMotorHandle = getObjectHandle(vrep, clientID, 'remoteApiControlledBubbleRobLeftMotor');
rightMotorHandle = getObjectHandle(vrep, clientID, 'remoteApiControlledBubbleRobRightMotor');
noseSensorHandle = getObjectHandle(vrep, clientID, 'remoteApiControlledBubbleRobSensingNose');
%leftAntennaHandle = getObjectHandle(vrep, clientID, 'remoteApiControlledBubbleRobSensingNose3');
%rightAntennaHandle = getObjectHandle(vrep, clientID, 'remoteApiControlledBubbleRobSensingNose2');
% Start the simulation
if vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot_wait)
fprintf('Failed to start simulation\n')
return
end
% Initialize previous motor commands for tracking movement update
leftMotorCommandPrev = 0;
rightMotorCommandPrev = 0;
% Loop until the action function returns an empty action for the
% left motor command
while true
% Read from the three sensors
noseSensor = readSensor(vrep, clientID, noseSensorHandle);
%leftSensor = readSensor(vrep, clientID, leftAntennaHandle);
%rightSensor = readSensor(vrep, clientID, rightAntennaHandle);
% Action function converts sensor readings (0,1) into left and
% right motor commands (-1,0,1)
if nargin > 1
[leftMotorCommand, rightMotorCommand] = actionFunction(noseSensor, leftSensor, rightSensor, functionInput);
else
%[leftMotorCommand, rightMotorCommand] = actionFunction(noseSensor, leftSensor, rightSensor);
[leftMotorCommand, rightMotorCommand] = actionFunction(noseSensor);
end
% Done!
if isempty(leftMotorCommand)
break
end
% Set motor speeds using new action commands
if leftMotorCommand ~= leftMotorCommandPrev || ...
rightMotorCommand ~= rightMotorCommandPrev
setMotorSpeed(vrep, clientID, leftMotorHandle, leftMotorCommand*MOTOR_SPEED)
setMotorSpeed(vrep, clientID, rightMotorHandle, rightMotorCommand*MOTOR_SPEED)
end
% Track previous motor commands for movement update
leftMotorCommandPrev = leftMotorCommand;
rightMotorCommandPrev = rightMotorCommand;
% Check success
if leftSensor && rightSensor
sound(success_sound, success_fs)
fprintf('Success!!!\n')
setMotorSpeed(vrep, clientID, leftMotorHandle, 0)
setMotorSpeed(vrep, clientID, rightMotorHandle, 0)
pause(3)
break
end
end
% Stop the simulation on the server
vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot_wait);
else
error('Failed connecting to remote API server')
end
% Clean up
close(gcf)
display('Done')
vrep.simxFinish(clientID)
vrep.delete(); % call the destructor
% Handle all failures with a helpful stack trace
catch err
close(gcf)
fprintf('Error: %s\nTrace:\n', err.message)
for k = 1:length(err.stack)
level = err.stack(k);
fprintf('On line %d of function %s in file %s\n', ...
level.line, level.name, level.file)
end
vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot_wait);
vrep.simxFinish(clientID); % close the line if still open
vrep.delete(); % call the destructor
end
% Helper functions --------------------------------------------------------
function handle = getObjectHandle(vrep, clientID, objectName)
[errorCode,handle] = vrep.simxGetObjectHandle(clientID, objectName, vrep.simx_opmode_oneshot_wait);
if errorCode
error('Failed to get object named %s', objectName)
end
function detectionState = readSensor(vrep, clientID, sensorHandle)
[errorCode,detectionState] = vrep.simxReadProximitySensor(clientID, sensorHandle, vrep.simx_opmode_continuous);
if errorCode
detectionState = 0;
end
function setMotorSpeed(vrep, clientID, jointHandle, motorSpeed)
vrep.simxSetJointTargetVelocity(clientID, jointHandle, motorSpeed, vrep.simx_opmode_oneshot);