alternative publisher for Joy_node
Hi everyone,
I'm trying to publish to modify the Publisher tutorial (Python)) and simplify this tutorial in order to read some values from the joy_node, do some operations and publish the new values in another topic in the foo array. This is my file talker2.py in Python under ROS Indigo. I know there are errors (probably in the foo variable), but I don't know what is really wrong. Someone can help me?
#!/usr/bin/env python
import rospy
from std_msgs.msg import Float32MultiArray
from sensor_msgs.msg import Joy
def joyread(data):
foo[0] = 4*data.axes[1]
foo[1] = 4*data.axes[0]
foo[2] = 15*data.buttons[14]
pub.publish(foo)
def talker2():
global pub
pub = rospy.Publisher('chatter', Float32MultiArray,queue_size=10)
rospy.Subscriber("joy", Joy, joyread)
rospy.init_node('talker2', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
rospy.sleep(1.0)
if __name__ == '__main__':
try:
talker2()
except rospy.ROSInterruptException:
pass
And this is the error (declaring public foo does not solve anything)
[ERROR] [WallTime: 1479037245.575886] bad callback: <function joyread at 0x7f5b6f923c08>
Traceback (most recent call last):
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback
cb(msg)
File "/home/marcor/catkin_ws/src/beginner_tutorials/scripts/talker2.py", line 44, in joyread
foo[0] = 4*data.axes[1]
NameError: global name 'foo' is not defined
Thank you all. (PS Joy_node "alone" works fine)
UPDATE After NEngelhard answer, here the solution. I modified the first function as follows and it works!
def joyread(data):
foo = Float32MultiArray()
foo.data.append(255*data.axes[1])
foo.data.append(255*data.buttons[14])
pub.publish(foo)