Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawner check for currently running controllers on shutdown #364

Open
wants to merge 6 commits into
base: kinetic-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions controller_manager/scripts/spawner
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import yaml
from controller_manager_msgs.srv import *
from std_msgs.msg import *
import argparse
import controller_manager.controller_manager_interface
megantuttle marked this conversation as resolved.
Show resolved Hide resolved

loaded = []

Expand All @@ -55,16 +56,21 @@ def shutdown():
rospy.loginfo("Shutting down spawner. Stopping and unloading controllers...")

try:
# unloader
# get list of running controllers
_running_controllers = []
_loaded_controllers = controller_manager.controller_manager_interface.list_controllers()
for c in _loaded_controllers:
if c.state == "running":
_running_controllers.append(c.name)

# create unloader and switcher
unload_controller = rospy.ServiceProxy(unload_controller_service, UnloadController)

# switcher
switch_controller = rospy.ServiceProxy(switch_controller_service, SwitchController)

rospy.loginfo("Stopping all controllers...");
switch_controller([], loaded, SwitchControllerRequest.STRICT)
switch_controller([], _running_controllers, SwitchControllerRequest.STRICT)
rospy.loginfo("Unloading all loaded controllers...");
for name in reversed(loaded):
for name in reversed(_running_controllers):
rospy.logout("Trying to unload %s" % name)
unload_controller(name)
rospy.logout("Succeeded in unloading %s" % name)
Expand Down Expand Up @@ -191,9 +197,6 @@ def main():

rospy.loginfo("Controller Spawner: Loaded controllers: %s" % ', '.join(loaded))

if rospy.is_shutdown():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whiy did you remove this block?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed redundant since a shutdown hook is defined, and rospy.spin() simply keeps python from exiting until rospy.core.is_shutdown() is set (see rospy/client.py).

return

# start controllers is requested
if autostart:
resp = switch_controller(loaded, [], 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
#! /usr/bin/env python
megantuttle marked this conversation as resolved.
Show resolved Hide resolved
from __future__ import print_function
import rospy
from controller_manager_msgs.srv import *


def list_controller_types():
rospy.wait_for_service('controller_manager/list_controller_types')
s = rospy.ServiceProxy('controller_manager/list_controller_types', ListControllerTypes)
resp = s.call(ListControllerTypesRequest())
for t in resp.types:
print(t)

print t

def reload_libraries(force_kill, restore=False):
def reload_libraries(force_kill, restore = False):
rospy.wait_for_service('controller_manager/reload_controller_libraries')
s = rospy.ServiceProxy('controller_manager/reload_controller_libraries', ReloadControllerLibraries)

list_srv = rospy.ServiceProxy('controller_manager/list_controllers', ListControllers)
load_srv = rospy.ServiceProxy('controller_manager/load_controller', LoadController)
switch_srv = rospy.ServiceProxy('controller_manager/switch_controller', SwitchController)

print("Restore:", restore)
print "Restore:", restore
if restore:
originally = list_srv.call(ListControllersRequest())

resp = s.call(ReloadControllerLibrariesRequest(force_kill))
if resp.ok:
print("Successfully reloaded libraries")
print "Successfully reloaded libraries"
result = True
else:
print("Failed to reload libraries. Do you still have controllers loaded?")
print "Failed to reload libraries. Do you still have controllers loaded?"
result = False

if restore:
Expand All @@ -39,62 +36,59 @@ def reload_libraries(force_kill, restore=False):
switch_srv(start_controllers=to_start,
stop_controllers=[],
strictness=SwitchControllerRequest.BEST_EFFORT)
print("Controllers restored to original state")
print "Controllers restored to original state"
return result


def list_controllers():
controller_list = []
rospy.wait_for_service('controller_manager/list_controllers')
s = rospy.ServiceProxy('controller_manager/list_controllers', ListControllers)
resp = s.call(ListControllersRequest())
if len(resp.controller) == 0:
print("No controllers are loaded in mechanism control")
print "No controllers are loaded in mechanism control"
else:
for c in resp.controller:
hwi = list(set(r.hardware_interface for r in c.claimed_resources))
print('%s - %s ( %s )' % (c.name, '+'.join(hwi), c.state))
hwi = list(set(r.hardware_interface for r in c.claimed_resources))
print '%s - %s ( %s )'%(c.name, '+'.join(hwi), c.state)
controller_list.append(c)
return controller_list


def load_controller(name):
rospy.wait_for_service('controller_manager/load_controller')
s = rospy.ServiceProxy('controller_manager/load_controller', LoadController)
resp = s.call(LoadControllerRequest(name))
if resp.ok:
print("Loaded", name)
print "Loaded", name
return True
else:
print("Error when loading", name)
print "Error when loading", name
return False


def unload_controller(name):
rospy.wait_for_service('controller_manager/unload_controller')
s = rospy.ServiceProxy('controller_manager/unload_controller', UnloadController)
resp = s.call(UnloadControllerRequest(name))
if resp.ok == 1:
print("Unloaded %s successfully" % name)
print "Unloaded %s successfully" % name
return True
else:
print("Error when unloading", name)
print "Error when unloading", name
return False


def start_controller(name):
return start_stop_controllers([name], True)


def start_controllers(names):
return start_stop_controllers(names, True)


def stop_controller(name):
return start_stop_controllers([name], False)


def stop_controllers(names):
return start_stop_controllers(names, False)


def start_stop_controllers(names, st):
rospy.wait_for_service('controller_manager/switch_controller')
s = rospy.ServiceProxy('controller_manager/switch_controller', SwitchController)
Expand All @@ -108,13 +102,13 @@ def start_stop_controllers(names, st):
resp = s.call(SwitchControllerRequest(start, stop, strictness))
if resp.ok == 1:
if st:
print("Started {} successfully".format(names))
megantuttle marked this conversation as resolved.
Show resolved Hide resolved
print "Started {} successfully".format(names)
else:
print("Stopped {} successfully".format(names))
print "Stopped {} successfully".format(names)
return True
else:
if st:
print("Error when starting ", names)
print "Error when starting ", names
else:
print("Error when stopping ", names)
print "Error when stopping ", names
return False