|
| 1 | +# Copyright 2023 ros2_control Development Team |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from controller_manager import set_hardware_component_state |
| 16 | + |
| 17 | +from ros2cli.node.direct import add_arguments |
| 18 | +from ros2cli.node.strategy import NodeStrategy |
| 19 | +from ros2cli.verb import VerbExtension |
| 20 | +from lifecycle_msgs.msg import State |
| 21 | + |
| 22 | +from ros2controlcli.api import add_controller_mgr_parsers, LoadedHardwareComponentNameCompleter |
| 23 | + |
| 24 | + |
| 25 | +class SetHardwareComponentStateVerb(VerbExtension): |
| 26 | + """Adjust the state of the hardware component.""" |
| 27 | + |
| 28 | + def add_arguments(self, parser, cli_name): |
| 29 | + add_arguments(parser) |
| 30 | + arg = parser.add_argument( |
| 31 | + "hardware_component_name", help="Name of the hardware_component to be changed" |
| 32 | + ) |
| 33 | + arg.completer = LoadedHardwareComponentNameCompleter() |
| 34 | + arg = parser.add_argument( |
| 35 | + "state", |
| 36 | + choices=["unconfigured", "inactive", "active"], |
| 37 | + help="State in which the hardware component should be changed to", |
| 38 | + ) |
| 39 | + add_controller_mgr_parsers(parser) |
| 40 | + |
| 41 | + def main(self, *, args): |
| 42 | + with NodeStrategy(args) as node: |
| 43 | + |
| 44 | + if args.state == "unconfigured": |
| 45 | + |
| 46 | + unconfigured_state = State() |
| 47 | + unconfigured_state.id = State.PRIMARY_STATE_UNCONFIGURED |
| 48 | + unconfigured_state.label = "unconfigured" |
| 49 | + |
| 50 | + response = set_hardware_component_state( |
| 51 | + node, args.controller_manager, args.hardware_component_name, unconfigured_state |
| 52 | + ) |
| 53 | + if not response.ok: |
| 54 | + return "Error cleaning up hardware component, check controller_manager logs" |
| 55 | + |
| 56 | + if args.state == "inactive": |
| 57 | + inactive_state = State() |
| 58 | + inactive_state.id = State.PRIMARY_STATE_INACTIVE |
| 59 | + inactive_state.label = "inactive" |
| 60 | + |
| 61 | + response = set_hardware_component_state( |
| 62 | + node, args.controller_manager, args.hardware_component_name, inactive_state |
| 63 | + ) |
| 64 | + if not response.ok: |
| 65 | + return "Error stopping hardware component, check controller_manager logs" |
| 66 | + |
| 67 | + if args.state == "active": |
| 68 | + |
| 69 | + active_state = State() |
| 70 | + active_state.id = State.PRIMARY_STATE_ACTIVE |
| 71 | + active_state.label = "active" |
| 72 | + |
| 73 | + response = set_hardware_component_state( |
| 74 | + node, args.controller_manager, args.hardware_component_name, active_state |
| 75 | + ) |
| 76 | + if not response.ok: |
| 77 | + return "Error activating hardware component, check controller_manager logs" |
| 78 | + |
| 79 | + print(f"Successfully set {args.hardware_component_name} to state {response.state}") |
| 80 | + return 0 |
0 commit comments