MccMount is now inheritor of MccFiniteStateMachine

This commit is contained in:
Timur A. Fatkhullin 2025-06-09 22:58:58 +03:00
parent 47413792e5
commit 172d38f5e2
3 changed files with 43 additions and 168 deletions

View File

@ -126,7 +126,8 @@ set(MOUNT_SERVER_APP_SRC mount.h mount_state.h mount_server.cpp comm_server.h co
mount_astrom_default.h mcc_coord.h mount_pz.h mcc_fsm.h mcc_fsm_utils.h)
set(MOUNT_SERVER_APP mount_server)
add_executable(${MOUNT_SERVER_APP} ${MOUNT_SERVER_APP_SRC}
mcc_finite_state_machine.h)
mcc_finite_state_machine.h
mcc_mount_events_states.h)
# target_include_directories(${MOUNT_SERVER_APP} PUBLIC ${ERFA_INCLUDE_DIR})
target_link_libraries(${MOUNT_SERVER_APP} ${CNTR_PROTO_LIB} spdlog::spdlog_header_only ERFA_LIB)

View File

@ -0,0 +1,29 @@
#pragma once
/* MOUNT CONTROL COMPONENTS LIBRARY */
/*
* BASIC EVENTS AND MOUNT STATES DEFINITIONS
*/
#include "mount.h"
namespace mcc
{
template <MccMountType MOUNT_TYPE,
std::derived_from<MccMountConfig> CONFIG_TYPE,
std::derived_from<MccMount<MOUNT_TYPE, CONFIG_TYPE>> MountT>
struct MccMountInitEvent {
static constexpr std::string_view ID = "MCC-MOUNT-INIT-EVENT";
MccMountInitEvent(MountT& mount) : _mount(mount) {}
MountT mount() const { return _mount; }
private:
MountT& _mount;
};
} // namespace mcc

View File

@ -11,6 +11,7 @@
#include "spdlog/sinks/null_sink.h"
#include "mcc_coord.h"
#include "mcc_finite_state_machine.h"
#include "mcc_spdlog.h"
#include "mcc_traits.h"
#include "mount_astrom.h"
@ -27,33 +28,6 @@
namespace mcc
{
namespace traits
{
// mount state type concept
template <typename T>
concept mcc_mount_state_c = requires(T t, const T t_const) {
{ t_const.ident() } -> std::same_as<std::string_view>;
// requires mcc_is_callable<typename T::error_callback_t>;
// requires mcc_is_callable<typename T::enter_callback_t>;
// requires mcc_is_callable<typename T::exit_callback_t>;
{ t.enter() } -> std::same_as<void>;
{ t.exit() } -> std::same_as<void>;
{ t.stop() } -> std::same_as<void>;
typename T::state_data_t;
{ t.stateData() } -> std::same_as<typename T::state_data_t>;
requires std::movable<T>;
};
} // namespace traits
/* SOME BASIC DATA STRUCTURES DEFINITIONS */
@ -230,7 +204,7 @@ struct MccProhibitedZone1 {
// implements a Finite State Machine Pattern
template <MccMountType MOUNT_TYPE, std::derived_from<MccMountConfig> CONFIG_TYPE = MccMountConfig>
class MccMount : public utils::MccSpdlogLogger
class MccMount : public fsm::MccFiniteStateMachine, public utils::MccSpdlogLogger
{
public:
static constexpr MccMountType mountType = MOUNT_TYPE;
@ -248,105 +222,14 @@ public:
};
/* mount state base class */
class MccMountState
{
static constexpr std::string_view emptyIdentString{"EMPTY-STRING-MOUNT-STATE-IDENTIFICATOR"};
public:
MccMountState(MccMount& mount, traits::mcc_input_char_range auto const& ident)
: _mount(mount), _ident({ident.begin(), ident.end()})
{
_mount.logTrace("Create MccMountState instance: {}", (void*)this);
if (!_ident.size()) {
_mount.logWarn("An empty input mount state identificator! Use of default value!");
_ident = emptyIdentString;
}
}
MccMountState(MccMount& mount, const char* ident) : MccMountState(mount, std::string_view(ident)) {}
MccMountState(MccMountState&&) = default; // movable
MccMountState& operator=(MccMountState&&) = default;
MccMountState(const MccMountState&) = delete; // non-copyable
MccMountState& operator=(const MccMountState&) = delete;
virtual ~MccMountState()
{
_mount.logTrace("Delete MccMountState instance: {}", (void*)this);
};
std::string_view ident() const
{
return _ident;
}
void enter(this std::derived_from<MccMountState> auto&& self)
{
std::forward<decltype(self)>(self).enterImpl();
}
void stop(this std::derived_from<MccMountState> auto&& self)
{
std::forward<decltype(self)>(self).stopImpl();
}
void exit(this std::derived_from<MccMountState> auto&& self, std::derived_from<MccMountState> auto next_state)
{
std::forward<decltype(self)>(self).exitImpl(std::move(next_state));
}
protected:
MccMount& _mount;
std::string_view _ident;
void enterImpl()
{
_mount.logWarn("call no-op MccMountState::enterImpl()");
}
void stopImpl()
{
_mount.logWarn("call no-op MccMountState::stopImpl()");
}
void exitImpl(std::derived_from<MccMountState> auto next_state)
{
_mount.setMountState(std::move(next_state));
}
};
// default IDLE state
class MccMountStateIDLE : public MccMountState
{
public:
MccMountStateIDLE(MccMount& mount) : MccMountState(mount, "DEFAULT IDLE STATE") {}
private:
void enterImpl()
{
this->_mount.logInfo("Enter to defalut IDLE state");
}
void stopImpl()
{
this->_mount.logWarn("Ignore stop for default IDLE state!");
}
};
/* Constructors and destructor */
MccMount(traits::mcc_input_char_range auto const& logger_mark = "[MOUNT]",
template <fsm::traits::fsm_state_c InitStateT>
MccMount(InitStateT,
traits::mcc_input_char_range auto const& logger_mark = "[MOUNT]",
std::shared_ptr<spdlog::logger> logger = spdlog::null_logger_mt("NULL"))
: utils::MccSpdlogLogger(logger), _exitCurrentState([]() {}), _currentStateName([]() {})
: fsm::MccFiniteStateMachine(InitStateT{}), utils::MccSpdlogLogger(logger)
{
std::istringstream strst;
@ -361,42 +244,17 @@ public:
updateIERSDatabase(IERS_DB_EARTH_ORIENT);
}
virtual ~MccMount()
{
logDebug("Delete MccMount class instance: thread = {}", getThreadId());
}
virtual ~MccMount() { logDebug("Delete MccMount class instance: thread = {}", getThreadId()); }
/* Public methods */
template <traits::mcc_mount_state_c StateT>
void setMountState(StateT state)
{
_exitCurrentState(); // exit from current state
_exitCurrentState = [&state]() { state.exit(); };
_currentStateName = [&state]() { return state.name(); };
state.enter();
}
std::string_view currenStateName() const
{
return _currentStateName();
}
MccMountPosition getMountData() const noexcept
{
return _currentMountOrient.load();
}
MccMountPosition getMountData() const noexcept { return _currentMountOrient.load(); }
// geo location setters/getters
void setGeoLocation(const MccMountSiteInfo& geoloc)
{
_geoLocation.store(geoloc);
}
void setGeoLocation(const MccMountSiteInfo& geoloc) { _geoLocation.store(geoloc); }
void setSiteLatitude(const MccAngle& lat)
{
@ -424,23 +282,14 @@ public:
}
MccMountSiteInfo getGeoLocation() const
{
return _geoLocation.load();
}
MccMountSiteInfo getGeoLocation() const { return _geoLocation.load(); }
// current meteo setters/getters
void setMeteo(const MccMountMeteo& meteo)
{
_currentMeteo.store(meteo);
}
void setMeteo(const MccMountMeteo& meteo) { _currentMeteo.store(meteo); }
MccMountMeteo getMeteo() const
{
return _currentMeteo.load();
}
MccMountMeteo getMeteo() const { return _currentMeteo.load(); }
/* prohibited zone related methods */
@ -561,10 +410,6 @@ public:
protected:
mount_config_t _mountCurrentConfig;
// std::shared_ptr<spdlog::logger> _mountLogger;
std::function<void()> _exitCurrentState;
std::function<std::string_view()> _currentStateName;
// time scales related databases
astrom::MccLeapSeconds _leapSecondsDB;
astrom::MccIersBulletinA _earthOrientDB;