Browse Source

Добавлен класс, учитывающий время загрузки процессора. На сигнал теперь накладывается низкочастотный фильтр шумов.

master
Nikita Romanenko 6 years ago
parent
commit
8aff74e5c9
  1. 1
      .gitignore
  2. 36
      src/CpuLoad.h
  3. 34
      src/main.cpp

1
.gitignore

@ -2,3 +2,4 @@
.piolibdeps
.vscode/c_cpp_properties.json
lib
lib/readme.txt

36
src/CpuLoad.h

@ -0,0 +1,36 @@
#include <arduino.h>
// updateCpuTime отвечает за обновление статистики загрузки Cpu
// updateCpuTime(false) - размещается в начале loop()
// updateCpuTime(true) - размещается в конце loop()
// printCpuTime выводит среднее время работы 10 циклов loop в мкс
// printCpuLoad выводит прикидочную нагрузку loop цикла
static long int cpuTime1 = 0;
static long int cpuTime2 = 0;
static int cpuLoadAvgTime = 0;
static long int cpuTimeLastUpdate = 0;
static void updateCpuTime(bool state) {
switch(state) {
case 0: cpuTime1 = micros();
return;
case 1: cpuTime2 = micros();
cpuLoadAvgTime = cpuLoadAvgTime*0.9 + (cpuTime2 - cpuTime1);
return;
}
}
static void printCpuTime () {
if (millis() - cpuTimeLastUpdate > 2000) {
Serial.printf("Time for update: %ld \n", cpuLoadAvgTime);
cpuTimeLastUpdate = millis();
}
}
static void printCpuLoad () {
if (millis() - cpuTimeLastUpdate > 2000) {
Serial.printf("CPU Load: %ld \n", cpuLoadAvgTime/10000);
cpuTimeLastUpdate = millis();
}
}

34
src/main.cpp

@ -8,6 +8,7 @@
#include <Sensor.h>
#include <WebSocketsServer.h>
#include <ArduinoJson.h>
#include "CpuLoad.h"
const char *ssid = "NodeMCU";
@ -15,7 +16,7 @@ ESP8266WebServer server(80);
Sensor accelerometer = Sensor();
Graph graphAcc = Graph(200, 50);
int lastDataSend = 0;
long int lastDataSend = 0;
WebSocketsServer webSocket = WebSocketsServer(81);
@ -54,14 +55,25 @@ void cookAccelerometer(String &in, int *in_array, int array_size) {
root.printTo(*output);
}
int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) {
return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100);
}
void updateAccelerometer()
{
if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.graphUpdateRate))
{
accelerometer.lastValue = map(analogRead(A0), 512, 768, 200, 0);
{
int filteredAcc = 0;
accelerometer.lastValue = map(analogRead(A0), 368, 768, 200, 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.graphCounter < graphAcc.graphSize - 1)
{
graphAcc.graphArray[graphAcc.graphCounter] = accelerometer.lastValue;
{
graphAcc.graphArray[graphAcc.graphCounter] = filteredAcc;
graphAcc.graphCounter++;
}
else
@ -70,7 +82,7 @@ void updateAccelerometer()
{
graphAcc.graphArray[i] = graphAcc.graphArray[i + 1];
}
graphAcc.graphArray[graphAcc.graphCounter] = accelerometer.lastValue;
graphAcc.graphArray[graphAcc.graphCounter] = filteredAcc;
}
@ -78,6 +90,7 @@ void updateAccelerometer()
}
}
void handleNotFound()
{
String message = "File Not Found\n\n";
@ -96,7 +109,7 @@ void handleNotFound()
}
void sendAccelerometerData() {
if (millis() - lastDataSend > 100) {
if (millis() - lastDataSend > 50) {
String word;
cookAccelerometer(word, graphAcc.graphArray, graphAcc.graphSize);
webSocket.broadcastTXT(word);
@ -153,9 +166,14 @@ void setup(void)
}
void loop(void)
{
{
updateCpuTime(0);
server.handleClient();
webSocket.loop();
updateAccelerometer();
sendAccelerometerData();
updateCpuTime(1);
printCpuLoad();
}
Loading…
Cancel
Save