-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPedometerScript.cs
More file actions
47 lines (38 loc) · 1.39 KB
/
Copy pathPedometerScript.cs
File metadata and controls
47 lines (38 loc) · 1.39 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PedometerScript : MonoBehaviour {
public float loLim = 0.005f; // level to fall to the low state
public float hiLim = 0.1f; // level to go to high state (and detect step)
public int steps = 0; // step counter - counts when comp state goes high private
bool stateH = false; // comparator state
public float fHigh = 10.0f; // noise filter control - reduces frequencies above fHigh private
public float curAcc = 0f; // noise filter
public float fLow = 0.1f; // average gravity filter control - time constant about 1/fLow
float avgAcc = 0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void FixedUpdate(){ // filter input.acceleration using Lerp
curAcc = Mathf.Lerp(curAcc, Input.acceleration.magnitude, Time.deltaTime * fHigh);
avgAcc = Mathf.Lerp(avgAcc, Input.acceleration.magnitude, Time.deltaTime * fLow);
float delta = curAcc-avgAcc; // gets the acceleration pulses
if (!stateH){ // if state == low...
if (delta>hiLim){ // only goes high if input > hiLim
stateH = true;
steps++; // count step with each step up
NewStepDetected ();
}
} else {
if (delta<loLim){ // only goes low if input < loLim
stateH = false;
}
}
}
void NewStepDetected(){
//Do whatever you want with each step
}
}