source.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 # Copyright (c) 2016, Tal Regev.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Willow Garage nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 import sys
00036 import time
00037 import math
00038 import rospy
00039 import numpy
00040 import cv2
00041 
00042 import sensor_msgs.msg
00043 from cv_bridge import CvBridge
00044 
00045 
00046 # Send black pic with a circle as regular and compressed ros msgs.
00047 class Source:
00048 
00049     def __init__(self):
00050         self.pub            = rospy.Publisher("/opencv_tests/images", sensor_msgs.msg.Image)
00051         self.pub_compressed = rospy.Publisher("/opencv_tests/images/compressed", sensor_msgs.msg.CompressedImage)
00052 
00053     def spin(self):
00054         time.sleep(1.0)
00055         started = time.time()
00056         counter = 0
00057         cvim = numpy.zeros((480, 640, 1), numpy.uint8)
00058         ball_xv = 10
00059         ball_yv = 10
00060         ball_x = 100
00061         ball_y = 100
00062 
00063         cvb = CvBridge()
00064 
00065         while not rospy.core.is_shutdown():
00066 
00067             cvim.fill(0)
00068             cv2.circle(cvim, (ball_x, ball_y), 10, 255, -1)
00069 
00070             ball_x += ball_xv
00071             ball_y += ball_yv
00072             if ball_x in [10, 630]:
00073                 ball_xv = -ball_xv
00074             if ball_y in [10, 470]:
00075                 ball_yv = -ball_yv
00076 
00077             self.pub.publish(cvb.cv2_to_imgmsg(cvim))
00078             self.pub_compressed.publish(cvb.cv2_to_compressed_imgmsg(cvim))
00079             time.sleep(0.03)
00080 
00081 
00082 def main(args):
00083     s = Source()
00084     rospy.init_node('Source')
00085     try:
00086         s.spin()
00087         rospy.spin()
00088         outcome = 'test completed'
00089     except KeyboardInterrupt:
00090         print "shutting down"
00091         outcome = 'keyboard interrupt'
00092     rospy.core.signal_shutdown(outcome)
00093 
00094 if __name__ == '__main__':
00095     main(sys.argv)


opencv_tests
Author(s): James Bowman
autogenerated on Thu Jun 6 2019 21:23:40