00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
00044
00045
00046
00047
00048
00049
00050
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
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
00073
00074
00075
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
00083
00084
00085
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
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
00153
00154
00155
00156