demo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 #  Copyright (c) 2010, UC Regents
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 University of California 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 roslib; roslib.load_manifest('starmac_kinect_obstacle_avoidance')
00036 import rospy
00037 import starmac_kinect.msg
00038 import flyer_controller.msg
00039 import joy.msg
00040 
00041 class Demo(object):
00042     paused = False
00043     clear_time = None
00044     pause_cmd = flyer_controller.msg.control_mode_cmd('pause')
00045     proceed_cmd = flyer_controller.msg.control_mode_cmd('proceed')
00046     started = False
00047     
00048     def __init__(self):
00049         self.pub_cmd = rospy.Publisher("control_mode_autosequence/cmd", flyer_controller.msg.control_mode_cmd)
00050         self.sub_joy = rospy.Subscriber("teleop_flyer/joy", joy.msg.Joy, self.joy_cb)
00051         
00052     def joy_cb(self, joy_msg):
00053         if (not self.started) and (joy_msg.buttons[9] == 1):
00054             self.started = True
00055             rospy.loginfo("Starting demo..")
00056             self.sub_obstacle = rospy.Subscriber("kinect_estimator/obstacle", starmac_kinect.msg.Obstacle, self.obstacle_cb)
00057             
00058     def obstacle_cb(self, obs_msg):
00059         zpos = obs_msg.location.z
00060         obstacle = (obs_msg.obstacle_found and zpos < 1.0)
00061         rospy.logdebug('Got obstacle message, obstacle_found=%d obstacle z=%f' 
00062                       % (obs_msg.obstacle_found, zpos))
00063         # rotate obstacle location into body frame
00064         # if less than threshold, issue pause command
00065         if obstacle and not self.paused:
00066             rospy.loginfo('Obstacle at %f, Pausing...' % zpos)
00067             self.send_pause()
00068             self.paused = True
00069         # If no obstacles for 3 seconds, issue proceed command
00070         if self.paused:
00071             if self.clear_time is None:
00072                 self.clear_time = rospy.Time.now()
00073             else:
00074                 if (not obstacle) and \
00075                         (rospy.Time.now() - self.clear_time).to_sec() > 3.0:
00076                     rospy.loginfo('Proceed..')
00077                     self.send_proceed()
00078                     self.clear_time = None
00079                     self.paused = False
00080         
00081     def send_pause(self):
00082         rospy.loginfo('Sending PAUSE command')
00083         self.pub_cmd.publish(self.pause_cmd)
00084         
00085     def send_proceed(self):
00086         rospy.loginfo('Sending PROCEED command')
00087         self.pub_cmd.publish(self.proceed_cmd)
00088         
00089     def spin(self):
00090         rospy.spin()
00091             
00092 if __name__ == "__main__":
00093     rospy.init_node('kinect_obstacle_avoidance_demo')
00094     self = Demo()
00095     self.spin()


starmac_kinect_obstacle_avoidance
Author(s): bouffard
autogenerated on Sun Jan 5 2014 11:38:55