diff --git a/src/main.cpp b/src/main.cpp index c8c57da..35a9b19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,14 +7,13 @@ #include #include #include "CpuLoad.h" +#include #include "arduinoFFT.h" -bool fftMode = true; arduinoFFT FFT = arduinoFFT(); const char *ssid = "LabWork_1"; - ESP8266WebServer server(80); Sensor accelerometer = Sensor(); Graph graphAcc = Graph(256, 50); @@ -44,14 +43,8 @@ void handleAccelerometer() const int arr_size = 7000; char *html_code = new char[arr_size]; memset(html_code, '\0', sizeof(char) * arr_size); - bias += getHtml(HTML_BEGIN, *html_code, arr_size, 0); - // bias += getHtml(STYLE, *html_code, arr_size, bias); - // bias += getHtml(SCRIPT, *html_code, arr_size, bias); - // bias += getHtml(HTML_END, *html_code, arr_size, bias); - server.send(200, "text/html", html_code); - delete[] html_code; } @@ -82,54 +75,75 @@ int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) { void updateAccelerometer() { - if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate)) - { - int filteredSignal = 0; - int sourceSignal = analogRead(A0); - accelerometer.lastValue = map(sourceSignal, 0, 1023, graphAcc.size-1, 0); + int filteredSignal = 0; + int sourceSignal = analogRead(A0); + accelerometer.lastValue = map(sourceSignal, 0, 1023, graphAcc.size-1, 0); - if (graphAcc.counter != 0) { - //filteredSignal = lowFreqFilter(accelerometer.lastValue, graphAcc.array[acc.graphCounter - 1], 0.5); - filteredSignal = graphAcc.array[graphAcc.counter - 1] + 0.5 * (accelerometer.lastValue - graphAcc.array[graphAcc.counter - 1]); - } + if (graphAcc.counter != 0) { + filteredSignal = graphAcc.array[graphAcc.counter - 1] + 0.5 * (accelerometer.lastValue - graphAcc.array[graphAcc.counter - 1]); + } - if (graphAcc.counter < graphAcc.size - 1) - { - graphAcc.array[graphAcc.counter] = filteredSignal; - graphAcc.source[graphAcc.counter] = sourceSignal; - graphAcc.counter++; - } - else + if (graphAcc.counter < graphAcc.size - 1) + { + graphAcc.array[graphAcc.counter] = filteredSignal; + graphAcc.source[graphAcc.counter] = sourceSignal; + graphAcc.counter++; + } + else + { + for (int i = 0; i != graphAcc.counter; i++) { - for (int i = 0; i != graphAcc.counter; i++) - { - graphAcc.array[i] = graphAcc.array[i + 1]; - graphAcc.source[i] = graphAcc.source[i + 1]; - } - graphAcc.array[graphAcc.counter] = filteredSignal; - graphAcc.source[graphAcc.counter] = sourceSignal; - - //start fft - for (int i = 0; i < graphAcc.size; i++) { - vReal[i] = (double) graphAcc.source[i]; - vImag[i] = 0; - } - FFT.Windowing(vReal, graphAcc.size, FFT_WIN_TYP_RECTANGLE, FFT_FORWARD); - FFT.Compute(vReal, vImag, graphAcc.size, FFT_FORWARD); - FFT.ComplexToMagnitude(vReal, vImag, graphAcc.size); - for (int i = 0; i < graphAcc.size/2; i++) { - graphAcc.fft[i] = map((int) vReal[i], 0, 15000, 200, 0); - } + graphAcc.array[i] = graphAcc.array[i + 1]; + graphAcc.source[i] = graphAcc.source[i + 1]; + } + graphAcc.array[graphAcc.counter] = filteredSignal; + graphAcc.source[graphAcc.counter] = sourceSignal; + graphAcc.peakFreqArray[graphAcc.counter] = peakFreq; + + //start fft + for (int i = 0; i < graphAcc.size; i++) { + vReal[i] = (double) graphAcc.source[i]; + vImag[i] = 0; + } + FFT.Windowing(vReal, graphAcc.size, FFT_WIN_TYP_RECTANGLE, FFT_FORWARD); + FFT.Compute(vReal, vImag, graphAcc.size, FFT_FORWARD); + FFT.ComplexToMagnitude(vReal, vImag, graphAcc.size); + for (int i = 0; i < graphAcc.size/2; i++) { + graphAcc.fft[i] = map((int) vReal[i], 0, 15000, 200, 0); + } peakFreq = FFT.MajorPeak(vReal, graphAcc.size, 30); + for (int i = 0; i != 127; i++) { + graphAcc.peakFreqArray[i] = graphAcc.peakFreqArray[i + 1]; } - - accelerometer.lastUpdate = millis(); + graphAcc.peakFreqArray[127] = peakFreq; } } - +void stepUpdater () { + int sensorsUpdateRate = 15; // samples/sec + double slidingWindow = 1.2; //secs + double fftWindow = 8.5; //secs + for (int i = 0; i < sensorsUpdateRate * fftWindow; i = i + sensorsUpdateRate * slidingWindow) { + + int amplCounter = 0; + int cycleCounter = 0; + double stepCounter = 0; + for (int j = i; (j < (sensorsUpdateRate * slidingWindow + i)) && (j < sensorsUpdateRate * fftWindow); j++) { + + if ((0.7 < graphAcc.peakFreqArray[j]) && (graphAcc.peakFreqArray[j] < 2.2)) { + amplCounter++; + stepCounter = stepCounter + (graphAcc.peakFreqArray[j] / (sensorsUpdateRate * slidingWindow)); + } + cycleCounter++; + } + if ((amplCounter / cycleCounter) > 0.7) { + steps = steps + (int)(stepCounter); + } + ESP.wdtFeed(); + } +} void handleNotFound() {