0
$\begingroup$

Rosanswers logo

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()

Originally posted by dshimano on ROS Answers with karma: 129 on 2015-03-02

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2015-03-02

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by dshimano on 2015-03-04:
OK. After looking at it again I realized it was a garbage code. Thanks.

Comment by gvdhoorn on 2015-03-04:
No problem. Re-reading my answer it seems a bit harsh, hope you didn't take it personal.

Comment by freedom696 on 2017-10-25:
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~

Comment by ahendrix on 2017-10-26:
You cannot call rospy.init_node more than once in a python program.

Comment by freedom696 on 2017-10-26:
yeah!I just realized that a moment ago! thank you all the same.

Comment by rajk97 on 2020-06-28:
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?

Comment by gvdhoorn on 2020-06-29:
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.

Comment by sea-bass on 2021-01-26:
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 :(

Comment by gvdhoorn on 2021-01-27:
No, and I don't expect this to ever become supported in ROS 1.

In ROS 1: one process == one node.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.