mountcontrol/cxx/mcc_slew_model.h
2025-07-25 12:09:38 +03:00

376 lines
13 KiB
C++

#pragma once
/* MOUNT CONTROL COMPONENTS LIBRARY */
/* A VERY SIMPLE SLEW MODEL GENERIC IMPLEMENTATION */
#include "mcc_mount_concepts.h"
#include "mcc_mount_telemetry.h"
#include "mcc_slew_guiding_model_common.h"
namespace mcc
{
enum class MccSimpleSlewModelErrorCode : int {
ERROR_OK,
ERROR_UNSUPPORTED_COORD_PAIR,
ERROR_IN_PROHIBITED_ZONE,
ERROR_ASTROM_COMP,
ERROR_TELEMETRY_DATA,
ERROR_PEC_COMP,
ERROR_HARDWARE_SETPOS,
ERROR_HARDWARE_GETPOS,
ERROR_SLEW_TIMEOUT
};
} // namespace mcc
namespace std
{
template <>
class is_error_code_enum<mcc::MccSimpleSlewModelErrorCode> : public true_type
{
};
} // namespace std
namespace mcc
{
/* error category definition */
// error category
struct MccSimpleSlewModelCategory : public std::error_category {
MccSimpleSlewModelCategory() : std::error_category() {}
const char* name() const noexcept
{
return "ADC_GENERIC_DEVICE";
}
std::string message(int ec) const
{
MccSimpleSlewModelErrorCode err = static_cast<MccSimpleSlewModelErrorCode>(ec);
switch (err) {
case MccSimpleSlewModelErrorCode::ERROR_OK:
return "OK";
case MccSimpleSlewModelErrorCode::ERROR_UNSUPPORTED_COORD_PAIR:
return "slew model: unsupported coordinate pair";
case MccSimpleSlewModelErrorCode::ERROR_IN_PROHIBITED_ZONE:
return "slew model: position is in prohibited zone";
case MccSimpleSlewModelErrorCode::ERROR_ASTROM_COMP:
return "slew model: cannot perform astrometrical computations";
case MccSimpleSlewModelErrorCode::ERROR_TELEMETRY_DATA:
return "slew model: cannot get telemetry data";
case MccSimpleSlewModelErrorCode::ERROR_PEC_COMP:
return "slew model: cannot compute PEC corrections";
case MccSimpleSlewModelErrorCode::ERROR_HARDWARE_SETPOS:
return "slew model: cannot set position";
case MccSimpleSlewModelErrorCode::ERROR_HARDWARE_GETPOS:
return "slew model: cannot get position";
default:
return "UNKNOWN";
}
}
static const MccSimpleSlewModelCategory& get()
{
static const MccSimpleSlewModelCategory constInst;
return constInst;
}
};
inline std::error_code make_error_code(MccSimpleSlewModelErrorCode ec)
{
return std::error_code(static_cast<int>(ec), MccSimpleSlewModelCategory::get());
}
/*
* It is very simple slew model!
* There are no any complex routes (bypass of prohibited),
* just a strait path from current point to target
*
*/
template <traits::mcc_logger_c LoggerT = MccNullLogger>
class MccSimpleSlewModel : public LoggerT
{
public:
using LoggerT::logDebug;
using LoggerT::logError;
using LoggerT::logInfo;
using LoggerT::logMessage;
using LoggerT::logWarn;
typedef std::error_code error_t;
static constexpr size_t defaultAdjustSuccessCycles = 5;
struct slew_point_t : MccCelestialPoint {
// target-mount coordinate difference to start adjusting of slewing (in radians)
coord_t adjustCoordDiff{(double)MccAngle{10.0_degs}};
// coordinates difference to stop slewing (in radians)
coord_t slewToleranceRadius{(double)MccAngle{5.0_arcsecs}};
// coordinates polling interval in seconds
std::chrono::duration<double> coordPollingInterval{0.1};
bool stopAfterSlew{false};
std::chrono::seconds slewTimeout{3600};
coord_t slewXRate{0.0}; // maximal slewing rate (0 means move with maximal allowed rate)
coord_t slewYRate{0.0}; // maximal slewing rate (0 means move with maximal allowed rate)
coord_t adjustXRate{
(double)MccAngle{5.0_arcmins}}; // maximal adjusting rate (a rate at the final slewing stage)
coord_t adjustYRate{
(double)MccAngle{5.0_arcmins}}; // maximal adjusting rate (a rate at the final slewing stage)
// number of consecutive measurements within slewPrecision radius to stop adjusting of slewing
size_t withinToleranceCycleNumber{10};
// maximal allowed number of adjusting cycles
size_t maxAdjustingCycleNumber{100};
};
template <traits::mcc_mount_controls_c MOUNT_CONTROLS_T, typename... LoggerCtorArgTs>
MccSimpleSlewModel(MOUNT_CONTROLS_T& mount_controls, LoggerCtorArgTs&&... ctor_args)
requires(!std::same_as<LoggerT, MccNullLogger>)
: LoggerT(std::forward<LoggerCtorArgTs>(ctor_args)...)
{
logDebug(std::format("Create 'MccSimpleSlewModel' class instance ({})", (void*)this));
init(mount_controls);
}
template <traits::mcc_mount_controls_c MOUNT_CONTROLS_T>
MccSimpleSlewModel(MOUNT_CONTROLS_T& mount_controls)
requires(std::same_as<LoggerT, MccNullLogger>)
{
init(mount_controls);
}
virtual ~MccSimpleSlewModel()
{
logDebug(std::format("Delete 'MccSimpleSlewModel' class instance ({})", (void*)this));
}
error_t slew(slew_point_t pars)
{
error_t res_err = _slewFunc(std::move(pars));
return res_err;
}
protected:
std::function<error_t(const slew_point_t&)> _slewFunc{};
void init(auto& mount_controls)
{
// deduce controls types
using astrom_engine_t = decltype(mount_controls.astrometryEngine);
using hardware_t = decltype(mount_controls.hardware);
using pec_t = decltype(mount_controls.PEC);
using telemetry_t = decltype(mount_controls.telemetry);
static_assert(std::derived_from<telemetry_t, MccMountTelemetry<astrom_engine_t, pec_t, hardware_t,
typename telemetry_t::mount_telemetry_data_t>>,
"TELEMETRY CLASS MUST BE A DESCENDANT OF 'MccMountTelemetry' ONE!");
using tpl_pz_t = decltype(mount_controls.prohibitedZones);
static constexpr size_t Nzones = std::tuple_size_v<tpl_pz_t>;
const auto p_mount_controls = &mount_controls;
_slewFunc = [p_mount_controls, this](slew_point_t slew_point) {
auto& astrom_engine = p_mount_controls->astrometryEngine;
auto& hardware = p_mount_controls->hardware;
auto& pec = p_mount_controls->PEC;
auto& telemetry = p_mount_controls->telemetry;
using coord_t = typename astrom_engine_t::coord_t;
using jd_t = typename astrom_engine_t::juldate_t;
typename hardware_t::axes_pos_t ax_pos;
error_t res_err;
typename astrom_engine_t::error_t ast_err;
typename pec_t::error_t pec_err;
typename telemetry_t::error_t t_err;
typename telemetry_t::mount_telemetry_data_t t_data;
coord_t ra_icrs, dec_icrs;
if (slew_point.adjustSuccessCycles == 0) {
slew_point.adjustSuccessCycles = 5;
}
// first, compute encoder coordinates
ax_pos.time_point = astrom_engine_t::timePointNow();
t_err = telemetry.toHardware(slew_point, ax_pos.time_point, ax_pos.x, ax_pos.y);
if (!t_err) {
// SETUP TARGET SKY POINT
t_err = telemetry.setTarget(slew_point);
}
if (t_err) {
if constexpr (std::same_as<decltype(t_err), error_t>) {
logError(std::format("An telemetry error occured: code = {} ({})", t_err.value(), t_err.message()));
return t_err;
} else {
if constexpr (traits::mcc_formattable<decltype(t_err)>) {
logError(std::format("An telemetry error occured: code = {}", t_err));
}
return MccSimpleSlewModelErrorCode::ERROR_TELEMETRY_DATA;
}
}
// start moving the mount (it is assumed this is asynchronous operation!!!)
ax_pos.xrate = slew_point.slewXRate;
ax_pos.yrate = slew_point.slewYRate;
typename hardware_t::error_t hw_err = hardware->setPos(ax_pos);
if (hw_err) {
if constexpr (std::same_as<decltype(hw_err), error_t>) {
logError(
std::format("An hardware error occured: code = {} ({})", hw_err.value(), hw_err.message()));
return hw_err;
} else {
if constexpr (traits::mcc_formattable<decltype(hw_err)>) {
logError(std::format("An hardware error occured: code = {}", hw_err));
}
return MccSimpleSlewModelErrorCode::ERROR_HARDWARE_SETPOS;
}
}
typename hardware_t::axes_pos_t::time_point_t prev_time_point{};
auto adj_ax_pos = ax_pos; // to prevent possible effects in hardware 'setPos' method
adj_ax_pos.xrate = slew_point.adjustXRate;
adj_ax_pos.yrate = slew_point.adjustYRate;
typename telemetry_t::mount_telemetry_data_t::coord_t xr, yr, coord_diff2,
adj_rad2 = slew_point.adjustCoordDiff * slew_point.adjustCoordDiff,
tol_rad2 = slew_point.slewToleranceRadius * slew_point.slewToleranceRadius;
std::array<bool, Nzones> in_zone_flag;
size_t i_adj_cycle = 0;
size_t i_in_tol_cycle = 0;
bool in_adj_mode = false;
auto coord_diff_func = [](auto& t_data) {
typename telemetry_t::mount_telemetry_data_t::coord_t xr, yr;
if constexpr (mccIsEquatorialMount(pec_t::mountType)) {
xr = t_data.tagRA - t_data.mntHA;
yr = t_data.tagDEC - t_data.mntDEC;
} else if constexpr (mccIsAltAzMount(pec_t::mountType)) {
xr = t_data.tagAZ - t_data.mntAZ;
yr = t_data.tagALT - t_data.mntALT;
} else {
static_assert(false, "UNSUPPORTED MOUNT TYPE!");
}
return xr * xr + yr * yr;
};
auto cycle_func = [&](auto t_data) mutable {
// check for prohibited zones
auto t_err = mccCheckInZonePZTuple(*telemetry, p_mount_controls->prohibitedZones, in_zone_flag);
if (t_err) {
if constexpr (std::same_as<decltype(t_err), error_t>) {
logError(
std::format("An telemetry error occured: code = {} ({})", t_err.value(), t_err.message()));
res_err = t_err;
} else {
if constexpr (traits::mcc_formattable<decltype(t_err)>) {
logError(std::format("An telemetry error occured: code = {}", t_err));
}
res_err = MccSimpleSlewModelErrorCode::ERROR_TELEMETRY_DATA;
}
return;
}
// t_data was updated in caller!!!
coord_diff2 = coord_diff_func(t_data);
if (coord_diff2 < adj_rad2) { // adjusting mode
in_adj_mode = true;
hw_err = hardware->setPos(adj_ax_pos);
if (!hw_err) {
++i_adj_cycle;
if (coord_diff2 < tol_rad2) {
++i_in_tol_cycle;
if (i_in_tol_cycle == slew_point.withinToleranceCycleNumber) {
res_err = MccSimpleSlewModelErrorCode::ERROR_OK;
return;
}
}
if (i_adj_cycle == slew_point.maxAdjustingCycleNumber) {
// res_err = max iter namber was exceeded
return;
}
} else {
if constexpr (std::same_as<decltype(hw_err), error_t>) {
logError(std::format("An hardware error occured: code = {} ({})", hw_err.value(),
hw_err.message()));
res_err = hw_err;
} else {
if constexpr (traits::mcc_formattable<decltype(hw_err)>) {
logError(std::format("An hardware error occured: code = {}", hw_err));
}
res_err = MccSimpleSlewModelErrorCode::ERROR_HARDWARE_SETPOS;
}
return;
}
} else {
if (in_adj_mode) { // ?!!!!!!!!!!!!!
}
}
};
auto start_poll_tm = std::chrono::steady_clock::now();
// NOTE: TARGET COORDINATES WILL BE UPDATED FOR CURRENT TIME-POINT IN TELEMETRY-CLASS!!!
auto iter = telemetry.addCallbackFunc(cycle_func);
while (true) {
if ((std::chrono::steady_clock::now() - start_poll_tm) > slew_point.slewTimeout) {
logError("Waiting time for completion of slewing expired!");
return MccSimpleSlewModelErrorCode::ERROR_SLEW_TIMEOUT;
}
}
return MccSimpleSlewModelErrorCode::ERROR_OK;
};
}
};
// static_assert(traits::mcc_slew_model_c<>);
} // namespace mcc