-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensc472rc.m
120 lines (102 loc) · 3.27 KB
/
ensc472rc.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
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
%%
% Mobilit-E
%%
%connect to phone and get accel data
clc
clear
pause(0.5);
%% Global Variables
help_email = '[email protected]';
help_email3 = '[email protected]';
help_email2 = '[email protected]';
mins_to_run = 5;
graph_size = 200; %10 per second
data = zeros(graph_size,1);
gravity_constant = 9.89;
average_step_time = 0;
gait_threshold = 0;
gait_on = 0;
fall_detect = 0;
%% SETUP
m = mobiledev;
m.AccelerationSensorEnabled = 1;
%m.OrientationSensorEnabled = 1;
m.Logging = 1;
%% Initialize data for rolling plot
figure(1)
p = plot(data);
axis([0 graph_size -40 40]);
title('Step Force vs Time');
%% Wait for 3 Seconds
pause(3)
disp('Start Now'); %Start Walking
tic
%%
while (toc < 60*mins_to_run && fall_detect == 0)%run for X mins
%get new z coordinates
[acceldata,time] = accellog(m);
if length(acceldata) > graph_size
mag = sqrt(acceldata(end-graph_size-1:end,1).^2 + acceldata(end-graph_size-1:end,2).^2 + acceldata(end-graph_size-1:end,3).^2)-gravity_constant;
data = mag(end-graph_size-1:end);
curtimes = time(end-graph_size-1:end);
new_data = mag(end-80-1:end);
new_times = time(end-80-1:end);
minPeakHeight = std(data);
[pks, locs] = findpeaks(new_data, 'MINPEAKHEIGHT', minPeakHeight);
numSteps = numel(pks);
%% Detect Falls
for j = 1:length(pks)
if pks(j) > 23
fall_detect = 1;
disp('FALL DETECTED');
mailer(help_email);
mailer(help_email2);
mailer(help_email3);
end
end
%%END DETECT FALLS
step_time = length(pks)-1;
for i = 1:length(pks)-1
step_time(i) = time(locs(i+1))-time(locs(i));
end
current_average_step_time = mean(step_time);
if abs(current_average_step_time - average_step_time) > 0.40*average_step_time
gait_threshold = gait_threshold + 1;
if gait_threshold > 40
current_average_step_time
gait_on = 1;
gait_threshold = 0;
disp('GAIT DETECTED, PLEASE FIX, pausing 20 seconds!');
pause(20);
end
else
if gait_on ~= 0
disp('FINE NOW');
end
gait_threshold = 0;
gait_on = 0;
average_step_time = (average_step_time*7 + current_average_step_time)/8;
end
elseif length(acceldata) == 190
mag = sqrt(acceldata(:,1).^2 + acceldata(:,2).^2 + acceldata(:,3).^2)-gravity_constant;
data(1:length(acceldata)) = mag;
minPeakHeight = std(data);
[pks, locs] = findpeaks(data, 'MINPEAKHEIGHT', minPeakHeight);
numSteps = numel(pks);
step_time = length(pks)-1;
if average_step_time == 0
for i = 1:length(pks)-1
step_time(i) = time(locs(i+1))-time(locs(i));
end
average_step_time = mean(step_time);
end
else
mag = sqrt(acceldata(:,1).^2 + acceldata(:,2).^2 + acceldata(:,3).^2)-gravity_constant;
mean(mag)
data(1:length(acceldata)) = mag;
end
% redraw plot
p.YData = data;
drawnow
end
disp('SESSION ENDED');