Ask Your Question
1

alternative publisher for Joy_node

asked 2016-11-13 05:46:44 -0500

marcoresk gravatar image

updated 2016-11-13 10:26:05 -0500

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

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-11-13 09:06:33 -0500

NEngelhard gravatar image

updated 2016-11-13 09:10:02 -0500

def joyread(data): foo[0] = 4*data.axes[1]

You are missing the declaration. foo should be a Float32MultiArray, but then you'd rather have to write in foo.data.

Something closer to this should work: (I'm not quite sure how the foo.layout has to be initialized)

def joyread(data): 
  foo = Float32MultiArray()
  foo.data.append(4*data.axes[1])
  (...)
edit flag offensive delete link more

Comments

It works! It was the missing line you wrote. Thank you so much! You save my day.

marcoresk gravatar image marcoresk  ( 2016-11-13 10:21:52 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-11-13 05:46:44 -0500

Seen: 244 times

Last updated: Nov 13 '16