1

I have Rosbag file which contains messages on various topics, each topic has its own frequency. This data has been captured from a hardware device streaming data, and data from all topics would "reach" at the same time to be used for different algorithms.
I wish to simulate this using the rosbag file(think of it as every topic has associated an array of data) and it is imperative that this data streaming process start at the same time so that the data can be in sync.

I do this via launching different publishers on different threads (I am open to other approaches as well, this was the only one I could think of.), but the threads do not start at the same time, by the time thread 3 starts, thread 1 would be considerably ahead.

How may I achieve this?

Edit - I understand that launching at the exact same time is not possible, but maybe I can get away with a launch extremely close to each other as well. Is there any way to ensure this?

Edit2 - Since the main aim is to get the data stream in Sync, I was wondering about the warmup effect of the thread(suppose a thread1 starts from 3.3GHz and reaches to 4.2GHz by the time thread2 starts at 3.2). Would this have a significant effect (I can always warm them up before starting the publishing process, but I am curious whether it would have a pronounced effect)

TIA

Atharva Dubey
  • 832
  • 1
  • 8
  • 25
  • 3
    Simply, you can't. That is physically impossible – RoQuOTriX Sep 29 '21 at 05:43
  • 8
    Use a barrier to synchronize multiple threads at a check point. However, even then, when you release all of them there's no guarantee they'll all be scheduled by the OS when you want them. Short of running on a hard realtime OS, I don't see how you can absolutely guarantee that otherwise. – Tanveer Badar Sep 29 '21 at 05:45
  • 1
    @TanveerBadar A hard realtime OS can't do that either. It can provide guarantees that a deadline is either met or that an error will be signaled in some manner (e.g. execution of the system terminates) if the deadline is not met. If a deadline is met for the process of launching a set of threads, that still doesn't guarantee that all the threads were launched at exactly the same time. A deadline of zero (i.e. the time between starting a process and ending it) is not possible if some action is actually performed by the process. – Peter Sep 29 '21 at 06:02
  • Maybe other question: Why do you want to achieve this? This sounds horrible to maintain without any profit – RoQuOTriX Sep 29 '21 at 06:12
  • @RoQuOTriX, thank you for replying, this streaming data would be used for localization, essentially what observations were associated with what state. That is why sync is important. Maybe a little here and there would be okay, but the margin is very less. Why not with the actual hardware, well because I cannot physically be present – Atharva Dubey Sep 29 '21 at 06:14
  • 1
    Are you familiar with real time development and hard real time constraints? First what you need is a frequency on how often (like every 2 ms you need to update the calculated data) then you should find out, how many algorithms are running on how many threads and how you could achieve that each algorithm has "finished" on your hard time constraint, do not think about started. But of course it makes also sense to "sync" the start with a data barrier for example. – RoQuOTriX Sep 29 '21 at 06:22
  • 2
    Also I would not start and stop the threads. You need to implement worker threads which you can feed with work and are idle if no work is there – RoQuOTriX Sep 29 '21 at 06:23
  • @RoQuOTriX, I have no idea about real-time development,. I will explore your idea further. – Atharva Dubey Sep 29 '21 at 06:31
  • Disregarding the threading aspect I'm a little confused as to why you need to "synchronize" messages coming off a rosbag file? ROS already does that for you. – BTables Sep 29 '21 at 16:46
  • Even if you managed to publish two messages _exactly_ simultaneously, no single consumer can receive two messages exactly simultaneously. So who will be able to tell the difference? What _exactly_ are you trying to achieve? – Useless Sep 30 '21 at 13:22

1 Answers1

0

As others have stated in the comments you cannot guarantee threads launch at exactly the same time. To address your overall goal: you're going about solving this problem the wrong way, from a ROS perspective. Instead of manually publishing data and trying to get it in sync, you should be using the rosbag api. This way you can actually guarantee messages have the same timestamp. Note that this doesn't guarantee they will be sent out at the exact same time, because they won't. You can put a message into a bag file directly like this

import rosbag
from std_msgs.msg import Int32, String

bag = rosbag.Bag('test.bag', 'w')

try:
    s = String()
    s.data = 'foo'

    i = Int32()
    i.data = 42

    bag.write('chatter', s)
    bag.write('numbers', i)
finally:
    bag.close()

For more complex types that include a Header field simply edit the header.stamp portion to keep timestamps consistent

BTables
  • 4,413
  • 2
  • 11
  • 30
  • Hello @BTable, thank you for your reply. I am currently using the rosbag API itself, I essentially create three different views for three separate topics(I have three topics) and I spawn the three topics on three different threads. I am fairly new to ROS and I am not aware of all the "tips and tricks" of it. please let me know if you have an idea about it. – Atharva Dubey Sep 30 '21 at 03:48
  • @AtharvaDubey if you're using the api there is no reason for multiple threads. It will create and write to a bag file just like you would with a text file. Please see my edit for slightly more context and a brief example. – BTables Sep 30 '21 at 13:11