Skip to content

Commit 18bd04f

Browse files
authored
Dep/hifitime (#13)
* move hifitime to master branch * dep/hifitime integration for testing --------- Signed-off-by: Guillaume W. Bres <[email protected]>
1 parent edbeeb2 commit 18bd04f

File tree

3 files changed

+46
-147
lines changed

3 files changed

+46
-147
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ html = [
3636
[dependencies]
3737
thiserror = "2"
3838
maud = { version = "0.26", optional = true }
39-
hifitime = { version = "4.0", optional = true }
40-
gnss-rs = { version = "2.3.5", features = ["serde", "domes", "cospar"], optional = true }
39+
hifitime = { git = "https://github.com/nyx-space/hifitime", branch = "master", optional = true }
40+
gnss-rs = { git = "https://github.com/rtk-rs/gnss", branch = "dep/hifitime", features = ["serde", "domes", "cospar"], optional = true }

src/processing/time/absolute.rs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ impl GnssAbsoluteTime {
2424
/// Add a new [TimePolynomial] to this management pool.
2525
/// Usually right after its publication.
2626
pub fn add_polynomial(&mut self, polynomial: TimePolynomial) {
27-
self.polynomials
28-
.retain(|t| t.ref_epoch.time_scale != polynomial.ref_epoch.time_scale);
27+
self.polynomials.retain(|poly| {
28+
let same_ref = poly.ref_epoch.time_scale == polynomial.ref_epoch.time_scale;
29+
30+
let same_lhs = poly.ref_epoch.time_scale == polynomial.ref_epoch.time_scale;
31+
!(same_ref && same_lhs)
32+
});
2933

3034
self.polynomials.push(polynomial);
3135
}
@@ -37,43 +41,25 @@ impl GnssAbsoluteTime {
3741
return Some(t);
3842
}
3943

40-
if let Some(polynomials) = self
44+
if let Some(poly) = self
4145
.polynomials
4246
.iter()
4347
.find(|poly| poly.lhs_timescale == t.time_scale && poly.ref_epoch.time_scale == target)
4448
{
45-
Some(polynomials.epoch_time_correction(true, t, target))
46-
} else if let Some(polynomials) = self
49+
Some(
50+
t.precise_timescale_conversion(true, poly.ref_epoch, poly.polynomial, target)
51+
.unwrap(),
52+
)
53+
} else if let Some(poly) = self
4754
.polynomials
4855
.iter()
4956
.find(|poly| poly.ref_epoch.time_scale == t.time_scale && poly.lhs_timescale == target)
5057
{
51-
Some(polynomials.epoch_time_correction(false, t, target))
58+
Some(
59+
t.precise_timescale_conversion(false, poly.ref_epoch, poly.polynomial, target)
60+
.unwrap(),
61+
)
5262
} else {
53-
//GPST-UTC=a
54-
//UTC=GPST-a = forward
55-
//GPST=a+UTC = backward
56-
57-
// try to form a combination
58-
for poly_1 in self.polynomials.iter() {
59-
if poly_1.lhs_timescale == t.time_scale && poly_1.ref_epoch.time_scale != target {
60-
for poly_2 in self.polynomials.iter() {
61-
if poly_2.lhs_timescale != t.time_scale
62-
&& poly_2.ref_epoch.time_scale == target
63-
{
64-
// GPST-UTC
65-
// GPST-GST=a1 GST-UTC=a2
66-
// GPST-GST + GST-UTC a1 + GST + a2-GST
67-
let mut correction = poly_1.correction_duration(t);
68-
correction += poly_2.correction_duration(t);
69-
70-
let converted = t.to_time_scale(target);
71-
return Some(converted + correction);
72-
}
73-
}
74-
}
75-
}
76-
7763
None
7864
}
7965
}
@@ -82,15 +68,19 @@ impl GnssAbsoluteTime {
8268
#[cfg(test)]
8369
mod test {
8470
use crate::{GnssAbsoluteTime, TimePolynomial};
85-
use hifitime::{Epoch, TimeScale};
71+
use hifitime::{Duration, Epoch, Polynomial, TimeScale};
8672
use std::str::FromStr;
8773

8874
#[test]
8975
fn test_direct_absolute_time() {
9076
let polynomials = [TimePolynomial {
9177
lhs_timescale: TimeScale::GST,
9278
ref_epoch: Epoch::from_str("2020-01-01T00:00:00 GPST").unwrap(),
93-
polynomials: (1.0E-9, 1.0E-10, 0.0),
79+
polynomial: Polynomial {
80+
constant: Duration::from_seconds(1.0E-9),
81+
rate: Duration::from_seconds(1.0E-10),
82+
accel: Duration::from_seconds(0.0),
83+
},
9484
}];
9585

9686
let solver = GnssAbsoluteTime::new(&polynomials);
@@ -117,12 +107,20 @@ mod test {
117107
TimePolynomial {
118108
lhs_timescale: TimeScale::GST,
119109
ref_epoch: Epoch::from_str("2020-01-01T00:00:00 UTC").unwrap(),
120-
polynomials: (1.0E-9, 1.0E-10, 0.0),
110+
polynomial: Polynomial {
111+
constant: Duration::from_seconds(1.0E-9),
112+
rate: Duration::from_seconds(1.0E-10),
113+
accel: Duration::from_seconds(0.0),
114+
},
121115
},
122116
TimePolynomial {
123117
lhs_timescale: TimeScale::UTC,
124118
ref_epoch: Epoch::from_str("2020-01-01T00:00:00 GPST").unwrap(),
125-
polynomials: (2.0E-9, 2.0E-10, 0.0),
119+
polynomial: Polynomial {
120+
constant: Duration::from_seconds(2.0E-9),
121+
rate: Duration::from_seconds(2.0E-10),
122+
accel: Duration::from_seconds(0.0),
123+
},
126124
},
127125
];
128126

@@ -154,11 +152,12 @@ mod test {
154152

155153
assert_eq!(t_gpst_utc.time_scale, TimeScale::UTC);
156154

157-
let t_gst_gpst = solver
158-
.epoch_time_correction(t_gst, TimeScale::GPST)
159-
.unwrap();
155+
// not feasible yet
156+
// let t_gst_gpst = solver
157+
//  .epoch_time_correction(t_gst, TimeScale::GPST)
158+
//  .unwrap();
160159

161-
assert_eq!(t_gst_gpst.time_scale, TimeScale::GPST);
160+
// assert_eq!(t_gst_gpst.time_scale, TimeScale::GPST);
162161

163162
// let t_gst_gpst = solver.epoch_time_correction(t_gst, TimeScale::GPST)
164163
// .unwrap();

src/processing/time/polynomial.rs

Lines changed: 7 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hifitime::{Duration, Epoch, TimeScale, Unit};
1+
use hifitime::{Epoch, Polynomial, TimeScale};
22

33
/// [TimePolynomial] allows precise [Epoch] translation to another [TimeScale].
44
/// For example, |[TimeScale::GPST]-[TimeScale::UTC]| when referencing [TimeScale::GPST] to [TimeScale::UTC].
@@ -9,8 +9,8 @@ pub struct TimePolynomial {
99
/// Reference [Epoch] expressed in the [TimeScale] in which the polynomials apply,
1010
/// with 1 ns accuracy.
1111
pub ref_epoch: Epoch,
12-
/// Polynomials (s, s.s⁻¹, s.s⁻²)
13-
pub polynomials: (f64, f64, f64),
12+
/// [Polynomial]
13+
pub polynomial: Polynomial,
1414
}
1515

1616
impl TimePolynomial {
@@ -21,14 +21,14 @@ impl TimePolynomial {
2121
ref_week: u32,
2222
ref_tow: u64,
2323
ref_timescale: TimeScale,
24-
polynomials: (f64, f64, f64),
24+
polynomial: Polynomial,
2525
) -> Self {
2626
Self::from_reference_time_of_week_nanos(
2727
lhs_timescale,
2828
ref_week,
2929
ref_tow * 1_000_000_000,
3030
ref_timescale,
31-
polynomials,
31+
polynomial,
3232
)
3333
}
3434

@@ -39,114 +39,14 @@ impl TimePolynomial {
3939
ref_week: u32,
4040
ref_tow: u64,
4141
ref_timescale: TimeScale,
42-
polynomials: (f64, f64, f64),
42+
polynomial: Polynomial,
4343
) -> Self {
4444
let ref_epoch = Epoch::from_time_of_week(ref_week, ref_tow, ref_timescale);
4545

4646
Self {
4747
ref_epoch,
4848
lhs_timescale,
49-
polynomials,
49+
polynomial,
5050
}
5151
}
52-
53-
pub fn correction_duration(&self, t: Epoch) -> Duration {
54-
let t = t.to_time_scale(self.lhs_timescale);
55-
let t_ref = self.ref_epoch.to_time_scale(self.lhs_timescale);
56-
57-
let dt_s = (t - t_ref).to_seconds();
58-
let (a0, a1, a2) = self.polynomials;
59-
60-
Duration::from_seconds(a0 + a1 * dt_s + a2 * dt_s.powi(2))
61-
}
62-
63-
/// Converts and corrects provided [Epoch] into provided [TimeScale], with 1 ns accuracy.
64-
pub fn epoch_time_correction(&self, forward: bool, t: Epoch, timescale: TimeScale) -> Epoch {
65-
let t_ref = self.ref_epoch.to_time_scale(t.time_scale);
66-
67-
// supports any offset to reference instant.
68-
// While usually this should remain within the same week (or even much closer)
69-
let dt_s = (t - t_ref).to_seconds();
70-
71-
let (a0, a1, a2) = self.polynomials;
72-
let dt_s = a0 + a1 * dt_s + a2 * dt_s.powi(2);
73-
74-
let converted = t.to_time_scale(timescale);
75-
76-
if forward {
77-
converted - dt_s * Unit::Second
78-
} else {
79-
converted + dt_s * Unit::Second
80-
}
81-
}
82-
}
83-
84-
#[cfg(test)]
85-
mod test {
86-
use crate::TimePolynomial;
87-
use hifitime::{Epoch, TimeScale, Unit};
88-
89-
#[test]
90-
fn test_time_polynomials() {
91-
let t_ref_gpst = Epoch::from_gregorian(2020, 1, 1, 0, 0, 0, 0, TimeScale::GPST);
92-
93-
let (a0, a1, a2) = (1.0E-9, 1.0E-10, 0.0);
94-
95-
let time_offset = TimePolynomial {
96-
ref_epoch: t_ref_gpst,
97-
polynomials: (a0, a1, a2),
98-
lhs_timescale: TimeScale::UTC,
99-
};
100-
101-
// +1 s
102-
let t = t_ref_gpst + 1.0 * Unit::Second;
103-
104-
let t_utc = time_offset.epoch_time_correction(true, t, TimeScale::UTC);
105-
106-
assert_eq!(t_utc.time_scale, TimeScale::UTC);
107-
108-
assert_eq!(
109-
t_utc,
110-
t.to_time_scale(TimeScale::UTC) - (a0 * Unit::Second + 1.0 * Unit::Second * a1)
111-
);
112-
113-
let reversed = time_offset.epoch_time_correction(false, t_utc, TimeScale::GPST);
114-
assert_eq!(reversed, t);
115-
116-
// test backwards
117-
let backwards = time_offset.epoch_time_correction(false, t, TimeScale::GPST);
118-
assert_eq!(backwards.time_scale, TimeScale::GPST);
119-
120-
// +1 ns
121-
let t = t_ref_gpst + 1.0 * Unit::Nanosecond;
122-
123-
let t_utc = time_offset.epoch_time_correction(true, t, TimeScale::UTC);
124-
125-
assert_eq!(t_utc.time_scale, TimeScale::UTC);
126-
127-
let correction_s = a0 + 1.0E-9 * a1;
128-
assert_eq!(
129-
t_utc,
130-
t.to_time_scale(TimeScale::UTC) - correction_s * Unit::Second
131-
);
132-
133-
let reversed = time_offset.epoch_time_correction(false, t_utc, TimeScale::GPST);
134-
assert_eq!(reversed, t);
135-
136-
// +1 us
137-
let t = t_ref_gpst + 1000.0 * Unit::Nanosecond;
138-
139-
let t_utc = time_offset.epoch_time_correction(true, t, TimeScale::UTC);
140-
141-
assert_eq!(t_utc.time_scale, TimeScale::UTC);
142-
143-
let correction_s = a0 + 1000.0E-9 * a1;
144-
assert_eq!(
145-
t_utc,
146-
t.to_time_scale(TimeScale::UTC) - correction_s * Unit::Second
147-
);
148-
149-
let reversed = time_offset.epoch_time_correction(false, t_utc, TimeScale::GPST);
150-
assert_eq!(reversed, t);
151-
}
15252
}

0 commit comments

Comments
 (0)