This commit is contained in:
Timur A. Fatkhullin 2025-07-30 22:47:55 +03:00
parent c39496ff25
commit ca9dcdfa68
2 changed files with 51 additions and 47 deletions

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 */
@ -31,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";
}
@ -138,11 +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;
typedef std::list<std::shared_ptr<std::packaged_task<update_callback_func_t>>> cc_t;
MccMountTelemetry(astrom_engine_t& astrom_engine, pec_t& pec, hardware_t& hardware)
: base_t(astrom_engine, pec), _hardware(hardware)
{
@ -265,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;
}
@ -315,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

@ -187,6 +187,7 @@ public:
protected:
std::atomic_bool _stopRequested{false};
std::function<error_t(const slew_point_t&)> _slewFunc{};
std::mutex _updateMutex{};
void init(auto& mount_controls)
{
@ -345,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 {
@ -371,14 +372,33 @@ 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.data(t_data);
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) {