Skip to content

Commit bdafcc7

Browse files
committed
Predictive PEC: use the monotonic clock rather than the system wall clock
GP guider needs accurate time interval measurements, and is not interested in the wall clock. std::chrono::steady_clock is a better choice than system_clock. Testing: - gp guider unit tests pass
1 parent 4b08eff commit bdafcc7

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

contributions/MPI_IS_gaussian_process/src/gaussian_process_guider.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@
6161
#define HYSTERESIS 0.1 // for the hybrid mode
6262

6363
GaussianProcessGuider::GaussianProcessGuider(guide_parameters parameters)
64-
: start_time_(std::chrono::system_clock::now()), last_time_(std::chrono::system_clock::now()), control_signal_(0),
65-
prediction_(0), last_prediction_end_(0), dither_steps_(0), dithering_active_(false), dither_offset_(0.0),
66-
circular_buffer_data_(CIRCULAR_BUFFER_SIZE), covariance_function_(), output_covariance_function_(),
67-
gp_(covariance_function_), learning_rate_(DEFAULT_LEARNING_RATE), parameters(parameters)
64+
: start_time_(clock::now()), last_time_(clock::now()), control_signal_(0), prediction_(0), last_prediction_end_(0),
65+
dither_steps_(0), dithering_active_(false), dither_offset_(0.0), circular_buffer_data_(CIRCULAR_BUFFER_SIZE),
66+
covariance_function_(), output_covariance_function_(), gp_(covariance_function_), learning_rate_(DEFAULT_LEARNING_RATE),
67+
parameters(parameters)
6868
{
6969
circular_buffer_data_.push_front(data_point()); // add first point
7070
circular_buffer_data_[0].control = 0; // set first control to zero
@@ -86,7 +86,7 @@ GaussianProcessGuider::~GaussianProcessGuider() { }
8686

8787
void GaussianProcessGuider::SetTimestamp()
8888
{
89-
auto current_time = std::chrono::system_clock::now();
89+
auto current_time = clock::now();
9090
double delta_measurement_time = std::chrono::duration<double>(current_time - last_time_).count();
9191
last_time_ = current_time;
9292
get_last_point().timestamp = std::chrono::duration<double>(current_time - start_time_).count() -
@@ -238,7 +238,7 @@ double GaussianProcessGuider::PredictGearError(double prediction_location)
238238
// in the first step of each sequence, use the current time stamp as last prediction end
239239
if (last_prediction_end_ < 0.0)
240240
{
241-
last_prediction_end_ = std::chrono::duration<double>(std::chrono::system_clock::now() - start_time_).count();
241+
last_prediction_end_ = std::chrono::duration<double>(clock::now() - start_time_).count();
242242
}
243243

244244
// prediction from the last endpoint to the prediction point
@@ -296,7 +296,7 @@ double GaussianProcessGuider::result(double input, double SNR, double time_step,
296296
// the starting time is set at the first call of result after startup or reset
297297
if (get_number_of_measurements() == 1)
298298
{
299-
start_time_ = std::chrono::system_clock::now();
299+
start_time_ = clock::now();
300300
last_time_ = start_time_; // this is OK, since last_time_ only provides a minor correction
301301
}
302302

@@ -325,7 +325,7 @@ double GaussianProcessGuider::result(double input, double SNR, double time_step,
325325
{
326326
if (prediction_point < 0.0)
327327
{
328-
prediction_point = std::chrono::duration<double>(std::chrono::system_clock::now() - start_time_).count();
328+
prediction_point = std::chrono::duration<double>(clock::now() - start_time_).count();
329329
}
330330
// the point of highest precision shoud be between now and the next step
331331
UpdateGP(prediction_point + 0.5 * time_step);
@@ -380,7 +380,7 @@ double GaussianProcessGuider::deduceResult(double time_step, double prediction_p
380380
{
381381
if (prediction_point < 0.0)
382382
{
383-
prediction_point = std::chrono::duration<double>(std::chrono::system_clock::now() - start_time_).count();
383+
prediction_point = std::chrono::duration<double>(clock::now() - start_time_).count();
384384
}
385385
// the point of highest precision should be between now and the next step
386386
UpdateGP(prediction_point + 0.5 * time_step);
@@ -416,8 +416,8 @@ void GaussianProcessGuider::reset()
416416
circular_buffer_data_[0].control = 0; // set first control to zero
417417

418418
last_prediction_end_ = -1.0; // the negative value signals we didn't predict yet
419-
start_time_ = std::chrono::system_clock::now();
420-
last_time_ = std::chrono::system_clock::now();
419+
start_time_ = clock::now();
420+
last_time_ = clock::now();
421421

422422
dither_offset_ = 0.0;
423423
dither_steps_ = 0;
@@ -573,7 +573,7 @@ void GaussianProcessGuider::inject_data_point(double timestamp, double input, do
573573
last_prediction_end_ = timestamp;
574574
get_last_point().timestamp = timestamp; // overrides the usual HandleTimestamps();
575575

576-
start_time_ = std::chrono::system_clock::now() - std::chrono::seconds((int) timestamp);
576+
start_time_ = clock::now() - std::chrono::seconds((int) timestamp);
577577

578578
add_one_point(); // add new point here, since the control is for the next point in time
579579
HandleControls(control); // already store control signal

contributions/MPI_IS_gaussian_process/src/gaussian_process_guider.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ enum Hyperparameters
7171
class GaussianProcessGuider
7272
{
7373
public:
74+
typedef std::chrono::steady_clock clock;
75+
7476
struct data_point
7577
{
7678
double timestamp;
@@ -113,8 +115,8 @@ class GaussianProcessGuider
113115
};
114116

115117
private:
116-
std::chrono::system_clock::time_point start_time_; // reference time
117-
std::chrono::system_clock::time_point last_time_;
118+
clock::time_point start_time_; // reference time
119+
clock::time_point last_time_;
118120

119121
double control_signal_;
120122
double prediction_;

contributions/MPI_IS_gaussian_process/tests/gaussian_process/gp_guider_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ TEST_F(GPGTest, timer_test)
244244
GPG->result(1.0, 2.0, 3.0);
245245
std::this_thread::sleep_for(std::chrono::milliseconds(wait));
246246

247-
auto time_start = std::chrono::system_clock::now();
247+
auto time_start = GaussianProcessGuider::clock::now();
248248
GPG->result(1.0, 2.0, 3.0);
249249
double first_time = GPG->get_second_last_point().timestamp;
250250
std::this_thread::sleep_for(std::chrono::milliseconds(wait));
251-
auto time_end = std::chrono::system_clock::now();
251+
auto time_end = GaussianProcessGuider::clock::now();
252252
GPG->result(1.0, 2.0, 3.0);
253253
double second_time = GPG->get_second_last_point().timestamp;
254254

src/guide_algorithm_gaussian_process.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ void GuideAlgorithmGaussianProcess::GuidingStarted()
10191019
bool need_reset = true;
10201020
double ra_offset; // RA delta in SI seconds
10211021

1022-
auto now = std::chrono::system_clock::now();
1022+
auto now = std::chrono::steady_clock::now();
10231023

10241024
double prev_ra = guiding_ra_;
10251025
guiding_ra_ = CurrentRA();
@@ -1092,7 +1092,7 @@ void GuideAlgorithmGaussianProcess::GuidingStopped()
10921092
double period_length = GPG->GetGPHyperparameters()[PKPeriodLength];
10931093
pConfig->Profile.SetDouble(GetConfigPath() + "/gp_period_per_kern", period_length);
10941094

1095-
guiding_stopped_time_ = std::chrono::system_clock::now();
1095+
guiding_stopped_time_ = std::chrono::steady_clock::now();
10961096
}
10971097

10981098
void GuideAlgorithmGaussianProcess::GuidingPaused() { }

src/guide_algorithm_gaussian_process.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class GuideAlgorithmGaussianProcess : public GuideAlgorithm
122122
bool block_updates_; // Don't update GP if guiding is disabled
123123
double guiding_ra_; // allow resuming guiding after guiding stopped if there is no change in RA
124124
PierSide guiding_pier_side_;
125-
std::chrono::system_clock::time_point guiding_stopped_time_; // time guiding stopped
125+
std::chrono::steady_clock::time_point guiding_stopped_time_; // time guiding stopped
126126

127127
protected:
128128
double GetControlGain() const;

0 commit comments

Comments
 (0)