Ask Your Question
0

ROS Callback counter [closed]

asked 2014-08-19 08:52:20 -0600

PaTTo gravatar image

updated 2014-08-19 10:52:17 -0600

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
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by PaTTo
close date 2014-08-19 14:19:58.176964

Comments

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

Dan Lazewatsky gravatar imageDan Lazewatsky ( 2014-08-19 09:47:15 -0600 )edit

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

PaTTo gravatar imagePaTTo ( 2014-08-19 10:22:08 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-08-19 11:43:37 -0600

PaTTo gravatar image

updated 2014-08-19 11:43:57 -0600

"global count" must be set in callback function.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-08-19 08:52:20 -0600

Seen: 281 times

Last updated: Aug 19 '14