Why the requests (python) in ros subscriber keeps posting the old messages?
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 ...