mirror of
https://github.com/okalachev/flix.git
synced 2025-07-27 09:39:33 +00:00
Implement receiving and sending SERIAL_CONTROL message Use global defined print function instead of Serial.printf
80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
|
|
// Repository: https://github.com/okalachev/flix
|
|
|
|
// In-RAM logging
|
|
|
|
#include "vector.h"
|
|
|
|
#define LOG_RATE 100
|
|
#define LOG_DURATION 10
|
|
#define LOG_PERIOD 1.0 / LOG_RATE
|
|
#define LOG_SIZE LOG_DURATION * LOG_RATE
|
|
|
|
float tFloat;
|
|
Vector attitudeEuler;
|
|
Vector attitudeTargetEuler;
|
|
|
|
struct LogEntry {
|
|
const char *name;
|
|
float *value;
|
|
};
|
|
|
|
LogEntry logEntries[] = {
|
|
{"t", &tFloat},
|
|
{"rates.x", &rates.x},
|
|
{"rates.y", &rates.y},
|
|
{"rates.z", &rates.z},
|
|
{"ratesTarget.x", &ratesTarget.x},
|
|
{"ratesTarget.y", &ratesTarget.y},
|
|
{"ratesTarget.z", &ratesTarget.z},
|
|
{"attitude.x", &attitudeEuler.x},
|
|
{"attitude.y", &attitudeEuler.y},
|
|
{"attitude.z", &attitudeEuler.z},
|
|
{"attitudeTarget.x", &attitudeTargetEuler.x},
|
|
{"attitudeTarget.y", &attitudeTargetEuler.y},
|
|
{"attitudeTarget.z", &attitudeTargetEuler.z},
|
|
{"thrustTarget", &thrustTarget}
|
|
};
|
|
|
|
const int logColumns = sizeof(logEntries) / sizeof(logEntries[0]);
|
|
float logBuffer[LOG_SIZE][logColumns];
|
|
|
|
void prepareLogData() {
|
|
tFloat = t;
|
|
attitudeEuler = attitude.toEulerZYX();
|
|
attitudeTargetEuler = attitudeTarget.toEulerZYX();
|
|
}
|
|
|
|
void logData() {
|
|
if (!armed) return;
|
|
static int logPointer = 0;
|
|
static double logTime = 0;
|
|
if (t - logTime < LOG_PERIOD) return;
|
|
logTime = t;
|
|
|
|
prepareLogData();
|
|
|
|
for (int i = 0; i < logColumns; i++) {
|
|
logBuffer[logPointer][i] = *logEntries[i].value;
|
|
}
|
|
|
|
logPointer++;
|
|
if (logPointer >= LOG_SIZE) {
|
|
logPointer = 0;
|
|
}
|
|
}
|
|
|
|
void dumpLog() {
|
|
// Print header
|
|
for (int i = 0; i < logColumns; i++) {
|
|
print("%s%s", logEntries[i].name, i < logColumns - 1 ? "," : "\n");
|
|
}
|
|
// Print data
|
|
for (int i = 0; i < LOG_SIZE; i++) {
|
|
if (logBuffer[i][0] == 0) continue; // skip empty records
|
|
for (int j = 0; j < logColumns; j++) {
|
|
print("%g%s", logBuffer[i][j], j < logColumns - 1 ? "," : "\n");
|
|
}
|
|
}
|
|
}
|