documentation checked and edit
for all files in lib folder
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
/**
|
||||
* @file calibrateCompass.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-23
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#include "calibrateCompass.h"
|
||||
|
||||
CalibrateCompass::CalibrateCompass(QMC5883LCompass *compass)
|
||||
: compass{compass}
|
||||
{
|
||||
this->state = State::Ready;
|
||||
this->clearData();
|
||||
this->activateOnlyChilds();
|
||||
}
|
||||
|
||||
void CalibrateCompass::runAsChild()
|
||||
{
|
||||
if (this->state != State::Calibrating)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool changed = false;
|
||||
|
||||
this->compass->read();
|
||||
const int xAxis = this->compass->getX();
|
||||
const int yAxis = this->compass->getY();
|
||||
const int zAxis = this->compass->getZ();
|
||||
|
||||
if (xAxis < this->data.data[0][0])
|
||||
{
|
||||
this->data.data[0][0] = xAxis;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (xAxis > this->data.data[0][1])
|
||||
{
|
||||
this->data.data[0][1] = xAxis;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (yAxis < this->data.data[1][0])
|
||||
{
|
||||
this->data.data[1][0] = yAxis;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (yAxis > this->data.data[1][1])
|
||||
{
|
||||
this->data.data[1][1] = yAxis;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (zAxis < this->data.data[2][0])
|
||||
{
|
||||
this->data.data[2][0] = zAxis;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (zAxis > this->data.data[2][1])
|
||||
{
|
||||
this->data.data[2][1] = zAxis;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed)
|
||||
{
|
||||
this->lastChange = millis();
|
||||
}
|
||||
|
||||
if (millis() - this->lastChange > this->maxTimeWithoutChange)
|
||||
{
|
||||
this->state = State::Finished;
|
||||
this->checkDataValidity();
|
||||
}
|
||||
}
|
||||
|
||||
void CalibrateCompass::run() {}
|
||||
|
||||
void CalibrateCompass::start()
|
||||
{
|
||||
if (this->state != State::Ready)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this->clearData();
|
||||
this->state = State::Calibrating;
|
||||
this->compass->clearCalibration();
|
||||
this->lastChange = millis();
|
||||
}
|
||||
|
||||
void CalibrateCompass::useData()
|
||||
{
|
||||
if (!this->dataValid)
|
||||
{
|
||||
std::cout << "CalibrateCompass::useData - Data not valid" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
this->compass->setCalibration(this->data.data[0][0],
|
||||
this->data.data[0][1],
|
||||
this->data.data[1][0],
|
||||
this->data.data[1][1],
|
||||
this->data.data[2][0],
|
||||
this->data.data[2][1]);
|
||||
|
||||
std::cout << "CalibrateCompass::useData " << *this << std::endl;
|
||||
}
|
||||
|
||||
void CalibrateCompass::removeCalibration()
|
||||
{
|
||||
this->compass->clearCalibration();
|
||||
}
|
||||
|
||||
void CalibrateCompass::reset()
|
||||
{
|
||||
this->clearData();
|
||||
this->state = State::Ready;
|
||||
}
|
||||
|
||||
void CalibrateCompass::saveData()
|
||||
{
|
||||
if (!this->dataValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Preferences preferences;
|
||||
preferences.begin("compass", false);
|
||||
|
||||
preferences.putInt("xLow", this->data.data[0][0]);
|
||||
preferences.putInt("xHigh", this->data.data[0][1]);
|
||||
preferences.putInt("yLow", this->data.data[1][0]);
|
||||
preferences.putInt("yHigh", this->data.data[1][1]);
|
||||
preferences.putInt("zLow", this->data.data[2][0]);
|
||||
preferences.putInt("zHigh", this->data.data[2][1]);
|
||||
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void CalibrateCompass::loadData()
|
||||
{
|
||||
Preferences preferences;
|
||||
preferences.begin("compass", true);
|
||||
|
||||
this->data.data[0][0] = preferences.getInt("xLow", 0);
|
||||
this->data.data[0][1] = preferences.getInt("xHigh", 0);
|
||||
this->data.data[1][0] = preferences.getInt("yLow", 0);
|
||||
this->data.data[1][1] = preferences.getInt("yHigh", 0);
|
||||
this->data.data[2][0] = preferences.getInt("zLow", 0);
|
||||
this->data.data[2][1] = preferences.getInt("zHigh", 0);
|
||||
|
||||
preferences.end();
|
||||
this->checkDataValidity();
|
||||
}
|
||||
|
||||
void CalibrateCompass::clearData()
|
||||
{
|
||||
for (uint8_t i = 0; i < 3; i++)
|
||||
{
|
||||
this->data.data[i][0] = 0;
|
||||
this->data.data[i][1] = 0;
|
||||
}
|
||||
this->dataValid = false;
|
||||
}
|
||||
|
||||
void CalibrateCompass::checkDataValidity()
|
||||
{
|
||||
int sum = 0;
|
||||
for (uint8_t i = 0; i < 3; i++)
|
||||
{
|
||||
if (this->data.data[i][0] > INT16_MAX || this->data.data[i][0] < INT16_MIN || this->data.data[i][1] > INT16_MAX || this->data.data[i][1] < INT16_MIN)
|
||||
{
|
||||
this->dataValid = false;
|
||||
return;
|
||||
}
|
||||
sum += this->data.data[i][0];
|
||||
sum += this->data.data[i][1];
|
||||
}
|
||||
this->dataValid = static_cast<bool>(sum);
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const CalibrateCompass &caliComp)
|
||||
{
|
||||
stream << "(";
|
||||
stream << caliComp.data.data[0][0];
|
||||
stream << ", ";
|
||||
stream << caliComp.data.data[0][1];
|
||||
stream << ", ";
|
||||
stream << caliComp.data.data[1][0];
|
||||
stream << ", ";
|
||||
stream << caliComp.data.data[1][1];
|
||||
stream << ", ";
|
||||
stream << caliComp.data.data[2][0];
|
||||
stream << ", ";
|
||||
stream << caliComp.data.data[2][1];
|
||||
stream << ")";
|
||||
return stream;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @file calibrateCompass.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief Contains a class to calibrate the compass module
|
||||
* @version 0.1
|
||||
* @date 2023-05-23
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QMC5883LCompass.h>
|
||||
#include <Preferences.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "component.h"
|
||||
|
||||
/**
|
||||
* @brief A Class to calibrate the compass
|
||||
*
|
||||
* This class reads the raw values of the compass while
|
||||
* the rove have to be moved. The lowest and highest values
|
||||
* are used to calibrate the compass to the current location.
|
||||
*/
|
||||
class CalibrateCompass : public Component
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief State of the calibration process
|
||||
*/
|
||||
enum State
|
||||
{
|
||||
Ready,
|
||||
Calibrating,
|
||||
Finished
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Type for calibration data
|
||||
*
|
||||
* The data consist of 6 values. For each for the 3 axis
|
||||
* are to integer needed.
|
||||
*/
|
||||
struct CalibrationData
|
||||
{
|
||||
int data[3][2];
|
||||
};
|
||||
|
||||
CalibrateCompass(QMC5883LCompass *compass);
|
||||
|
||||
/**
|
||||
* @brief Starts the calibration
|
||||
*/
|
||||
void start();
|
||||
|
||||
/**
|
||||
* @brief Use the measured calibration data
|
||||
*/
|
||||
void useData();
|
||||
|
||||
/**
|
||||
* @brief Remove the measured calibration data
|
||||
*/
|
||||
void removeCalibration();
|
||||
|
||||
/**
|
||||
* @brief Reset the calibration process to start again
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* @brief Save the measured calibration data to the flash
|
||||
*/
|
||||
void saveData();
|
||||
|
||||
/**
|
||||
* @brief Load the measured calibration data from the flash
|
||||
*
|
||||
*/
|
||||
void loadData();
|
||||
|
||||
State getState() const { return this->state; }
|
||||
CalibrationData getCalibrationData() const { return this->data; }
|
||||
|
||||
/**
|
||||
* @brief Makes the calibration data printable with std::cout()
|
||||
*
|
||||
* @param stream
|
||||
* @param caliComp
|
||||
* @return std::ostream&
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &stream, const CalibrateCompass &caliComp);
|
||||
|
||||
private:
|
||||
void runAsChild() override;
|
||||
void run() override;
|
||||
void checkDataValidity();
|
||||
|
||||
QMC5883LCompass *compass;
|
||||
State state = State::Ready;
|
||||
CalibrationData data{};
|
||||
|
||||
void clearData();
|
||||
|
||||
bool dataValid = false;
|
||||
const uint16_t maxTimeWithoutChange = 10000;
|
||||
uint32_t lastChange = 0;
|
||||
};
|
||||
Reference in New Issue
Block a user