Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
SPS_Team4_Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eunhye Choi
SPS_Team4_Project
Commits
6332f53c
Commit
6332f53c
authored
1 year ago
by
Eunhye Choi
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
a92462da
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pulse/pulse.c
+200
-0
200 additions, 0 deletions
pulse/pulse.c
with
200 additions
and
0 deletions
pulse/pulse.c
0 → 100644
+
200
−
0
View file @
6332f53c
#include
<stdio.h>
#include
<unistd.h>
#include
<pthread.h>
#include
<wiringPi.h>
#include
<wiringPiSPI.h>
#define RATE_SIZE 10
typedef
struct
{
int
channel
;
int
BPM
;
int
bus
;
int
device
;
int
max_speed_hz
;
}
Pulsesensor
;
typedef
struct
{
int
bus
;
int
device
;
}
MCP3008
;
void
MCP3008_open
(
MCP3008
*
adc
)
{
wiringPiSPISetup
(
adc
->
bus
,
adc
->
max_speed_hz
);
}
int
MCP3008_read
(
MCP3008
*
adc
,
int
channel
)
{
int
cmd1
=
4
|
2
|
((
channel
&
4
)
>>
2
);
int
cmd2
=
(
channel
&
3
)
<<
6
;
unsigned
char
data
[
3
];
data
[
0
]
=
cmd1
;
data
[
1
]
=
cmd2
;
data
[
2
]
=
0
;
wiringPiSPIDataRW
(
adc
->
bus
,
data
,
3
);
int
result
=
((
data
[
1
]
&
15
)
<<
8
)
+
data
[
2
];
return
result
;
}
void
MCP3008_close
(
MCP3008
*
adc
)
{
wiringPiSPIClose
(
adc
->
bus
);
}
void
getBPMLoop
(
Pulsesensor
*
sensor
,
MCP3008
*
adc
)
{
int
rate
[
RATE_SIZE
]
=
{
0
};
int
sampleCounter
=
0
;
int
lastBeatTime
=
0
;
int
P
=
512
;
int
T
=
512
;
int
thresh
=
525
;
int
amp
=
100
;
int
firstBeat
=
1
;
int
secondBeat
=
0
;
int
IBI
=
600
;
int
Pulse
=
0
;
int
lastTime
=
(
int
)
time
(
NULL
)
*
1000
;
while
(
1
)
{
// MCP3008을 통해 센서 값을 읽어옴
int
Signal
=
MCP3008_read
(
adc
,
sensor
->
channel
);
int
currentTime
=
(
int
)
time
(
NULL
)
*
1000
;
// 시간 간격을 측정하여 심장 박동의 타이밍 추적
// 마지막 측정으로부터 현재까지 경과된 시간, 측정 시간을 계속 누적
sampleCounter
+=
currentTime
-
lastTime
;
// lastTiem 업데이트
lastTime
=
currentTime
;
int
N
=
sampleCounter
-
lastBeatTime
;
if
(
Signal
<
thresh
&&
N
>
(
IBI
/
5
.
0
)
*
3
)
{
if
(
Signal
<
T
)
{
T
=
Signal
;
}
}
if
(
Signal
>
thresh
&&
Signal
>
P
)
{
P
=
Signal
;
}
if
(
N
>
250
)
{
if
(
Signal
>
thresh
&&
Pulse
==
0
&&
N
>
(
IBI
/
5
.
0
)
*
3
)
{
Pulse
=
1
;
IBI
=
sampleCounter
-
lastBeatTime
;
lastBeatTime
=
sampleCounter
;
if
(
secondBeat
)
{
secondBeat
=
0
;
for
(
int
i
=
0
;
i
<
RATE_SIZE
;
i
++
)
{
rate
[
i
]
=
IBI
;
}
}
if
(
firstBeat
)
{
firstBeat
=
0
;
secondBeat
=
1
;
continue
;
}
for
(
int
i
=
0
;
i
<
RATE_SIZE
-
1
;
i
++
)
{
rate
[
i
]
=
rate
[
i
+
1
];
}
rate
[
RATE_SIZE
-
1
]
=
IBI
;
int
runningTotal
=
0
;
for
(
int
i
=
0
;
i
<
RATE_SIZE
;
i
++
)
{
runningTotal
+=
rate
[
i
];
}
int
averageIBI
=
runningTotal
/
RATE_SIZE
;
sensor
->
BPM
=
60000
/
averageIBI
;
}
}
if
(
Signal
<
thresh
&&
Pulse
==
1
)
{
Pulse
=
0
;
amp
=
P
-
T
;
thresh
=
amp
/
2
+
T
;
P
=
thresh
;
T
=
thresh
;
}
if
(
N
>
2500
)
{
thresh
=
512
;
P
=
512
;
T
=
512
;
lastBeatTime
=
sampleCounter
;
firstBeat
=
1
;
secondBeat
=
0
;
sensor
->
BPM
=
0
;
}
usleep
(
5000
);
}
}
void
startAsyncBPM
(
Pulsesensor
*
sensor
,
MCP3008
*
adc
)
{
// 스레드를 시작하여 getBPMLoop 함수를 실행
pthread_t
tid
;
pthread_create
(
&
tid
,
NULL
,
(
void
*
)
&
getBPMLoop
,
sensor
,
adc
);
}
void
stopAsyncBPM
(
Pulsesensor
*
sensor
)
{
// 스레드를 중지하고 BPM을 초기화
sensor
->
BPM
=
0
;
}
int
main
()
{
Pulsesensor
sensor
;
sensor
.
channel
=
0
;
sensor
.
BPM
=
0
;
MCP3008
adc
;
adc
.
bus
=
0
;
adc
.
device
=
0
;
adc
.
max_speed_hz
=
1000000
;
MCP3008_open
(
&
adc
);
startAsyncBPM
(
&
sensor
,
&
adc
);
while
(
1
)
{
int
bpm
=
sensor
.
BPM
;
if
(
bpm
-
155
>
0
)
{
printf
(
"BPM: %d
\n
"
,
bpm
-
155
);
}
else
{
printf
(
"No Heartbeat found
\n
"
);
}
usleep
(
1500000
);
// 1.5초 대기
}
stopAsyncBPM
(
&
sensor
);
MCP3008_close
(
&
adc
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment