test_classes.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2008, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 PKG = "pr2_camera_synchronizer"
00035 import roslib; roslib.load_manifest(PKG)
00036 from pr2_camera_synchronizer.synchronizer_classes import *
00037 import unittest
00038 import pr2_camera_synchronizer.cfg.CameraSynchronizerConfig as Config
00039                              
00040 DIGITS = 16
00041 
00042 def issorted(l):
00043     copy = list(l)
00044     copy.sort()
00045     return copy == l
00046 
00047 def interlace(l1, l2):
00048     l = zip(l1, l2)
00049     return list(sum(l, ()))
00050 
00051 class TestProjector(unittest.TestCase):
00052     def runCase(self, rate, length, shift, out_repeat_period, out_proj_end, out_alt_end, out_noproj_end, level = lvl_projector):
00053         config = { 
00054                 param_proj_rate : rate,  
00055                 "projector_pulse_length" : length,
00056                 "projector_pulse_shift" : shift,
00057                 "projector_tweak" : 0,
00058                 "projector_mode" : Config.CameraSynchronizer_ProjectorOn,
00059                 }
00060         proj = Projector()
00061         proj.process_update(config, level)
00062 
00063         if level & lvl_projector != 0:
00064             self.assert_(math.fmod(config["projector_rate"], ETHERCAT_INTERVAL)) # Integer multiples of ethercat rate
00065             self.assert_(config["projector_pulse_length"] >= length, "Pulse length %f"%config["projector_pulse_length"]) # Never shorten the pulse length
00066             self.assertEqual(config["projector_pulse_length"], proj.pulse_length) # Pulse lengths are consistent
00067             self.assert_(math.fmod(proj.pulse_length, ETHERCAT_INTERVAL) == 0, "Pulse length %f"%proj.pulse_length) # Integer multiples of ethercat rate
00068             self.assertEqual(len(proj.pulse_ends), 4) # Right number of pulses 
00069             self.assertEqual(len(proj.pulse_starts), 4) # Right number of pulses
00070             self.assert_(issorted(interlace(proj.pulse_starts, proj.pulse_ends))) # Starts and stops are in right order
00071             self.assertEqual(4. / proj.repeat_period, config["projector_rate"]) # Base period and repeat period are consistent.
00072 
00073         self.assertEqual(out_repeat_period, proj.repeat_period) 
00074         self.assertEqual(out_proj_end, proj.proj_end_offset)
00075         self.assertEqual(out_noproj_end, proj.noproj_end_offset)
00076         self.assertAlmostEqual(out_alt_end, proj.alt_end_offset, DIGITS)
00077 
00078         return proj, config
00079 
00080     def testBasicFunctionality(self):
00081         proj,config = self.runCase(30, 0.002, 0,   0.132, 0.003, 0.036, 0.032)
00082 
00083 class TestCamera(unittest.TestCase):
00084   def runCase(self, proj_rate, proj_length, proj_shift, rate, trig_mode, out_period, out_ext_trig, out_imager_period, out_end_offset, level = 1, reset_camera = False):
00085       paramnames = dict((name,name) for name in camera_parameters)
00086       config = {
00087               param_proj_rate : proj_rate,  
00088               "projector_pulse_length" : proj_length,
00089               "projector_pulse_shift" : proj_shift,
00090               "projector_tweak" : 0,
00091               "projector_mode" : Config.CameraSynchronizer_ProjectorOn,
00092               param_rate : rate,
00093               param_trig_mode : trig_mode,
00094               "camera_reset" : reset_camera,
00095               }
00096 
00097       proj = Projector()
00098       proj.process_update(config, lvl_projector)
00099       camera = Camera("test_cam", proj, 1, **paramnames)
00100       camera.process_update(config, level)
00101 
00102       if level & 1 == 0:
00103           self.assert_(math.fmod(camera.period, ETHERCAT_INTERVAL) == 0) # Integer multiples of ethercat rate
00104           self.assertEqual(config[param_rate], 1. / camera.period) # Rate was correctly set in config
00105       
00106       self.assert_(out_period > 0, "Period %f"%out_period) # Non-zero period
00107       self.assertAlmostEqual(camera.period, out_period, DIGITS) # Correct camera period
00108       self.assertEqual(camera.ext_trig, out_ext_trig) # Ext trig when expected
00109       self.assert_(out_imager_period > 0, "Imager period %f"%out_imager_period) # Non-zero imager period
00110       self.assertAlmostEqual(camera.imager_period, out_imager_period, DIGITS) # Correct imager period
00111       self.assertAlmostEqual(camera.end_offset, out_end_offset, DIGITS) # Correct end offset
00112 
00113       return camera, proj, config
00114 
00115   def testBasicAlwaysProj(self): 
00116       self.runCase(60, 0.001, 0,   30, Config.CameraSynchronizer_WithProjector,      0.034,  True,  0.033, 0.002) # Always proj
00117   def testBasicAlternateProj(self):
00118       self.runCase(60, 0.001, 0,   30, Config.CameraSynchronizer_AlternateProjector, 0.034,  True,  0.033, 0.019) # Alternate proj
00119   def testBasicNoProj(self):
00120       self.runCase(60, 0.001, 0,   30, Config.CameraSynchronizer_WithoutProjector,   0.034,  True,  0.033, 0.016) # No proj
00121   def testBasicFreeRun(self):
00122       self.runCase(60, 0.001, 0,   30, Config.CameraSynchronizer_InternalTrigger,   1/30.0, False, 1/30.0,    -1) # Freerun
00123 
00124 if __name__ == '__main__':
00125     import rostest
00126     import rospy
00127     import time
00128 
00129     # Not starting a node, so we can't use rospy's time functions.
00130     rospy.get_time = time.time
00131     
00132     rostest.rosrun(PKG, 'test_projector', TestProjector)
00133     rostest.rosrun(PKG, 'test_camera', TestCamera)
00134     killAsynchronousUpdaters()


pr2_camera_synchronizer
Author(s): Blaise Gassend
autogenerated on Thu Sep 3 2015 11:31:20