...
This commit is contained in:
@@ -5,66 +5,74 @@
|
||||
|
||||
/* MOUNT TELEMETRY OBJECT POSSIBLE GENERIC IMPLEMENTATION */
|
||||
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
|
||||
#include "mcc_mount_concepts.h"
|
||||
#include "mcc_mount_telemetry_astrom.h"
|
||||
|
||||
namespace mcc
|
||||
{
|
||||
enum class MccMountTelemetryErrorCode : int { ERROR_OK, ERROR_HARDWARE };
|
||||
|
||||
/* error category definition */
|
||||
|
||||
// error category
|
||||
struct MccMountTelemetryCategory : public std::error_category {
|
||||
MccMountTelemetryCategory() : std::error_category() {}
|
||||
|
||||
const char* name() const noexcept
|
||||
{
|
||||
return "ADC_GENERIC_DEVICE";
|
||||
}
|
||||
|
||||
std::string message(int ec) const
|
||||
{
|
||||
MccMountTelemetryErrorCode err = static_cast<MccMountTelemetryErrorCode>(ec);
|
||||
|
||||
switch (err) {
|
||||
case MccMountTelemetryErrorCode::ERROR_OK:
|
||||
return "OK";
|
||||
case MccMountTelemetryErrorCode::ERROR_HARDWARE:
|
||||
return "hardware request failed";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
static const MccMountTelemetryAstromTransformCategory& get()
|
||||
{
|
||||
static const MccMountTelemetryAstromTransformCategory constInst;
|
||||
return constInst;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline std::error_code make_error_code(MccMountTelemetryErrorCode ec)
|
||||
{
|
||||
return std::error_code(static_cast<int>(ec), MccMountTelemetryCategory::get());
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace mcc
|
||||
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
template <>
|
||||
class is_error_code_enum<mcc::MccMountTelemetryErrorCode> : public true_type
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
namespace mcc
|
||||
{
|
||||
|
||||
|
||||
namespace traits
|
||||
{
|
||||
/* DEFAULT TELEMETRY DATA CLASS */
|
||||
|
||||
/* enhanced telemetry data concept */
|
||||
/* in general it correspond to definitions used in astrometry engine (see its concept) */
|
||||
|
||||
template <typename T, typename ASTROM_ENGINE_T, typename PEC_T>
|
||||
concept mcc_mount_telemetry_enh_data_c = mcc_mount_telemetry_data_c<T> && requires(T t) {
|
||||
requires traits::mcc_astrom_engine_c<ASTROM_ENGINE_T>;
|
||||
requires traits::mcc_mount_pec_c<PEC_T>;
|
||||
|
||||
// check for types definitions and its consistency
|
||||
requires std::same_as<typename T::coord_t, typename ASTROM_ENGINE_T::coord_t>;
|
||||
|
||||
requires std::same_as<typename T::time_point_t, typename ASTROM_ENGINE_T::time_point_t>;
|
||||
|
||||
requires std::same_as<typename T::juldate_t,
|
||||
typename ASTROM_ENGINE_T::juldate_t>; // a type to represent Julian date
|
||||
|
||||
requires std::same_as<typename T::sideral_time_t,
|
||||
typename ASTROM_ENGINE_T::sideral_time_t>; // a type to represent sideral time
|
||||
|
||||
requires std::same_as<typename T::pa_t,
|
||||
typename ASTROM_ENGINE_T::pa_t>; // a type to represent parallactic angle
|
||||
// typename T::eo_t; // a type to represent equation of origins
|
||||
|
||||
requires std::same_as<decltype(t.jd), typename T::juldate_t>; // Julian date
|
||||
requires std::same_as<decltype(t.siderTime), typename T::sideral_time_t>; // sideral time
|
||||
requires std::same_as<decltype(t.mntRA), typename T::pa_t>; // parallactic angle
|
||||
|
||||
// encoder-measured (non-corrected for PCS) current mount position and moving speed (in radians, radians/s)
|
||||
// X - HA, Y - DEC for equatorial-type mount; X - AZ, Y - ALT for horizontal-type one
|
||||
requires std::same_as<decltype(t.mntPosX), typename T::coord_t>;
|
||||
requires std::same_as<decltype(t.mntPosY), typename T::coord_t>;
|
||||
requires std::same_as<decltype(t.mntRateX), typename T::coord_t>;
|
||||
requires std::same_as<decltype(t.mntRateY), typename T::coord_t>;
|
||||
|
||||
// current refraction coefficients
|
||||
requires std::same_as<decltype(t.currRefrCoeffs), typename PEC_T::pec_result_t>;
|
||||
// current refraction correction (for mntALT)
|
||||
requires std::same_as<decltype(t.currRefr), typename T::coord_t>;
|
||||
|
||||
// PEC (pointing error correction):
|
||||
// X - HA, Y - DEC for equatorial-type mount; X - AZ, Y - ALT for horizontal-type one
|
||||
requires std::same_as<decltype(t.pecX), typename T::coord_t>;
|
||||
requires std::same_as<decltype(t.pecY), typename T::coord_t>;
|
||||
};
|
||||
|
||||
} // namespace traits
|
||||
|
||||
|
||||
// default generic telemetry data definition
|
||||
template <traits::mcc_astrom_engine_c ASTROM_ENGINE_T, traits::mcc_mount_pec_c PEC_T>
|
||||
struct MccMountTelemetryData {
|
||||
typedef typename ASTROM_ENGINE_T::coord_t coord_t;
|
||||
@@ -78,6 +86,13 @@ struct MccMountTelemetryData {
|
||||
juldate_t jd; // Julian date
|
||||
sideral_time_t siderTime; // local apperant sideral time
|
||||
|
||||
// target sky point ICRS and current coordinates
|
||||
coord_t tagRA_ICRS, tagDEC_ICRS;
|
||||
coord_t tagRA, tagDEC;
|
||||
coord_t tagHA;
|
||||
coord_t tagAZ, tagALT;
|
||||
pa_t tagPA;
|
||||
|
||||
// encoder-measured current mount coordinates
|
||||
coord_t mntRA, mntDEC;
|
||||
coord_t mntHA;
|
||||
@@ -100,57 +115,58 @@ struct MccMountTelemetryData {
|
||||
coord_t pecX, pecY;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* DEFAULT TELEMETRY MANAGER CLASS */
|
||||
|
||||
template <traits::mcc_astrom_engine_c ASTROM_ENGINE_T,
|
||||
traits::mcc_mount_pec_c PEC_T,
|
||||
traits::mcc_mount_hardware_c HARDWARE_T,
|
||||
traits::mcc_mount_telemetry_enh_data_c<ASTROM_ENGINE_T, PEC_T> DATA_T =
|
||||
std::derived_from<MccMountTelemetryData<ASTROM_ENGINE_T, PEC_T>> DATA_T =
|
||||
MccMountTelemetryData<ASTROM_ENGINE_T, PEC_T>>
|
||||
class MccMountTelemetry
|
||||
class MccMountTelemetry : public MccMountTelemetryAstromTransform<ASTROM_ENGINE_T, PEC_T>
|
||||
{
|
||||
typedef MccMountTelemetryAstromTransform<ASTROM_ENGINE_T, PEC_T> base_t;
|
||||
|
||||
struct point_t {
|
||||
MccCoordPairKind coordPairKind;
|
||||
typename ASTROM_ENGINE_T::coord_t x, y;
|
||||
};
|
||||
|
||||
public:
|
||||
typedef ASTROM_ENGINE_T astrom_engine_t;
|
||||
typedef PEC_T pec_t;
|
||||
typedef HARDWARE_T hardware_t;
|
||||
|
||||
enum error_t : int { TEL_ERROR_OK = 0, TEL_ERROR_HARDWARE, TEL_ERROR_ASTROMETRY_COMP, TEL_ERROR_PEC };
|
||||
|
||||
// check for coordinate types consistency
|
||||
static_assert(std::convertible_to<typename hardware_t::coord_t, typename astrom_engine_t::coord_t>,
|
||||
"HARDWARE COORDINATE TYPE MUST BE CONVERTIBLE TO ASTROMETRY ENGINE ONE!");
|
||||
|
||||
static_assert(std::convertible_to<typename hardware_t::coord_t, typename pec_t::coord_t>,
|
||||
"HARDWARE COORDINATE TYPE MUST BE CONVERTIBLE TO PEC ONE!");
|
||||
|
||||
// static_assert(std::convertible_to<typename pec_t::coord_t, typename astrom_engine_t::coord_t>,
|
||||
// "ASTROMETRY ENGINE COORDINATE TYPE MUST BE CONVERTIBLE TO PEC ONE!");
|
||||
|
||||
// mandatory arithmetic operations
|
||||
static_assert( // for CIO-based apparent RA computation and PEC correction addition (see below)
|
||||
requires(typename astrom_engine_t::coord_t v1,
|
||||
typename astrom_engine_t::coord_t v2,
|
||||
typename pec_t::coord_t v3) {
|
||||
{ v1 + v2 } -> std::convertible_to<typename astrom_engine_t::coord_t>;
|
||||
{ v1 - v2 } -> std::convertible_to<typename astrom_engine_t::coord_t>;
|
||||
v1 += v3;
|
||||
},
|
||||
"ASTROMETRY ENGINE COORDINATE TYPE MUST DEFINE '+', '+=' AND '-' ARITHMETIC OPERATIONS!");
|
||||
|
||||
|
||||
// check for time point types consistency
|
||||
static_assert(std::convertible_to<typename hardware_t::time_point_t, typename astrom_engine_t::time_point_t>,
|
||||
"HARDWARE TIME-POINT TYPE MUST BE CONVERTIBLE TO ASTROMETRY ENGINE ONE!");
|
||||
|
||||
typedef DATA_T mount_telemetry_data_t;
|
||||
|
||||
typedef std::error_code error_t;
|
||||
|
||||
typedef std::function<void(mount_telemetry_data_t)> update_callback_func_t;
|
||||
typedef std::list<update_callback_func_t> update_callback_container_t;
|
||||
|
||||
MccMountTelemetry(astrom_engine_t& astrom_engine, pec_t& pec, hardware_t& hardware)
|
||||
: _astromEngine(astrom_engine), _pec(pec), _hardware(hardware)
|
||||
: base_t(astrom_engine, pec), _hardware(hardware)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~MccMountTelemetry() = default;
|
||||
|
||||
// update current data method
|
||||
|
||||
// set current target sky point coordinates (update all data)
|
||||
template <traits::mcc_celestial_point_c PointT>
|
||||
error_t setTarget(PointT tag_point)
|
||||
{
|
||||
std::lock_guard lock{_updateMutex};
|
||||
|
||||
auto err = this->toICRS(tag_point, _data.utc, _data.tagRA_ICRS, _data.tagDEC_ICRS);
|
||||
if (!err) {
|
||||
err = update();
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
error_t update()
|
||||
{
|
||||
mount_telemetry_data_t current_data;
|
||||
@@ -159,91 +175,133 @@ public:
|
||||
|
||||
auto err = _hardware.getPos(ax_pos);
|
||||
if (err) {
|
||||
// logging?!!!
|
||||
return TEL_ERROR_HARDWARE;
|
||||
}
|
||||
|
||||
_data.utc = ax_pos.time_point;
|
||||
_data.mntPosX = static_cast<typename astrom_engine_t::coord_t>(ax_pos.x);
|
||||
_data.mntPosY = static_cast<typename astrom_engine_t::coord_t>(ax_pos.y);
|
||||
_data.mntRateX = static_cast<typename astrom_engine_t::coord_t>(ax_pos.xrate);
|
||||
_data.mntRateY = static_cast<typename astrom_engine_t::coord_t>(ax_pos.yrate);
|
||||
|
||||
// compute Julian date
|
||||
auto ast_err = _astromEngine.greg2jul(_data.time_point, _data.jd);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
}
|
||||
|
||||
// compute local apparent sideral time
|
||||
ast_err = _astromEngine.apparentSiderTime(_data.jd, _data.siderTime, true);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
}
|
||||
|
||||
// compute equation of origins
|
||||
typename astrom_engine_t::eo_t eo;
|
||||
ast_err = _astromEngine.eqOrigins(_data.jd, eo);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
}
|
||||
|
||||
|
||||
|
||||
typename pec_t::pec_result_t pec_res;
|
||||
auto pec_err = _pec.compute(ax_pos.x, ax_pos.y, pec_res);
|
||||
if (pec_err) {
|
||||
return TEL_ERROR_PEC;
|
||||
}
|
||||
|
||||
if constexpr (mccIsEquatorialMount(pec_t::mountType)) {
|
||||
_data.mntHA = _data.mntPosX;
|
||||
_data.mntDEC = _data.mntPosY;
|
||||
_data.mntHA += pec_res.dx;
|
||||
_data.mntDEC += pec_res.dy;
|
||||
|
||||
ast_err = _astromEngine.hadec2azalt(_data.mntHA, _data.mntDEC, _data.mntAZ, _data.mntALT);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
if constexpr (std::same_as<error_t, decltype(err)>) {
|
||||
return err;
|
||||
}
|
||||
} else if constexpr (mccIsAltAzMount(pec_t::mountType)) {
|
||||
_data.mntAZ = _data.mntPosX;
|
||||
_data.mntALT = _data.mntPosY;
|
||||
_data.mntAZ += pec_res.dx;
|
||||
_data.mntALT += pec_res.dy;
|
||||
return MccMountTelemetryErrorCode::ERROR_HARDWARE;
|
||||
}
|
||||
|
||||
ast_err = _astromEngine.azalt2hadec(_data.mntAZ, _data.mntALT, _data.mntHA, _data.mntDEC);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
error_t res_err;
|
||||
typename astrom_engine_t::error_t ast_err{};
|
||||
|
||||
// times
|
||||
current_data.utc = ax_pos.time_point;
|
||||
// Julian date
|
||||
ast_err = this->_astromEngine.greg2jul(current_data.time_point, current_data.jd);
|
||||
if (!ast_err) {
|
||||
// local apparent sideral time
|
||||
ast_err = this->_astromEngine.apparentSiderTime(current_data.jd, current_data.siderTime, true);
|
||||
}
|
||||
|
||||
if (ast_err) {
|
||||
if constexpr (std::same_as<typename astrom_engine_t::error_t, error_t>) {
|
||||
return ast_err;
|
||||
}
|
||||
|
||||
return MccMountTelemetryAstromTransformErrorCode::ERROR_ASTROMETRY_COMP;
|
||||
}
|
||||
|
||||
|
||||
// encoder coordinates
|
||||
current_data.mntPosX = static_cast<typename astrom_engine_t::coord_t>(ax_pos.x);
|
||||
current_data.mntPosY = static_cast<typename astrom_engine_t::coord_t>(ax_pos.y);
|
||||
current_data.mntRateX = static_cast<typename astrom_engine_t::coord_t>(ax_pos.xrate);
|
||||
current_data.mntRateY = static_cast<typename astrom_engine_t::coord_t>(ax_pos.yrate);
|
||||
|
||||
|
||||
|
||||
// correction for PEC
|
||||
if constexpr (base_t::equatorialMount) {
|
||||
res_err = this->toApparent(point_t{.coordPairKind = MccCoordPairKind::COORDS_KIND_XY,
|
||||
.x = current_data.mntPosX,
|
||||
.y = current_data.mntPosY},
|
||||
current_data.utc, current_data.mntHA, current_data.mntDEC);
|
||||
if (!res_err) {
|
||||
ast_err = this->_astromEngine.hadec2azalt(current_data.mntHA, current_data.mntDEC, current_data.mntAZ,
|
||||
current_data.mntALT);
|
||||
}
|
||||
} else if constexpr (base_t::altAzMount) {
|
||||
res_err = this->toApparent(point_t{.coordPairKind = MccCoordPairKind::COORDS_KIND_XY,
|
||||
.x = current_data.mntPosX,
|
||||
.y = current_data.mntPosY},
|
||||
current_data.utc, current_data.mntAZ, current_data.mntALT);
|
||||
if (!res_err) {
|
||||
ast_err = this->_astromEngine.azalt2hadec(current_data.mntAZ, current_data.mntALT, current_data.mntHA,
|
||||
current_data.mntDEC);
|
||||
}
|
||||
} else {
|
||||
static_assert(false, "UNSUPPORTED MOUNT TYPE!");
|
||||
}
|
||||
|
||||
// compute CIO-based apparent RA
|
||||
_data.mntRA = _data.siderTime - _data.mntHA + eo;
|
||||
|
||||
// compute PA
|
||||
ast_err = _astromEngine.hadec2pa(_data.mntHA, _data.mntDEC, _data.mntPA);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
if (res_err) {
|
||||
return res_err;
|
||||
}
|
||||
|
||||
ast_err = _astromEngine.refraction(_data.currRefrCoeffs);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
// PA angle
|
||||
if (!ast_err) {
|
||||
ast_err = this->_astromEngine.hadec2pa(current_data.mntHA, current_data.mntDEC, current_data.mntPA);
|
||||
if (!ast_err) {
|
||||
// target coordinates (assuming its ICRS RA-DEC are already given or computed)
|
||||
typename mount_telemetry_data_t::eo_t eo;
|
||||
ast_err = this->_astromEngine.icrs2obs(current_data.tagRA_ICRS, current_data.tagDEC_ICRS,
|
||||
current_data.jd, current_data.tagRA, current_data.tagDEC,
|
||||
current_data.tagHA, current_data.tagAZ, current_data.tagALT, eo);
|
||||
|
||||
if (!ast_err) {
|
||||
current_data.mntRA = current_data.siderTime - current_data.mntHA + eo; // CIO based mount RA
|
||||
ast_err = this->_astromEngine.hadec2pa(current_data.tagHA, current_data.tagDEC, current_data.tagPA);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ast_err = _astromEngine.refractCorrection(_data.mntALT, _data.currRefrCoeffs, _data.currRefr);
|
||||
if (ast_err) {
|
||||
return TEL_ERROR_ASTROMETRY_COMP;
|
||||
if constexpr (std::same_as<typename astrom_engine_t::error_t, error_t>) {
|
||||
return ast_err;
|
||||
}
|
||||
|
||||
return MccMountTelemetryAstromTransformErrorCode::ERROR_ASTROMETRY_COMP;
|
||||
}
|
||||
|
||||
std::lock_guard lock{_updateMutex};
|
||||
|
||||
_data = std::move(current_data);
|
||||
|
||||
return TEL_ERROR_OK;
|
||||
std::lock_guard cl_lock{_callbackMutex};
|
||||
|
||||
// callbacks will be invoked with its own copy of telemetry data!
|
||||
for (auto& func : _callbackFuncs) {
|
||||
std::thread t([this, &func] {
|
||||
auto d = _data;
|
||||
func(std::move(d));
|
||||
});
|
||||
t.detach();
|
||||
}
|
||||
|
||||
return MccMountTelemetryErrorCode::ERROR_OK;
|
||||
}
|
||||
|
||||
|
||||
update_callback_container_t::iterator addCallbackFunc(update_callback_func_t func)
|
||||
{
|
||||
std::lock_guard lock{_callbackMutex};
|
||||
|
||||
return _callbackFuncs.emplace_back(std::move(func));
|
||||
}
|
||||
|
||||
void delCallbackFunc(update_callback_container_t::iterator iter)
|
||||
{
|
||||
std::lock_guard lock{_callbackMutex};
|
||||
|
||||
if (_callbackFuncs.size()) {
|
||||
_callbackFuncs.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
void clearCallbackFuncs()
|
||||
{
|
||||
std::lock_guard lock{_callbackMutex};
|
||||
|
||||
_callbackFuncs.clear();
|
||||
}
|
||||
|
||||
error_t data(mount_telemetry_data_t& data)
|
||||
@@ -252,32 +310,16 @@ public:
|
||||
|
||||
data = std::move(_data);
|
||||
|
||||
return TEL_ERROR_OK;
|
||||
}
|
||||
|
||||
|
||||
std::string_view errorString(error_t err) const
|
||||
{
|
||||
if (err == TEL_ERROR_OK) {
|
||||
return "OK";
|
||||
} else if (err == TEL_ERROR_ASTROMETRY_COMP) {
|
||||
return "astrometry computation error";
|
||||
} else if (err == TEL_ERROR_PEC) {
|
||||
return "PEC computation error";
|
||||
} else if (err == TEL_ERROR_HARDWARE) {
|
||||
return "hardware request error";
|
||||
} else {
|
||||
return "unknown error";
|
||||
}
|
||||
return MccMountTelemetryErrorCode::ERROR_OK;
|
||||
}
|
||||
|
||||
protected:
|
||||
mount_telemetry_data_t _data{};
|
||||
astrom_engine_t& _astromEngine;
|
||||
pec_t& _pec;
|
||||
hardware_t& _hardware;
|
||||
update_callback_container_t _callbackFuncs{};
|
||||
|
||||
std::mutex _updateMutex;
|
||||
std::mutex _callbackMutex;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user