-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor.m
More file actions
142 lines (118 loc) · 4.8 KB
/
Copy pathSensor.m
File metadata and controls
142 lines (118 loc) · 4.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
classdef Sensor < SimulationObject & Equipment
%SENSOR Occupancy sensor class
% Detailed explanation goes here
properties
View
Source
Graphic
end
properties (SetAccess = protected)
State
RelativeTime
MotionDetected
end
properties (Dependent, SetAccess = protected)
Shape
end
properties (Access = private)
ConsecutiveRecording
end
methods
function obj = import(obj, filePath, position, view, id, startDate, consecutive)
obj.ID = id;
obj.View = view;
obj.Position = position;
obj.ConsecutiveRecording = consecutive;
warning('off','MATLAB:table:ModifiedAndSavedVarnames');
obj.Source = readtable(filePath,'Format','%d %{MM/dd/yy hh:mm:ss a}D %f %s %s %s %s');
warning('on','MATLAB:table:ModifiedAndSavedVarnames')
obj.convertSource(startDate, consecutive);
end
function shape = get.Shape(obj)
switch obj.View
case 'narrow'
r = 5.06;
case 'wide'
r = 7.33;
end
theta = linspace(0,2*pi);
x = r*cos(theta);
y = r*sin(theta) + 1;
shape = [x', y'];
end
function obj = convertSource(obj, startDate, consecutive)
absoluteTime = obj.Source{:,2};
motion = obj.Source{:,3};
idxNan = isnan(motion);
absoluteTime(idxNan) = [];
motion(idxNan) = [];
if consecutive
idxObservation = absoluteTime >= startDate & absoluteTime < dateshift(startDate,'start','day',2);
absoluteTime = absoluteTime(idxObservation);
relativeTime = absoluteTime - startDate;
motion = motion(idxObservation);
else
idxObservation1 = absoluteTime >= startDate & absoluteTime < dateshift(startDate,'start','day',1);
startDate2 = dateshift(startDate,'dayofweek','Monday');
idxObservation2 = absoluteTime >= startDate2 & absoluteTime < dateshift(startDate2,'start','day',1);
absoluteTime1 = absoluteTime(idxObservation1);
relativeTime1 = absoluteTime1 - startDate;
motion1 = motion(idxObservation1);
absoluteTime2 = absoluteTime(idxObservation2);
relativeTime2 = absoluteTime2 - startDate2;
motion2 = motion(idxObservation2);
relativeTime = vertcat(relativeTime1, relativeTime2);
motion = vertcat(motion1, motion2);
end
% Ensure unique sample times priority to last repeated entry
[C, ia, ic] = unique(seconds(relativeTime),'last','legacy');
relativeTime = relativeTime(ia);
motion = motion(ia);
obj.RelativeTime = relativeTime;
obj.MotionDetected = logical(motion);
obj.determineState;
end
function obj = determineState(obj)
t0 = vertcat(duration(0,0,-1), obj.RelativeTime, duration(48,0,0));
m0 = double(vertcat(false, obj.MotionDetected, false));
t = obj.SimRelTime;
obj.State = logical(interp1(t0, m0, t, 'previous'));
end
function render(obj, sim, varargin)
if nargin == 3
arrayfun(@(o)prender(o, sim, varargin{1}),obj);
else
arrayfun(@(o)prender(o, sim),obj);
end
end
end
methods (Access = private)
function prender(obj, sim, varargin)
X = obj.Shape(:,1) + obj.Position(1);
Y = obj.Shape(:,2) + obj.Position(2);
Z = ones(size(X));
if nargin == 3
ax = varargin{1};
else
ax = gca;
end
if isgraphics(obj.Graphic)
obj.Graphic.XData = X;
obj.Graphic.YData = Y;
obj.Graphic.ZData = Z;
else
obj.Graphic = patch(ax,'XData',X,'YData',Y,'ZData',Z,'FaceColor',[66 142 189]/255,'EdgeColor','none','FaceAlpha',.5);
end
state = obj.State(obj.SimRelTime == sim.Clock);
if state
obj.Graphic.Visible = 'on';
obj.Graphic.FaceAlpha = 0.5;
else
% obj.Graphic.Visible = 'off';
obj.Graphic.Visible = 'on';
obj.Graphic.FaceAlpha = 0;
obj.Graphic.EdgeColor = [66 142 189]/255;
end
end
end
end