Skip to content

Commit

Permalink
Add mock robot controller (#5)
Browse files Browse the repository at this point in the history
* add mock system

* fix description

* update bringup

* move mock robot

* add pyrobot (pytroller-based)

* add pytroller to VCS

* add pytroller logic

* pyrobot: add config + spawner to hk1d_bringup

* add rotation Z inertia in URDF

* lining pytroller

* add pre-commit exclude

* linting mock robot
  • Loading branch information
tpoignonec authored Nov 9, 2023
1 parent a881cda commit 5f0289d
Show file tree
Hide file tree
Showing 30 changed files with 1,836 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#
# See https://github.com/pre-commit/pre-commit

exclude: "hk1d_arduino/*"
exclude: |
(?x)(
^hk1d_arduino/|
^hk1d_mock_component_controllers/hk1d_mock_pyrobot/
)
repos:
# Standard hooks
Expand Down
38 changes: 35 additions & 3 deletions hk1d_bringup/config/hk1d_controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,45 @@ controller_manager:
joint_state_broadcaster:
type: joint_state_broadcaster/JointStateBroadcaster

force_sensor_broadcaster:
type: force_torque_sensor_broadcaster/ForceTorqueSensorBroadcaster

forward_effort_controller:
type: forward_command_controller/ForwardCommandController

mock_system_controller:
type: hk1d_mock_robot/Hk1dMockRobot

mock_system_pycontroller:
type: hk1d_mock_pyrobot/Hk1DMockPyrobot

mock_system_pycontroller:
ros__parameters:
joints:
- joint_1
- force_sensor
interface_full_names:
- joint_1/position
- joint_1/velocity
- force_sensor/force.0

force_sensor_broadcaster:
ros__parameters:
frame_id: ft_sensor
interface_names:
force:
x: force_sensor/force.0

forward_position_controller:
forward_effort_controller:
ros__parameters:
joints:
- joint1
- joint2
- joint_1
interface_name: effort


mock_system_controller:
ros__parameters:
joint_name: joint_1
ft_sensor:
frame: ft_sensor
interface_name: force_sensor/force.0
24 changes: 24 additions & 0 deletions hk1d_bringup/launch/hk1d.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.conditions import IfCondition
from launch.substitutions import Command, FindExecutable, LaunchConfiguration, PathJoinSubstitution
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare
Expand Down Expand Up @@ -186,11 +187,34 @@ def get_robot_description_semantic_content():
executable="spawner",
arguments=["joint_state_broadcaster", "--controller-manager", "/controller_manager"],
)
force_sensor_broadcaster_spawner = Node(
package="controller_manager",
executable="spawner",
arguments=["force_sensor_broadcaster", "--controller-manager", "/controller_manager"],
)

mock_controller_name = "mock_system_pycontroller"
mock_system_spawner = Node(
package="controller_manager",
executable="spawner",
arguments=[mock_controller_name, "--controller-manager", "/controller_manager"],
condition=IfCondition(use_fake_hardware),
)

force_forward_controller_spawner = Node(
package="controller_manager",
executable="spawner",
arguments=["forward_effort_controller", "--controller-manager", "/controller_manager"],
condition=IfCondition(use_fake_hardware),
)

nodes = [
control_node,
robot_state_pub_node,
joint_state_broadcaster_spawner,
force_sensor_broadcaster_spawner,
mock_system_spawner,
force_forward_controller_spawner,
rviz_node,
]

Expand Down
3 changes: 2 additions & 1 deletion hk1d_description/ros2_control/hk1d.r2c_hardware.xacro
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<hardware>
<xacro:if value="${use_fake_hardware}">
<plugin>mock_components/GenericSystem</plugin>
<param name="calculate_dynamics">true</param>
<param name="calculate_dynamics">false</param>
<param name="mock_sensor_commands">true</param>
</xacro:if>
<xacro:unless value="${use_fake_hardware}">
<plugin> ethercat_driver/EthercatDriver </plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@
<!-- Maxon motor (ethercat) -->

<joint name="${prefix}joint_1">
<state_interface name="position"/>
<state_interface name="velocity"/>
<state_interface name="effort"/>

<command_interface name="${command_interface}"/>
<state_interface name="position">
<param name="initial_value">0.0</param>
</state_interface>
<state_interface name="velocity">
<param name="initial_value">0.0</param>
</state_interface>
<state_interface name="effort">
<param name="initial_value">0.0</param>
</state_interface>

<command_interface name="effort"/>
<command_interface name="reset_fault"/>

<xacro:if value="${use_fake_hardware}">
<command_interface name="position"/>
<command_interface name="velocity"/>
</xacro:if>

<xacro:unless value="${use_fake_hardware}">
<ec_module name="${prefix}motor_Maxon">
<plugin>ethercat_generic_plugins/EcCiA402Drive</plugin>
Expand Down
5 changes: 5 additions & 0 deletions hk1d_description/urdf/hk1d.urdf.xacro
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
</geometry>
<xacro:material_link_1/>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="0.25"/>
<inertia ixx="0.0" ixy="0.0" ixz="0.0" iyy="0.0" iyz="0.0" izz="0.25" />
</inertial>
</link>

<link name="${prefix}ft_sensor" />
Expand Down
161 changes: 161 additions & 0 deletions hk1d_mock_component_controllers/hk1d_mock_pyrobot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
cmake_minimum_required(VERSION 3.16)
project(hk1d_mock_pyrobot LANGUAGES CXX)

if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
add_compile_options(-Wall -Wextra -Wpedantic -Wconversion)
endif()

set(THIS_PACKAGE_INCLUDE_DEPENDS
controller_interface
generate_parameter_library
hardware_interface
pluginlib
rclcpp
rclcpp_lifecycle
realtime_tools
)

find_package(ament_cmake REQUIRED)
foreach(Dependency IN ITEMS ${THIS_PACKAGE_INCLUDE_DEPENDS})
find_package(${Dependency} REQUIRED)
endforeach()

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

# Set the parameter header file name
set(PARAM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/hk1d_mock_pyrobot_parameters/include)
set(PARAM_HEADER_FILE ${PARAM_INCLUDE_DIR}/hk1d_mock_pyrobot_parameters.hpp)

# Make logic build directory
set(LOGIC_DIR ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}/hk1d_mock_pyrobot_logic)
set(LOGIC_INCLUDE_DIR ${LOGIC_DIR}/include/hk1d_mock_pyrobot)
file(MAKE_DIRECTORY ${LOGIC_DIR})
file(MAKE_DIRECTORY ${LOGIC_INCLUDE_DIR})

file (REMOVE ${LOGIC_DIR}/hk1d_mock_pyrobot_logic.cpp)
file (REMOVE ${LOGIC_DIR}/hk1d_mock_pyrobot_logic.h)
file (REMOVE ${LOGIC_INCLUDE_DIR}/hk1d_mock_pyrobot_logic.h)

add_custom_command(
OUTPUT ${LOGIC_DIR}/hk1d_mock_pyrobot_logic.cpp ${LOGIC_DIR}/hk1d_mock_pyrobot_logic.h
COMMAND cython3 -3 --cplus ${CMAKE_CURRENT_SOURCE_DIR}/src/hk1d_mock_pyrobot_logic.pyx
-o ${LOGIC_DIR}/hk1d_mock_pyrobot_logic.cpp -I ${PARAM_INCLUDE_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/src/hk1d_mock_pyrobot_logic.pyx
${CMAKE_CURRENT_SOURCE_DIR}/script/hk1d_mock_pyrobot_logic_impl.py
)

# Copy the header file into the include directory
add_custom_command(
OUTPUT ${LOGIC_INCLUDE_DIR}/hk1d_mock_pyrobot_logic.h
COMMAND ${CMAKE_COMMAND} -E copy
${LOGIC_DIR}/hk1d_mock_pyrobot_logic.h ${LOGIC_INCLUDE_DIR}/hk1d_mock_pyrobot_logic.h
DEPENDS ${LOGIC_DIR}/hk1d_mock_pyrobot_logic.h
)

generate_parameter_library(
hk1d_mock_pyrobot_parameters
src/hk1d_mock_pyrobot_parameters.yaml
)

file (REMOVE ${PARAM_INCLUDE_DIR}/hk1d_mock_pyrobot_parameters.pxd)

# Generate the pxd for the library
add_custom_command(
OUTPUT ${PARAM_INCLUDE_DIR}/hk1d_mock_pyrobot_parameters.pxd
COMMAND ros2 run pytroller_tools generate_pxd ${PARAM_INCLUDE_DIR}/hk1d_mock_pyrobot_parameters.pxd ${PARAM_HEADER_FILE}
DEPENDS ${PARAM_HEADER_FILE}
)

add_library(hk1d_mock_pyrobot SHARED
src/hk1d_mock_pyrobot.cpp
${LOGIC_DIR}/hk1d_mock_pyrobot_logic.cpp
${LOGIC_INCLUDE_DIR}/hk1d_mock_pyrobot_logic.h
${PARAM_INCLUDE_DIR}/hk1d_mock_pyrobot_parameters.pxd
)
target_compile_features(hk1d_mock_pyrobot PUBLIC cxx_std_17)
target_include_directories(hk1d_mock_pyrobot PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/hk1d_mock_pyrobot>
)
target_include_directories(hk1d_mock_pyrobot PUBLIC
$<BUILD_INTERFACE:${LOGIC_DIR}/include>
$<INSTALL_INTERFACE:include/hk1d_mock_pyrobot>
)
target_link_libraries(hk1d_mock_pyrobot PUBLIC
hk1d_mock_pyrobot_parameters
${PYTHON_LIBRARIES}
)
ament_target_dependencies(hk1d_mock_pyrobot PUBLIC ${THIS_PACKAGE_INCLUDE_DEPENDS})
# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(hk1d_mock_pyrobot PRIVATE "PYTROLLER_BUILDING_DLL")
pluginlib_export_plugin_description_file(controller_interface controller_plugin.xml)


if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
find_package(ament_cmake_gmock REQUIRED)
find_package(controller_manager REQUIRED)
find_package(hardware_interface REQUIRED)
find_package(ros2_control_test_assets REQUIRED)

ament_lint_auto_find_test_dependencies()

# Load test
add_rostest_with_parameters_gmock(
test_load_hk1d_mock_pyrobot
test/test_load_hk1d_mock_pyrobot.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/test_params.yaml
)
target_link_libraries(test_load_hk1d_mock_pyrobot
hk1d_mock_pyrobot
)
ament_target_dependencies(test_load_hk1d_mock_pyrobot
controller_manager
hardware_interface
ros2_control_test_assets
)

# Controller test
add_rostest_with_parameters_gmock(
test_hk1d_mock_pyrobot
test/test_hk1d_mock_pyrobot.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/test_params.yaml
)

target_link_libraries(test_hk1d_mock_pyrobot
hk1d_mock_pyrobot
)

ament_target_dependencies(test_load_hk1d_mock_pyrobot
controller_manager
hardware_interface
)

endif()

install(
DIRECTORY ${LOGIC_DIR}/include
DESTINATION include/hk1d_mock_pyrobot
)
install(
DIRECTORY include/
DESTINATION include/hk1d_mock_pyrobot
)
install(
TARGETS
hk1d_mock_pyrobot
hk1d_mock_pyrobot_parameters
EXPORT export_hk1d_mock_pyrobot
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
INCLUDES DESTINATION include
)

ament_export_targets(export_hk1d_mock_pyrobot HAS_LIBRARY_TARGET)
ament_export_dependencies(${THIS_PACKAGE_INCLUDE_DEPENDS})
ament_package()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<library path="hk1d_mock_pyrobot">
<class name="hk1d_mock_pyrobot/Hk1DMockPyrobot"
type="hk1d_mock_pyrobot::Hk1DMockPyrobot"
base_class_type="controller_interface::ControllerInterface">
<description>
Python controller for ros2_control
</description>
</class>
</library>
Loading

0 comments on commit 5f0289d

Please sign in to comment.