Skip to content

Commit 0b414d0

Browse files
Add GenericSystem to GPIO example (ros-controls#429)
* Add GenericSystem to GPIO example * Update doc * Clarify section with generic_system
1 parent ff8dd8b commit 0b414d0

File tree

4 files changed

+140
-13
lines changed

4 files changed

+140
-13
lines changed

example_10/bringup/launch/rrbot.launch.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,27 @@
1414

1515

1616
from launch import LaunchDescription
17-
from launch.actions import RegisterEventHandler
17+
from launch.actions import DeclareLaunchArgument, RegisterEventHandler
1818
from launch.event_handlers import OnProcessExit
19-
from launch.substitutions import Command, FindExecutable, PathJoinSubstitution
19+
from launch.substitutions import Command, FindExecutable, PathJoinSubstitution, LaunchConfiguration
2020

2121
from launch_ros.actions import Node
2222
from launch_ros.substitutions import FindPackageShare
2323

2424

2525
def generate_launch_description():
26+
# Declare arguments
27+
declared_arguments = []
28+
declared_arguments.append(
29+
DeclareLaunchArgument(
30+
"use_mock_hardware",
31+
default_value="false",
32+
description="Start robot with mock hardware mirroring command to its states.",
33+
)
34+
)
35+
# Initialize Arguments
36+
use_mock_hardware = LaunchConfiguration("use_mock_hardware")
37+
2638
# Get URDF via xacro
2739
robot_description_content = Command(
2840
[
@@ -35,6 +47,9 @@ def generate_launch_description():
3547
"rrbot.urdf.xacro",
3648
]
3749
),
50+
" ",
51+
"use_mock_hardware:=",
52+
use_mock_hardware,
3853
]
3954
)
4055
robot_description = {"robot_description": robot_description_content}
@@ -94,4 +109,4 @@ def generate_launch_description():
94109
delay_robot_controller_spawner_after_joint_state_broadcaster_spawner,
95110
]
96111

97-
return LaunchDescription(nodes)
112+
return LaunchDescription(declared_arguments + nodes)

example_10/description/ros2_control/rrbot.ros2_control.xacro

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
<?xml version="1.0"?>
22
<robot xmlns:xacro="http://www.ros.org/wiki/xacro">
33

4-
<xacro:macro name="rrbot_ros2_control" params="name prefix">
4+
<xacro:macro name="rrbot_ros2_control" params="name prefix use_mock_hardware">
55

66
<ros2_control name="${name}" type="system">
7-
<hardware>
8-
<plugin>ros2_control_demo_example_10/RRBotSystemWithGPIOHardware</plugin>
9-
<param name="example_param_hw_start_duration_sec">0</param>
10-
<param name="example_param_hw_stop_duration_sec">3.0</param>
11-
<param name="example_param_hw_slowdown">100</param>
12-
</hardware>
7+
<xacro:unless value="${use_mock_hardware}">
8+
<hardware>
9+
<plugin>ros2_control_demo_example_10/RRBotSystemWithGPIOHardware</plugin>
10+
<param name="example_param_hw_start_duration_sec">0</param>
11+
<param name="example_param_hw_stop_duration_sec">3.0</param>
12+
<param name="example_param_hw_slowdown">100</param>
13+
</hardware>
14+
</xacro:unless>
15+
<xacro:if value="${use_mock_hardware}">
16+
<hardware>
17+
<plugin>mock_components/GenericSystem</plugin>
18+
<param name="calculate_dynamics">true</param>
19+
</hardware>
20+
</xacro:if>
1321

1422
<joint name="${prefix}joint1">
1523
<command_interface name="position">
@@ -34,7 +42,9 @@
3442
</gpio>
3543
<gpio name="flange_vacuum">
3644
<command_interface name="vacuum"/>
37-
<state_interface name="vacuum"/> <!-- Needed to know current state of the input -->
45+
<state_interface name="vacuum">
46+
<param name="initial_value">1.0</param>
47+
</state_interface>
3848
</gpio>
3949
</ros2_control>
4050

example_10/description/urdf/rrbot.urdf.xacro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ https://github.com/ros-simulation/gazebo_ros_demos/blob/kinetic-devel/rrbot_desc
66
-->
77
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="2dof_robot">
88
<xacro:arg name="prefix" default="" />
9+
<xacro:arg name="use_mock_hardware" default="false" />
910

1011
<!-- Import RRBot macro -->
1112
<xacro:include filename="$(find ros2_control_demo_description)/rrbot/urdf/rrbot_description.urdf.xacro" />
@@ -24,6 +25,6 @@ https://github.com/ros-simulation/gazebo_ros_demos/blob/kinetic-devel/rrbot_desc
2425
</xacro:rrbot>
2526

2627
<xacro:rrbot_ros2_control
27-
name="RRBot" prefix="$(arg prefix)" />
28+
name="RRBot" prefix="$(arg prefix)" use_mock_hardware="$(arg use_mock_hardware)"/>
2829

2930
</robot>

example_10/doc/userdoc.rst

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,103 @@ The *RRBot* URDF files can be found in the ``description/urdf`` folder.
9191
[RRBotSystemWithGPIOHardware]: Got command 0.5 for GP output 0!
9292
[RRBotSystemWithGPIOHardware]: Got command 0.7 for GP output 1!
9393
94+
7. Let's introspect the ros2_control hardware component. Calling
95+
96+
.. code-block:: shell
97+
98+
ros2 control list_hardware_components
99+
100+
should give you
101+
102+
.. code-block:: shell
103+
104+
Hardware Component 1
105+
name: RRBot
106+
type: system
107+
plugin name: ros2_control_demo_example_10/RRBotSystemWithGPIOHardware
108+
state: id=3 label=active
109+
command interfaces
110+
joint1/position [available] [claimed]
111+
joint2/position [available] [claimed]
112+
flange_analog_IOs/analog_output1 [available] [claimed]
113+
flange_vacuum/vacuum [available] [claimed]
114+
115+
This shows that the custom hardware interface plugin is loaded and running. If you work on a real
116+
robot and don't have a simulator running, it is often faster to use the ``mock_components/GenericSystem``
117+
hardware component instead of writing a custom one. Stop the launch file and start it again with
118+
an additional parameter
119+
120+
.. code-block:: shell
121+
122+
ros2 launch ros2_control_demo_example_10 rrbot.launch.py use_mock_hardware:=True
123+
124+
Calling ``list_hardware_components`` with the ``-v`` option
125+
126+
.. code-block:: shell
127+
128+
ros2 control list_hardware_components -v
129+
130+
now should give you
131+
132+
.. code-block:: shell
133+
134+
Hardware Component 1
135+
name: RRBot
136+
type: system
137+
plugin name: mock_components/GenericSystem
138+
state: id=3 label=active
139+
command interfaces
140+
joint1/position [available] [claimed]
141+
joint2/position [available] [claimed]
142+
flange_analog_IOs/analog_output1 [available] [claimed]
143+
flange_vacuum/vacuum [available] [claimed]
144+
state interfaces
145+
joint1/position [available]
146+
joint2/position [available]
147+
flange_analog_IOs/analog_output1 [available]
148+
flange_analog_IOs/analog_input1 [available]
149+
flange_analog_IOs/analog_input2 [available]
150+
flange_vacuum/vacuum [available]
151+
152+
One can see that the plugin ``mock_components/GenericSystem`` was now loaded instead: It will mirror the command interfaces to state interfaces with identical name. Call
153+
154+
.. code-block:: shell
155+
156+
ros2 topic echo /gpio_controller/inputs
157+
158+
again and you should see that - unless commands are received - the values of the state interfaces are now ``nan`` except for the vacuum interface.
159+
160+
.. code-block:: shell
161+
162+
interface_names:
163+
- flange_analog_IOs/analog_output1
164+
- flange_analog_IOs/analog_input1
165+
- flange_analog_IOs/analog_input2
166+
- flange_vacuum/vacuum
167+
values:
168+
- .nan
169+
- .nan
170+
- .nan
171+
- 1.0
172+
173+
This is, because for the vacuum interface an initial value of ``1.0`` is set in the URDF file.
174+
175+
.. code-block:: xml
176+
177+
<gpio name="flange_vacuum">
178+
<command_interface name="vacuum"/>
179+
<state_interface name="vacuum">
180+
<param name="initial_value">1.0</param>
181+
</state_interface>
182+
</gpio>
183+
184+
Call again
185+
186+
.. code-block:: shell
187+
188+
ros2 topic pub /gpio_controller/commands std_msgs/msg/Float64MultiArray "{data: [0.5,0.7]}"
189+
190+
and you will see that the GPIO command interfaces will be mirrored to their respective state interfaces.
94191

95192
Files used for this demos
96193
-------------------------
@@ -104,7 +201,11 @@ Files used for this demos
104201

105202
- RViz configuration: `rrbot.rviz <https://github.com/ros-controls/ros2_control_demos/tree/{REPOS_FILE_BRANCH}/ros2_control_demo_description/rrbot/rviz/rrbot.rviz>`__
106203

107-
- Hardware interface plugin: `rrbot.cpp <https://github.com/ros-controls/ros2_control_demos/tree/{REPOS_FILE_BRANCH}/example_10/hardware/rrbot.cpp>`__
204+
- Hardware interface plugin:
205+
206+
+ `rrbot.cpp <https://github.com/ros-controls/ros2_control_demos/tree/{REPOS_FILE_BRANCH}/example_10/hardware/rrbot.cpp>`__
207+
+ `generic_system.cpp <https://github.com/ros-controls/ros2_control/tree/{REPOS_FILE_BRANCH}/hardware_interface/src/mock_components/generic_system.cpp>`__
208+
108209
- GPIO controller: `gpio_controller.cpp <https://github.com/ros-controls/ros2_control_demos/tree/{REPOS_FILE_BRANCH}/example_10/controllers/gpio_controller.cpp>`__
109210

110211

0 commit comments

Comments
 (0)