• Synchronizing clock of script node with host

Hello,

I am trying to synchronize the messages send by a script node with the time of the host.

Specifically, I need to know when a message was sent from the script node over xlinkout to host. Using datetime.datetime.now() in a script node and appending it to the message (Buffer) results in a wrong timestamp. Moreover I noticed, the messages sent by a SpatialLocationCalculator-Node to a script node have other attributes (and timestamps) then same messages sent to a host. For example the attribute "getTimestampDevice" is missing.

Is there a equivalent to dai.Clock.now() but for the script node? Can you set the clock to start to count from a different timestamp or something similar?

  • device: OAK-D-PoE
  • bootloader-version: 0.0.24
  • depthai: 2.20.2

    Hi werkilan
    Anything depthai related can be run inside the script node.
    Perhaps you could use node.warn(f"{Clock.now()}") to view the timestamp.

    Thanks,
    Jaka

    Hey @jakaskerl thanks for the reply.

    Unfortunately node.warn(f"{Clock.now()}") (in script node) returns a different timestamp then depthai.Clock.now()(on host side).

    Is it maybe possible to go the other way around and get the actual host time within a script node?

    Thanks,

    werkilan

      Hi werkilan
      Could you paste some minimal repro code so I can see how your pipeline is setup? I'm not sure I fully understand how you are trying to synchronize the messages.

      Thanks,
      Jaka

      6 days later

      Hello @jakaskerl

      Sorry for the late reply, here is an example:

          device_info = dai.DeviceInfo(device_ip)
      
          pipeline = dai.Pipeline()
      
          # script node
          script_node = pipeline.create(dai.node.Script)
          script_node.setScript(
              """
              import time, datetime
              import marshal
      
              while True:
                  
                  # a timestamp that should represent the actual time corresponding to host-time
                  clock_now = Clock.now().total_seconds()
                  datetime_now = datetime.datetime.now().timestamp()
      
                  # creating a buffer-object to send the timestamp to host
                  msg = marshal.dumps({'clock_now': clock_now, 'datetime_now': datetime_now}, 2)
                  bufs = Buffer(len(msg))
                  bufs.getData()[:] = msg
      
                  # send buffer
                  node.io['host'].send(bufs)
      
                  time.sleep(1)
      
              """
          )
      
          script_xout = pipeline.create(dai.node.XLinkOut)
          script_xout.setStreamName("script_out")
          script_xout.input.setQueueSize(1)
          script_xout.input.setBlocking(False)
          script_node.outputs["host"].link(script_xout.input)
      
      
          with dai.Device(pipeline, device_info) as device:
      
              script_out = device.getOutputQueue("script_out")
              while True:
                  script_msg = script_out.tryGet()
      
                  if script_msg is not None:
      
                      res = marshal.loads(script_msg.getData())
                      device_clock_now = datetime.timedelta(seconds=res["clock_now"])
                      device_datetime_now = datetime.datetime.fromtimestamp(res["datetime_now"])
                      dai_clock = dai.Clock.now()
                      actual_time = datetime.datetime.now()
      
                      print(f"device_clock_now {device_clock_now}, device_datetime_now {device_datetime_now} \
                            \ndai_clock {dai_clock}, actual_time {actual_time}")
                  
                  time.sleep(0.1)

      Result:

      Expectation:

      device clock now, device datetime now and dai clock have at least the same reference point and have milliseconds inbetween eachother

      I basically want to know when the given timestamp in the scriptnode is but in actual time (of the host)

      Thanks,

      werkilan

        Hi werkilan
        The device won't know actual time since it doesn't have a battery powered clock. This means you will need to send the reference time when you boot the device. Afaik the only way to do it is by sending a buffer to the script node.

        I'll dig around some more and notify if I find something.

        Thanks,
        Jaka