-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadData.m
71 lines (58 loc) · 1.78 KB
/
loadData.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function sends a friction map to the TPaD
% note: you will need to connect to the TPaD separately for this to work
%
% Inputs: port = name & info of open port, needed for PIC communication
% DCfriction = a number, usually = 2^15
% gradient = vector 19200 long
% roughness = vector 19200 long
%
% Outputs: result = 'true' or 'false', depending if load was successful
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [result] = loadData(port,DCfriction,gradient,roughness)
%% make sure DCfriction & roughness values are within accepted range
if DCfriction > 2^16-1; DCfriction = 2^16-1; end
if DCfriction < 0; DCfriction = 0; end
idx = find(roughness > 2^16-1 - DCfriction);
if ~isempty(idx); roughness(idx) = 2^16-1 - DCfriction; end
idx = find(roughness < 0 - DCfriction);
if ~isempty(idx); roughness(idx) = 0 - DCfriction; end
%% send data
%Tell TPaD I'm about to send it data
fwrite(port,'S');
%send DCfriction
bytes = fliplr(typecast(int16(DCfriction),'uint8'));
fwrite(port,bytes);
%send gradient array
chunk = 256; k=1;
for i=1:length(gradient)/chunk
bytes = [];
for j=1:chunk
bytes = [bytes fliplr(typecast(int16(gradient(k)),'uint8'))];
k=k+1;
end
fwrite(port,bytes);
end
%send roughness array
chunk = 256; k=1;
for i=1:length(roughness)/chunk
bytes = [];
for j=1:chunk
bytes = [bytes fliplr(typecast(int16(roughness(k)),'uint8'))];
k=k+1;
end
fwrite(port,bytes);
end
%line feed
fwrite(port,char(10));
%% receive data
%TPaD should respond 'F' if finished
response = char(fread(port,1));
if response == 'F'
result = 'true';
else
result = 'false';
end
flushinput(port);
end