ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

So I ran/fixed your code.

Remove time.sleep(1./5.) from the callback and add time.sleep(0.01) to the while true loop.

Perhaps the main needs to give some time for the callbacks to function.

#!/usr/bin/python
import roslibpy
import time
import json
import logging
logger = logging.getLogger("roslibpy")
handler = logging.StreamHandler()
logger.addHandler(handler)

def to_epoch(stamp):
    stamp_secs = stamp["secs"]
    stamp_nsecs = stamp["nsecs"]
    return stamp_secs + (stamp_nsecs*1e-9)

def from_epoch(stamp):
    stamp_secs = int(stamp)
    stamp_nsecs = (stamp - stamp_secs) * 1e9
    return {"secs": stamp_secs, "nsecs": stamp_nsecs}


def sub(msg):
    # The `age` of the message should remain constant, but it increases seemingly indefinetly
    age = time.time() - to_epoch(msg["stamp"])
    print("Age of message: %6.3f seconds" % age)
 #   time.sleep(1./5.)


ros = roslibpy.Ros("ws://localhost:9090")
ros.run()

tt = roslibpy.Topic(ros, "/test", "std_msgs/Header", queue_size=1)

ts = roslibpy.Topic(ros, "/test", "std_msgs/Header", queue_length=1)
ts.subscribe(sub)

try:
    seq = 0
    while True:
        seq += 1
        header = {"frame_id": "test", "stamp": from_epoch(time.time()), "seq": seq}
        tt.publish(roslibpy.Message(header))
        time.sleep(0.01)
except KeyboardInterrupt:
    ros.terminate()

So I ran/fixed made a change and got your code.code to work on my machine. Initially got the same results as you.

Remove time.sleep(1./5.) from the callback and add time.sleep(0.01) to the while true loop.

Perhaps the main needs to give some time for the callbacks to function.

#!/usr/bin/python
import roslibpy
import time
import json
import logging
logger = logging.getLogger("roslibpy")
handler = logging.StreamHandler()
logger.addHandler(handler)

def to_epoch(stamp):
    stamp_secs = stamp["secs"]
    stamp_nsecs = stamp["nsecs"]
    return stamp_secs + (stamp_nsecs*1e-9)

def from_epoch(stamp):
    stamp_secs = int(stamp)
    stamp_nsecs = (stamp - stamp_secs) * 1e9
    return {"secs": stamp_secs, "nsecs": stamp_nsecs}


def sub(msg):
    # The `age` of the message should remain constant, but it increases seemingly indefinetly
    age = time.time() - to_epoch(msg["stamp"])
    print("Age of message: %6.3f seconds" % age)
 #   time.sleep(1./5.)


ros = roslibpy.Ros("ws://localhost:9090")
ros.run()

tt = roslibpy.Topic(ros, "/test", "std_msgs/Header", queue_size=1)

ts = roslibpy.Topic(ros, "/test", "std_msgs/Header", queue_length=1)
ts.subscribe(sub)

try:
    seq = 0
    while True:
        seq += 1
        header = {"frame_id": "test", "stamp": from_epoch(time.time()), "seq": seq}
        tt.publish(roslibpy.Message(header))
        time.sleep(0.01)
except KeyboardInterrupt:
    ros.terminate()

So I made a change and got your code to work on my machine. Initially got the same results as you.

Remove time.sleep(1./5.) from the callback and add time.sleep(0.01) to the while true loop.

Perhaps the main needs to give some time for the callbacks to function.

#!/usr/bin/python
import roslibpy
import time
import json
import logging
logger = logging.getLogger("roslibpy")
handler = logging.StreamHandler()
logger.addHandler(handler)

def to_epoch(stamp):
    stamp_secs = stamp["secs"]
    stamp_nsecs = stamp["nsecs"]
    return stamp_secs + (stamp_nsecs*1e-9)

def from_epoch(stamp):
    stamp_secs = int(stamp)
    stamp_nsecs = (stamp - stamp_secs) * 1e9
    return {"secs": stamp_secs, "nsecs": stamp_nsecs}


def sub(msg):
    # The `age` of the message should remain constant, but it increases seemingly indefinetly
    age = time.time() - to_epoch(msg["stamp"])
    print("Age of message: %6.3f seconds" % age)
 #   time.sleep(1./5.)


ros = roslibpy.Ros("ws://localhost:9090")
ros.run()

tt = roslibpy.Topic(ros, "/test", "std_msgs/Header", queue_size=1)

ts = roslibpy.Topic(ros, "/test", "std_msgs/Header", queue_length=1)
ts.subscribe(sub)

try:
    seq = 0
    while True:
        seq += 1
        header = {"frame_id": "test", "stamp": from_epoch(time.time()), "seq": seq}
        tt.publish(roslibpy.Message(header))
        time.sleep(0.01)
except KeyboardInterrupt:
    ros.terminate()


Age of message:  0.003 seconds
Age of message:  0.002 seconds
Age of message:  0.003 seconds
Age of message:  0.002 seconds
Age of message:  0.002 seconds
Age of message:  0.003 seconds
Age of message:  0.001 seconds
Age of message:  0.001 seconds
Age of message:  0.003 seconds
Age of message:  0.002 seconds
Age of message:  0.002 seconds