-
Notifications
You must be signed in to change notification settings - Fork 13
/
quadClient.m
137 lines (112 loc) · 4.94 KB
/
quadClient.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
function quadClient()
pause on;
disp('Program started');
% Connection parameters
IPADDRESS = '127.0.0.1';
PORT = 20146;
WAIT_UNTIL_CONNECTED = true;
DO_NOT_RECONNECT_ONCE_DISCONNECTED = true;
TIME_OUT_IN_MSEC = 5000;
COMM_THREAD_CYCLE_IN_MS = 1;
% Load the V-REP remote API
vrep = remApi('remoteApi');
vrep.simxFinish(-1); % just in case, close all opened connections
% 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);
if clientID == -1
disp('Failed connecting to remote API server');
end
targetObj= getObjectHandle(vrep, clientID, 'Quadricopter_target');
% 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
%Actuators --------------------------------------------------------
prop1Handle = getObjectHandle(vrep, clientID, 'Quadricopter_propeller1');
prop2Handle = getObjectHandle(vrep, clientID, 'Quadricopter_propeller2');
prop3Handle = getObjectHandle(vrep, clientID, 'Quadricopter_propeller3');
prop4Handle = getObjectHandle(vrep, clientID, 'Quadricopter_propeller4');
propellerRespondable1 = getObjectHandle(vrep, clientID, 'Quadricopter_propeller_respondable1');
propellerRespondable2 = getObjectHandle(vrep, clientID, 'Quadricopter_propeller_respondable2');
propellerRespondable3 = getObjectHandle(vrep, clientID, 'Quadricopter_propeller_respondable3');
propellerRespondable4 = getObjectHandle(vrep, clientID, 'Quadricopter_propeller_respondable4');
% 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
prop1Prev = 0;
prop2Prev = 0;
prop3Prev = 0;
prop4Prev = 0;
motorCommand = 1000;
t = linspace(0, 10*pi);
x = 0;
% Loop until the action function returns an empty action for the
while true
if x > 99
x = 0;
end
x = x + 1;
err = 0;
[err, robotPosition] = vrep.simxGetObjectPosition (clientID, targetObj ,-1, vrep.simx_opmode_oneshot_wait);
sprintf('targetPos:%d\n', robotPosition(1))
%robotPosition(1) = robotPosition(1) + .01;
%robotPosition(2) = robotPosition(2) + .01;
robotPosition(3) = robotPosition(3) + .1*sin(t(x)/5);
vrep.simxSetObjectPosition(clientID, targetObj, -1, robotPosition, vrep.simx_opmode_oneshot)
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
disp('Program ended');
end %End 'Try catch'
end %End quadClient
% Helper functions --------------------------------------------------------
function check(vrep, retVal, textError, textNoError)
if (retVal==vrep.simx_error_noerror)
disp(textNoError);
else
disp(textError);
end
end
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)
else
sprintf('Got Object %s', objectName)
end
end
function detectionState = readSensor(vrep, clientID, sensorHandle)
[errorCode,detectionState] = vrep.simxReadProximitySensor(clientID, sensorHandle, vrep.simx_opmode_continuous);
if errorCode
detectionState = 0;
end
end
function setMotorSpeed(vrep, clientID, jointHandle, motorSpeed)
vrep.simxSetJointTargetVelocity(clientID, jointHandle, motorSpeed, vrep.simx_opmode_oneshot);
end