controller_manager_scripts.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import unittest
3 import rospy
4 import subprocess
5 
6 # output of controller_manager list, will by combined dynamically
7 myc1_running='my_controller1 - hardware_interface::EffortJointInterface ( running )\n'
8 myc1_stopped='my_controller1 - hardware_interface::EffortJointInterface ( stopped )\n'
9 myc2_running='my_controller2 - hardware_interface::EffortJointInterface ( running )\n'
10 myc2_stopped='my_controller2 - hardware_interface::EffortJointInterface ( stopped )\n'
11 
12 
13 # output of other commands
14 loaded_fmt = 'Loaded %s\n'
15 unloaded_fmt = 'Unloaded %s successfully\n'
16 stopped_fmt = "Stopped ['%s'] successfully\n"
17 started_fmt = "Started ['%s'] successfully\n"
18 
19 no_controllers = 'No controllers are loaded in mechanism control\n'
20 reload_response = 'Restore: False\nSuccessfully reloaded libraries\n'
21 
22 # run controller_manager in process
23 def run_cm(*args):
24  return subprocess.check_output('rosrun controller_manager controller_manager ' + ' '.join(args), shell=True)
25 
26 # helper function that return the actual and expected response
27 def load_c(name):
28  return run_cm('load', name), loaded_fmt % name
29 
30 def unload_c(name):
31  return run_cm('unload', name), unloaded_fmt % name
32 
33 def stop_c(name):
34  return run_cm('stop', name), stopped_fmt % name
35 
36 def start_c(name):
37  return run_cm('start', name), started_fmt % name
38 
39 def spawn_c(name):
40  return run_cm('spawn', name), loaded_fmt % name + started_fmt % name
41 
42 def kill_c(name):
43  return run_cm('kill', name), stopped_fmt % name + unloaded_fmt % name
44 
45 class TestUtils(unittest.TestCase):
46  def test_scripts(self):
47  rospy.wait_for_service("/controller_manager/list_controllers", 2.0)
48 
49  # ensure that test controller is available
50  listed_types = run_cm('list-types').split()
51  self.assertIn('controller_manager_tests/EffortTestController', listed_types)
52 
53  # ensure that no controllers are loaded
54  self.assertEqual(run_cm('list'), no_controllers)
55 
56  # spawn (load+start) my_controller1 externally
57  s1 = subprocess.Popen('rosrun controller_manager spawner my_controller1 __name:=myspawner', shell=True)
58  rospy.sleep(1.0)
59  self.assertEqual(run_cm('list'), myc1_running)
60 
61  # load my_controller2
62  self.assertEqual(*load_c('my_controller2'))
63  self.assertEqual(run_cm('list'), myc1_running + myc2_stopped)
64 
65  # stop my_controller1
66  self.assertEqual(*stop_c('my_controller1'))
67  self.assertEqual(run_cm('list'), myc1_stopped + myc2_stopped)
68 
69  # start my_controller2
70  self.assertEqual(*start_c('my_controller2'))
71  self.assertEqual(run_cm('list'), myc1_stopped + myc2_running)
72 
73  # stop my_controller2
74  self.assertEqual(*stop_c('my_controller2'))
75  self.assertEqual(run_cm('list'), myc1_stopped + myc2_stopped)
76 
77  # start my_controller1
78  self.assertEqual(*start_c('my_controller1'))
79  self.assertEqual(run_cm('list'), myc1_running + myc2_stopped)
80 
81  # stop my_controller1 externally
82  u1 = subprocess.Popen('rosrun controller_manager unspawner my_controller1 __name:=myunspawner', shell=True)
83  rospy.sleep(1.0)
84  self.assertEqual(run_cm('list'), myc1_stopped + myc2_stopped)
85 
86  # kill unspawner -> restart my_controller1
87  subprocess.check_call('rosnode kill /myunspawner', shell=True)
88  self.assertEqual(u1.wait(), 0)
89  self.assertEqual(run_cm('list'), myc1_running + myc2_stopped)
90 
91  # kill spawner -> stop+unload my_controller1
92  subprocess.check_call('rosnode kill /myspawner', shell=True)
93  self.assertEqual(0, s1.wait())
94  self.assertEqual(run_cm('list'), myc2_stopped)
95 
96  # spawn (load+start) my_controller1
97  self.assertEqual(*spawn_c('my_controller1'))
98  self.assertEqual(run_cm('list'), myc2_stopped + myc1_running)
99 
100  # kill (stop+unload) my_controller1
101  self.assertEqual(*kill_c('my_controller1'))
102  self.assertEqual(run_cm('list'), myc2_stopped)
103 
104  # unload my_controller2
105  self.assertEqual(*unload_c('my_controller2'))
106  self.assertEqual(run_cm('list'), no_controllers)
107 
108  self.assertEqual(run_cm('reload-libraries'), reload_response)
109 
110 if __name__ == '__main__':
111  import rostest
112  rostest.rosrun('controller_manager_msgs',
113  'controller_manager_scripts_rostest',
114  TestUtils)


controller_manager_tests
Author(s): Vijay Pradeep, Adolfo Rodriguez Tsouroukdissian
autogenerated on Fri Jun 7 2019 22:00:10