diff --git a/cxx/mount.h b/cxx/mount.h index 772d5b1..bc2cb2a 100644 --- a/cxx/mount.h +++ b/cxx/mount.h @@ -3,10 +3,21 @@ /* MOUNT CONTROL COMPONENTS LIBRARY */ #include +#include #include #include #include #include +#include "spdlog/sinks/null_sink.h" + + +// low-level functions +namespace lowlevel +{ +#include "../LibSidServo/sidservo.h" +} // namespace lowlevel + + namespace mcc { @@ -19,9 +30,8 @@ template concept mcc_mount_state_c = requires(T t, const T t_const) { { t_const.ident() } -> std::same_as; - typename T::context_t; - { t.enter(std::declval()) } -> std::same_as; - { t.exit(std::declval()) } -> std::same_as; + { t.enter() } -> std::same_as; + { t.exit() } -> std::same_as; }; @@ -37,12 +47,19 @@ class MccMount public: static constexpr MccMountType mountType = MOUNT_TYPE; - /* mount main-cycle variable quantities */ - struct mount_state_t { - // time-related - time_point_t siderTime; // sideral time + /* low-level hardware related constants */ - // target (user-input) current coordinates (in radians) + // number of attempts to get low-level hardware data before emit an error + static constexpr uint8_t LOWLEVEL_HW_POLL_ATTEMPTS = 10; + + /* mount main-cycle variable quantities (mount orientation) */ + struct mount_orient_t { + // time-related + std::chrono::system_clock::time_point utc; + time_point_t mjd; // modified Julian date + time_point_t siderTime; // sideral time (in radians) + + // apparent target (user-input) current coordinates (in radians) mnt_coord_t tagRA, tagDEC; mnt_coord_t tagHA; mnt_coord_t tagAZ, tagZD; @@ -66,10 +83,15 @@ public: }; + struct mount_config_t { + uint8_t hw_poll_attempts = LOWLEVEL_HW_POLL_ATTEMPTS; + }; - MccMount() + + + MccMount(std::shared_ptr logger = spdlog::null_logger_mt("NULL")) + : _mountLogger(logger), _exitCurrentState([]() {}) { - _exitCurrentState = []() {}; // do nothing } virtual ~MccMount() {} @@ -78,17 +100,46 @@ public: void setMountState(StateT& state) { _exitCurrentState(); // exit from current state - _exitCurrentState = [&state, this]() { state.exit(this); }; + _exitCurrentState = [&state]() { state.exit(); }; - state.enter(this); + state.enter(); + } + + + mount_orient_t getMountData() const noexcept + { + return _currentMountOrient.load(); } protected: + mount_config_t _mountCurrentConfig; + + std::shared_ptr _mountLogger; std::function _exitCurrentState; - std::atomic _mountCurrentState; + std::atomic _currentMountOrient; - void updateMountState() {} + void updateMountState() + { + mount_orient_t orient; + lowlevel::mountdata_t hw_mdata; + + // get data from encoders + decltype(mount_config_t::hw_poll_attempts) n_attempts = 0; + do { + auto ret_code = lowlevel::Mount.getMountData(&hw_mdata); + if (ret_code != lowlevel::MCC_E_OK) { + ++n_attempts; + } + } while (n_attempts < _mountCurrentConfig.hw_poll_attempts); + + if (n_attempts >= _mountCurrentConfig.hw_poll_attempts) { + // log error heres + return; + } + + _currentMountOrient.store(orient); + } }; } // namespace mcc