Skip to content

Commit cbfdc5e

Browse files
test(python): migrate to ros2
1 parent c8983eb commit cbfdc5e

3 files changed

Lines changed: 58 additions & 29 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(CMAKE_COLOR_DIAGNOSTICS ON)
1414
option(INSTALL_DOCUMENTATION "Generate and install the documentation" OFF)
1515
option(USE_ROS2 "Use ROS2" OFF)
1616
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)
17+
option(ENABLE_PYBULLET_TEST "Enable tests with pybullet simulation" OFF)
1718

1819
project(centroidal_control_collection LANGUAGES CXX)
1920
include(GNUInstallDirs) # For CMAKE_INSTALL_LIBDIR
@@ -80,13 +81,12 @@ endif()
8081

8182
if(USE_ROS2)
8283
ament_export_include_directories(include)
83-
ament_export_libraries(CCC)
8484
ament_export_dependencies(
8585
rclcpp
8686
qp_solver_collection
8787
force_control_collection
8888
nmpc_ddp
8989
)
90-
ament_export_targets(${PROJECT_NAME})
90+
ament_export_targets(${PROJECT_NAME} HAS_LIBRARY_TARGET)
9191
ament_package()
9292
endif()

tests/CMakeLists.txt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ foreach(NAME IN LISTS CCC_gtest_list)
3333
add_CCC_test(${NAME})
3434
endforeach()
3535

36-
option(ENABLE_PYBULLET_TEST "Enable tests with pybullet simulation" OFF)
37-
# if(ENABLE_PYBULLET_TEST)
38-
# find_package(rostest REQUIRED)
36+
if(ENABLE_PYBULLET_TEST)
37+
if(USE_ROS2)
38+
find_package(launch_testing_ament_cmake)
39+
ament_add_gtest(TestSimDdpSingleRigidBody src/TestSimDdpSingleRigidBody.cpp TIMEOUT 600)
40+
ament_target_dependencies(TestSimDdpSingleRigidBody
41+
rclcpp
42+
std_msgs
43+
std_srvs
44+
)
3945

40-
# set(CCC_rostest_list
41-
# TestSimDdpSingleRigidBody
42-
# )
43-
44-
# if(USE_ROS2)
45-
# foreach(NAME IN LISTS CCC_rostest_list)
46-
# add_rostest_gtest(${NAME} test/${NAME}.test src/${NAME}.cpp)
47-
# target_link_libraries(${NAME} CCC)
48-
# endforeach()
49-
# endif()
50-
# endif()
46+
target_link_libraries(TestSimDdpSingleRigidBody CCC)
47+
install(TARGETS TestSimDdpSingleRigidBody DESTINATION lib/${PROJECT_NAME})
48+
add_launch_test(scripts/simSingleRigidBody.py)
49+
install(DIRECTORY scripts DESTINATION share/${PROJECT_NAME}/tests)
50+
endif()
51+
endif()

tests/scripts/simSingleRigidBody.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/env python3
22

33
import numpy as np
44
import pybullet
55
import pybullet_data
6+
import unittest
7+
import sys
68

7-
import rospy
8-
from tf import transformations
9+
import rclpy
10+
from tf_transformations import quaternion_from_euler, euler_from_quaternion
911
from std_msgs.msg import Float64MultiArray
10-
12+
import launch
13+
import launch_testing
14+
import pytest
15+
from launch_ros.actions import Node
16+
17+
@pytest.mark.launch_test
18+
def generate_test_description():
19+
test_sim_ddp_srb = Node(
20+
package='centroidal_control_collection',
21+
executable='TestSimDdpSingleRigidBody',
22+
name='test_sim_ddp_srb',
23+
output='screen'
24+
)
25+
26+
content = {}
27+
28+
return (
29+
launch.LaunchDescription([
30+
test_sim_ddp_srb,
31+
launch_testing.actions.ReadyToTest(),
32+
]),
33+
content
34+
)
1135

1236
class SimSingleRigidBody(object):
1337
def __init__(self, enable_gui=True):
38+
self.node = rclpy.create_node("sim")
1439
# Instantiate simulator
1540
if enable_gui:
1641
pybullet.connect(pybullet.GUI)
@@ -56,8 +81,8 @@ def __init__(self, enable_gui=True):
5681
self.force_line_uid_list = []
5782

5883
# Setup ROS
59-
self.state_pub = rospy.Publisher("state", Float64MultiArray, queue_size=1)
60-
self.control_sub = rospy.Subscriber("control", Float64MultiArray, self.callback, queue_size=1)
84+
self.state_pub = self.node.create_publisher(Float64MultiArray, "state", 1)
85+
self.control_sub = self.node.create_subscription(Float64MultiArray, "control", self.callback, 1)
6186

6287
def runOnce(self):
6388
""""Run simulation step once."""
@@ -106,15 +131,15 @@ def runOnce(self):
106131
def getState(self):
107132
""""Get state [c, alpha, v, omega]."""
108133
c, quat = pybullet.getBasePositionAndOrientation(bodyUniqueId=self.body_uid)
109-
alpha = transformations.euler_from_quaternion(quat, axes="rzyx")
134+
alpha = euler_from_quaternion(quat, axes="rzyx")
110135
v, omega = pybullet.getBaseVelocity(bodyUniqueId=self.body_uid)
111-
return np.array([c, alpha, v, omega]).flatten()
136+
return np.array([c, alpha, v, omega]).flatten().tolist()
112137

113138
def setState(self, state):
114139
"""Set state [c, alpha, v, omega]."""
115140
pybullet.resetBasePositionAndOrientation(bodyUniqueId=self.body_uid,
116141
posObj=state[0:3],
117-
ornObj=transformations.quaternion_from_euler(*state[3:6], axes="rzyx"))
142+
ornObj=quaternion_from_euler(*state[3:6], axes="rzyx"))
118143
pybullet.resetBaseVelocity(objectUniqueId=self.body_uid,
119144
linearVelocity=state[6:9],
120145
angularVelocity=state[9:12])
@@ -129,7 +154,7 @@ def demo():
129154
sim = SimSingleRigidBody(True)
130155

131156
t = 0.0 # [sec]
132-
rate = rospy.Rate(1.0 / sim.dt)
157+
rate = sim.node.create_rate(1.0 / sim.dt)
133158
while pybullet.isConnected():
134159
# Run simulation step
135160
sim.runOnce()
@@ -138,7 +163,10 @@ def demo():
138163
rate.sleep()
139164
t += sim.dt
140165

166+
class TestSimSingleRigidBody(unittest.TestCase):
167+
def __init__(self, *args):
168+
super().__init__(*args)
141169

142-
if __name__ == "__main__":
143-
rospy.init_node("sim")
144-
demo()
170+
def test(self):
171+
rclpy.init(args=sys.argv)
172+
demo()

0 commit comments

Comments
 (0)