component_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import sys
00019 import time
00020 import unittest
00021 
00022 import rospy
00023 import rostest
00024 from trajectory_msgs.msg import *
00025 from simple_script_server import *
00026 from control_msgs.msg import *
00027 
00028 class UnitTest(unittest.TestCase):
00029     def __init__(self, *args):
00030         super(UnitTest, self).__init__(*args)
00031         rospy.init_node('component_test')
00032         self.message_received = False
00033         self.sss=simple_script_server()
00034 
00035     def setUp(self):
00036         self.errors = []
00037 
00038     def test_component(self):
00039         # get parameters
00040         try:
00041             # component
00042             if not rospy.has_param('~component'):
00043                 self.fail('Parameter component does not exist on ROS Parameter Server')
00044             component = rospy.get_param('~component')
00045 
00046             # movement command
00047             if not rospy.has_param('~target'):
00048                 self.fail('Parameter target does not exist on ROS Parameter Server')
00049             target = rospy.get_param('~target')
00050 
00051             # time to wait before
00052             wait_time = rospy.get_param('~wait_time',5)
00053 
00054             # error range
00055             if not rospy.has_param('~error_range'):
00056                 self.fail('Parameter error_range does not exist on ROS Parameter Server')
00057             error_range = rospy.get_param('~error_range')
00058 
00059         except KeyError, e:
00060             self.fail('Parameters not set properly')
00061 
00062         print """
00063               Component: %s
00064               Target: %s
00065               Wait Time: %s
00066               Error Range: %s"""%(component, target, wait_time, error_range)
00067 
00068         # check parameters
00069         # \todo do more parameter tests
00070         if error_range < 0.0:
00071             error_msg = "Parameter error_range should be positive, but is " + error_range
00072             self.fail(error_msg)
00073         if wait_time < 0.0:
00074             error_msg = "Parameter wait_time should be positive, but is " + wait_time
00075             self.fail(error_msg)
00076 
00077         # init subscribers
00078         command_topic = "/" + component + "_controller/command"
00079         state_topic = "/" + component + "_controller/state"
00080         sub_command_topic = rospy.Subscriber(command_topic, JointTrajectory, self.cb_command)
00081         sub_state_topic = rospy.Subscriber(state_topic, JointTrajectoryControllerState, self.cb_state)
00082 
00083         # init component
00084         init_handle = self.sss.init(component)
00085         if init_handle.get_error_code() != 0:
00086             error_msg = 'Could not initialize ' + component
00087             self.fail(error_msg)
00088 
00089         # start actual test
00090         print "Waiting for messages"
00091         # give the topics some seconds to receive messages
00092         wallclock_timeout_t = time.time() + wait_time
00093         while not self.message_received and time.time() < wallclock_timeout_t:
00094             #print "###debug here###"
00095             time.sleep(0.1)
00096         if not self.message_received:
00097             self.fail('No state message received within wait_time')
00098 
00099         # send commands to component
00100         move_handle = self.sss.move(component,target)
00101         # move_handle = self.sss.move("arm","folded")
00102         if move_handle.get_error_code() != 0:
00103             error_msg = 'Could not move ' + component
00104             self.fail(error_msg + "; errorCode: " + str(move_handle.get_error_code()))
00105 
00106         # get last point out of trajectory
00107         traj_endpoint = self.command_traj.points[len(self.command_traj.points)-1]
00108 
00109         # Start evaluation
00110         timeout_t = traj_endpoint.time_from_start.to_sec()*0.5 # movement should already be finished, but let wait with an additional buffer of 50% times the desired time
00111         rospy.sleep(timeout_t)
00112         print "Done waiting, validating results"
00113         actual_pos = self.actual_pos # fix current position configuration for later evaluation
00114 
00115         # checking if target position is realy reached
00116         for i in range(len(traj_endpoint.positions)):
00117             self.assert_(((traj_endpoint.positions[i] - actual_pos[i]) < error_range), "Target position out of error_range")
00118 
00119     # callback functions
00120     def cb_state(self, msg):
00121         self.message_received = True
00122         self.actual_pos = msg.actual.positions
00123     def cb_command(self, msg):
00124         self.command_traj = msg
00125 
00126 if __name__ == '__main__':
00127     try:
00128         rostest.run('rostest', 'component_test', UnitTest, sys.argv)
00129     except KeyboardInterrupt, e:
00130         pass
00131     print "exiting"
00132 


cob_gazebo
Author(s): Felix Messmer , Nadia Hammoudeh Garcia , Florian Weisshardt
autogenerated on Sat Jun 8 2019 18:52:23