Ask Your Question
1

rospy.init_node() has already been called with different arguments

asked 2015-03-02 10:20:13 -0600

dshimano gravatar image

updated 2017-03-23 14:23:55 -0600

130s gravatar image

Hi, I'm running indigo on a RPi.

I have a script to use a joystick to run a servo, which I'll paste below. I know the code subscribing to this is working, because when I publish angles to it using rostopic, the servo turns. I was hoping that when I use this script, I could push the joystick up, and the servo would turn more positive. I based my usage of the joy node on this link from the ROS Wiki page. I got the following error.

[ERROR] [WallTime: 1425312842.009290] bad callback: <function joy_talker at 0x224c1b0>
Traceback (most recent call last):
  File "/home/pi/catkin_ws/src/ros_comm/rospy/src/rospy/topics.py", line 702, in _invoke_callback
    cb(msg)
  File "/home/pi/catkin_ws/src/servo/joy.py", line 8, in joy_talker
    rospy.init_node('talker', anonymous=True)
  File "/home/pi/catkin_ws/src/ros_comm/rospy/src/rospy/client.py", line 274, in init_node
    raise rospy.exceptions.ROSException("rospy.init_node() has already been called with different arguments: "+str(_init_node_args))
ROSException: rospy.init_node() has already been called with different arguments: ('servo_joy', ['/home/pi/catkin_ws/src/servo/joy.py'], False, None, False, False)

#!/usr/bin/env python
import rospy
from std_msgs.msg import Float32
from sensor_msgs.msg import Joy

def joy_talker(msg):
        pub = rospy.Publisher('angle', Float32, queue_size=10)
        rospy.init_node('talker', anonymous=True)
        while not rospy.is_shutdown():
                rospy.logdebug("Joy Recieved: [Vx:%f, Wz:%f]"%(msg.axes[1],msg.$
                angle  = msg.axes[1]  * 180
                rospy.loginfo(angle)
        pub.publish(angle)
        rate.sleep()


if __name__ == '__main__':
        rospy.init_node('servo_joy')

        rospy.Subscriber("joy", Joy, joy_talker)

        rospy.spin()
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2015-03-02 11:34:03 -0600

gvdhoorn gravatar image

updated 2015-03-02 11:35:59 -0600

Move all initialisation of publishers / subscribers out of your joy_talker callback. Only publish messages inside callbacks, never (well .. never ..) create anything related to message publication and / or subscription in them. Also: no node initialisation statements inside callbacks.

Callbacks are called upon reception of a message, a single message. How do you expect the code you posted to work? Upon reception of the first Joy msg, your callback will be invoked, which (ignoring the call to rospy.init_node(..) for now) then enters an (potentially) infinite while-loop. pub.publish(..) will never be invoked.


Edit: if you based your code on the example from the link you posted, where did the extra while-loop and node initialisation come from? As you can see in the xbox example, the msg callback only performs some very simple calculations, before publishing a new message.

edit flag offensive delete link more

Comments

1

OK. After looking at it again I realized it was a garbage code. Thanks.

dshimano gravatar imagedshimano ( 2015-03-04 08:41:28 -0600 )edit
1

No problem. Re-reading my answer it seems a bit harsh, hope you didn't take it personal.

gvdhoorn gravatar imagegvdhoorn ( 2015-03-04 10:14:53 -0600 )edit

hi! I have encounter the same error, but the error was occured when i initialise multiple ros node. do you know how to resolve this problem? thanks! best wishes from China~

freedom696 gravatar imagefreedom696 ( 2017-10-25 23:01:54 -0600 )edit

You cannot call rospy.init_node more than once in a python program.

ahendrix gravatar imageahendrix ( 2017-10-26 00:47:53 -0600 )edit

yeah!I just realized that a moment ago! thank you all the same.

freedom696 gravatar imagefreedom696 ( 2017-10-26 00:50:56 -0600 )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: 2015-03-02 10:20:13 -0600

Seen: 3,088 times

Last updated: Mar 23 '17