-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_machine_nine.m
84 lines (64 loc) · 2.21 KB
/
state_machine_nine.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
function state_machine_nine(serPort)
flag=0;
% Sets forward velocity using differential system
SetDriveWheelsCreate(serPort, 0.5, 0.5);
% Initialise variables plus 1st sensor read for each.
idx1 = 1;
% We need to check each sonar for valid reading in case is out of range.
SonRead = ReadSonar(serPort, 3);
if ~any(SonRead) SonLF(idx1) = 100;
else SonLF(idx1) = SonRead;
end
SonRead = ReadSonar(serPort, 2);
if ~any(SonRead) SonFF(idx1) = 100;
else SonFF(idx1) = SonRead;
end
t=0;
while (t~=1)
Camera = CameraSensorCreate(serPort);
if (any(Camera))
flag=1;
elseif (~any(Camera))
flag=0;
end
switch(flag)
case (0)
[BumpRight,BumpLeft,WheDropRight,WheDropLeft,WheDropCaster,BumpFront] = BumpsWheelDropsSensorsRoomba(serPort);
if ~BumpRight && ~BumpLeft && ~BumpFront
SetDriveWheelsCreate(serPort, 0.5, 0.5);
elseif BumpRight
turnAngle (serPort, .2, 70);
elseif BumpLeft
turnAngle (serPort, .2, -70);
else
turnAngle (serPort, .2, 100);
end
pause (.1);
Camera = CameraSensorCreate(serPort);
case (1)
turnAngle (serPort, .2, (Camera * 6));
SetDriveWheelsCreate(serPort, 0.5, 0.5);
Dist1=Dist1+ DistRead;
pause (.1);
Camera = CameraSensorCreate(serPort);
if (~any(Camera))
t=1;
break;
end
end
end
while (t~=0)
% If distance out of limits turn to get near/away from the wall
if ( SonLF(idx1) > 0.31 ) turnAngle(serPort, .2, 5);
elseif ( SonLF(idx1) < 0.28 ) turnAngle(serPort, .2, -5);
end
% Read Front Sonar and correct if out-of-range
SonRead = ReadSonar(serPort, 2);
if ~any(SonRead) SonFF(idx1) = 100;
else SonFF(idx1) = SonRead;
end
% It too near a wall it means needs to turn sharp right!
if ( SonFF(idx1) < 0.30 ) turnAngle(serPort, .2, -70);
end
end
end