ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

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

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

dshimano gravatar image

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

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 -0500

gvdhoorn gravatar image

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

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 image dshimano  ( 2015-03-04 08:41:28 -0500 )edit
1

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

gvdhoorn gravatar image gvdhoorn  ( 2015-03-04 10:14:53 -0500 )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 image freedom696  ( 2017-10-25 23:01:54 -0500 )edit

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

ahendrix gravatar image ahendrix  ( 2017-10-26 00:47:53 -0500 )edit

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

freedom696 gravatar image freedom696  ( 2017-10-26 00:50:56 -0500 )edit

Is it not possible even after giving the command rospy.signal_shutdown("Shutdown node") to shutdown the node inside python function to start another node?

rajk97 gravatar image rajk97  ( 2020-06-28 14:49:23 -0500 )edit

No, it's not supported -- at least last time I checked.

Some resources are not completely released, causing rospy.init_node(..) to error out when attempting to call it twice.

But do please check, as rospy has seen quite some fixes since the last time this was discussed in this question.

gvdhoorn gravatar image gvdhoorn  ( 2020-06-29 03:15:33 -0500 )edit

It's still not supported (at least in Melodic). Normally I only would need 1 node, but when running unit tests using pytest it seems to treat all of my separate test files as 1 process and blocks me from starting multiple nodes. Bummer :(

sea-bass gravatar image sea-bass  ( 2021-01-26 17:10:31 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2015-03-02 10:20:13 -0500

Seen: 8,062 times

Last updated: Mar 23 '17