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

Calling switch_controller service from python requires controller being loaded #511

Open
rhaschke opened this issue Jan 26, 2023 · 2 comments

Comments

@rhaschke
Copy link
Contributor

When switching controllers from the cmdline via rosservice call /controller_manager/switch_controller ... it works out of the box. However doing the same from python code always failed silently (response was ok=True, but controller wasn't started as confirmed via rqt controller_manager plugin). Experimenting a little bit, I noticed that I need to load the controller in advance when switching programmatically. However calling the service from cmdline and from python should be the same, shouldn't it?

This works:

#!/usr/bin/env python3
import rospy
from controller_manager_msgs.srv import SwitchController, LoadController

def call(ns, cls, **kwargs):
    rospy.wait_for_service(ns)
    service = rospy.ServiceProxy(ns, cls)
    response = service(**kwargs)
    print(response.ok)
    if not response.ok:
        print(response)

try:
    # When calling the switch_controller service here, I need to ensure that the controller is loaded.
    # When calling the service via cmdline (rosservice call /controller_manager/switch_controller ...) it works without loading!?
    call("/controller_manager/load_controller", LoadController,
         name="position_joint_trajectory_controller")

    call("/controller_manager/switch_controller", SwitchController,
         start_controllers=["position_joint_trajectory_controller"],
         stop_controllers=["effort_joint_trajectory_controller"],
         strictness=1, start_asap=False, timeout=0.0)

except rospy.ServiceException as e:
    print("Service call failed: %s" % e)
@ZZWang21
Copy link

ZZWang21 commented Jun 9, 2023

I am planning to do this recently, will try this code. Thanks.

A small question: is it possible to write python code controlling rqt to switch controllers?

@rhaschke
Copy link
Contributor Author

rhaschke commented Nov 9, 2023

Is it possible to write python code controlling rqt to switch controllers

I don't think so. However, if you switch controllers, rqt's controller_manager plugin should reflect the new state of the controllers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants