diff --git a/src/Graph.cpp b/src/Graph.cpp index ed1c842..f6951e1 100644 --- a/src/Graph.cpp +++ b/src/Graph.cpp @@ -1,8 +1,10 @@ #include "Graph.h" Graph::Graph (int graphSize, int graphUpdateRate) { - this->graphSize = graphSize; - this->graphUpdateRate = graphUpdateRate; // Раз в секунду - graphArray = new int[graphSize]; - graphCounter = 0; + this->size = graphSize; + this->updateRate = graphUpdateRate; // Раз в секунду + array = new int[size]; + fft = new int[size/2]; + source = new int[size]; + counter = 0; } \ No newline at end of file diff --git a/src/Graph.h b/src/Graph.h index e6349c6..b0f3f2a 100644 --- a/src/Graph.h +++ b/src/Graph.h @@ -1,9 +1,11 @@ class Graph { public: - int graphSize; - int graphUpdateRate; - int * graphArray; - int graphCounter; + int size; + int updateRate; + int * array; + int * fft; + int * source; + int counter; Graph (int graphSize, int graphUpdateRate); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e8f3439..9f879f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,14 +9,20 @@ #include #include #include "CpuLoad.h" -#include + +#include "arduinoFFT.h" +bool fftMode = true; +arduinoFFT FFT = arduinoFFT(); + const char *ssid = "LabWork_1"; ESP8266WebServer server(80); -DNSServer dnsServer; Sensor accelerometer = Sensor(); -Graph graphAcc = Graph(200, 50); +Graph graphAcc = Graph(256, 50); + +double vReal[256]; +double vImag[256]; long int lastDataSend = 0; @@ -61,39 +67,54 @@ int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) { return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100); } -// TODO: необходимо перекомпоновать функцию updateAccelerometer() void updateAccelerometer() { - if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.graphUpdateRate)) + if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate)) { - int filteredAcc = 0; - accelerometer.lastValue = map(analogRead(A0), 0, 1023, 200, 0); + int filteredSignal = 0; + int sourceSignal = analogRead(A0); + accelerometer.lastValue = map(sourceSignal, 0, 1023, graphAcc.size-1, 0); - if (graphAcc.graphCounter != 0) { - //filteredAcc = lowFreqFilter(accelerometer.lastValue, graphAcc.graphArray[graphAcc.graphCounter - 1], 0.5); - filteredAcc = graphAcc.graphArray[graphAcc.graphCounter - 1] + 0.5 * (accelerometer.lastValue - graphAcc.graphArray[graphAcc.graphCounter - 1]); + 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.graphCounter < graphAcc.graphSize - 1) + if (graphAcc.counter < graphAcc.size - 1) { - graphAcc.graphArray[graphAcc.graphCounter] = filteredAcc; - graphAcc.graphCounter++; + graphAcc.array[graphAcc.counter] = filteredSignal; + graphAcc.source[graphAcc.counter] = sourceSignal; + graphAcc.counter++; } else { - for (int i = 0; i != graphAcc.graphCounter; i++) + for (int i = 0; i != graphAcc.counter; i++) { - graphAcc.graphArray[i] = graphAcc.graphArray[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; + + //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.graphArray[graphAcc.graphCounter] = filteredAcc; } - accelerometer.lastUpdate = millis(); } } + void handleNotFound() { String message = "File Not Found\n\n"; @@ -105,21 +126,26 @@ void handleNotFound() message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) - { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; - } server.send(404, "text/plain", message); } void sendAccelerometerData() { - if (millis() - lastDataSend > 50) { + + int updateRate = 0; + if (fftMode) updateRate = 200; + else updateRate = graphAcc.updateRate; + + if (millis() - lastDataSend > updateRate) { String word; - cookAccelerometer(word, graphAcc.graphArray, graphAcc.graphSize); + cookAccelerometer(word, graphAcc.fft, graphAcc.size/2); webSocket.broadcastTXT(word); + Serial.println(word); lastDataSend = millis(); } } + void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_TEXT: @@ -129,13 +155,14 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length } + void setup(void) { IPAddress apIP(192, 168, 1, 1); Serial.begin(115200); Serial.setDebugOutput(true); Serial.println(); - + WiFi.setAutoConnect(false); WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); WiFi.softAP(ssid);