pan_tilt.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00029 
00030 import roslib; roslib.load_manifest('pan_tilt_robotis')
00031 
00032 import time
00033 import sys, optparse
00034 
00035 import numpy as np, math
00036 
00037 import hrl_lib.util as ut
00038 import robotis.robotis_servo as rs
00039 
00040 
00041 class PanTilt():
00042     
00043     ## Assumes that both pan and tilt servos are controlled using the
00044     # same usb2dynamixel adaptor.
00045     # @param dev_name - name of serial device of the servo controller (e.g. '/dev/robot/servo0')
00046     # @param pan_id -  servo id for the pan Robotis servo.
00047     # @param pan_id -  servo id for the tilt Robotis servo.
00048     # @param baudrate - for the servo controller (usb2dynamixel)
00049     # @param pan_speed - max pan speed (radians/sec)
00050     # @param tilt_speed - max tilt speed (radians/sec)
00051     def __init__(self, dev_name, pan_id, tilt_id, baudrate=57600,
00052                  pan_speed = math.radians(180),
00053                  tilt_speed = math.radians(180)):
00054         self.pan_servo = rs.robotis_servo(dev_name,pan_id,baudrate,
00055                                           max_speed = pan_speed)
00056         self.tilt_servo = rs.robotis_servo(dev_name,tilt_id,baudrate,
00057                                            max_speed = tilt_speed)
00058 
00059         self.max_pan = self.pan_servo.max_ang
00060         self.min_pan = self.pan_servo.min_ang
00061 
00062         self.max_tilt = self.tilt_servo.max_ang
00063         self.min_tilt = self.tilt_servo.min_ang
00064 
00065 
00066     ## return (pan,tilt) angles in RADIANS.
00067     def get_pan_tilt(self):
00068         pan = self.pan_servo.read_angle()
00069         tilt = self.tilt_servo.read_angle()
00070         return pan, -tilt
00071 
00072     ## set (pan,tilt) angles in RADIANS.
00073     # blocks until the pan and tilt angles are attained.
00074     # @param pan - pan angle (RADIANS)
00075     # @param tilt - tilt angle (RADIANS)
00076     def set_pan_tilt(self, pan, tilt, speed=math.radians(180)):
00077         self.pan_servo.move_angle(pan, angvel=speed, blocking=False)
00078         self.tilt_servo.move_angle(tilt, angvel=speed, blocking=True)
00079         self.pan_servo.move_angle(pan, angvel=speed, blocking=True)
00080 
00081 
00082     ## new pan,tilt = current pan,tilt + pan_d,tilt_d
00083     # blocks until the pan and tilt angles are attained.
00084     # @param pan - pan angle (RADIANS)
00085     # @param tilt - tilt angle (RADIANS)
00086     def set_pan_tilt_delta(self,pan_d,tilt_d):
00087         p,t = self.get_pan_tilt()
00088         self.set_pan_tilt(p+pan_d,t+tilt_d)
00089 
00090     def set_ptz_angles_rad(self, pan, tilt):
00091         print 'pan_tilt.set_ptz_angles_rad: WARNING this function has been deprecated. use set_pan_tilt'
00092         self.set_pan_tilt(pan, tilt)
00093 
00094     def set_ptz_values(self, pan, tilt, blocking=True):
00095         print 'pan_tilt.set_ptz_values: WARNING this function has been deprecated. use set_pan_tilt'
00096         self.set_pan_tilt(pan, tilt)
00097 
00098     def get_ptz_angles_rad(self):
00099         print 'pan_tilt.get_ptz_angles_rad: WARNING this function has been deprecated. use set_pan_tilt'
00100         return self.get_pan_tilt()
00101 
00102     def get_ptz_values(self):
00103         print 'pan_tilt.get_ptz_values: WARNING this function has been deprecated. use set_pan_tilt'
00104         p, t = self.get_pan_tilt()
00105         #return p, t
00106         return math.degrees(p), math.degrees(t)
00107 
00108 
00109 if __name__ == '__main__':
00110 
00111     p = optparse.OptionParser()
00112     p.add_option('-d', action='store', type='string', dest='servo_dev_name',
00113                  default='/dev/robot/pan_tilt0', help='servo device string. [default= /dev/robot/pan_tilt0]')
00114     p.add_option('--pan_id', action='store', type='int', dest='pan_id',
00115                  help='id of the pan servo',default=None)
00116     p.add_option('--tilt_id', action='store', type='int', dest='tilt_id',
00117                  help='id of the tilt servo',default=None)
00118     p.add_option('--pan', action='store', type='float', dest='pan',
00119                  help='pan angle (degrees).',default=None)
00120     p.add_option('--tilt', action='store', type='float', dest='tilt',
00121                  help='tilt angle (degrees).',default=None)
00122 
00123     opt, args = p.parse_args()
00124 
00125     servo_dev_name = opt.servo_dev_name
00126     pan_id = opt.pan_id
00127     tilt_id = opt.tilt_id
00128 
00129     pan = opt.pan
00130     tilt = opt.tilt
00131 
00132     if pan_id == None:
00133         print 'Please provide a pan_id'
00134         print 'Exiting...'
00135         sys.exit()
00136     if tilt_id == None:
00137         print 'Please provide a tilt_id'
00138         print 'Exiting...'
00139         sys.exit()
00140     if pan == None:
00141         print 'Please provide a pan (angle)'
00142         print 'Exiting...'
00143         sys.exit()
00144     if tilt == None:
00145         print 'Please provide a tilt (angle)'
00146         print 'Exiting...'
00147         sys.exit()
00148 
00149     ptu = PanTilt(servo_dev_name,pan_id,tilt_id)
00150     ptu.set_pan_tilt(math.radians(pan),math.radians(tilt))
00151 
00152 # For EL-E:
00153 # python pan_tilt.py -d /dev/robot/servos_pan_tilt_hat  --pan_id=6 --tilt_id=18 --pan=0 --tilt=0
00154 
00155 
00156 


pan_tilt_robotis
Author(s): Advait Jain, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 12:05:39