Skip to content

Commit

Permalink
Merge pull request #17 from Legohead259/serial-debug-change
Browse files Browse the repository at this point in the history
Logging Overhaul
  • Loading branch information
Legohead259 authored Nov 24, 2022
2 parents 21d4120 + c01d25e commit 0c7b95d
Show file tree
Hide file tree
Showing 19 changed files with 651 additions and 137 deletions.
29 changes: 26 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,31 @@ Release sections
### Security
-->

## [UNRELEASED] 1.4.0 -
### TODO
## 1.4.0 - 2022-11-23

### Added
- Added a massive rework to the logging system
- Added seven different logging levels that can be used to track events
- Consolidated all logging functions into an object-oriented approach, allowing more flexibility
- Loggers can specify which streams they send data to and what the information looks like
- Ability to dynamically switch between streaming the event log to the Serial monitor or the file system, depending on USB connection
- Expanded the configuration options that can be saved to SPIFFS
- Log level for print streaming
- Log level for event file
- Added various `info`, `fatal`, and `verbose` statements throughout code to track flow

### Changed
- Changed static logging behavior to object-oriented
- Moved `fusion.h` to `AHRS/` folder since that makes more sense
- Removed state update behavior in `updateSystemState()`, for now

### Deprecated
- Deprecated the old `logging` files - they currently have the file extension `.bkp` for safekeeping - remove in next update!

### Removed
- Removed BNO055 files

---

## 1.3.0 - 2022-11-20

Expand All @@ -27,7 +50,7 @@ Release sections
- Added a function to log data to the file in a binary format
- Added an `updateTimestamp()` function to update the data packet epoch
- Added ability to change IMU information from configuration data
- Aded ability to change sensor fusion update rate from configuration data
- Added ability to change sensor fusion update rate from configuration data
- Expanded the configuration options that can be saved to SPIFFS
- More device information
- WiFi information
Expand Down
40 changes: 25 additions & 15 deletions src/sensors/fusion.h → src/AHRS/fusion.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef IMU_H
#define IMU_H

#include "lsm6dso32.h"
#include "../AHRS/MahonyAHRS.h"
#include "../sensors/lsm6dso32.h"
#include "MahonyAHRS.h"
#include "../utility/imumaths.h"
#include "../data.h"
#include "../filesystem/logger.h"

uint8_t fusionUpdateRate = 200; // Hz - Default: 200
unsigned long fusionUpdateInterval = 1000/fusionUpdateRate; // time between IMU polls [ms]
Expand All @@ -27,17 +28,20 @@ sensors_vec_t eulerAngles;

#define N_state 6 // Number of states - Acceleration (XYZ), Gyroscope (XYZ)
#define N_obs 6 // Number of observation - Acceleration (XYZ), Gyroscope (XYZ)
#define r_a 0.5 // Acceleration measurement noise (m/sec)
#define r_a 0.5 // Acceleration measurement noise (m/sec/sec)
#define r_g 1.0 // Gyroscope measurement noise (deg/sec)
#define r_m 1.0 // Magnetometer measurement noise (nT)
#define q_a 0.1 // Process error - acceleration
#define q_g 0.1 // Process error - gyroscope
#define q_m 0.1 // Process error - magnetometer

BLA::Matrix<N_obs> obs; // observation vector
KALMAN<N_state, N_obs> K_imu; // Kalman filter object
unsigned long currMillis;
float dt;

void initFusion() {
diagLogger->info("Initializing fusion algorithm...");
// Initialize Kalman Filter

// time evolution matrix (whatever... it will be updated in loop)
Expand Down Expand Up @@ -65,6 +69,9 @@ void initFusion() {
0.0, 0.0, 0.0, r_g2, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, r_g2, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, r_g2 };
diagLogger->verbose("Accelerometer variance set to: %d m/s/s", r_a);
diagLogger->verbose("Gyroscope variance set to: %d m/s/s", r_g);
diagLogger->verbose("Magnetometer variance set to: %d m/s/s", r_m);

// model covariance matrix
float q_a2 = q_a*q_a;
Expand All @@ -75,8 +82,12 @@ void initFusion() {
0.0, 0.0, 0.0, q_g2, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, q_g2, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, q_g2 };
diagLogger->verbose("Accelerometer process noise set to: %d", q_a);
diagLogger->verbose("Gyroscope process noise set to: %d", q_g);
diagLogger->verbose("Magnetometer process noise set to: %d", q_m);

currMillis = millis();
diagLogger->info("done!");
}

void calcLinAccel() {
Expand All @@ -89,10 +100,7 @@ void calcLinAccel() {
data.linAccelY = data.accelY - gravBody.y();
data.linAccelZ = data.accelZ - gravBody.z();

// DEBUG statement
#ifdef IMU_DEBUG
DEBUG_SERIAL_PORT.printf("LinAccel X: %0.3f \t\t Y: %0.3f \t\t Z: %0.3f \t\t m/s/s\n\r", data.accelX, data.accelY, data.accelZ);
#endif
diagLogger->verbose("LinAccel X: %0.3f \t Y: %0.3f \t Z: %0.3f", data.linAccelX, data.linAccelY, data.linAccelZ);
}

void updateFusion() {
Expand All @@ -111,6 +119,10 @@ void updateFusion() {
data.rawGyroY = gyro.gyro.y;
data.rawGyroZ = gyro.gyro.z;

diagLogger->verbose("RawAccel X: %0.3f \t Y: %0.3f \t Z: %0.3f", data.rawAccelX, data.rawAccelY, data.rawAccelZ);
diagLogger->verbose("RawGyro X: %0.3f \t Y: %0.3f \t Z: %0.3f", data.rawGyroX, data.rawGyroY, data.rawGyroZ);


// --------------------------
// -- Update Kalman Filter --
// --------------------------
Expand All @@ -136,14 +148,8 @@ void updateFusion() {
data.gyroY = K_imu.x(4);
data.gyroZ = K_imu.x(5);

#ifdef KALMAN_IMU_DEBUG
DEBUG_SERIAL_PORT.printf("Acceleration X: %0.3f | %0.3f \n\r", K_imu.x(0), data.accelX);
DEBUG_SERIAL_PORT.printf("Acceleration Y: %0.3f | %0.3f \n\r", K_imu.x(1), data.accelY);
DEBUG_SERIAL_PORT.printf("Acceleration Z: %0.3f | %0.3f \n\r", K_imu.x(2), data.accelZ);
DEBUG_SERIAL_PORT.printf("Gyroscope X: %0.3f | %0.3f \n\r", K_imu.x(3), data.gyroX);
DEBUG_SERIAL_PORT.printf("Gyroscope Y: %0.3f | %0.3f \n\r", K_imu.x(4), data.gyroY);
DEBUG_SERIAL_PORT.printf("Gyroscope Z: %0.3f | %0.3f \n\r", K_imu.x(5), data.gyroZ);
#endif
diagLogger->verbose("Accel X: %0.3f \t Y: %0.3f \t Z: %0.3f", data.accelX, data.accelY, data.accelZ);
diagLogger->verbose("Gyroscope X: %0.3f \t Y: %0.3f \t Z: %0.3f", data.gyroX, data.gyroY, data.gyroZ);

// --------------------------
// -- Update Mahony Filter --
Expand All @@ -154,6 +160,8 @@ void updateFusion() {
data.roll = mahony.getRoll();
data.pitch = mahony.getPitch();
data.yaw = mahony.getYaw();

diagLogger->verbose("Attitude R: %0.3f \t P: %0.3f \t Y: %0.3f", data.roll, data.pitch, data.yaw);

float _quat[4];
mahony.getQuaternionComps(_quat);
Expand All @@ -162,6 +170,8 @@ void updateFusion() {
data.quatY = _quat[2];
data.quatZ = _quat[3];

diagLogger->verbose("Quaternion W: %0.3f \t X: %0.3f \t Y: %0.3f \t Z: %0.3f", data.quatW, data.quatX, data.quatY, data.quatZ);

// ------------------------------------
// -- Calculate linear accelerations --
// ------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/ThetisLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@

// Miscellaneous functions
#include "misc/neopixel.h"
#include "misc/rtc.h"

// Filesystem functions
#include "filesystem/ThetisFS.h"
#include "filesystem/config.h"
#include "filesystem/logging.h"
#include "filesystem/logger.h"

// Status functions
#include "states.h"

// Sensor functions
#include "sensors/fusion.h"
#include "AHRS/fusion.h"

// Radio functions
#include "radios/gps.h"
Expand Down
8 changes: 2 additions & 6 deletions src/filesystem/ThetisFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ bool initSDCard() {
}

bool initSPIFFS() {
#ifdef SDCARD_DEBUG
DEBUG_SERIAL_PORT.print("Initializing SPIFFS filesystem...");
#endif
diagLogger->info("Initializing SPIFFS filesystem...");
bool _success = SPIFFS.begin();
#ifdef SDCARD_DEBUG
DEBUG_SERIAL_PORT.println(_success ? "done!" : "Failed to initialize filesystem!");
#endif
diagLogger->info(_success ? "done!" : "Failed to initialize filesystem!");
return _success;
}

Expand Down
1 change: 1 addition & 0 deletions src/filesystem/ThetisFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <SD.h>
#include <SPI.h>
#include <SPIFFS.h>
#include "logger.h"

bool initSDCard();
bool initSPIFFS();
Expand Down
50 changes: 24 additions & 26 deletions src/filesystem/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,43 @@ Config config;
* Opens the given file on the SD card.
* Returns true if successful, false if not.
*
* configFileName = the name of the configuration file on the SD card.
* configFilename = the name of the configuration file on the SD card.
*
* NOTE: SD.begin() must be called before calling our begin().
*/
bool Config::begin(const char *configFileName, uint8_t maxLineLength) {
bool Config::begin(const char *configFilename, uint8_t maxLineLength) {
diagLogger->info("Initializing configuration reader...");
_lineLength = 0;
_lineSize = 0;
_valueIndex = -1;
_atEnd = true;

/*
* Allocate a buffer for the current line.
*/
// Allocate a buffer for the current line.
_lineSize = maxLineLength + 1;
_line = (char *) malloc(_lineSize);
if (_line == 0) {
#ifdef CONFIG_DEBUG
DEBUG_SERIAL_PORT.println("out of memory");
#endif
diagLogger->fatal("Out of memory for configuration file!");
_atEnd = true;
return false;
}

/*
* To avoid stale references to configFileName
* we don't save it. To minimize memory use, we don't copy it.
*/

_file = SPIFFS.open(configFileName, FILE_READ);
// To avoid stale references to configFilename we don't save it. To minimize memory use, we don't copy it.
_file = SPIFFS.open(configFilename, FILE_READ);
if (!_file) {
#ifdef CONFIG_DEBUG
DEBUG_SERIAL_PORT.print("Could not open file: ");
DEBUG_SERIAL_PORT.println(configFileName);
#endif
diagLogger->fatal("Could not open file: %s", configFilename);
_atEnd = true;
return false;
}

// Initialize our reader
_atEnd = false;

diagLogger->info("done!");
return true;
}

void Config::loadConfigurations() {
diagLogger->info("Reading configurations...");
while (readNextSetting()) {
char _logBuf[64] = "";

Expand All @@ -76,6 +68,7 @@ void Config::loadConfigurations() {
strcpy(configData.HW_REVISION, getValue());
sprintf(_logBuf, "Setting device hardware revision to: %s", configData.HW_REVISION);
}

// ----- WiFi Settings -----
else if (nameIs("wifiEnable")) {
configData.wifiEnable = getBooleanValue();
Expand All @@ -100,6 +93,7 @@ void Config::loadConfigurations() {
sprintf(_logBuf, "Device will %s: %s", configData.wifiMode == 1 ? "require password" : "use password",
configData.password);
}

// ----- Sensor Settings -----
else if (nameIs("accelRange")) {
configData.accelRange = getIntValue();
Expand All @@ -125,25 +119,29 @@ void Config::loadConfigurations() {
configData.fusionUpdateRate = getIntValue();
sprintf(_logBuf, "Fusion update rate set to: %d Hz", configData.fusionUpdateRate);
}

// ----- Logging Settings -----
else if (nameIs("loggingUpdateRate")) {
configData.loggingUpdateRate= getIntValue();
sprintf(_logBuf, "Logging update rate set to: %d Hz", configData.loggingUpdateRate);
}
else if (nameIs("eventLogLevel")) {
configData.eventLogLevel = getIntValue();
sprintf(_logBuf, "Minimum event log level set to: %d", configData.eventLogLevel);
else if (nameIs("logPrintLevel")) {
configData.logPrintLevel = getIntValue();
sprintf(_logBuf, "Minimum event print log level set to: %d", configData.logPrintLevel);
}
else if (nameIs("logFileLevel")) {
configData.logFileLevel = getIntValue();
sprintf(_logBuf, "Minimum event file log level set to: %d", configData.logFileLevel);
}
else {
sprintf(_logBuf, "Unknown parameter discovered: %s", getName());
diagLogger->warn("Unknown parameter discovered: %s", getName());
continue;
}

// TODO: Make this an event log
#ifdef CONFIG_DEBUG
DEBUG_SERIAL_PORT.println(_logBuf);
#endif // CONFIG_DEBUG
diagLogger->info("%s", _logBuf);
}
end();
diagLogger->info("done!");
}

/*
Expand Down
4 changes: 3 additions & 1 deletion src/filesystem/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <Arduino.h>
#include <SPIFFS.h>
// #include <Ethernet.h>
#include "logger.h"

struct config_data_t {
uint8_t DEVICE_ID;
Expand All @@ -33,7 +34,8 @@ struct config_data_t {
uint8_t fusionUpdateRate;

uint8_t loggingUpdateRate;
uint8_t eventLogLevel;
uint8_t logPrintLevel;
uint8_t logFileLevel;
};

extern config_data_t configData;
Expand Down
Loading

0 comments on commit 0c7b95d

Please sign in to comment.