Robotics StackExchange | Archived questions

ROS Callback counter

Hey there,

I need to make a node, where if a position message arrives make a vector with the actual position in x and the 4 previous values. But it doesn't work, I think is a easy code, but I am a beginner.

I cant even work with a simpler vector. I only can work with the last message position. Hope anyone can help me.

import roslib
import rospy
import math
import numpy as np
from posotion.msg import RobotPos

v = np.zeros(5)
count=0


def callback(msg):

    v[count]=msg.position
    print v


# Intializes everything
def start():
    global count
    rospy.Subscriber("robot_pos", RobotPos, callback)
    count += 1 #I also tried to use this in callback function but it doesnt work
    rospy.init_node('robot_pub') 
    rospy.spin()

if __name__ == '__main__':
    try:
        start()        
    except rospy.ROSInterruptException:
        pass

Asked by PaTTo on 2014-08-19 08:52:20 UTC

Comments

What line does it say this error is on? Did you typo "count" as "cont" somewhere that isn't included in your post?

Asked by Dan Lazewatsky on 2014-08-19 09:47:15 UTC

Added the full code, I obtain only the first field of the vector as the actual value. v[0] = msg.position the rest are zeros. count += 1 doesnt works there in each spin. And if I print count, it always has the same value

Asked by PaTTo on 2014-08-19 10:22:08 UTC

Is it possible that you couldn't import the right message types? Because you wrote: From posOtion.msg import RobotPos

Asked by MaxM on 2020-07-07 06:40:48 UTC

Answers

"global count" must be set in callback function.

Asked by PaTTo on 2014-08-19 11:43:37 UTC

Comments