1- #! /usr/bin/env python
1+ #! /usr/bin/env python3
22
33import numpy as np
44import pybullet
55import 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
911from 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
1236class 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