Ask Your Question
0

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

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

stevemartin gravatar image

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

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:
            return
        rospy.loginfo("callback")
        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)
                self.send_to_socket("rotate_direction")
                self.to_initial_orientation()
                self.send_to_socket("move_direction")
                rospy.loginfo("The distance is %f", self.distance)
                self.to_zero()
                self.x = pose.pose.position.x
                self.y = pose.pose.position.y

                time.sleep(2)
        self.to_initial_orientation()
        r = requests.post(path_url, json=self.path_msg)
        self.path_msg["path"] = []
        r.close()

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 = "https://ros-bridge.herokuapp.com/p/"
host = "https://havit-web.herokuapp.com"
email = "edmund_@gmail.com"
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 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

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

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

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

UPDATE:

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

Comments

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

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

Cheers, see my update.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-06 06:07:47 -0500 )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 -0500 )edit

Simply because the only occurrence of requests.post 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 -0500 )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 -0500 )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

2 followers

Stats

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

Seen: 84 times

Last updated: Dec 06 '18