1- use crate :: {
2- clock:: { Clock , ClockType } ,
3- RclrsError ,
4- } ;
1+ use crate :: clock:: { Clock , ClockType } ;
52use crate :: { Node , ParameterValue , QoSProfile , Subscription , QOS_PROFILE_CLOCK } ;
63use rosgraph_msgs:: msg:: Clock as ClockMsg ;
74use std:: fmt;
@@ -72,7 +69,7 @@ impl TimeSourceBuilder {
7269 }
7370
7471 /// Builds the `TimeSource` and attaches the provided `Node` and `Clock`.
75- pub fn build ( self ) -> TimeSource {
72+ pub fn build ( self ) -> Result < TimeSource , ClockMismatchError > {
7673 let mut source = TimeSource {
7774 _node : self . node . clone ( ) ,
7875 _clocks : Arc :: new ( Mutex :: new ( vec ! [ ] ) ) ,
@@ -82,12 +79,15 @@ impl TimeSourceBuilder {
8279 _clock_subscription : None ,
8380 _last_time_msg : Arc :: new ( Mutex :: new ( None ) ) ,
8481 } ;
85- source. attach_clock ( self . clock ) ;
82+ source. attach_clock ( self . clock ) ? ;
8683 source. attach_node ( self . node ) ;
87- source
84+ Ok ( source)
8885 }
8986}
9087
88+ /// Error created when a non RosTime clock is attached but the `use_sim_time` parameter is set.
89+ /// This case could behave in unexpected ways since the /clock topic would be ignored even though
90+ /// the user explicitly requested to use it through a command line argument.
9191#[ derive( Debug ) ]
9292pub struct ClockMismatchError ( ClockType ) ;
9393
@@ -104,7 +104,7 @@ impl fmt::Display for ClockMismatchError {
104104impl TimeSource {
105105 /// Creates a new time source with default parameters.
106106 pub fn new ( node : Arc < Node > , clock : Arc < Clock > ) -> Self {
107- TimeSourceBuilder :: new ( node, clock) . build ( )
107+ TimeSourceBuilder :: new ( node, clock) . build ( ) . unwrap ( )
108108 }
109109
110110 /// Attaches the given clock to the `TimeSource`, enabling the `TimeSource` to control it.
@@ -138,23 +138,22 @@ impl TimeSource {
138138
139139 /// Attaches the given node to to the `TimeSource`, using its interface to read the
140140 /// `use_sim_time` parameter and create the clock subscription.
141- pub fn attach_node ( & mut self , node : Arc < Node > ) -> Result < ( ) , RclrsError > {
141+ pub fn attach_node ( & mut self , node : Arc < Node > ) {
142142 self . _node = node;
143143
144144 // TODO(luca) register a parameter callback
145145 if let Some ( sim_param) = self . _node . get_parameter ( "use_sim_time" ) {
146146 match sim_param {
147147 ParameterValue :: Bool ( val) => {
148- self . set_ros_time ( val) ? ;
148+ self . set_ros_time ( val) ;
149149 }
150150 // TODO(luca) more graceful error handling?
151151 _ => panic ! ( "use_sim_time parameter must be boolean" ) ,
152152 }
153153 }
154- Ok ( ( ) )
155154 }
156155
157- fn set_ros_time ( & mut self , enable : bool ) -> Result < ( ) , RclrsError > {
156+ fn set_ros_time ( & mut self , enable : bool ) {
158157 let updated = self
159158 . _ros_time_active
160159 . fetch_update ( Ordering :: Relaxed , Ordering :: Relaxed , |prev| {
@@ -169,13 +168,8 @@ impl TimeSource {
169168 for clock in self . _clocks . lock ( ) . unwrap ( ) . iter ( ) {
170169 clock. set_ros_time ( enable) ;
171170 }
172- self . _clock_subscription = if enable {
173- Some ( self . create_clock_sub ( ) ?)
174- } else {
175- None
176- } ;
171+ self . _clock_subscription = enable. then ( || self . create_clock_sub ( ) ) ;
177172 }
178- Ok ( ( ) )
179173 }
180174
181175 fn update_clock ( clock : & Arc < Clock > , nanoseconds : i64 ) {
@@ -189,22 +183,21 @@ impl TimeSource {
189183 }
190184 }
191185
192- fn create_clock_sub ( & self ) -> Result < Arc < Subscription < ClockMsg > > , RclrsError > {
186+ fn create_clock_sub ( & self ) -> Arc < Subscription < ClockMsg > > {
193187 let ros_time_active = self . _ros_time_active . clone ( ) ;
194188 let clocks = self . _clocks . clone ( ) ;
195189 let last_time_msg = self . _last_time_msg . clone ( ) ;
196- self . _node . create_subscription :: < ClockMsg , _ > (
197- "/clock" ,
198- self . _clock_qos ,
199- move |msg : ClockMsg | {
190+ // Safe to unwrap since the function will only fail if invalid arguments are provided
191+ self . _node
192+ . create_subscription :: < ClockMsg , _ > ( "/clock" , self . _clock_qos , move |msg : ClockMsg | {
200193 if ros_time_active. load ( Ordering :: Relaxed ) {
201194 let nanoseconds: i64 =
202195 ( msg. clock . sec as i64 * 1_000_000_000 ) + msg. clock . nanosec as i64 ;
203196 * last_time_msg. lock ( ) . unwrap ( ) = Some ( msg) ;
204197 Self :: update_all_clocks ( & clocks, nanoseconds) ;
205198 }
206- } ,
207- )
199+ } )
200+ . unwrap ( )
208201 }
209202}
210203
0 commit comments