#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_STOPPED, ERROR_SLEW_ADJ_MAXITER, ERROR_SLEW_TIMEOUT }; } // namespace mcc namespace std { template <> class is_error_code_enum : 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(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"; case MccSimpleSlewModelErrorCode::ERROR_SLEW_STOPPED: return "slew model: stopped"; case MccSimpleSlewModelErrorCode::ERROR_SLEW_ADJ_MAXITER: return "slew model: max number of adjusting iteration was exceeded"; case MccSimpleSlewModelErrorCode::ERROR_SLEW_TIMEOUT: return "slew model: timeout occured while slewing"; 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(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 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; using slew_point_t = MccSlewAndGuidingPoint; template MccSimpleSlewModel(MOUNT_CONTROLS_T& mount_controls, LoggerCtorArgTs&&... ctor_args) requires(!std::same_as) : LoggerT(std::forward(ctor_args)...) { logDebug(std::format("Create 'MccSimpleSlewModel' class instance ({})", (void*)this)); init(mount_controls); } template MccSimpleSlewModel(MOUNT_CONTROLS_T& mount_controls) requires(std::same_as) { init(mount_controls); } MccSimpleSlewModel(MccSimpleSlewModel&&) = default; MccSimpleSlewModel& operator=(MccSimpleSlewModel&&) = default; MccSimpleSlewModel(const MccSimpleSlewModel&) = default; MccSimpleSlewModel& operator=(const MccSimpleSlewModel&) = default; virtual ~MccSimpleSlewModel() { logDebug(std::format("Delete 'MccSimpleSlewModel' class instance ({})", (void*)this)); } error_t slew(slew_point_t pars) { _stopRequested = false; error_t res_err = _slewFunc(std::move(pars)); return res_err; } error_t stop() { _stopRequested = true; return MccSimpleSlewModelErrorCode::ERROR_OK; } protected: std::atomic_bool _stopRequested{false}; std::function _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 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; const auto p_mount_controls = &mount_controls; _slewFunc = [p_mount_controls, this](slew_point_t slew_point) { _stopRequested = false; 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.withinToleranceCycleNumber == 0) { slew_point.withinToleranceCycleNumber = MccSlewAndGuidingPoint::defaultWithinToleranceCycleNumber; } // 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) { logError(std::format("An telemetry error occured: code = {} ({})", t_err.value(), t_err.message())); return t_err; } else { if constexpr (traits::mcc_formattable) { 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; ax_pos.moving_type = hardware_t::hw_moving_type_t::HW_MOVE_SLEWING; typename hardware_t::error_t hw_err = hardware->setPos(ax_pos); if (hw_err) { if constexpr (std::same_as) { logError( std::format("An hardware error occured: code = {} ({})", hw_err.value(), hw_err.message())); return hw_err; } else { if constexpr (traits::mcc_formattable) { 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; adj_ax_pos.moving_type = hardware_t::hw_moving_type_t::HW_MOVE_ADJUSTING; 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 in_zone_flag; size_t i_adj_cycle = 0; size_t i_in_tol_cycle = 0; bool in_adj_mode = false; // compute new hardware coordinate of target auto compute_new_coord = [](auto const& t_data, typename hardware_t::axes_pos_t& hw_pos) { // current celestial position of target is already computed for given time point // so one needs only correct apparent coordinates for PEC corrections hw_pos.time_point = t_data.time_point; if constexpr (mccIsEquatorialMount(pec_t::mountType)) { hw_pos.x = t_data.tagHA - t_data.pecX; hw_pos.y = t_data.tagDEC - t_data.pecY; } else if constexpr (mccIsAltAzMount(pec_t::mountType)) { hw_pos.x = t_data.tagAZ - t_data.pecX; hw_pos.y = t_data.tagALT - t_data.pecY; } else { static_assert(false, "UNSUPPORTED MOUNT TYPE!"); } }; auto cycle_func = [&](auto t_data) mutable { if (_stopRequested) { res_err = MccSimpleSlewModelErrorCode::ERROR_SLEW_STOPPED; } // check for prohibited zones if (mccCheckInZonePZTuple(t_data, p_mount_controls->prohibitedZones, in_zone_flag)) { return MccSimpleSlewModelErrorCode::ERROR_IN_PROHIBITED_ZONE; }; // t_data was updated in caller!!! auto coord_diff = telemetry.targetToMountDiff(); if (_stopRequested) { res_err = MccSimpleSlewModelErrorCode::ERROR_SLEW_STOPPED; } if (coord_diff.r2 < adj_rad2) { // adjusting mode in_adj_mode = true; compute_new_coord(t_data, adj_ax_pos); hw_err = hardware->setPos(adj_ax_pos); if (!hw_err) { ++i_adj_cycle; if (coord_diff.r2 < 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 = MccSimpleSlewModelErrorCode::ERROR_SLEW_ADJ_MAXITER; return; } } else { if constexpr (std::same_as) { logError(std::format("An hardware error occured: code = {} ({})", hw_err.value(), hw_err.message())); res_err = hw_err; } else { if constexpr (traits::mcc_formattable) { logError(std::format("An hardware error occured: code = {}", hw_err)); } res_err = MccSimpleSlewModelErrorCode::ERROR_HARDWARE_SETPOS; } return; } } else { // continue to slewing if (in_adj_mode) { // ?!!!!!!!!!!!!! slew again?!!! in_adj_mode = false; i_adj_cycle = 0; i_in_tol_cycle = 0; compute_new_coord(t_data, ax_pos); typename hardware_t::error_t hw_err = hardware->setPos(ax_pos); if (hw_err) { if constexpr (std::same_as) { logError(std::format("An hardware error occured: code = {} ({})", hw_err.value(), hw_err.message())); return hw_err; } else { if constexpr (traits::mcc_formattable) { logError(std::format("An hardware error occured: code = {}", hw_err)); } return MccSimpleSlewModelErrorCode::ERROR_HARDWARE_SETPOS; } } } } }; auto start_poll_tm = std::chrono::steady_clock::now(); // NOTE: TARGET COORDINATES WILL BE UPDATED FOR CURRENT TIME-POINT IN TELEMETRY-CLASS!!! while (true) { t_err = telemetry.waitForUpdatedData(t_data, slew_point.telemetryUpdateTimeout); if (t_err) { std::string err_str = "An error occured while waiting for updated telemetry"; if constexpr (std::same_as) { 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) { std::format_to(std::back_inserter(err_str), ": code = {}", t_err.value()); } logError(err_str); return MccSimpleSlewModelErrorCode::ERROR_TELEMETRY_DATA; } } cycle_func(t_data); if (res_err) { return res_err; } 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; }; } }; } // namespace mcc