1+ use :: glib:: property:: PropertyGet ;
12use gst:: glib;
23use gst:: glib:: WeakRef ;
34use gst:: prelude:: * ;
@@ -6,7 +7,7 @@ use gstrswebrtc::signaller::SignallableExt;
67use gstrswebrtc:: signaller:: Signaller ;
78use gstrswebrtc:: signaller:: WebRTCSignallerRole ;
89use gstwebrtc:: WebRTCDataChannel ;
9- use log:: { debug, error, info, warn} ;
10+ use log:: { debug, error, info, trace , warn} ;
1011use prost:: Message ;
1112use reachy_api:: bridge:: any_command:: Command :: ArmCommand ;
1213use reachy_api:: bridge:: service_response:: Response ;
@@ -15,6 +16,7 @@ use reachy_api::bridge::{AnyCommand, AnyCommands};
1516use reachy_api:: reachy:: kinematics:: Matrix4x4 ;
1617use reachy_api:: reachy:: part:: arm:: ArmCartesianGoal ;
1718use reachy_api:: reachy:: { Reachy , ReachyState , ReachyStatus } ;
19+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
1820use std:: sync:: { Arc , Mutex } ;
1921use std:: time:: Duration ;
2022use std:: time:: Instant ;
@@ -32,6 +34,7 @@ impl Simulator {
3234 peer_id : String ,
3335 rx_stop_signal : std:: sync:: mpsc:: Receiver < bool > ,
3436 frequency : u16 ,
37+ bench_mode : bool ,
3538 ) -> Self {
3639 let main_loop = Arc :: new ( glib:: MainLoop :: new ( None , false ) ) ;
3740 let main_loop_clone = main_loop. clone ( ) ;
@@ -46,8 +49,13 @@ impl Simulator {
4649 signaller. set_property ( "producer-peer-id" , & peer_id) ;
4750
4851 let _reachy: Arc < Mutex < Option < Reachy > > > = Arc :: new ( Mutex :: new ( None ) ) ;
49- let ( pipeline, webrtcbin) =
50- Simulator :: setup_webrtc ( & peer_id, _reachy. clone ( ) , main_loop. clone ( ) , frequency) ;
52+ let ( pipeline, webrtcbin) = Simulator :: setup_webrtc (
53+ & peer_id,
54+ _reachy. clone ( ) ,
55+ main_loop. clone ( ) ,
56+ frequency,
57+ bench_mode,
58+ ) ;
5159
5260 signaller. connect_closure (
5361 "error" ,
@@ -137,6 +145,7 @@ impl Simulator {
137145 reachy : Arc < Mutex < Option < Reachy > > > ,
138146 main_loop : Arc < glib:: MainLoop > ,
139147 frequency : u16 ,
148+ bench_mode : bool ,
140149 ) -> ( gst:: Pipeline , gst:: Element ) {
141150 let pipeline = gst:: Pipeline :: builder ( )
142151 . name ( format ! ( "session-pipeline-{peer_id}" ) )
@@ -155,7 +164,13 @@ impl Simulator {
155164 match ret {
156165 Ok ( gst:: StateChangeSuccess :: Success ) | Ok ( gst:: StateChangeSuccess :: Async ) => {
157166 // Pipeline state changed successfully
158- Simulator :: configure_data_channels ( webrtcbin_ref, reachy, main_loop, frequency) ;
167+ Simulator :: configure_data_channels (
168+ webrtcbin_ref,
169+ reachy,
170+ main_loop,
171+ frequency,
172+ bench_mode,
173+ ) ;
159174 }
160175 Ok ( gst:: StateChangeSuccess :: NoPreroll ) => {
161176 error ! ( "Failed to transition pipeline to PLAYING: No preroll data available" ) ;
@@ -172,6 +187,7 @@ impl Simulator {
172187 reachy : Arc < Mutex < Option < Reachy > > > ,
173188 main_loop : Arc < glib:: MainLoop > ,
174189 frequency : u16 ,
190+ bench_mode : bool ,
175191 ) {
176192 webrtcbin. upgrade ( ) . unwrap ( ) . connect_closure (
177193 "on-data-channel" ,
@@ -196,7 +212,13 @@ impl Simulator {
196212 Simulator :: configure_reachy_audit_channel( channel) ;
197213 } else if label. starts_with( "reachy_command_lossy" ) {
198214 debug!( "Received reachy command lossy data channel" ) ;
199- Simulator :: send_commands( channel. clone( ) , reachy, main_loop, frequency) ;
215+ Simulator :: send_commands(
216+ channel. clone( ) ,
217+ reachy,
218+ main_loop,
219+ frequency,
220+ bench_mode,
221+ ) ;
200222 } else if label. starts_with( "reachy_command_reliable" ) {
201223 debug!( "Received reachy command reliable data channel" ) ;
202224 Simulator :: turn_on_arms( channel, reachy) ;
@@ -213,16 +235,37 @@ impl Simulator {
213235 reachy : Arc < Mutex < Option < Reachy > > > ,
214236 main_loop : Arc < glib:: MainLoop > ,
215237 frequency : u16 ,
238+ bench_mode : bool ,
216239 ) {
217240 let radius = 0.2f64 ; //Circle radius
218241 let fixed_x = 0.4f64 ; // Fixed x-coordinate
219242 let center_y = 0f64 ;
220243 let center_z = 0.1f64 ; // Center of the circle in y-z plane
221- //let frequency = 100; //Update frequency in Hz
222- let sample_duration = Duration :: from_millis ( 1000 / frequency as u64 ) ;
244+ //let mut frequency = frequency as u64; //Update frequency in Hz
245+ let frequency = Arc :: new ( AtomicU64 :: new ( frequency as u64 ) ) ;
246+ let frequency_clone = frequency. clone ( ) ;
247+ //let mut sample_duration = Duration::from_millis(1000 / frequency);
223248 let circle_period = 3f64 ;
224249 let t0 = Instant :: now ( ) ;
225250
251+ let main_loop_clone = main_loop. clone ( ) ;
252+ if bench_mode {
253+ std:: thread:: spawn ( move || {
254+ while main_loop_clone. is_running ( ) {
255+ std:: thread:: sleep ( Duration :: from_millis ( 50 ) ) ;
256+
257+ let mut frequency_local = frequency_clone. load ( Ordering :: Relaxed ) ;
258+ frequency_local += 1 ;
259+ //debug!("frequency: {}", frequency_local);
260+ if frequency_local > 1500 {
261+ main_loop_clone. quit ( ) ;
262+ } else {
263+ frequency_clone. store ( frequency_local, Ordering :: Relaxed ) ;
264+ }
265+ }
266+ } ) ;
267+ }
268+
226269 std:: thread:: spawn ( move || {
227270 while main_loop. is_running ( ) {
228271 let elapsed_time = t0. elapsed ( ) ;
@@ -315,7 +358,18 @@ impl Simulator {
315358 } ;
316359 let data = glib:: Bytes :: from_owned ( commands. encode_to_vec ( ) ) ;
317360 channel. send_data ( Some ( & data) ) ;
361+ let sample_duration =
362+ Duration :: from_millis ( 1000 / frequency. load ( Ordering :: Relaxed ) ) ;
318363 std:: thread:: sleep ( sample_duration) ;
364+ /*if bench_mode {
365+ //sample_duration += Duration::from_millis(100);
366+ frequency += 1;
367+ sample_duration = Duration::from_millis(1000 / frequency);
368+ //debug!("duration millis: {}", sample_duration.as_millis());
369+ if frequency > 1500 {
370+ main_loop.quit();
371+ }
372+ }*/
319373 }
320374 } ) ;
321375 }
@@ -377,7 +431,7 @@ impl Simulator {
377431 }
378432 } ;
379433
380- debug ! ( "Received state: {:?}" , state) ;
434+ trace ! ( "Received state: {:?}" , state) ;
381435 } ) ;
382436 }
383437
@@ -395,7 +449,7 @@ impl Simulator {
395449 }
396450 } ;
397451
398- debug ! ( "Received status: {:?}" , status) ;
452+ trace ! ( "Received status: {:?}" , status) ;
399453 } ) ;
400454 }
401455
0 commit comments