This commit is contained in:
2025-07-31 22:47:26 +03:00
parent 9fbd858086
commit 750d29ceb9
8 changed files with 240 additions and 415 deletions

View File

@@ -7,9 +7,9 @@
#include "mcc_mount_concepts.h"
#include "mcc_mount_telemetry.h"
#include "mcc_slew_guiding_model_common.h"
namespace mcc
{
@@ -24,6 +24,7 @@ enum class MccSimpleGuidingModelErrorCode : int {
ERROR_INVALID_CONTEXT_PARAM,
ERROR_INVALID_THRESH,
ERROR_INVALID_CORR_RANGE,
ERROR_GUIDING_STOPPED
};
} // namespace mcc
@@ -48,10 +49,7 @@ namespace mcc
struct MccSimpleGuidingModelCategory : public std::error_category {
MccSimpleGuidingModelCategory() : std::error_category() {}
const char* name() const noexcept
{
return "ADC_GENERIC_DEVICE";
}
const char* name() const noexcept { return "ADC_GENERIC_DEVICE"; }
std::string message(int ec) const
{
@@ -76,6 +74,8 @@ struct MccSimpleGuidingModelCategory : public std::error_category {
return "guiding model: invalid guiding residual threshold";
case MccSimpleGuidingModelErrorCode::ERROR_INVALID_CORR_RANGE:
return "guiding model: invalid guiding correction range";
case MccSimpleGuidingModelErrorCode::ERROR_GUIDING_STOPPED:
return "guiding model: stopped";
default:
return "UNKNOWN";
}
@@ -136,34 +136,30 @@ public:
typedef std::error_code error_t;
struct guiding_context_t {
double corrThresh{MccAngle("00:00:00.2"_dms)}; // correction threshold
double correctionRange[2]{MccAngle("00:00:00.5"_dms), MccAngle("00:00:05"_dms)};
std::chrono::duration<double> predictedTrackDuration{10.0}; // 10 seconds
std::chrono::duration<double> predictedTrackResolution{0.1}; // 0.1 seconds
};
struct guiding_point_t : MccCelestialPoint {
coord_t corrThresh{(double)MccAngle("00:00:00.2"_dms)}; // correction threshold
coord_t correctionRange[2]{(double)MccAngle(0.5_arcsecs), (double)MccAngle(5.0_arcsecs)};
// timeout to wait telemetry update (in seconds, as floating-point)
std::chrono::duration<double> telemetryUpdateTimeout{1.0};
};
template <traits::mcc_mount_controls_c MOUNT_CONTROLS_T, typename... LoggerCtorArgTs>
MccSimpleGuidingModel(MOUNT_CONTROLS_T& mount_controls, guiding_context_t context, LoggerCtorArgTs&&... ctor_args)
MccSimpleGuidingModel(MOUNT_CONTROLS_T& mount_controls, LoggerCtorArgTs&&... ctor_args)
requires(!std::same_as<LoggerT, MccNullLogger>)
: LoggerT(std::forward<LoggerCtorArgTs>(ctor_args)...)
{
logDebug(std::format("Create 'MccSimpleGuidingModel' class instance ({})", (void*)this));
init(mount_controls, std::move(context));
init(mount_controls);
}
template <traits::mcc_mount_controls_c MOUNT_CONTROLS_T>
MccSimpleGuidingModel(MOUNT_CONTROLS_T& mount_controls, guiding_context_t context)
MccSimpleGuidingModel(MOUNT_CONTROLS_T& mount_controls)
requires(std::same_as<LoggerT, MccNullLogger>)
{
init(mount_controls, std::move(context));
init(mount_controls);
}
virtual ~MccSimpleGuidingModel()
@@ -172,98 +168,46 @@ public:
}
error_t guiding(guiding_point_t guiding_point)
{
return _guidingFunc(std::move(guiding_point));
}
error_t guiding(guiding_point_t guiding_point) { return _guidingFunc(std::move(guiding_point)); }
error_t stopGuiding(bool off)
{
_doCorrection = off;
}
error_t stopGuiding(bool off) { _doCorrection = off; }
bool inGuiding()
{
return _doCorrection;
}
bool inGuiding() { return _doCorrection; }
error_t stop() { return MccSimpleGuidingModelErrorCode::ERROR_OK; }
protected:
std::function<error_t(guiding_point_t)> _guidingFunc{};
std::atomic_bool _doCorrection{true};
std::atomic_bool _stopRequested{false};
error_t init(auto& mount_controls, guiding_context_t context)
error_t 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>;
size_t predicted_Npoints = context.predictedTrackDuration / context.predictedTrackResolution;
if (predicted_Npoints == 0) {
return MccSimpleGuidingModelErrorCode::ERROR_INVALID_CONTEXT_PARAM;
}
auto resi_thresh2 = context.corrThresh * context.corrThresh;
if (utils::isEqual(resi_thresh2, 0.0)) {
return MccSimpleGuidingModelErrorCode::ERROR_INVALID_THRESH;
}
const auto p_mount_controls = &mount_controls;
auto check_zones = [p_mount_controls, this]() {
return [this]<size_t... Is>(std::index_sequence<Is...>) {
error_t ret;
_guidingFunc = [p_mount_controls, this](guiding_point_t guiding_point) {
_stopRequested = false;
(
[&ret]() {
if constexpr (Is > 0) {
if (ret) {
return;
}
}
typename telemetry_t::mount_telemetry_data_t tdata;
auto tel_err = p_mount_controls->telemetry.data(tdata);
if (tel_err) {
if constexpr (std::same_as<decltype(tel_err), error_t>) {
ret = tel_err;
} else {
ret = MccSimpleGuidingModelErrorCode::ERROR_TELEMETRY_DATA;
}
} else {
ret = std::get<Is>(p_mount_controls->prohibitedZones).inZone(tdata)
? MccSimpleGuidingModelErrorCode::ERROR_IN_PROHIBITED_ZONE
: MccSimpleGuidingModelErrorCode::ERROR_OK;
if (ret) {
auto log_str = std::format("given coordinates are in prohibited zone '{}'",
std::get<Is>(p_mount_controls->prohibitedZones).name());
logError(log_str);
}
}
}(),
...);
return ret;
}(std::make_index_sequence<Nzones>{});
};
_guidingFunc = [p_mount_controls, context = std::move(context), predicted_Npoints, this](
this auto&& self, guiding_point_t guiding_point) {
if (context.correctionRange[0] >= context.correctionRange[1]) {
if (guiding_point.correctionRange[0] >= guiding_point.correctionRange[1]) {
return MccSimpleGuidingModelErrorCode::ERROR_INVALID_THRESH;
}
auto low_corr_limit = context.correctionRange[0] * context.correctionRange[0];
auto high_corr_limit = context.correctionRange[1] * context.correctionRange[1];
auto low_corr_limit = guiding_point.correctionRange[0] * guiding_point.correctionRange[0];
auto high_corr_limit = guiding_point.correctionRange[1] * guiding_point.correctionRange[1];
auto& astrom_engine = p_mount_controls->astrometryEngine;
auto& hardware = p_mount_controls->hardware;
@@ -283,206 +227,91 @@ protected:
typename telemetry_t::mount_telemetry_data_t t_data;
// first, compute ICRS coordinates of given guiding point
coord_t ra_icrs, dec_icrs;
const auto p_astrom_engine = &astrom_engine;
const auto p_pec = &pec;
auto predictedPos = [p_astrom_engine, predicted_Npoints, &context, &ra_icrs, &dec_icrs](
jd_t start, std::vector<guiding_point_t>& track) {
if (track.size() < predicted_Npoints) {
track.resize(predicted_Npoints);
}
coord_t ha, ra_app, dec_app, az, alt, eo;
typename astrom_engine_t::error_t ast_err;
typename pec_t::error_t pec_err;
typename pec_t::pec_result_t pec_res;
for (auto& g_point : track) {
ast_err = p_astrom_engine->icrs2obs(ra_icrs, dec_icrs, start, ra_app, dec_app, ha, az, alt, eo);
if (ast_err) {
if constexpr (std::same_as<decltype(ast_err), error_t>) {
logError(
std::format("An error occured while performing astrometry computations: code = {} ({})",
ast_err.value(), ast_err.message()));
return ast_err;
} else {
if constexpr (traits::mcc_formattable<decltype(ast_err)>) {
logError(std::format(
"An error occured while performing astrometry computations: code = {}", ast_err));
}
return MccSimpleGuidingModelErrorCode::ERROR_ASTROM_COMP;
}
}
if constexpr (mccIsEquatorialMount(pec_t::mountType)) { // use of HA and DEC
g_point.coordPairKind = MccCoordPairKind::COORDS_KIND_HADEC_APP;
g_point.x = ha;
g_point.y = dec_app;
} else if constexpr (mccIsAltAzMount(pec_t::mountType)) { // use of Az and Alt
g_point.coordPairKind = MccCoordPairKind::COORDS_KIND_AZALT;
g_point.x = az;
g_point.y = alt;
} else {
static_assert(false, "UNKNOWN MOUNT TYPE!");
}
start.mjd += context.predictedTrackResolution.count() / 86400.0;
}
return MccSimpleGuidingModelErrorCode::ERROR_OK;
}; // end of predictedPos lambda
if (guiding_point.coordPairKind == mcc::MccCoordPairKind::COORDS_KIND_XY) {
typename pec_t::pec_result_t pec_res;
pec_err = pec.compute(guiding_point.x, guiding_point.y, pec_res);
if (pec_err) {
if constexpr (std::same_as<decltype(pec_err), error_t>) {
logError(
std::format("An PEC error occured: code = {} ({})", pec_err.value(), pec_err.message()));
return pec_err;
} else {
if constexpr (traits::mcc_formattable<decltype(pec_err)>) {
logError(std::format("An PEC error occured: code = {}", pec_err));
}
return MccSimpleGuidingModelErrorCode::ERROR_PEC_COMP;
}
}
if constexpr (mccIsEquatorialMount(pec_t::mountType)) { // use of HA and DEC
guiding_point.coordPairKind = MccCoordPairKind::COORDS_KIND_HADEC_APP;
} else if constexpr (mccIsAltAzMount(pec_t::mountType)) { // use of Az and Alt
guiding_point.coordPairKind = MccCoordPairKind::COORDS_KIND_AZALT;
} else {
static_assert(false, "UNKNOWN MOUNT TYPE!");
}
guiding_point.x += pec_res.dx; // app HA/Az
guiding_point.y += pec_res.dy; // app DEC/Alt
res_err = self(std::move(guiding_point));
if (res_err) {
return res_err;
}
} else if (guiding_point.coordPairKind == mcc::MccCoordPairKind::COORDS_KIND_HADEC_APP) {
} else if (guiding_point.coordPairKind == mcc::MccCoordPairKind::COORDS_KIND_RADEC_APP) {
} else if (guiding_point.coordPairKind == mcc::MccCoordPairKind::COORDS_KIND_AZALT) {
} else if (guiding_point.coordPairKind == mcc::MccCoordPairKind::COORDS_KIND_AZZD) {
} else if (guiding_point.coordPairKind == mcc::MccCoordPairKind::COORDS_KIND_RADEC_ICRS) {
ra_icrs = guiding_point.x;
dec_icrs = guiding_point.y;
} else {
return MccSimpleGuidingModelErrorCode::ERROR_UNSUPPORTED_COORD_PAIR;
}
if (guiding_point.coordPairKind != mcc::MccCoordPairKind::COORDS_KIND_RADEC_ICRS) {
ast_err = astrom_engine.greg2jul(astrom_engine_t::timePointNow(), jd);
if (!ast_err) {
ast_err = astrom_engine.obs2icrs(guiding_point.coordPairKind, guiding_point.x, guiding_point.y, jd,
ra_icrs, dec_icrs);
}
if (ast_err) {
if constexpr (std::same_as<decltype(ast_err), error_t>) {
logError(
std::format("An error occured while performing astrometry computations: code = {} ({})",
ast_err.value(), ast_err.message()));
return ast_err;
} else {
if constexpr (traits::mcc_formattable<decltype(ast_err)>) {
logError(std::format("An error occured while performing astrometry computations: code = {}",
ast_err));
}
return MccSimpleGuidingModelErrorCode::ERROR_ASTROM_COMP;
}
}
}
coord_t ha, ra_app, dec_app, az, alt, eo;
coord_t xr, yr, coord_diff;
typename hardware_t::axes_pos_t ax_pos;
while (true) {
// check prohibited zones ...
if ((res_err = check_zones())) {
return res_err;
}
ax_pos.moving_type = hardware_t::hw_moving_type_t::HW_MOVE_GUIDING;
ast_err = astrom_engine.greg2jul(astrom_engine_t::timePointNow(), jd);
if (!ast_err) {
ast_err = astrom_engine.icrs2obs(ra_icrs, dec_icrs, jd, ra_app, dec_app, ha, az, alt, eo);
}
if (ast_err) {
if constexpr (std::same_as<decltype(ast_err), error_t>) {
logError(
std::format("An error occured while performing astrometry computations: code = {} ({})",
ast_err.value(), ast_err.message()));
return ast_err;
} else {
if constexpr (traits::mcc_formattable<decltype(ast_err)>) {
logError(std::format("An error occured while performing astrometry computations: code = {}",
ast_err));
}
return MccSimpleGuidingModelErrorCode::ERROR_ASTROM_COMP;
t_err = telemetry.setTarget(guiding_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 MccSimpleGuidingModelErrorCode::ERROR_TELEMETRY_DATA;
}
}
std::array<bool, std::tuple_size_v<tpl_pz_t>> in_zone_flag;
while (true) {
if (_stopRequested) {
res_err = MccSimpleGuidingModelErrorCode::ERROR_GUIDING_STOPPED;
}
t_err = telemetry.data(t_data);
// check prohibited zones ...
if (mccCheckInZonePZTuple(t_data, p_mount_controls->prohibitedZones, in_zone_flag)) {
return MccSimpleGuidingModelErrorCode::ERROR_IN_PROHIBITED_ZONE;
};
// suspend the thread here until telemetry data is updated
t_err = telemetry.waitForUpdatedData(t_data, guiding_point.telemetryUpdateTimeout);
if (t_err) {
std::string err_str = "An error occured while waiting for updated telemetry";
if constexpr (std::same_as<decltype(t_err), error_t>) {
logError(
std::format("An telemetry error occured: code = {} ({})", t_err.value(), t_err.message()));
std::format_to(std::back_inserter(err_str), ": code = {} ({})", t_err.value(), t_err.message());
logError(err_str);
return t_err;
} else {
if constexpr (traits::mcc_formattable<decltype(t_err)>) {
logError(std::format("An telemetry error occured: code = {}", t_err));
std::format_to(std::back_inserter(err_str), ": code = {}", t_err.value());
}
logError(err_str);
return MccSimpleGuidingModelErrorCode::ERROR_TELEMETRY_DATA;
}
}
if (_stopRequested) {
res_err = MccSimpleGuidingModelErrorCode::ERROR_GUIDING_STOPPED;
}
// compare t_data with computed coordinates ...
if (_doCorrection) {
if constexpr (mccIsEquatorialMount(pec_t::mountType)) {
xr = ha - t_data.mntHA;
yr = dec_app - t_data.mntDEC;
} else if constexpr (mccIsAltAzMount(pec_t::mountType)) {
xr = az - t_data.mntAZ;
yr = alt - t_data.mntALT;
} else {
static_assert(false, "UNSUPPORTED MOUNT TYPE!");
}
auto coord_diff = telemetry.targetToMountDiff();
coord_diff = xr * xr + yr * yr;
if (coord_diff < low_corr_limit) {
if (coord_diff.r2 < low_corr_limit) {
continue;
}
if (coord_diff > high_corr_limit) {
logWarn(std::format(
"guiding model: the 'mount-target' difference exceeds the limit (diff = {}; lim = {})",
(double)coord_diff, (double)high_corr_limit));
if (coord_diff.r2 > high_corr_limit) {
logWarn(
std::format("guiding model: the 'mount-target' square of difference exceeds the limit "
"(diff = {}; lim = {})",
(double)coord_diff.r2, (double)high_corr_limit));
continue;
}
// do correction
ax_pos.state = hardware_t::hw_state_t::HW_STATE_TRACK; // indicates to hardware level
ax_pos.x = xr;
ax_pos.y = yr;
// ax_pos.x = t_data.mntPosX;
// ax_pos.y = t_data.mntPosY;
// current celestial position of target is already computed for current time point
// so one needs only correct apparent coordinates for PEC corrections
ax_pos.time_point = t_data.time_point;
if constexpr (mccIsEquatorialMount(pec_t::mountType)) {
ax_pos.x = t_data.tagHA - t_data.pecX;
ax_pos.y = t_data.tagDEC - t_data.pecY;
} else if constexpr (mccIsAltAzMount(pec_t::mountType)) {
ax_pos.x = t_data.tagAZ - t_data.pecX;
ax_pos.y = t_data.tagALT - t_data.pecY;
} else {
static_assert(false, "UNSUPPORTED MOUNT TYPE!");
}
// asynchronous operation!
auto err = hardware.setPos(std::move(ax_pos));
auto err = hardware.setPos(ax_pos);
if (err) {
if constexpr (std::same_as<decltype(err), error_t>) {
logError(