-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.m
82 lines (63 loc) · 2.38 KB
/
test.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
% Copyright (c) 2020, Ian G. Bennett
% All rights reserved.
% Development funded by the University of Toronto, Department of Mechanical
% and Industrial Engineering.
% Distributed under GNU AGPLv3 license.
clear
close all
clc
% Initialize tcp server to read and respond to algorithm commands
[s_cmd, s_rply] = tcp_setup();
fopen(s_cmd);
fopen(s_rply);
% Robot Sensor Measurements
u = [0,0,0,0,0]; % Ultrasonic measurements
pos = [0,0,0]; % Position (x,y,rotation)
speed = 2;
rot_stuck = 90;
stepcount = 0;
while 1
% Take Measurements
for ct = 1:5
cmdstring = [strcat('u',num2str(ct)) newline];
u(ct) = tcpclient_write(cmdstring, s_cmd, s_rply);
end
ir = tcpclient_write(['i1' newline], s_cmd, s_rply);
odom = tcpclient_write(['o3' newline], s_cmd, s_rply);
gyro = tcpclient_write(['g1' newline], s_cmd, s_rply);
comp = tcpclient_write(['c1' newline], s_cmd, s_rply);
% Display Values
disp('Ultrasonic')
disp(u)
disp('IR Sensor')
disp(ir)
disp('Odometer')
disp(odom)
disp('Gyroscope')
disp(gyro)
disp('Compass')
disp(comp)
% Pick random direction, left or right, to try travelling in
direct_i = randperm(2,2)*2;
direct = -direct_i*90+270;
for ct = 1:2
if (u(1) > 4) && (u(2) > 1.5) && (u(4) > 1.5)
% If the way ahead is clear, go forward
cmdstring = [strcat('d1-',num2str(speed)) newline]; % Build command string to move bot
reply = tcpclient_write(cmdstring, s_cmd, s_rply);
break
elseif u(direct_i(ct)) > 4
% If not, pick a random direction that is clear and go that way
cmdstring = [strcat('r1-',num2str(direct(ct))) newline]; % Build command string to rotate bot
reply = tcpclient_write(cmdstring, s_cmd, s_rply);
cmdstring = [strcat('d1-',num2str(speed)) newline]; % Build command string to move bot
reply = tcpclient_write(cmdstring, s_cmd, s_rply);
break
elseif ct == 2
% If no directions clear, turn to the left
cmdstring = [strcat('r1-',num2str(rot_stuck)) newline]; % Rotate if stuck
reply = tcpclient_write(cmdstring, s_cmd, s_rply);
end
end
stepcount = stepcount+1;
end