ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Why the requests (python) in ros subscriber keeps posting the old messages?

asked 2018-12-06 04:55:36 -0600

stevemartin gravatar image

updated 2018-12-06 05:55:18 -0600

I have a ROS subscriber which listens to the topic, creates some message and then uses python requests to post that data. I am using an OOP model, where I have created a class called MessageHolder with initial values x = 0.1 y = 0.1.

Whenever the topic triggers a message, this class calls its own callback function, sets x and y to be the first message from the topic (which is a Path message, so it consists of multiple PoseStamped messages). For this particular example, x = 0.1 y = 6.1. Here is the callback function of that class:

def callback(self, msg):
        if len(msg.poses) == 0:
        self.x = msg.poses[0].pose.position.x
        self.y = msg.poses[1].pose.position.y
        rospy.loginfo("X : %f, Y : %f", self.x, self.y)
        for pose in msg.poses:
            if not isClose(pose.pose.orientation.z, self.th_x) or not isClose(pose.pose.orientation.w, self.th_y):
                self.th_x = pose.pose.orientation.z
                self.th_y = pose.pose.orientation.w
                self.distance_x = 100*abs(pose.pose.position.x - self.x)
                self.distance_y = 100*abs(pose.pose.position.y - self.y)
                self.distance = math.sqrt(self.distance_x**2 + self.distance_y**2)
                rospy.loginfo("The distance is %f", self.distance)
                self.x = pose.pose.position.x
                self.y = pose.pose.position.y

        r =, json=self.path_msg)
        self.path_msg["path"] = []

So for the first time, everything works great and it posts a 1 path message to the server. Then after few minutes, with no messages being posted to the topic the subscriber is subscribed, I can see another path message being posted on a server (please note, from the logs, I didn't see that the callback above being called). The message is exactly the same except the initial values being x = 0.1, y = 0.1 (like during the initialisation) but the object has not been reinitalized! What is going on? Why does the requests keep posting?

Here is the full class and subscriber code:

#! /usr/bin/env python

import sys
import json
import ast
import time
import requests
from collections import OrderedDict
import rospy
import socketIO_client
import math
from nav_msgs.msg import Path
from nav_msgs.srv import GetPlan
from geometry_msgs.msg import PoseStamped 
from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
import tf.transformations as T

path_url = ""
host = ""
email = ""
wheelme_id = 4255
socketio = socketIO_client.SocketIO(host, params={"email": email})

class MessageHolder:
    x = -0.1
    y = -0.1
    th_x = 0.0
    th_y = 0.0
    last_x = 0.0
    last_y = 0.0
    last_yaw = 0.0
    current_yaw = 0.0
    wheelme_id = None
    command = None
    path_msg = {"path" : []}
    distance = 0.0
    distance_x = 0.0
    distance_y = 0.0
    start = None

    def __init__(self, x, y, yaw, wheelme_id):
        rospy.loginfo("%s","OBJECT HAS BEEN INITIALIZED")
        self.x = x
        self.y = y
        self.last_yaw = yaw
        self.current_yaw = yaw
        self ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-12-06 05:31:11 -0600

updated 2018-12-06 06:07:40 -0600

If this callback is definitely not being called, then this looks like it is an issue with the http interface you've made in your class so technically not a ROS problem. Without seeing the rest of the class there's not much we can do.

Note, there's an unrelated minor bug in your code: Getting the initial point on the path should be:

    self.x = msg.poses[0].pose.position.x
    self.y = msg.poses[0].pose.position.y


The only thing I can think is that several path messages are being received at the very start and several callbacks are being executed. The unusual thing about the callback has a time.sleep(2) inside the loop meaning that it could easily take minutes to finish executing for a path with 60+ way-points. Try removing the sleep call and see how it behaves then.

edit flag offensive delete link more


@PeteBlackerThe3rd Please see my edit, I have added the full code

stevemartin gravatar image stevemartin  ( 2018-12-06 05:55:50 -0600 )edit

Cheers, see my update.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-06 06:07:47 -0600 )edit

@PeteBlackerThe3rd Yes, the issue still exists. Why do you think that the callback is being called several times instead of 1? I only read the rospy.loginfo("callback") only once.

stevemartin gravatar image stevemartin  ( 2018-12-06 06:48:41 -0600 )edit

Simply because the only occurrence of is in the callback function, so it's the only explanation for another message being received on the server. Unless it's possible more than one instance of this node is running at the same time?

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-06 07:27:05 -0600 )edit

You could try adding a sequence number into the path message that is POSTed to your server to help work out where they're coming from.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-06 07:29:13 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-12-06 04:55:36 -0600

Seen: 126 times

Last updated: Dec 06 '18