ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Callback function not updating values (Rospy)

asked 2016-10-03 10:27:13 -0600

piraka9011 gravatar image

updated 2016-10-03 16:08:57 -0600

I have a program used to read in command velocities from two sources (teleop/navigation), blend them, and then publish the blended velocity to the Turtlebot. While debugging, I noticed that the code never reaches the callback functions and thus, does not update any of the values I need to read in. I have the following:

class BSCFun:

    def __init__(self):
        # Init Node
        rospy.init_node('BSCFun', anonymous=True, log_level=rospy.DEBUG);
        self.pub = rospy.Publisher('/mobile_base/commands/velocity', Twist, queue_size=100);

        # ROS Subscribers
        self.odomSub = rospy.Subscriber('/odom', Odometry, self.odomCallback);
        self.goalSub = rospy.Subscriber('/move_base/current_goal', PoseStamped, self.goalCallback)
        self.velSub = rospy.Subscriber('/cmd_vel_mux/input/navi', Twist, self.optCmdCallback)
        self.teleSub = rospy.Subscriber('/cmd_vel_mux/input/teleop', Twist, self.userCmdCallback);
        self.rate = rospy.Rate(5);

        # Main Loop
        while not rospy.is_shutdown():
            self.BSCFun1();

    def BSCFun1(self):
        # do stuff and get a 'newVelocity'
        vMsg = Twist();
        vMsg.angular.z = newVelocity;
        self.pub.publish(vMsg);
        self.rate.sleep();

    def userCmdCallback(self, msg):
        self.xVelUser= msg.linear.x;
        self.wzVelUser = msg.angular.z; 

    def optCmdCallback(self, msg):
        self.xVel = msg.linear.x;
        self.wzVel = msg.angular.z;
        rospy.loginfo("Updating Opt. Cmd.");

and a bunch of other callbacks.

if __name__ == '__main__':
    try:
        bsc = BSCFun();
    except rospy.ROSInterruptException:
        pass

I use the data from the callbacks in BSCFun1() to calculate a newVelocity and publish accordingly, but the values are not updating and all I get are zeros (the values my variables are initialized in. What seems to be the problem?

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted
0

answered 2016-10-03 21:50:23 -0600

piraka9011 gravatar image

So it turns out that I was not using rospy.spin() and so ROS was not reading in data from the topics as they come in. I solved it by making my BSC class made up of only the __init__ and the callbacks and using the member variables I need in the __main__ function

class BSCFun:

    def __init__(self):
        # ROS Publishers
        self.pub = rospy.Publisher('/mobile_base/commands/velocity', Twist, queue_size=100);

        # ROS Subscribers
        self.odomSub = rospy.Subscriber('/odom', Odometry, self.odomCallback);
        self.goalSub = rospy.Subscriber('/move_base/current_goal', PoseStamped, self.goalCallback)
        self.velSub = rospy.Subscriber('/cmd_vel_mux/input/navi', Twist, self.optCmdCallback)
        self.teleSub = rospy.Subscriber('/cmd_vel_mux/input/teleop', Twist, self.userCmdCallback);
        self.rate = rospy.Rate(5);
        rospy.spin();    # Added this line

        # Removed BSCFun1()
    def publishMsg(self, msg):
        self.pub.Publish(msg);

    def userCmdCallback(self, msg):
        self.xVelUser= msg.linear.x;
        self.wzVelUser = msg.angular.z; 

    def optCmdCallback(self, msg):
        self.xVel = msg.linear.x;
        self.wzVel = msg.angular.z;
        rospy.loginfo("Updating Opt. Cmd.");

if __name__ == '__main__':
    # Init Node
    rospy.init_node('BSCFun', anonymous=True, log_level=rospy.DEBUG);
    bsc = BSCFun();
    # Stuck in 'spin'
    vMsg = Twist();
    try:
        # Code never actually reaches here...
        while not rospy.is_shutdown():
            # Blend velocity
            alpha = 0.5;
            newHeading = bsc.wzVel * (alpha - bsc.wzVelUser);
            vMsg.angular.z = newHeading;
            # Publish velocity
            bsc.publishMsg(vMsg);
    except rospy.ROSInterruptException:
        pass

My new problem is that ROS stays in its 'spin' state when the BSCFun class object is created and does not move on to the while loop in the main function. I would like to take the data from the subscribers callbacks and use it to 'blend' the velocity commands coming in (as seen in the while loop) but that is not working. I will post this as a new question but any suggestions would be appreciated.

edit flag offensive delete link more

Comments

I simply did the blending within one of the callback functions, that was the best way I found so far.

piraka9011 gravatar image piraka9011  ( 2016-10-06 22:10:29 -0600 )edit

Hello friends, I also encountered the same problem,Do you find a solution about it? Looking forward to your reply,thanks

wh11868 gravatar image wh11868  ( 2018-01-16 03:06:40 -0600 )edit

@wh11868 See my first comment for one approach. My current approach is to make a separate thread, using python's Threading package, that takes all the data from the CB functions and blends them. Note that this does not sync the data if that is important for you!

piraka9011 gravatar image piraka9011  ( 2018-01-23 08:09:00 -0600 )edit
0

answered 2016-10-03 15:43:22 -0600

krishna43 gravatar image

Hey,

  def BSCFun1(self):
    # do stuff
    vMsg = Twist();
    vMsg.angular.z = newVelocity;
    self.pub.publish(vMsg);
    self.rate.sleep();

It seems like you are always publishing newVeloclity. how are you updating it and where are you updating it?

edit flag offensive delete link more

Comments

It's in the ' do stuff section'. I keep recalculating a newVelocity and publish accordingly. My problem is that the callbacks are not updating my variables.

piraka9011 gravatar image piraka9011  ( 2016-10-03 16:07:37 -0600 )edit
0

answered 2016-10-03 14:22:50 -0600

Airuno2L gravatar image

updated 2016-10-03 14:25:20 -0600

Maybe it is a copy and paste error, but the way it is showing up on this page looks like everything under "class BSCFun:" needs to be indented once. The way it is now there isn't anything inside the BSCFun class, I would think you should be seeing an error though, maybe the try/except is hiding errors.

Also, its unrelated but you have "do stuff" in there as well.

edit flag offensive delete link more

Comments

Yes that was a copy-pasta error, and "do stuff" is essentially a huge line of commands doing some math so I just put that there. Fixed it. Still thats not the point of my question....

piraka9011 gravatar image piraka9011  ( 2016-10-03 15:30:27 -0600 )edit

what ever is happening in "do stuff" might be important to answering the question.

Airuno2L gravatar image Airuno2L  ( 2016-10-03 21:18:07 -0600 )edit
0

answered 2018-05-28 15:16:45 -0600

Sahand_Rez gravatar image

updated 2018-05-28 15:20:24 -0600

Hi, I was having the exact same problem. I was able to find a work around. Just put a rospy.sleep(0.1) at the very beginning of your while loop in the updated code (that you posted as an answer) or at the very beginning of BSCFun1 method. Apparently, during the sleep time, the callback functions are called and the values are updated.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2016-10-03 10:27:13 -0600

Seen: 3,428 times

Last updated: May 28 '18