ROS Publisher is not publishing continuously

Question:

My Publisher is not publishing continuously, can you please tell me how can I subscribe/publish and advertise services in the same time? thanks in advance.

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import rospy
from std_msgs.msg import String, Bool
from state_controller.srv import ActorChange, ActorChangeResponse

class StateController:
    def __init__(self):
        self.accum_plan = {}
        rospy.init_node("state_controller", anonymous=True, log_level=rospy.INFO)
        self.accPlan_pub = rospy.Publisher("accumulated_plan", String, queue_size=10)
        self.stateReader()

    def stateReader(self):
        """
        subscribe to model/lego_map/yumi_motion_status
        """
        self.accPlan_pub.publish(json.dumps(self.accum_plan))
        rospy.Subscriber("model", String, self.modelRec)
        rospy.Service("change_actor", ActorChange, self.changeActor)

        rospy.sleep(3)
        rospy.spin()

    ## Subscriber CallBacks
    def modelRec(self, data):
        """ """
        model = json.loads(data.data)
        if model != self.old_model:
            self.old_model = model

    def changeActor(self, req):
        """ """
        lego = req.data
        return ActorChangeResponse(True)

if __name__ == "__main__":
    try:
        controller_ = StateController()

    except rospy.ROSInterruptException:
        logging.error("Error in the State Controller")
Asked By: Bilal

||

Answers:

You probably meant to do something like

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import rospy
from std_msgs.msg import String, Bool
from state_controller.srv import ActorChange, ActorChangeResponse

class StateController:
    def __init__(self):
        self.accum_plan = {}
        rospy.init_node("state_controller", anonymous=True, log_level=rospy.INFO)
        self.accPlan_pub = rospy.Publisher("accumulated_plan", String, queue_size=10)
        rospy.Subscriber("model", String, self.modelRec)
        rospy.Service("change_actor", ActorChange, self.changeActor)
        self.stateReader()

    def stateReader(self):
        """
        subscribe to model/lego_map/yumi_motion_status
        """
        r = rospy.Rate(3) # 10hz
        while not rospy.is_shutdown():
            self.accPlan_pub.publish(json.dumps(self.accum_plan))
            r.sleep()

    ## Subscriber CallBacks
    def modelRec(self, data):
        """ """
        model = json.loads(data.data)
        if model != self.old_model:
            self.old_model = model

    def changeActor(self, req):
        """ """
        lego = req.data
        return ActorChangeResponse(True)

if __name__ == "__main__":
    try:
        controller_ = StateController()

    except rospy.ROSInterruptException:
        logging.error("Error in the State Controller")

Where your publishers/subscribers/services are set up in the constructor, and then using another method to trigger the loop of the publishing topic

Answered By: ignacio
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.