@@ -16,11 +16,13 @@ namespace UnitySensors.ROS.Publisher
1616
1717 [ SerializeField ]
1818 protected T _serializer ;
19+ private static int _publisher_count = 0 ;
1920
2021 private ROSConnection _ros ;
2122 private float _dt ;
2223 private float _frequency_inv ;
2324 private RosTopicState _topicState ;
25+ private int _publisher_id ;
2426
2527 public string topicName { get => _topicName ; set => _topicName = value ; }
2628 public float frequency
@@ -30,16 +32,39 @@ public float frequency
3032 {
3133 _frequency = Mathf . Max ( value , 0 ) ;
3234 _frequency_inv = 1.0f / _frequency ;
35+ InitializePublisherOffset ( ) ;
3336 }
3437 }
35-
36- protected virtual void Start ( )
38+ private void Awake ( )
3739 {
38- _dt = 0.0f ;
3940 _frequency_inv = 1.0f / _frequency ;
41+ _publisher_id = _publisher_count ;
42+ _publisher_count ++ ;
4043
41- _ros = ROSConnection . GetOrCreateInstance ( ) ;
44+ InitializePublisherOffset ( ) ;
45+ }
46+
47+ private void InitializePublisherOffset ( )
48+ {
49+ string publisherType = GetType ( ) . Name ;
50+ int typeHash = publisherType . GetHashCode ( ) ;
51+
52+ // Combine publisher ID and type to create a more dispersed value
53+ // Use coprime numbers and operations to increase dispersion
54+ float seed = ( _publisher_id * 16777619 + typeHash ) * 0.618033988749895f ;
4255
56+ // Ensure the offset is in [0, 1)
57+ float normalizedOffset = seed % 1.0f ;
58+ if ( normalizedOffset < 0 ) normalizedOffset += 1.0f ; // Ensure non-negative
59+
60+ _dt = normalizedOffset * _frequency_inv ;
61+
62+ Debug . Log ( $ "Publisher { GetType ( ) . Name } ID:{ _publisher_id } initialized with offset { normalizedOffset : F3} ({ _dt : F3} s)") ;
63+ }
64+
65+ protected virtual void Start ( )
66+ {
67+ _ros = ROSConnection . GetOrCreateInstance ( ) ;
4368 _serializer . Init ( ) ;
4469 }
4570
0 commit comments