Synchronisierung.
This commit is contained in:
51
Libraries/datalogger/Datalogger.cpp
Normal file
51
Libraries/datalogger/Datalogger.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "Datalogger.h"
|
||||
|
||||
Datalogger::Datalogger() {}
|
||||
|
||||
void Datalogger::addData(const std::map<String, String>& data) {
|
||||
DataEntry entry;
|
||||
entry.values = data;
|
||||
dataEntries.push_back(entry);
|
||||
}
|
||||
|
||||
void Datalogger::addConfig(String parameter, String value) {
|
||||
Config config = { parameter, value };
|
||||
configs.push_back(config);
|
||||
}
|
||||
|
||||
void Datalogger::logToSerial() {
|
||||
Serial.println("Configurations:");
|
||||
for (Config config : configs) {
|
||||
Serial.print(config.parameter);
|
||||
Serial.print("\t");
|
||||
Serial.println(config.value);
|
||||
}
|
||||
|
||||
Serial.println("\nData:");
|
||||
|
||||
if (!dataEntries.empty()) {
|
||||
// Print headers
|
||||
bool firstEntry = true;
|
||||
for (const auto& entry : dataEntries) {
|
||||
if (firstEntry) {
|
||||
for (const auto& pair : entry.values) {
|
||||
Serial.print(pair.first);
|
||||
Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
firstEntry = false;
|
||||
}
|
||||
// Print values
|
||||
for (const auto& pair : entry.values) {
|
||||
Serial.print(pair.second);
|
||||
Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Datalogger::clear() {
|
||||
dataEntries.clear();
|
||||
configs.clear();
|
||||
}
|
||||
31
Libraries/datalogger/Datalogger.h
Normal file
31
Libraries/datalogger/Datalogger.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef DATALOGGER_H
|
||||
#define DATALOGGER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
struct DataEntry {
|
||||
std::map<String, String> values;
|
||||
};
|
||||
|
||||
struct Config {
|
||||
String parameter;
|
||||
String value;
|
||||
};
|
||||
|
||||
class Datalogger {
|
||||
public:
|
||||
Datalogger();
|
||||
|
||||
void addData(const std::map<String, String>& data);
|
||||
void addConfig(String parameter, String value);
|
||||
void logToSerial();
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::vector<DataEntry> dataEntries;
|
||||
std::vector<Config> configs;
|
||||
};
|
||||
|
||||
#endif // DATALOGGER_H
|
||||
45
Libraries/datalogger/example.ino
Normal file
45
Libraries/datalogger/example.ino
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <Arduino.h>
|
||||
#include "Datalogger.h"
|
||||
|
||||
Datalogger logger;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
// Beispielkonfigurationen hinzufügen
|
||||
logger.addConfig("Sample Rate", "1Hz");
|
||||
logger.addConfig("Sensor Type", "Temperature");
|
||||
|
||||
// Beispieldaten hinzufügen
|
||||
std::map<String, String> data1;
|
||||
data1["Time"] = "0.1";
|
||||
data1["Input"] = "1";
|
||||
data1["Output"] = "5";
|
||||
logger.addData(data1);
|
||||
|
||||
std::map<String, String> data2;
|
||||
data2["Time"] = "0.2";
|
||||
data2["Input"] = "2";
|
||||
data2["Output"] = "10";
|
||||
logger.addData(data2);
|
||||
|
||||
std::map<String, String> data3;
|
||||
data3["Time"] = "0.3";
|
||||
data3["Input"] = "3";
|
||||
data3["Output"] = "15";
|
||||
logger.addData(data3);
|
||||
|
||||
// Daten und Konfigurationen über die serielle Schnittstelle ausgeben
|
||||
logger.logToSerial();
|
||||
|
||||
// Inhalte leeren
|
||||
logger.clear();
|
||||
|
||||
// Überprüfen, ob die Inhalte geleert wurden
|
||||
Serial.println("After clearing:");
|
||||
logger.logToSerial();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Hier könnte weitere Logik stehen
|
||||
}
|
||||
71
Libraries/datalogger/pid.cpp
Normal file
71
Libraries/datalogger/pid.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "PID.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
PID::PID(float kp, float ki, float kd) {
|
||||
this->kp = kp;
|
||||
this->ki = ki;
|
||||
this->kd = kd;
|
||||
this->minOutput = 0;
|
||||
this->maxOutput = 255;
|
||||
this->lastOutput = 0;
|
||||
this->sampleTime = 100; // Default sample time in milliseconds
|
||||
this->lastTime = millis();
|
||||
this->integral = 0;
|
||||
this->lastInput = 0;
|
||||
}
|
||||
|
||||
void PID::setTunings(float kp, float ki, float kd) {
|
||||
this->kp = kp;
|
||||
this->ki = ki;
|
||||
this->kd = kd;
|
||||
}
|
||||
|
||||
void PID::setOutputLimits(float min, float max) {
|
||||
this->minOutput = min;
|
||||
this->maxOutput = max;
|
||||
}
|
||||
|
||||
void PID::setSampleTime(int sampleTime) {
|
||||
this->sampleTime = sampleTime;
|
||||
}
|
||||
|
||||
void PID::reset() {
|
||||
this->integral = 0;
|
||||
this->lastInput = 0;
|
||||
this->lastTime = millis();
|
||||
}
|
||||
|
||||
float PID::compute(float setpoint, float input) {
|
||||
unsigned long now = millis();
|
||||
unsigned long timeChange = now - lastTime;
|
||||
|
||||
if (timeChange >= sampleTime) {
|
||||
// Calculate error
|
||||
float error = setpoint - input;
|
||||
|
||||
// Proportional term
|
||||
float Pout = kp * error;
|
||||
|
||||
// Integral term
|
||||
integral += (ki * error * timeChange / 1000.0);
|
||||
integral = constrain(integral, minOutput, maxOutput);
|
||||
|
||||
// Derivative term
|
||||
float derivative = (input - lastInput) / (timeChange / 1000.0);
|
||||
float Dout = kd * derivative;
|
||||
|
||||
// Compute output
|
||||
float output = Pout + integral - Dout;
|
||||
output = constrain(output, minOutput, maxOutput);
|
||||
|
||||
// Remember some variables for next time
|
||||
lastInput = input;
|
||||
lastTime = now;
|
||||
|
||||
lastOutput = output
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
return lastOutput;
|
||||
}
|
||||
32
Libraries/datalogger/pid.h
Normal file
32
Libraries/datalogger/pid.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef PID_H
|
||||
#define PID_H
|
||||
|
||||
class PID {
|
||||
public:
|
||||
PID(float kp, float ki, float kd);
|
||||
|
||||
void setTunings(float kp, float ki, float kd);
|
||||
void setOutputLimits(float min, float max);
|
||||
void setSampleTime(int sampleTime);
|
||||
|
||||
void reset();
|
||||
|
||||
float compute(float setpoint, float input);
|
||||
|
||||
private:
|
||||
float kp;
|
||||
float ki;
|
||||
float kd;
|
||||
|
||||
float minOutput;
|
||||
float maxOutput;
|
||||
float lastOutput;
|
||||
|
||||
int sampleTime;
|
||||
unsigned long lastTime;
|
||||
|
||||
float integral;
|
||||
float lastInput;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user