Skip to content

Commit c02e8c6

Browse files
jbangeloJADC362
andauthored
Rewrite time module in pure Rust (#125)
The next chunk of changes from #122. This re-implements the time related functionality in native Rust. This one is a bit larger, mostly from the fact that the conversions in the C implementation are very inter-dependent and so several new bits of functionality were pulled in to make the Rust implementation mirror the C implementation. There's a few bits of functionality thrown out because they caused some significant unsoundness, but I'm sure there is still several bits of unsoundness in this translation (e.g. several panics/asserts are still present). I'd prefer to keep this PR as close to a straight translation as possible, and re-consider the soundness concerns in a future PR. ## What's been changed - I don't think any implementations are still present but meaningfully modified ## What's been removed - `GloTime` - GLONASS time is particularly complicated and rarely used - `GpsTime::to_glo()` and `GpsTime::to_glo_hardcoded()` - See above - `impl Sub<GpsTime> for GpsTime` - It turns out a `std::time::Duration` can't hold a negative value. This implementation would occasionally panic because of this. [The `chrono` crate](https://docs.rs/chrono/latest/chrono/#time-delta--duration) handles this better, maybe we can move towards their approach in a later PR? - `UtcParams::decode()` - Decoding low-level messages seem unlikely to be needed - `impl Default for UtcParams` - Defaulting this data can cause unexpected errors - `impl Default for UtcTime` - Defaulting this type can cause unexpected errors ## What's been added - The `time::consts` module - `GpsTime::from_date()` and `GpsTime::from_date_hardcoded()` - `GpsTime::to_mjd()` and `GpsTime::to_mjd_hardcoded()` - `GpsTime::to_date()` and `GpsTime::to_date_hardcoded()` - `UtcTime::to_date()` - `MJD::to_gps()` and `MJD::to_gps_hardcoded()` - `MJD::to_date()` --------- Co-authored-by: Alejandro Duarte <[email protected]>
1 parent 9693bcd commit c02e8c6

File tree

12 files changed

+2343
-1725
lines changed

12 files changed

+2343
-1725
lines changed

swiftnav/src/coords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,8 @@ mod tests {
764764

765765
#[test]
766766
fn coordinate_epoch() {
767-
let initial_epoch = UtcTime::from_date(2020, 1, 1, 0, 0, 0.).to_gps_hardcoded();
768-
let new_epoch = UtcTime::from_date(2021, 1, 1, 0, 0, 0.).to_gps_hardcoded();
767+
let initial_epoch = UtcTime::from_parts(2020, 1, 1, 0, 0, 0.).to_gps_hardcoded();
768+
let new_epoch = UtcTime::from_parts(2021, 1, 1, 0, 0, 0.).to_gps_hardcoded();
769769
let initial_coord = Coordinate::with_velocity(
770770
ReferenceFrame::ITRF2020,
771771
ECEF::new(0.0, 0.0, 0.0),

swiftnav/src/ephemeris.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl Ephemeris {
320320
let result = unsafe {
321321
swiftnav_sys::calc_sat_state(
322322
&self.0,
323-
t.c_ptr(),
323+
&t.to_gps_time_t(),
324324
sat.pos.as_mut_array_ref(),
325325
sat.vel.as_mut_array_ref(),
326326
sat.acc.as_mut_array_ref(),
@@ -348,7 +348,7 @@ impl Ephemeris {
348348
let result = unsafe {
349349
swiftnav_sys::calc_sat_az_el(
350350
&self.0,
351-
t.c_ptr(),
351+
&t.to_gps_time_t(),
352352
pos.as_array_ref(),
353353
swiftnav_sys::satellite_orbit_type_t_MEO,
354354
&mut sat.az,
@@ -377,7 +377,7 @@ impl Ephemeris {
377377
let result = unsafe {
378378
swiftnav_sys::calc_sat_doppler(
379379
&self.0,
380-
t.c_ptr(),
380+
&t.to_gps_time_t(),
381381
pos.as_array_ref(),
382382
vel.as_array_ref(),
383383
swiftnav_sys::satellite_orbit_type_t_MEO,
@@ -401,13 +401,13 @@ impl Ephemeris {
401401

402402
pub fn detailed_status(&self, t: GpsTime) -> Status {
403403
Status::from_ephemeris_status_t(unsafe {
404-
swiftnav_sys::ephemeris_valid_detailed(&self.0, t.c_ptr())
404+
swiftnav_sys::ephemeris_valid_detailed(&self.0, &t.to_gps_time_t())
405405
})
406406
}
407407

408408
/// Is this ephemeris usable?
409409
pub fn is_valid_at_time(&self, t: GpsTime) -> bool {
410-
let result = unsafe { swiftnav_sys::ephemeris_valid(&self.0, t.c_ptr()) };
410+
let result = unsafe { swiftnav_sys::ephemeris_valid(&self.0, &t.to_gps_time_t()) };
411411
result == 1
412412
}
413413

swiftnav/src/ionosphere.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Ionosphere {
7373
/// * IS-GPS-200H, Section 20.3.3.5.1.7
7474
pub fn decode_parameters(words: &[u32; 8]) -> Result<Ionosphere, IonoDecodeFailure> {
7575
let mut iono = Ionosphere(swiftnav_sys::ionosphere_t {
76-
toa: GpsTime::unknown(),
76+
toa: GpsTime::default().to_gps_time_t(),
7777
a0: 0.0,
7878
a1: 0.0,
7979
a2: 0.0,
@@ -104,7 +104,7 @@ impl Ionosphere {
104104
///
105105
/// \return Ionospheric delay distance for GPS L1 frequency \[m\]
106106
pub fn calc_delay(&self, t: &GpsTime, lat_u: f64, lon_u: f64, a: f64, e: f64) -> f64 {
107-
unsafe { swiftnav_sys::calc_ionosphere(t.c_ptr(), lat_u, lon_u, a, e, &self.0) }
107+
unsafe { swiftnav_sys::calc_ionosphere(&t.to_gps_time_t(), lat_u, lon_u, a, e, &self.0) }
108108
}
109109
}
110110

@@ -193,7 +193,7 @@ mod tests {
193193
];
194194
let result = Ionosphere::new(
195195
/* reference data provided by u-blox receiver */
196-
GpsTime::new_unchecked(0, 0.),
196+
GpsTime::default(),
197197
0.0000000111758,
198198
0.0000000223517,
199199
-0.0000000596046,

swiftnav/src/reference_frame/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@
5757
//! let transformation = get_transformation(ReferenceFrame::ITRF2014, ReferenceFrame::NAD83_2011)
5858
//! .unwrap();
5959
//!
60-
//! let epoch_2020 = UtcTime::from_date(2020, 3, 15, 0, 0, 0.).to_gps_hardcoded();
60+
//! let epoch_2020 = UtcTime::from_parts(2020, 3, 15, 0, 0, 0.).to_gps_hardcoded();
6161
//! let itrf_coord = Coordinate::with_velocity(
6262
//! ReferenceFrame::ITRF2014, // The reference frame of the coordinate
6363
//! ECEF::new(-2703764.0, -4261273.0, 3887158.0), // The position of the coordinate
6464
//! ECEF::new(-0.221, 0.254, 0.122), // The velocity of the coordinate
6565
//! epoch_2020); // The epoch of the coordinate
6666
//!
67-
//! let epoch_2010 = UtcTime::from_date(2010, 1, 1, 0, 0, 0.).to_gps_hardcoded();
67+
//! let epoch_2010 = UtcTime::from_parts(2010, 1, 1, 0, 0, 0.).to_gps_hardcoded();
6868
//! let itrf_coord = itrf_coord.adjust_epoch(&epoch_2010); // Change the epoch of the coordinate
6969
//!
7070
//! let nad83_coord = transformation.transform(&itrf_coord);

swiftnav/src/solver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ pub fn calc_pvt(
416416
swiftnav_sys::calc_PVT(
417417
measurements.len() as u8,
418418
meas_ptr,
419-
tor.c_ptr(),
419+
&tor.to_gps_time_t(),
420420
settings.disable_raim,
421421
settings.disable_velocity,
422422
&obs_config,

0 commit comments

Comments
 (0)