Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D4-Embedded
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
D4-Furlough
D4-Embedded
Commits
75b31124
Commit
75b31124
authored
4 years ago
by
Robert Cheetham
Browse files
Options
Downloads
Patches
Plain Diff
Signed-off-by: Robert Cheetham <rec1g18@soton.ac.uk>
parent
9a4136c6
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
stepCounter.cpp
+82
-0
82 additions, 0 deletions
stepCounter.cpp
with
82 additions
and
0 deletions
stepCounter.cpp
0 → 100644
+
82
−
0
View file @
75b31124
#include
<Arduino.h>
#include
<Wire.h>
#include
<math.h>
const
int
MPU_addr
=
0x68
;
// I2C address of the MPU-6050
int16_t
AcX
,
AcY
,
AcZ
;
// These are the variables for reading the raw accelerometer value from the device.
double
rRaw
,
previousR
,
previousPreviousR
;
// The resultant sqrt((x^2)+(y^2)+(z^2)) and the previous ones.
double
total
;
// The total weighted as a low pass filter. Half current and quarter previous two.
double
maxTotal
,
minTotal
;
// The max and minimun in the current session.
int
steps
=
0
;
// Number of steps.
bool
below
;
// Boolean to determine if the value has gone 10000 less than the maximum in that period.
unsigned
long
lastTimeRead
=
0
;
// Used to determine how long until next reading
void
step
(){
// I imagine this part will be where the bluetooth implemention is?
steps
++
;
}
void
setup
(){
Wire
.
begin
();
Wire
.
beginTransmission
(
MPU_addr
);
Wire
.
write
(
0x6B
);
// PWR_MGMT_1 register
Wire
.
write
(
0
);
// set to zero (wakes up the MPU-6050)
Wire
.
endTransmission
(
true
);
Serial
.
begin
(
9600
);
previousR
=
14088.24
;
// To make sure the starting values arnt offset.
previousPreviousR
=
14088.24
;
// These are the common values.
below
=
true
;
maxTotal
=
2000
;
// Will obviously be overwritten in first instance.
minTotal
=
50000
;
}
void
loop
(){
unsigned
long
now
=
millis
();
// Checks current time
if
((
now
-
lastTimeRead
)
>
5
)
// Checks to see if enough time has elapsed (will reduce with ne sensor)
{
Wire
.
beginTransmission
(
MPU_addr
);
// Reads accelerometer
Wire
.
write
(
0x3B
);
//
Wire
.
endTransmission
(
false
);
Wire
.
requestFrom
(
MPU_addr
,
14
,
true
);
//
AcX
=
Wire
.
read
()
<<
8
|
Wire
.
read
();
//
AcY
=
Wire
.
read
()
<<
8
|
Wire
.
read
();
//
AcZ
=
Wire
.
read
()
<<
8
|
Wire
.
read
();
//
rRaw
=
sqrt
(
pow
(
AcX
,
2
)
+
pow
(
AcY
,
2
)
+
pow
(
AcZ
,
2
));
// The resultant of each axis
total
=
0.5
*
rRaw
+
0.25
*
previousR
+
0.25
*
previousPreviousR
;
// The curved amount
if
((
below
)
&&
(
total
-
minTotal
>=
10000
)
&&
(
total
>=
30000
)){
// Make sure above a certain value and has increased by a certain amount.
step
();
// These will be altered.
below
=
false
;
minTotal
=
total
;
// Resets minmum.
}
minTotal
=
min
(
minTotal
,
total
);
// Overrides min/max
maxTotal
=
max
(
maxTotal
,
total
);
if
(
maxTotal
-
total
>=
10000
){
// Checks has dropped by 10000
maxTotal
=
total
;
below
=
true
;
}
lastTimeRead
=
now
;
// Resets to current amounts
previousPreviousR
=
previousR
;
previousR
=
rRaw
;
Serial
.
print
(
"
\n
"
);
// Just prints steps, obviously remove.
Serial
.
print
(
steps
);
Serial
.
print
(
","
);
}
delay
(
5
);
}
\ No newline at end of file
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