Skip to content

Commit d13ff4d

Browse files
saikishormergify[bot]
authored andcommitted
[CM] Set default strictness of switch_controllers using parameters (#2168)
(cherry picked from commit ad2efc6)
1 parent 6c91ed5 commit d13ff4d

File tree

7 files changed

+59
-15
lines changed

7 files changed

+59
-15
lines changed

controller_manager/controller_manager/controller_manager_services.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,15 @@ def switch_controllers(
275275
controller_manager_name,
276276
deactivate_controllers,
277277
activate_controllers,
278-
strict,
278+
strictness,
279279
activate_asap,
280280
timeout,
281281
call_timeout=10.0,
282282
):
283283
request = SwitchController.Request()
284284
request.activate_controllers = activate_controllers
285285
request.deactivate_controllers = deactivate_controllers
286-
if strict:
287-
request.strictness = SwitchController.Request.STRICT
288-
else:
289-
request.strictness = SwitchController.Request.BEST_EFFORT
286+
request.strictness = strictness
290287
request.activate_asap = activate_asap
291288
request.timeout = rclpy.duration.Duration(seconds=timeout).to_msg()
292289
return service_caller(

controller_manager/controller_manager/spawner.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
set_controller_parameters_from_param_files,
3131
bcolors,
3232
)
33+
from controller_manager_msgs.srv import SwitchController
3334
from controller_manager.controller_manager_services import ServiceNotFoundError
3435

3536
from filelock import Timeout, FileLock
@@ -164,6 +165,7 @@ def main(args=None):
164165
controller_manager_timeout = args.controller_manager_timeout
165166
service_call_timeout = args.service_call_timeout
166167
switch_timeout = args.switch_timeout
168+
strictness = SwitchController.Request.STRICT
167169

168170
if param_files:
169171
for param_file in param_files:
@@ -299,7 +301,7 @@ def main(args=None):
299301
controller_manager_name,
300302
[],
301303
[controller_name],
302-
True,
304+
strictness,
303305
True,
304306
switch_timeout,
305307
service_call_timeout,
@@ -324,7 +326,7 @@ def main(args=None):
324326
controller_manager_name,
325327
[],
326328
controller_names,
327-
True,
329+
strictness,
328330
True,
329331
switch_timeout,
330332
service_call_timeout,
@@ -358,7 +360,7 @@ def main(args=None):
358360
controller_manager_name,
359361
controller_names,
360362
[],
361-
True,
363+
strictness,
362364
True,
363365
switch_timeout,
364366
service_call_timeout,

controller_manager/controller_manager/unspawner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import warnings
2020

2121
from controller_manager import switch_controllers, unload_controller
22+
from controller_manager_msgs.srv import SwitchController
2223
from controller_manager.controller_manager_services import ServiceNotFoundError
2324

2425
import rclpy
@@ -51,6 +52,7 @@ def main(args=None):
5152
controller_names = args.controller_names
5253
controller_manager_name = args.controller_manager
5354
switch_timeout = args.switch_timeout
55+
strictness = SwitchController.Request.STRICT
5456

5557
node = Node("unspawner_" + controller_names[0])
5658
try:
@@ -60,7 +62,7 @@ def main(args=None):
6062
controller_manager_name,
6163
controller_names,
6264
[],
63-
True,
65+
strictness,
6466
True,
6567
switch_timeout,
6668
)

controller_manager/src/controller_manager.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,14 +1321,22 @@ controller_interface::return_type ControllerManager::switch_controller_cb(
13211321
}
13221322
if (strictness == 0)
13231323
{
1324-
RCLCPP_WARN(
1324+
std::string default_strictness = params_->defaults.switch_controller.strictness;
1325+
// Convert to uppercase
1326+
std::transform(
1327+
default_strictness.begin(), default_strictness.end(), default_strictness.begin(),
1328+
[](unsigned char c) { return std::toupper(c); });
1329+
RCLCPP_WARN_ONCE(
13251330
get_logger(),
13261331
"Controller Manager: to switch controllers you need to specify a "
13271332
"strictness level of controller_manager_msgs::SwitchController::STRICT "
1328-
"(%d) or ::BEST_EFFORT (%d). Defaulting to ::BEST_EFFORT",
1333+
"(%d) or ::BEST_EFFORT (%d). When unspecified, the default is %s",
13291334
controller_manager_msgs::srv::SwitchController::Request::STRICT,
1330-
controller_manager_msgs::srv::SwitchController::Request::BEST_EFFORT);
1331-
strictness = controller_manager_msgs::srv::SwitchController::Request::BEST_EFFORT;
1335+
controller_manager_msgs::srv::SwitchController::Request::BEST_EFFORT,
1336+
default_strictness.c_str());
1337+
strictness = params_->defaults.switch_controller.strictness == "strict"
1338+
? controller_manager_msgs::srv::SwitchController::Request::STRICT
1339+
: controller_manager_msgs::srv::SwitchController::Request::BEST_EFFORT;
13321340
}
13331341

13341342
std::string activate_list, deactivate_list;

controller_manager/src/controller_manager_parameters.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ controller_manager:
2525
}
2626
}
2727

28+
defaults:
29+
switch_controller:
30+
strictness: {
31+
type: string,
32+
default_value: "best_effort",
33+
description: "The default switch controller strategy. This strategy is used when no strategy is specified in the switch_controller service call.",
34+
validation: {
35+
not_empty<>: null,
36+
one_of<>: [[
37+
"strict",
38+
"best_effort",
39+
]],
40+
}
41+
}
42+
2843
diagnostics:
2944
threshold:
3045
controller_manager:

doc/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ Changes from `(PR #1688) <https://github.com/ros-controls/ros2_control/pull/1688
183183
* To access the automatically created ``Command-/StateInterfaces`` we provide the ``std::unordered_map<std::string, InterfaceDescription>``, where the string is the fully qualified name of the interface and the ``InterfaceDescription`` is the configuration of the interface. The ``std::unordered_map<>`` are divided into ``type_state_interfaces_`` and ``type_command_interfaces_`` where type can be: ``joint``, ``sensor``, ``gpio`` and ``unlisted``. E.g. the ``CommandInterfaces`` for all joints can be found in the ``joint_command_interfaces_`` map. The ``unlisted`` includes all interfaces not listed in the ``ros2_control`` XML-tag but were created by overriding the ``export_unlisted_command_interfaces()`` or ``export_unlisted_state_interfaces()`` function by creating some custom ``Command-/StateInterfaces``.
184184

185185

186+
controller_manager
187+
******************
188+
* The default strictness of the ``swtich_controllers`` can now we be chosen using ROS 2 parameters. The default behaviour is still left to ``BEST_EFFORT`` (`#2168 <https://github.com/ros-controls/ros2_control/pull/2168>`_).
189+
186190
ros2controlcli
187191
**************
188192
* Spawner colours were added to ``list_controllers`` depending upon active or inactive (`#1409 <https://github.com/ros-controls/ros2_control/pull/1409>`_)

ros2controlcli/ros2controlcli/verb/switch_controllers.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from controller_manager import switch_controllers, bcolors
16+
from controller_manager_msgs.srv import SwitchController
1617

1718
from ros2cli.node.direct import add_arguments
1819
from ros2cli.node.strategy import NodeStrategy
@@ -40,7 +41,17 @@ def add_arguments(self, parser, cli_name):
4041
help="Name of the controllers to be activated",
4142
)
4243
arg.completer = LoadedControllerNameCompleter(["inactive"])
43-
parser.add_argument("--strict", action="store_true", help="Strict switch")
44+
strictness_group = parser.add_mutually_exclusive_group(required=False)
45+
strictness_group.add_argument(
46+
"--strict",
47+
help="Set the switch_controllers service strictness to strict",
48+
action="store_true",
49+
)
50+
strictness_group.add_argument(
51+
"--best-effort",
52+
help="Set the switch_controllers service strictness to best effort",
53+
action="store_true",
54+
)
4455
parser.add_argument("--activate-asap", action="store_true", help="Start asap controllers")
4556
parser.add_argument(
4657
"--switch-timeout",
@@ -53,12 +64,17 @@ def add_arguments(self, parser, cli_name):
5364

5465
def main(self, *, args):
5566
with NodeStrategy(args).direct_node as node:
67+
strictness = 0
68+
if args.strict:
69+
strictness = SwitchController.Request.STRICT
70+
elif args.best_effort:
71+
strictness = SwitchController.Request.BEST_EFFORT
5672
response = switch_controllers(
5773
node,
5874
args.controller_manager,
5975
args.deactivate,
6076
args.activate,
61-
args.strict,
77+
strictness,
6278
args.activate_asap,
6379
args.switch_timeout,
6480
)

0 commit comments

Comments
 (0)