This commit is contained in:
Timur A. Fatkhullin 2025-06-16 18:00:53 +03:00
parent 5a5854ccdd
commit 64a3544bd8
4 changed files with 132 additions and 79 deletions

View File

@ -490,12 +490,18 @@ public:
} }
template <traits::fsm_event_c EvT> template <traits::fsm_event_c EvT>
auto dispatchEvent(EvT&& event = EvT()) auto dispatchEvent(EvT&& event)
requires std::default_initializable<EvT>
{ {
return dispatchEvent(event); return dispatchEvent(event);
} }
template <traits::fsm_event_c EvT>
auto dispatchEvent()
requires std::default_initializable<EvT>
{
return dispatchEvent(EvT{});
}
std::string_view currentStateID() const std::string_view currentStateID() const
{ {
return _currentStateID; return _currentStateID;

View File

@ -271,7 +271,7 @@ public:
// binary arithmetic operations // binary arithmetic operations
template <std::convertible_to<MccAngle> T1, std::convertible_to<MccAngle> T2> template <std::convertible_to<MccAngle> T1, std::convertible_to<MccAngle> T2>
auto operator+(const T1& v1, const T2& v2) static auto operator+(const T1& v1, const T2& v2)
{ {
static_assert(std::convertible_to<T1, T2> || std::convertible_to<T2, T1>, "INCOMPATIBLE TYPES!"); static_assert(std::convertible_to<T1, T2> || std::convertible_to<T2, T1>, "INCOMPATIBLE TYPES!");
@ -281,7 +281,7 @@ auto operator+(const T1& v1, const T2& v2)
} }
template <std::convertible_to<MccAngle> T1, std::convertible_to<MccAngle> T2> template <std::convertible_to<MccAngle> T1, std::convertible_to<MccAngle> T2>
auto operator-(const T1& v1, const T2& v2) static auto operator-(const T1& v1, const T2& v2)
{ {
static_assert(std::convertible_to<T1, T2> || std::convertible_to<T2, T1>, "INCOMPATIBLE TYPES!"); static_assert(std::convertible_to<T1, T2> || std::convertible_to<T2, T1>, "INCOMPATIBLE TYPES!");
@ -292,7 +292,7 @@ auto operator-(const T1& v1, const T2& v2)
template <std::convertible_to<MccAngle> T1, std::convertible_to<MccAngle> T2> template <std::convertible_to<MccAngle> T1, std::convertible_to<MccAngle> T2>
auto operator*(const T1& v1, const T2& v2) static auto operator*(const T1& v1, const T2& v2)
{ {
if constexpr (std::is_arithmetic_v<T1>) { if constexpr (std::is_arithmetic_v<T1>) {
return v2 *= v1; return v2 *= v1;
@ -304,7 +304,7 @@ auto operator*(const T1& v1, const T2& v2)
} }
template <std::convertible_to<MccAngle> T1, typename T2> template <std::convertible_to<MccAngle> T1, typename T2>
auto operator/(const T1& v1, const T2& v2) static auto operator/(const T1& v1, const T2& v2)
requires std::is_arithmetic_v<T2> requires std::is_arithmetic_v<T2>
{ {
return v1 /= v2; return v1 /= v2;
@ -343,34 +343,19 @@ class MccAngleAZ : public MccAngle
using MccAngle::MccAngle; using MccAngle::MccAngle;
}; };
class MccAngleALT; // just forward declaration
class MccAngleZD : public MccAngle class MccAngleZD : public MccAngle
{ {
public: public:
using MccAngle::MccAngle; using MccAngle::MccAngle;
MccAngleZD(const MccAngleALT&);
}; };
class MccAngleALT : public MccAngle class MccAngleALT : public MccAngle
{ {
public: public:
using MccAngle::MccAngle; using MccAngle::MccAngle;
MccAngleALT(const MccAngleZD& zd)
{
_angleInRads = std::numbers::pi / 2.0 - (double)zd;
}
}; };
MccAngleZD::MccAngleZD(const MccAngleALT& alt)
{
_angleInRads = std::numbers::pi / 2.0 - (double)alt;
}
class MccAngleX : public MccAngle // some co-longitude coordinate class MccAngleX : public MccAngle // some co-longitude coordinate
{ {
using MccAngle::MccAngle; using MccAngle::MccAngle;

View File

@ -175,37 +175,53 @@ struct MccMountEventShutdown : public MccMountEventBase<MountT> {
template <traits::mcc_mount_c MountT> template <traits::mcc_mount_c MountT>
struct MccMountStateBase { struct MccMountStateBase {
// template <std::derived_from<MccMountEventBase<MountT>> EvT>
// void exit(this auto&& self, EvT& event)
// {
// using self_t = std::remove_cvref_t<decltype(self)>;
// std::forward<decltype(self)>(self).exitImpl(event);
// event.mount().logDebug("Exit from '{}' state due to '{}' event ...", self_t::ID, EvT::ID);
// }
// template <std::derived_from<MccMountEventBase<MountT>> EvT>
// void enter(this auto&& self, EvT& event)
// {
// using self_t = std::remove_cvref_t<decltype(self)>;
// event.mount().logDebug("Enter to '{}' state due to '{}' event ...", self_t::ID, EvT::ID);
// std::forward<decltype(self)>(self).enterImpl(event);
// }
protected:
// template <std::derived_from<MccMountEventBase<MountT>> EvT>
// void exitImpl(EvT& event)
// {
// event.mount().logWarn("Call an empty MccMountStateBase::exitImpl method!!! Event type is '{}'", EvT::ID);
// }
// template <std::derived_from<MccMountEventBase<MountT>> EvT>
// void enterImpl(EvT& event)
// {
// event.mount().logWarn("Call an empty MccMountStateBase::enterImpl method!!! Event type is '{}'", EvT::ID);
// }
// MccMountStateBase() = default;
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void exit(this auto&& self, EvT& event) void exitLog(this auto&& self, EvT& event)
{ {
using self_t = std::remove_cvref_t<decltype(self)>; using self_t = std::remove_cvref_t<decltype(self)>;
std::forward<decltype(self)>(self).exitImpl(event);
event.mount().logDebug("Exit from '{}' state due to '{}' event ...", self_t::ID, EvT::ID); event.mount().logDebug("Exit from '{}' state due to '{}' event ...", self_t::ID, EvT::ID);
} }
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void enter(this auto&& self, EvT& event) void enterLog(this auto&& self, EvT& event)
{ {
using self_t = std::remove_cvref_t<decltype(self)>; using self_t = std::remove_cvref_t<decltype(self)>;
event.mount().logDebug("Enter to '{}' state due to '{}' event ...", self_t::ID, EvT::ID); event.mount().logDebug("Enter to '{}' state due to '{}' event ...", self_t::ID, EvT::ID);
std::forward<decltype(self)>(self).enterImpl(event);
}
protected:
template <std::derived_from<MccMountEventBase<MountT>> EvT>
void exitImpl(EvT& event)
{
event.mount().logWarn("Call an empty MccMountStateBase::exitImpl method!!! Event type is '{}'", EvT::ID);
}
template <std::derived_from<MccMountEventBase<MountT>> EvT>
void enterImpl(EvT& event)
{
event.mount().logWarn("Call an empty MccMountStateBase::enterImpl method!!! Event type is '{}'", EvT::ID);
} }
}; };
@ -242,25 +258,30 @@ struct MccMountStateStop : MccMountStateBase<MountT> {
using transition_t = fsm::fsm_transition_table_t<std::pair<MccMountEventIDLE<MountT>, MccMountStateIDLE<MountT>>>; using transition_t = fsm::fsm_transition_table_t<std::pair<MccMountEventIDLE<MountT>, MccMountStateIDLE<MountT>>>;
protected: // protected:
void exitImpl(MccMountEventIDLE<MountT>& event) void exit(MccMountEventIDLE<MountT>& event)
{ {
// normal exit from the state // normal exit from the state
this->exitLog(event);
} }
// normal bihavior (transit to IDLE state after stopping) // normal behavior (transit to IDLE state after stopping)
void enterImpl(MccMountEventStop<MountT>& event) // void enter(MccMountEventStop<MountT>& event)
{ // {
auto mount = event.mount(); // this->enterLog(event);
mount.stopMount();
// switch to IDLE state // auto mount = event.mount();
mount.template dispatchEvent<MccMountEventIDLE<MountT>>({mount}); // mount.stopMount();
}
// // switch to IDLE state
// mount.template dispatchEvent<MccMountEventIDLE<MountT>>({mount});
// }
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void enterImpl(EvT& event) void enter(EvT& event)
{ {
this->enterLog(event);
auto mount = event.mount(); auto mount = event.mount();
mount.stopMount(); mount.stopMount();
@ -268,7 +289,7 @@ protected:
if constexpr (std::same_as<EvT, MccMountEventStop<MountT>>) { if constexpr (std::same_as<EvT, MccMountEventStop<MountT>>) {
mount.logInfo("Stop reason: {}", event.reason()); mount.logInfo("Stop reason: {}", event.reason());
// normal bihavior (transit to IDLE state after stopping) // normal behavior (transit to IDLE state after stopping)
mount.template dispatchEvent<MccMountEventIDLE<MountT>>({mount}); mount.template dispatchEvent<MccMountEventIDLE<MountT>>({mount});
} else { } else {
mount.logInfo("Stop reason: special state"); mount.logInfo("Stop reason: special state");
@ -285,15 +306,18 @@ struct MccMountStateInit : MccMountStateBase<MountT> {
using transition_t = fsm::fsm_transition_table_t<std::pair<MccMountEventIDLE<MountT>, MccMountStateIDLE<MountT>>>; using transition_t = fsm::fsm_transition_table_t<std::pair<MccMountEventIDLE<MountT>, MccMountStateIDLE<MountT>>>;
protected: // protected:
void exitImpl(MccMountEventIDLE<MountT>& event) void exit(MccMountEventIDLE<MountT>& event)
{ {
// normal exit from the state // normal exit from the state
this->exitLog(event);
} }
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void enterImpl(EvT& event) void enter(EvT& event)
{ {
this->enterLog(event);
auto mount = event.mount(); auto mount = event.mount();
mount.initMount(); mount.initMount();
@ -318,33 +342,50 @@ struct MccMountStateError : MccMountStateBase<MountT> {
std::pair<MccMountEventStop<MountT>, MccMountStateError<MountT>>, std::pair<MccMountEventStop<MountT>, MccMountStateError<MountT>>,
std::pair<MccMountEventShutdown<MountT>, MccMountStateShutdown<MountT>>>; std::pair<MccMountEventShutdown<MountT>, MccMountStateShutdown<MountT>>>;
protected: // protected:
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void exitImpl(EvT& event) void exit(EvT& event)
{ {
this->exitLog(event);
event.mount().logWarn( event.mount().logWarn(
"The mount is in the error state!!! One must correct it before transit to any states! Event type is '{}'", "The mount is in the error state!!! One must correct it before transit to any states! Event type is '{}'",
EvT::ID); EvT::ID);
} }
void exitImpl(MccMountEventIDLE<MountT>& event) void exit(MccMountEventIDLE<MountT>& event)
{ {
event.mount().logWarning("Suppose the error was corrected!"); this->exitLog(event);
event.mount().logWarn("Suppose the error was corrected!");
} }
void exitImpl(MccMountEventInit<MountT>& event) void exit(MccMountEventInit<MountT>& event)
{ {
// normal exit from the state // normal exit from the state
this->exitLog(event);
} }
template <std::derived_from<MccMountEventBase<MountT>> EvT> void enter(MccMountEventError<MountT>& event)
void enterImpl(EvT& event)
{ {
this->enterLog(event);
auto err = event.eventData(); auto err = event.eventData();
event.mount().logError("The mount is in the error state: code = {}, category = {}, message = '{}'", err.value(), event.mount().logError("The mount is in the error state: code = {}, category = {}, message = '{}'", err.value(),
err.category().name(), err.message()); err.category().name(), err.message());
} }
// template <std::derived_from<MccMountEventBase<MountT>> EvT>
// void enter(EvT& event)
// {
// this->enterLog(event);
// auto err = event.eventData();
// event.mount().logError("The mount is in the error state: code = {}, category = {}, message = '{}'",
// err.value(),
// err.category().name(), err.message());
// }
}; };
@ -361,6 +402,18 @@ struct MccMountStateIDLE : MccMountStateBase<MountT> {
std::pair<MccMountEventGuiding<MountT>, MccMountStateGuiding<MountT>>, std::pair<MccMountEventGuiding<MountT>, MccMountStateGuiding<MountT>>,
std::pair<MccMountEventStop<MountT>, MccMountStateIDLE<MountT>>, std::pair<MccMountEventStop<MountT>, MccMountStateIDLE<MountT>>,
std::pair<MccMountEventShutdown<MountT>, MccMountStateShutdown<MountT>>>; std::pair<MccMountEventShutdown<MountT>, MccMountStateShutdown<MountT>>>;
template <std::derived_from<MccMountStateBase<MountT>> EvT>
void exit(EvT& event)
{
this->exitLog(event);
}
template <std::derived_from<MccMountStateBase<MountT>> EvT>
void enter(EvT& event)
{
this->enterLog(event);
}
}; };
@ -373,15 +426,18 @@ struct MccMountStateShutdown : MccMountStateBase<MountT> {
// only initialization // only initialization
using transition_t = fsm::fsm_transition_table_t<std::pair<MccMountEventInit<MountT>, MccMountStateInit<MountT>>>; using transition_t = fsm::fsm_transition_table_t<std::pair<MccMountEventInit<MountT>, MccMountStateInit<MountT>>>;
protected: // protected:
void exitImpl(MccMountEventInit<MountT>& event) void exit(MccMountEventInit<MountT>& event)
{ {
// normal exit from the state // normal exit from the state
this->exitLog(event);
} }
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void enterImpl(EvT& event) void enter(EvT& event)
{ {
this->enterLog(event);
event.mount().shutdownMount(); event.mount().shutdownMount();
} }
}; };
@ -402,16 +458,19 @@ struct MccMountStateSlew : MccMountStateBase<MountT> {
std::pair<MccMountEventStop<MountT>, MccMountStateStop<MountT>>, std::pair<MccMountEventStop<MountT>, MccMountStateStop<MountT>>,
std::pair<MccMountEventShutdown<MountT>, MccMountStateStop<MountT>>>; std::pair<MccMountEventShutdown<MountT>, MccMountStateStop<MountT>>>;
protected: // protected:
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void exitImpl(EvT& event) void exit(EvT& event)
{ {
this->exitLog(event);
} }
template <std::derived_from<MccMountEventSlew<MountT>> EvT> template <std::derived_from<MccMountEventSlew<MountT>> EvT>
void enterImpl(EvT& event) void enter(EvT& event)
{ {
event.mount().slewMount(/* params here ...*/); this->enterLog(event);
event.mount().slewMount(event.eventData());
} }
}; };
@ -432,16 +491,19 @@ struct MccMountStateGuiding : MccMountStateBase<MountT> {
std::pair<MccMountEventStop<MountT>, MccMountStateStop<MountT>>, std::pair<MccMountEventStop<MountT>, MccMountStateStop<MountT>>,
std::pair<MccMountEventShutdown<MountT>, MccMountStateStop<MountT>>>; std::pair<MccMountEventShutdown<MountT>, MccMountStateStop<MountT>>>;
protected: // protected:
template <std::derived_from<MccMountEventBase<MountT>> EvT> template <std::derived_from<MccMountEventBase<MountT>> EvT>
void exitImpl(EvT& event) void exit(EvT& event)
{ {
this->exitLog(event);
} }
template <std::derived_from<MccMountEventSlew<MountT>> EvT> template <std::derived_from<MccMountEventSlew<MountT>> EvT>
void enterImpl(EvT& event) void enter(EvT& event)
{ {
event.mount().slewMount(/* params here ...*/); this->enterLog(event);
event.mount().startGuiding();
} }
}; };

View File

@ -16,11 +16,11 @@ int tests_coord(int argc, char* argv[])
std::cout << "ang_degs = " << ang.degrees() << "\n"; std::cout << "ang_degs = " << ang.degrees() << "\n";
std::cout << "ang_rads = " << (float)ang << "\n"; std::cout << "ang_rads = " << (float)ang << "\n";
MccAngleALT alt(30.0_degs); // MccAngleALT alt(30.0_degs);
MccAngleZD zd(alt); // MccAngleZD zd(alt);
std::cout << "alt = " << alt.degrees() << "\n"; // std::cout << "alt = " << alt.degrees() << "\n";
std::cout << "zd (from alt) = " << zd.degrees() << "\n"; // std::cout << "zd (from alt) = " << zd.degrees() << "\n";
std::cout << "\n"; std::cout << "\n";