forked from lyphix/High_Level_Control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.m
389 lines (330 loc) · 10.7 KB
/
Main.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
%% Clean
clc;
close all;
%% Check List:
% 1.Power on Robot Dog
% 2.Connect Robot Dog Wifi: Unitree_Go393319A
% 3.Open Virtual Machine
% 4.Running /Desktop/unitree_matlab/build/udp_link press Enter to begin
% 5.Open Motive Check Broadcast and Network Interface
% 6.You are good for running
%% Robot Dog Network Parameters
% this IP is the vm ip
Robot_Dog_IP = '192.168.12.184';
Robot_Dog_Port = 1145;
%% Robot Dog Command Initialized
Control_Command = zeros(1,11,'single');
%velocity walking
Control_Command(1)=2;
%% Feedback Control Parameters
dt = 0.05; % time
% Porportional constant on velocity action
K_P_x = 0.6;
K_P_z = 0.6;
K_P_yaw = 0.5;
% Integral
K_I_x = 0.05;
K_I_z = 0.05;
K_I_yaw = 0.03;
% Derivative
K_D_x = 0.08;
K_D_z = 0.08;
K_D_yaw = 0.05;
% limit
propostional_x_limit = 0.6; % m/s
propostional_z_limit = 0.6; % m/s
propostional_yaw_limit = 40*pi/180; % rad/s
integral_x_limit = 0.1;
integral_z_limit = 0.1;
integral_yaw_limit = 5*pi/180;
derivative_x_limit = 0.1;
derivative_z_limit = 0.1;
derivative_yaw_limit = 5*pi/180;
%% Control Setting
% MODE
% Mode 1: Dog will go to Target_point.
% Mode 2: Dog will follow Way_Points.
% Mode 3: Dog will follow the path
Control_Mode=1;
% Target Point
%[x,z]
Target_Point=[0 0];
% YAW
% [0,360)
% -1: Disable yaw control
% -2: Control Yaw to the motion direction
% Yaw
% wall wall wall wall wall
% 0,359.9..
% ^ z
% |
% |
% 90 x <----O 270
%
% 180
%
% wall computer wall
yaw_set = 0;
% THRESHOLD
% Distance Threshold to switch to next way point
Distance_Threshold = 0.15;
% Cricel way points 18
Way_Points_center =[0,0];
Way_Points_radius =1.65;
Way_Points_theta = linspace(0,2*pi,120);
Way_Points_x=Way_Points_center(1)+Way_Points_radius*cos(Way_Points_theta);
Way_Points_z=Way_Points_center(2)+Way_Points_radius*sin(Way_Points_theta);
% %% Path Planning
% % generate random points
% random_points_number = 4;
% random_points_r = 1.6;
% random_points_center_x = 0;
% random_points_center_z = 0;
% random_points = zeros(random_points_number,2);
%
% for i = 1:random_points_number
% angle = 2*pi*rand;
% random_r = random_points_r * sqrt(rand);
% random_point_x = random_points_center_x + random_r * cos(angle);
% random_point_z = random_points_center_z + random_r * sin(angle);
% random_points(i,:)=[random_point_x random_point_z];
% end
%
% figure;
% viscircles([random_points_center_x, random_points_center_z], random_points_r,'LineStyle','--','Color','k');
% hold on;
% scatter(random_points(:,1), random_points(:,2),'red','filled');
% axis equal;
% hold off;
%% Instantiate client object to run Motive API commands
% Check list:
% 1.Broadcast Frame Date
% 2.Network Interface: Local Loopback
% https://optitrack.com/software/natnet-sdk/
% Create Motive client object
dllPath = fullfile('d:','StDroneControl','NatNetSDK','lib','x64','NatNetML.dll');
assemblyInfo = NET.addAssembly(dllPath); % Add API function calls
theClient = NatNetML.NatNetClientML(0);
% Create connection to localhost, data is now being streamed through client object
HostIP = '127.0.0.1';
theClient.Initialize(HostIP, HostIP);
Dog_ID = 1; % Rigid body ID of the drone from Motive
%% figure for movtion track
fig = figure();
ax = axes('Parent',fig);
arrow_length=0.2;
%circle for draw
circle_center =[0,0];
circle_radius =1.65;
circle_theta = linspace(0,2*pi,100);
circle_x=circle_center(1)+circle_radius*cos(circle_theta);
circle_y=circle_center(2)+circle_radius*sin(circle_theta);
%% Robot dog command
% Control_Command()
%
% +(11) +(9) -(11)
% |
% +(10) dog -(10)
% |
% -(9)
%
%% Motive coordiate frame
% wall wall wall wall wall
% ^ z
% |
% |
% x <----O y(pointing up)
%
%
% wall computer wall
%% Init Parameters
% index for way points
Way_Point_index=1;
integral_x = 0;
integral_z = 0;
integral_yaw = 0;
previous_error_x = 0;
previous_error_z = 0;
previous_error_yaw = 0;
breakflag = 0;
Dog_Pos_Record=[];
Dog_Pos_Record_Index = 1;
[Dog_Pos] = Get_Dog_Postion(theClient, Dog_ID);
time = Dog_Pos(1);
%% Main Loop
while true
% get position from camera
[Dog_Pos] = Get_Dog_Postion(theClient, Dog_ID); %[time, z, x, yaw]
Dog_Pos_Record=[Dog_Pos_Record;Dog_Pos Dog_Pos(1)-time];
Dog_Pos_Record_Index = Dog_Pos_Record_Index +1;
if Control_Mode == 2
Target_Point = [Way_Points_x(Way_Point_index) Way_Points_z(Way_Point_index)];
end
%% Feedback control
% Calculate Distance
Point_Dog = [Dog_Pos(2) Dog_Pos(3)]; %[x,z]
Vector_PD_TP = Target_Point-Point_Dog; % Get vector
Norm_Vector = norm(Vector_PD_TP); % Calculate norm
% rotation matrix to turn vector to robot dog frame
Rotation_matrix = [cosd(Dog_Pos(4)), -sind(Dog_Pos(4)) ; sind(Dog_Pos(4)),cosd(Dog_Pos(4)) ];
if Control_Mode==1 || Norm_Vector > Distance_Threshold
%vector in robot dog frame = error_x error_z
Vector_rotated = Rotation_matrix*Vector_PD_TP';
if yaw_set == -1 % not control yaw
error_yaw_command=0;
elseif yaw_set == -2 % control yaw to motion direction
angle_r = atan2(Vector_PD_TP(1),Vector_PD_TP(2));
yaw_set_mode2 = rad2deg(angle_r);
if yaw_set_mode2 < 0
yaw_set_mode2 = yaw_set_mode2+360;
end
error_yaw = yaw_set_mode2-Dog_Pos(4);
if error_yaw<-180
error_yaw_command=(360+error_yaw);
elseif error_yaw > 180
error_yaw_command=error_yaw-360;
else
error_yaw_command=error_yaw;
end
elseif yaw_set >=0 && yaw_set<360 % control yaw
error_yaw = yaw_set-Dog_Pos(4);
if error_yaw<-180
error_yaw_command=(360+error_yaw);
elseif error_yaw > 180
error_yaw_command=error_yaw-360;
else
error_yaw_command=error_yaw;
end
end
%% propostional
propostional_x = Vector_rotated(1) * K_P_x;
propostional_z = Vector_rotated(2) * K_P_z;
propostional_yaw = error_yaw_command*pi/180 * K_P_yaw;
% propostional limit
if propostional_x > propostional_x_limit
propostional_x = propostional_x_limit;
elseif propostional_x < -propostional_x_limit
propostional_x = -propostional_x_limit;
end
if propostional_z > propostional_z_limit
propostional_z = propostional_z_limit;
elseif propostional_z < -propostional_z_limit
propostional_z = -propostional_z_limit;
end
if propostional_yaw > propostional_yaw_limit
propostional_yaw = propostional_yaw_limit;
elseif propostional_yaw < -propostional_yaw_limit
propostional_yaw = -propostional_yaw_limit;
end
%% integral
integral_x = integral_x + K_I_x * Vector_rotated(1) * dt;
integral_z = integral_z + K_I_z * Vector_rotated(2) * dt;
integral_yaw = integral_yaw + K_I_yaw * error_yaw_command * dt;
% integral limit
if integral_x > integral_x_limit
integral_x = integral_x_limit;
elseif integral_x < -integral_x_limit
integral_x = -integral_x_limit;
end
if integral_z > integral_z_limit
integral_z = integral_z_limit;
elseif integral_z < -integral_z_limit
integral_z = -integral_z_limit;
end
if integral_yaw > integral_yaw_limit
integral_yaw = integral_yaw_limit;
elseif integral_yaw < -integral_yaw_limit
integral_yaw = -integral_yaw_limit;
end
%% derivative
derivative_x = K_D_x * (Vector_rotated(1) - previous_error_x) /dt;
derivative_z = K_D_z * (Vector_rotated(2) - previous_error_z) /dt;
derivative_yaw = K_D_yaw * (error_yaw_command - previous_error_yaw)/dt;
previous_error_x = Vector_rotated(1);
previous_error_z = Vector_rotated(2);
previous_error_yaw = error_yaw_command;
% derivative limit
if derivative_x > derivative_x_limit
derivative_x = derivative_x_limit;
elseif derivative_x < -derivative_x_limit
derivative_x = -derivative_x_limit;
end
if derivative_z > derivative_z_limit
derivative_z = derivative_z_limit;
elseif derivative_z < -derivative_z_limit
derivative_z = -derivative_z_limit;
end
if derivative_yaw > derivative_yaw_limit
derivative_yaw = derivative_yaw_limit;
elseif derivative_yaw < -derivative_yaw_limit
derivative_yaw = -derivative_yaw_limit;
end
%% set command
Control_Command(10) = propostional_x + integral_x + derivative_x; %x
Control_Command(9) = propostional_z + integral_z + derivative_z; %z
Control_Command(11) = propostional_yaw + integral_yaw + derivative_yaw; %yaw
else
% switch point
% clean integral
% integral_x = 0;
% integral_z = 0;
% integral_yaw = 0;
if Control_Mode == 2
% go to next way point
if length(Way_Points_z)>Way_Point_index
Way_Point_index=Way_Point_index+1;
else
%finished stop running
Control_Command(11) = 0;
Control_Command(10) = 0; %x
Control_Command(9) = 0; %z
breakflag = 1;
end
end
end
% print command
disp(Control_Command);
% send command to vitrual machine
Robot_Dog(Robot_Dog_IP,Robot_Dog_Port,Control_Command);
%% draw figure
plot(ax,circle_x,circle_y,'b-');
xlabel('X')
ylabel('Z')
hold on;
plot(ax,0,0,'.');
plot(ax,Target_Point(1),Target_Point(2),'.','Color','r','MarkerSize',20);
plot(ax,Way_Points_x,Way_Points_z,'o');
ax.DataAspectRatio=[1 1 1];
dy=arrow_length*cosd(Dog_Pos(4));
dx=arrow_length*sind(Dog_Pos(4));
quiver(Dog_Pos(2),Dog_Pos(3),dx,dy,'r','LineWidth',0.2,'MaxHeadSize',2);
plot(Dog_Pos_Record(:,2),Dog_Pos_Record(:,3),'Color','r');
set(gca,'XDir','reverse');
xlim(ax,[-3,3]);
ylim(ax,[-2,2]);
hold off;
drawnow;
%% Stop
if breakflag == 1
figure;
subplot(2,1,1);
plot(Dog_Pos_Record(:,5),Dog_Pos_Record(:,2));
xlabel('Time');
ylabel('X');
subplot(2,1,2);
plot(Dog_Pos_Record(:,5),Dog_Pos_Record(:,3));
xlabel('Time');
ylabel('Z');
figure;
plot(Dog_Pos_Record(:,2),Dog_Pos_Record(:,3));
xlabel('X');
ylabel('Z');
set(gca,'XDir','reverse');
xlim([-3,3]);
ylim([-2,2]);
daspect([1 1 1]);
break;
end
%% time pause
pause(dt);
end