|
|
@ -9,14 +9,20 @@ |
|
|
|
#include <WebSocketsServer.h> |
|
|
|
#include <ArduinoJson.h> |
|
|
|
#include "CpuLoad.h" |
|
|
|
#include <DNSServer.h> |
|
|
|
|
|
|
|
#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.graphArray[graphAcc.graphCounter] = filteredAcc; |
|
|
|
} |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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,6 +155,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setup(void) |
|
|
|
{ |
|
|
|
IPAddress apIP(192, 168, 1, 1); |
|
|
|