Erezn
Erezn

Reputation: 33

how to call rospy.init_node() twice

Im trying to call one node to do one action, and then continue to the next action. get the error: raise rospy.exceptions.ROSException("rospy.init_node() has already been called with different arguments: "+str(_init_node_args))

how can I stop the first node in order to continue to the next one?

I tried using rospy.signal_shutdown('Quit') but the hole program stopped, and not only the node that i wanted.

#!/usr/bin/env python

import os
import sys
import rospy
import baxter_interface
import time

import argparse

from geometry_msgs.msg import (    PoseStamped,    Pose,    Point,        Quaternion,)
from std_msgs.msg import Header
from baxter_core_msgs.srv import (    SolvePositionIK,        SolvePositionIKRequest,)

def ik_test(limb):
    rospy.init_node("rsdk_ik_service_client", disable_signals=True)
    ns = "ExternalTools/" + limb + "/PositionKinematicsNode/IKService"
    iksvc = rospy.ServiceProxy(ns, SolvePositionIK)
    ikreq = SolvePositionIKRequest()
    hdr = Header(stamp=rospy.Time.now(), frame_id='base')

    poses = {
        'left': PoseStamped(
            header=hdr,
            pose=Pose(
                position=Point(
                    x=0.657579481614,
                    y=0.851981417433,
                    z=0.0388352386502,
                ),
                orientation=Quaternion(
                    x=-0.366894936773,
                    y=0.885980397775,
                    z=0.108155782462,
                    w=0.162162481772,
                ),
            ),
        ),
        'right': PoseStamped(
            header=hdr,
            pose=Pose(
                position=Point(
                x=0.692002,
                y=-0.360751,
                z=-0.05133,
                ),
                orientation=Quaternion(
                    x=-0.105882425658, 
                    y=0.9364525476, 
                    z=-0.0241838726041,
                    w=0.333557608721,
                ),
            ),
        ),
    }

    ikreq.pose_stamp.append(poses[limb])

    try:
        rospy.wait_for_service(ns, 5.0)
        resp = iksvc(ikreq)
    except (rospy.ServiceException, rospy.ROSException), e:
        rospy.logerr("Service call failed: %s" % (e,))
        return 1

    if (resp.isValid[0]):
        print("SUCCESS - Valid Joint Solution Found:")
        # Format solution into Limb API-compatible dictionary
        limb_joints = dict(zip(resp.joints[0].name,         resp.joints[0].position))
        print limb_joints
    else:
        print("INVALID POSE - No Valid Joint Solution Found.")

    return limb_joints

def open_gripper():
    rospy.init_node('Hello_Baxter')

    limb = baxter_interface.Limb('right')
    from baxter_interface import Gripper
    right_gripper = Gripper('right')
    joints_names=        ['right_e0','right_e1','right_s0','right_s1','right_w0','right_w1','right_w2']

    r=rospy.Rate(10) #10Hz
    r.sleep()

    baxter_interface.gripper.Gripper('right').open() #Open right gripper
    return 0


def main():

    arg_fmt = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(formatter_class=arg_fmt,
                                 description=main.__doc__)
    parser.add_argument(
        '-l', '--limb', choices=['left', 'right'], required=True,
        help="the limb to test"
    )
    args = parser.parse_args(rospy.myargv()[1:])

    limb_joints=ik_test(args.limb)
    limb = baxter_interface.Limb('right')
    limb.move_to_joint_positions(limb_joints)

    open_gripper()

    return 0 #ik_test(args.limb)


if __name__ == '__main__':
    sys.exit(main())

I expected the first node to shut down and the other to init, but i get the error above.

Upvotes: 3

Views: 4412

Answers (1)

PeteBlackerThe3rd
PeteBlackerThe3rd

Reputation: 670

You seem to confusing the way that ROS nodes and python scripts are used. Your python script should be a single node. Within this node you can create and destroy different topics, services and actions at different times but you can only create one node once.

You should init a single at the start of your main function, then you can create the different services and actions that you need when you need them.

Hope this helps.

Upvotes: 1

Related Questions