-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecover.m
67 lines (60 loc) · 2.13 KB
/
Recover.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
classdef Recover < Behavior
properties(Access= 'private')
priority
end
properties(Constant)
Name = 'Recover';
end
methods(Access= 'public')
% Constructor
function obj = Recover(priority)
obj.priority = priority;
end
% Reactive
function bid = takeControl(~,sensors)
% Crash detected
bid = sensors.getBumpFront || sensors.getBumpLeft || sensors.getBumpRight;
end
function action(obj,serPort,sensors)
States.setgetVar(States.Recover);
if sensors.getBumpFront
% Stop
SetDriveWheelsCreate(serPort, 0, 0);
% Drive backward
travelDist(serPort,.3,-1);
% Randomize to prevent oscillations
randomAngle = obj.randomize(80, 100);
% Turn right
turnAngle (serPort, .2, -randomAngle);
elseif sensors.getBumpRight
% Stop
SetDriveWheelsCreate(serPort, 0, 0);
% Drive backward
travelDist(serPort,.3,-1);
% Randomize to prevent oscillations
randomAngle = obj.randomize(65, 75);
% Turn left
turnAngle (serPort, .2, randomAngle);
elseif sensors.getBumpLeft
% Stop
SetDriveWheelsCreate(serPort, 0, 0);
% Drive backward
travelDist(serPort,.3,-1);
% Randomize to prevent oscillations
% Turn right by random angle between -65 and -75 to prevent
randomAngle = obj.randomize(-65, -75);
% oscillations
turnAngle (serPort, .2, randomAngle);
end
end
function randomAngle = randomize(~,lowerBoundary,upperBoundary)
rng(0,'twister');
randomAngle = (upperBoundary-lowerBoundary).*rand() + lowerBoundary;
end
end
methods
function Priority = getPriority(obj)
Priority = obj.priority;
end
end
end