Browse Source

removed dns server; first implementation of fast fourier transform for step counting

master
Никита 7 years ago
parent
commit
92383f82dd
  1. 10
      src/Graph.cpp
  2. 10
      src/Graph.h
  3. 71
      src/main.cpp

10
src/Graph.cpp

@ -1,8 +1,10 @@
#include "Graph.h" #include "Graph.h"
Graph::Graph (int graphSize, int graphUpdateRate) { Graph::Graph (int graphSize, int graphUpdateRate) {
this->graphSize = graphSize; this->size = graphSize;
this->graphUpdateRate = graphUpdateRate; // Раз в секунду this->updateRate = graphUpdateRate; // Раз в секунду
graphArray = new int[graphSize]; array = new int[size];
graphCounter = 0; fft = new int[size/2];
source = new int[size];
counter = 0;
} }

10
src/Graph.h

@ -1,9 +1,11 @@
class Graph { class Graph {
public: public:
int graphSize; int size;
int graphUpdateRate; int updateRate;
int * graphArray; int * array;
int graphCounter; int * fft;
int * source;
int counter;
Graph (int graphSize, int graphUpdateRate); Graph (int graphSize, int graphUpdateRate);
}; };

71
src/main.cpp

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

Loading…
Cancel
Save