-
Notifications
You must be signed in to change notification settings - Fork 4
/
gtg.m
75 lines (58 loc) · 1.51 KB
/
gtg.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
function [ ] = gtg( )
%The Go to goal controller
global goal
global prev_heading_error
global total_heading_error
global flag_change
if flag_change
disp('Go to goal controller engaged');
flag_change = false;
end
velMult = 70.0; %mm/s
distThresh = 50.0; %mm
%Controller parameters
Kp = 0.02;
Kd = 0.0;
Ki = 0.0;
xd = goal(1,1);
yd = goal(2,1);
hgtg(1) = displayrobo();
%get current robot position
global state;
xa = state(1);
ya = state(2);
thetaa = rad2deg(state(3));
%Determine how far to rotate to face goal point
dt = (atan2(yd - ya, xd - xa) * (180/3.14159)) - thetaa;
dt = mod((dt + 180), 360) - 180; % restrict to (-180,180);
hgtg(2) = line([xa,xa+(1000.0*(cos(deg2rad(dt + thetaa))))],[ya,ya+(1000.0*(sin(deg2rad(dt + thetaa))))],'color','green');
drawnow;
%fprintf(' angle remaining: %f\n', dt);
%request robot to turn
W = (Kp*dt) + (Ki*total_heading_error) + (Kd*(dt - prev_heading_error));
total_heading_error = total_heading_error + dt;
prev_heading_error = dt;
%Calculate distance to goal
d = sqrt( (xd - xa)^2 + (yd - ya)^2 );
%fprintf(' distance remaining: %f\n', d);
%request robot velocity proportionally to distance remaining
vel = ((atan((d - distThresh)/25.0)) - (atan(dt/10.0)))*velMult;
set_motors(vel,W);
delete(hgtg(:));
%Check events
if(at_goal())
%call stop_robot();
guard(5);
elseif(at_obstacle())
%call ao_gtg();
guard(2);
elseif(no_progress())
if(chk_wall('l'))
%call fw('l');
guard(41);
else
%call fw('r');
guard(42);
end
end
end