Compare commits

...

2 Commits

Author SHA1 Message Date
ca9dcdfa68 ... 2025-07-30 22:48:27 +03:00
c39496ff25 ... 2025-07-30 22:48:27 +03:00
3 changed files with 83 additions and 57 deletions

View File

@ -60,10 +60,7 @@ struct MccNullLogger {
typedef int loglevel_t;
void setLogLevel(loglevel_t){};
loglevel_t getLogLevel() const
{
return 0;
};
loglevel_t getLogLevel() const { return 0; };
void logMessage(loglevel_t, const std::string&) {};
void logError(const std::string&) {};
@ -384,6 +381,7 @@ concept mcc_slew_model_c = requires(T t) {
// { t.slew(std::declval<typename T::slew_params_t>()) } -> std::same_as<typename T::error_t>;
{ t.slew(std::declval<typename T::slew_point_t>()) } -> std::same_as<typename T::error_t>;
{ t.stop() } -> std::same_as<typename T::error_t>;
};
@ -394,6 +392,7 @@ concept mcc_guiding_model_c = requires(T t) {
// start process of guiding
{ t.guiding(std::declval<typename T::guiding_point_t>()) } -> std::same_as<typename T::error_t>;
{ t.stop() } -> std::same_as<typename T::error_t>;
};

View File

@ -12,7 +12,7 @@
namespace mcc
{
enum class MccMountTelemetryErrorCode : int { ERROR_OK, ERROR_HARDWARE };
enum class MccMountTelemetryErrorCode : int { ERROR_OK, ERROR_HARDWARE, ERROR_DATA_TIMEOUT };
/* error category definition */
@ -20,10 +20,7 @@ enum class MccMountTelemetryErrorCode : int { ERROR_OK, ERROR_HARDWARE };
struct MccMountTelemetryCategory : public std::error_category {
MccMountTelemetryCategory() : 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
{
@ -34,6 +31,8 @@ struct MccMountTelemetryCategory : public std::error_category {
return "OK";
case MccMountTelemetryErrorCode::ERROR_HARDWARE:
return "hardware request failed";
case MccMountTelemetryErrorCode::ERROR_DATA_TIMEOUT:
return "an timeout occured while waiting for new data";
default:
return "UNKNOWN";
}
@ -105,7 +104,7 @@ struct MccMountTelemetryData {
coord_t mntRateX, mntRateY;
// current refraction coefficients
typename PEC_T::pec_result_t currRefrCoeffs;
typename ASTROM_ENGINE_T::refract_result_t currRefrCoeffs;
// current refraction correction (for mntALT)
coord_t currRefr;
@ -141,9 +140,6 @@ public:
typedef std::error_code error_t;
typedef std::function<void(mount_telemetry_data_t)> update_callback_func_t;
typedef std::list<update_callback_func_t> update_callback_container_t;
MccMountTelemetry(astrom_engine_t& astrom_engine, pec_t& pec, hardware_t& hardware)
: base_t(astrom_engine, pec), _hardware(hardware)
{
@ -266,49 +262,37 @@ public:
_data = std::move(current_data);
std::lock_guard cl_lock{_callbackMutex};
// callbacks will be invoked with its own copy of telemetry data!
for (auto& func : _callbackFuncs) {
std::thread t([this, &func] {
auto d = _data;
func(std::move(d));
});
t.detach();
}
// notify all threads for new telemetry data
_updateCondVar.notify_all();
return MccMountTelemetryErrorCode::ERROR_OK;
}
update_callback_container_t::iterator addCallbackFunc(update_callback_func_t func)
{
std::lock_guard lock{_callbackMutex};
return _callbackFuncs.emplace_back(std::move(func));
}
void delCallbackFunc(update_callback_container_t::iterator iter)
{
std::lock_guard lock{_callbackMutex};
if (_callbackFuncs.size()) {
_callbackFuncs.erase(iter);
}
}
void clearCallbackFuncs()
{
std::lock_guard lock{_callbackMutex};
_callbackFuncs.clear();
}
error_t data(mount_telemetry_data_t& data)
{
std::lock_guard lock{_updateMutex};
data = std::move(_data);
data = _data;
return MccMountTelemetryErrorCode::ERROR_OK;
}
// wait until data is updated or given timeout occurs
template <traits::mcc_time_duration_c DT>
error_t waitForUpdatedData(mount_telemetry_data_t& data, const DT& timeout)
{
auto timeout_tp = std::chrono::steady_clock::now() + timeout;
std::unique_lock lk(_updateMutex);
auto res = _updateCondVar.wait_until(
lk, timeout_tp, [last_time_point = _data.time_point, this]() { return last_time_point < _data.timepoint; });
if (res == std::cv_status::timeout) {
return MccMountTelemetryErrorCode::ERROR_DATA_TIMEOUT;
}
data = _data;
return MccMountTelemetryErrorCode::ERROR_OK;
}
@ -316,10 +300,9 @@ public:
protected:
mount_telemetry_data_t _data{};
hardware_t& _hardware;
update_callback_container_t _callbackFuncs{};
std::mutex _updateMutex;
std::mutex _callbackMutex;
std::condition_variable _updateCondVar;
};

View File

@ -24,6 +24,8 @@ enum class MccSimpleSlewModelErrorCode : int {
ERROR_PEC_COMP,
ERROR_HARDWARE_SETPOS,
ERROR_HARDWARE_GETPOS,
ERROR_SLEW_STOPPED,
ERROR_SLEW_ADJ_MAXITER,
ERROR_SLEW_TIMEOUT
};
@ -50,10 +52,7 @@ namespace mcc
struct MccSimpleSlewModelCategory : public std::error_category {
MccSimpleSlewModelCategory() : 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 +75,12 @@ struct MccSimpleSlewModelCategory : public std::error_category {
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";
}
@ -167,13 +172,22 @@ public:
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<error_t(const slew_point_t&)> _slewFunc{};
std::mutex _updateMutex{};
void init(auto& mount_controls)
{
@ -288,6 +302,10 @@ protected:
};
auto cycle_func = [&](auto t_data) mutable {
if (_stopRequested) {
res_err = MccSimpleSlewModelErrorCode::ERROR_SLEW_STOPPED;
}
// check for prohibited zones
auto t_err = mccCheckInZonePZTuple(*telemetry, p_mount_controls->prohibitedZones, in_zone_flag);
@ -308,6 +326,10 @@ protected:
// t_data was updated in caller!!!
coord_diff2 = coord_diff_func(t_data);
if (_stopRequested) {
res_err = MccSimpleSlewModelErrorCode::ERROR_SLEW_STOPPED;
}
if (coord_diff2 < adj_rad2) { // adjusting mode
in_adj_mode = true;
hw_err = hardware->setPos(adj_ax_pos);
@ -324,7 +346,7 @@ protected:
}
if (i_adj_cycle == slew_point.maxAdjustingCycleNumber) {
// res_err = max iter namber was exceeded
// res_err = max iter number was exceeded
return;
}
} else {
@ -350,13 +372,35 @@ protected:
auto start_poll_tm = std::chrono::steady_clock::now();
// auto timeout_point = std::chrono::steady_clock::now() + slew_point.slewTimeout;
auto wait_timeout = slew_point.slewTimeout / 5;
if (wait_timeout < std::chrono::seconds(1)) {
wait_timeout = std::chrono::seconds(1);
}
// NOTE: TARGET COORDINATES WILL BE UPDATED FOR CURRENT TIME-POINT IN TELEMETRY-CLASS!!!
auto iter = telemetry.addCallbackFunc(cycle_func);
while (true) {
t_err = telemetry.waitForUpdatedData(t_data, wait_timeout);
if (t_err) {
std::string err_str = "An error occured while waiting for updated telemetry";
logError(std::format("An error occured while waiting for updated telemetry: {}", t_err));
if constexpr (std::same_as<decltype(t_err), error_t>) {
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)>) {
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 ((std::chrono::steady_clock::now() - start_poll_tm) > slew_point.slewTimeout) {
logError("Waiting time for completion of slewing expired!");
return MccSimpleSlewModelErrorCode::ERROR_SLEW_TIMEOUT;