Browse Source

first ver of step counter method, may be some unstable work

master
Никита 7 years ago
parent
commit
ed1512726f
  1. 106
      src/main.cpp

106
src/main.cpp

@ -7,14 +7,13 @@
#include <WebSocketsServer.h> #include <WebSocketsServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "CpuLoad.h" #include "CpuLoad.h"
#include <Ticker.h>
#include "arduinoFFT.h" #include "arduinoFFT.h"
bool fftMode = true;
arduinoFFT FFT = arduinoFFT(); arduinoFFT FFT = arduinoFFT();
const char *ssid = "LabWork_1"; const char *ssid = "LabWork_1";
ESP8266WebServer server(80); ESP8266WebServer server(80);
Sensor accelerometer = Sensor(); Sensor accelerometer = Sensor();
Graph graphAcc = Graph(256, 50); Graph graphAcc = Graph(256, 50);
@ -44,14 +43,8 @@ void handleAccelerometer()
const int arr_size = 7000; const int arr_size = 7000;
char *html_code = new char[arr_size]; char *html_code = new char[arr_size];
memset(html_code, '\0', sizeof(char) * arr_size); memset(html_code, '\0', sizeof(char) * arr_size);
bias += getHtml(HTML_BEGIN, *html_code, arr_size, 0); 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); server.send(200, "text/html", html_code);
delete[] html_code; delete[] html_code;
} }
@ -82,54 +75,75 @@ int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) {
void updateAccelerometer() void updateAccelerometer()
{ {
if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate)) int filteredSignal = 0;
{ int sourceSignal = analogRead(A0);
int filteredSignal = 0; accelerometer.lastValue = map(sourceSignal, 0, 1023, graphAcc.size-1, 0);
int sourceSignal = analogRead(A0);
accelerometer.lastValue = map(sourceSignal, 0, 1023, graphAcc.size-1, 0);
if (graphAcc.counter != 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]);
filteredSignal = graphAcc.array[graphAcc.counter - 1] + 0.5 * (accelerometer.lastValue - graphAcc.array[graphAcc.counter - 1]); }
}
if (graphAcc.counter < graphAcc.size - 1) if (graphAcc.counter < graphAcc.size - 1)
{ {
graphAcc.array[graphAcc.counter] = filteredSignal; graphAcc.array[graphAcc.counter] = filteredSignal;
graphAcc.source[graphAcc.counter] = sourceSignal; graphAcc.source[graphAcc.counter] = sourceSignal;
graphAcc.counter++; graphAcc.counter++;
} }
else 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[i] = graphAcc.array[i + 1]; }
graphAcc.source[i] = graphAcc.source[i + 1]; graphAcc.array[graphAcc.counter] = filteredSignal;
} graphAcc.source[graphAcc.counter] = sourceSignal;
graphAcc.array[graphAcc.counter] = filteredSignal; graphAcc.peakFreqArray[graphAcc.counter] = peakFreq;
graphAcc.source[graphAcc.counter] = sourceSignal;
//start fft
//start fft for (int i = 0; i < graphAcc.size; i++) {
for (int i = 0; i < graphAcc.size; i++) { vReal[i] = (double) graphAcc.source[i];
vReal[i] = (double) graphAcc.source[i]; vImag[i] = 0;
vImag[i] = 0; }
} FFT.Windowing(vReal, graphAcc.size, FFT_WIN_TYP_RECTANGLE, FFT_FORWARD);
FFT.Windowing(vReal, graphAcc.size, FFT_WIN_TYP_RECTANGLE, FFT_FORWARD); FFT.Compute(vReal, vImag, graphAcc.size, FFT_FORWARD);
FFT.Compute(vReal, vImag, graphAcc.size, FFT_FORWARD); FFT.ComplexToMagnitude(vReal, vImag, graphAcc.size);
FFT.ComplexToMagnitude(vReal, vImag, graphAcc.size); for (int i = 0; i < graphAcc.size/2; i++) {
for (int i = 0; i < graphAcc.size/2; i++) { graphAcc.fft[i] = map((int) vReal[i], 0, 15000, 200, 0);
graphAcc.fft[i] = map((int) vReal[i], 0, 15000, 200, 0); }
}
peakFreq = FFT.MajorPeak(vReal, graphAcc.size, 30); peakFreq = FFT.MajorPeak(vReal, graphAcc.size, 30);
for (int i = 0; i != 127; i++) {
graphAcc.peakFreqArray[i] = graphAcc.peakFreqArray[i + 1];
} }
graphAcc.peakFreqArray[127] = peakFreq;
accelerometer.lastUpdate = millis();
} }
} }
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() void handleNotFound()
{ {

Loading…
Cancel
Save