Using service calls to get info

asked 2015-08-18 12:05:16 -0500

negotiator14 gravatar image

Hello, I am trying to write a node that will be sitting there listening to a NavSatFix topic and do rosservice calls only when the longitude and latitude have changed enough by some parameters that I will set up. I previously had a subscriber/publisher method that would do almost what I want but I don't want to re run the node every time I need it, I just want a service that that'll run on the background when it's needed. Here's the subscriber node that I've been using so far and that i want to convert to a service:

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import NavSatFix
import threading


class InfoGetter(object):
    def __init__(self):
        #event that will block until the info is received
        self._event = threading.Event()
        #attribute for storing the rx'd message
        self._msg = None

    def __call__(self, msg):
        #Uses __call__ so the object itself acts as the callback
        #save the data, trigger the event
        self._msg = msg
        self._event.set()

    def get_msg(self, timeout=None):
        """Blocks until the data is rx'd with optional timeout
        Returns the received message
        """
        self._event.wait(timeout)
        return self._msg

def infoGetter():
    rospy.init_node('infoGetter', anonymous=True)
    #Get the info
    ig = InfoGetter()
    rospy.Subscriber("/novatel/fix", NavSatFix, ig)
    #ig.get_msg() Blocks until message is received
    msg = ig.get_msg()
    ra = [msg.latitude, msg.longitude]
    return ra

I looked over at the tutorials and I sort have an idea on how to do it, but I feel it does the same thing as my subscriber program.

edit retag flag offensive close merge delete

Comments

It looks like you want a node that watches the topic and performs a task only when the latitude and longitude have changed significantly. A subscriber can do that. Why the service? Are you interested in a solution using just a subscriber?

sloretz gravatar image sloretz  ( 2015-08-18 15:19:09 -0500 )edit

That's the idea of the main program. Have a program on the background that's is sitting there running on the background and only do it's thing when the latitude and longitude have changed significantly. The code that I uploaded will be just a function that returns an array with lat and long.

negotiator14 gravatar image negotiator14  ( 2015-08-19 11:48:29 -0500 )edit

What I've envisioned is having my main program running on a loop and calling the infoGetter function. If lat and long changed then keep going. If it didn't then keep looping

negotiator14 gravatar image negotiator14  ( 2015-08-19 11:49:36 -0500 )edit