This commit is contained in:
Timur A. Fatkhullin
2025-11-17 03:07:54 +03:00
parent 0ce4430668
commit e0c8d8f39b
6 changed files with 133 additions and 161 deletions

View File

@@ -378,40 +378,32 @@ public:
};
/*
_setTargetFunc = [controls, this](MccCelestialPoint const& pt) {
// in the case of apparent input coordinates
// one must ensure the same time points
// arm internal update loop
_internalUpdatingStopSource = std::stop_source{};
_data.target.pair_kind = MccCoordPairKind::COORDS_KIND_RADEC_ICRS;
_data.target.time_point =
std::chrono::time_point_cast<typename decltype(_data.target.time_point)::duration>(pt.time_point);
_internalUpdatingLoopFuture = std::async(
std::launch::async,
[this](std::stop_token stoken) {
while (!(*_internalUpdatingLoopStop)) {
{
std::unique_lock ulock(*_internalUpdatingLoopMutex);
_internalUpdatingLoopCondVar->wait(ulock, [this]() -> bool { return *_dataUpdatingRequested; });
}
auto ret = controls->transformCoordinates(pt, &_data.target);
{
std::lock_guard lock_update(*_updateMutex);
if (!ret) {
if (pt.pair_kind == MccCoordPairKind::COORDS_KIND_RADEC_ICRS) {
_data.target.RA_ICRS = _data.target.X;
_data.target.DEC_ICRS = _data.target.Y;
*_isDataUpdated = false;
_lastUpdateError = _updateFunc(stoken);
}
// update apparent coordinates
ret = _updateTargetFunc(false, {});
} else { // apparent coordinates were computed above
// compute ICRS coordinates
MccCelestialPoint cpt{.pair_kind = MccCoordPairKind::COORDS_KIND_RADEC_ICRS};
ret = controls->transformCoordinates(pt, &cpt);
*_isDataUpdated = true;
_data.target.RA_ICRS = cpt.X;
_data.target.DEC_ICRS = cpt.Y;
// compute only hardware coordinates
ret = _updateTargetFunc(true, {});
// unlock all waiting threads
_updateCondVar->notify_all();
}
}
return mcc_deduce_error_code(ret, MccTelemetryErrorCode::ERROR_COORD_TRANSFORM);
};
*/
},
_internalUpdatingStopSource.get_token());
}
@@ -424,6 +416,8 @@ public:
virtual ~MccTelemetry()
{
*_internalUpdatingLoopStop = true;
stopInternalTelemetryDataUpdating();
if (_internalUpdatingFuture.valid()) {
@@ -552,33 +546,50 @@ public:
error_t updateTelemetryData(traits::mcc_time_duration_c auto const& timeout)
{
{
std::lock_guard thread_lock{*_updateMutex};
std::lock_guard lock(*_internalUpdatingLoopMutex);
*_dataUpdatingRequested = true;
}
std::unique_lock ulock(*_updateMutex);
std::stop_source stop_source;
_internalUpdatingLoopCondVar->notify_one();
*_dataUpdatingRequested = false;
*_isDataUpdated = false;
std::future<error_t> update_ft = std::async(std::launch::async, _updateFunc, stop_source.get_token());
// std::future<error_t> update_ft =
// std::async(std::launch::async, _updateFunc, _internalUpdatingStopSource.get_token());
auto status = update_ft.wait_for(timeout);
if (status == std::future_status::ready) {
*_isDataUpdated = true;
_lastUpdateError = update_ft.get();
} else if (status == std::future_status::deferred) { // std::async was invoked in this thread, get result
_lastUpdateError = update_ft.get();
if (!_lastUpdateError) {
*_isDataUpdated = true;
}
} else { // timeout
stop_source.request_stop();
_lastUpdateError = MccTelemetryErrorCode::ERROR_DATA_TIMEOUT;
}
bool ok = _updateCondVar->wait_for(ulock, timeout, [this]() -> bool { return *_isDataUpdated; });
if (!ok) {
_lastUpdateError = MccTelemetryErrorCode::ERROR_DATA_TIMEOUT;
}
// unblock waiting threads even in the case of timeout!
_updateCondVar->notify_all();
// {
// std::lock_guard thread_lock{*_updateMutex};
// std::stop_source stop_source;
// *_isDataUpdated = false;
// // std::future<error_t> update_ft = std::async(std::launch::async, _updateFunc, stop_source.get_token());
// // // std::future<error_t> update_ft =
// // // std::async(std::launch::async, _updateFunc, _internalUpdatingStopSource.get_token());
// // auto status = update_ft.wait_for(timeout);
// // if (status == std::future_status::ready) {
// // *_isDataUpdated = true;
// // _lastUpdateError = update_ft.get();
// // } else if (status == std::future_status::deferred) { // std::async was invoked in this thread, get
// // result
// // _lastUpdateError = update_ft.get();
// // if (!_lastUpdateError) {
// // *_isDataUpdated = true;
// // }
// // } else { // timeout
// // stop_source.request_stop();
// // _lastUpdateError = MccTelemetryErrorCode::ERROR_DATA_TIMEOUT;
// // }
// *_isDataUpdated = true;
// }
// // unblock waiting threads even in the case of timeout!
// _updateCondVar->notify_all();
// *_isDataUpdated = false;
@@ -731,6 +742,12 @@ protected:
std::unique_ptr<std::mutex> _updateMutex;
std::unique_ptr<std::condition_variable> _updateCondVar;
std::future<void> _internalUpdatingLoopFuture{};
std::unique_ptr<std::mutex> _internalUpdatingLoopMutex{new std::mutex()};
std::unique_ptr<std::condition_variable> _internalUpdatingLoopCondVar{new std::condition_variable()};
std::unique_ptr<std::atomic_bool> _internalUpdatingLoopStop{new std::atomic_bool{false}};
std::unique_ptr<std::atomic_bool> _dataUpdatingRequested{new std::atomic_bool{false}};
error_t _lastUpdateError{MccTelemetryErrorCode::ERROR_OK};
};