Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 import os, sys, string, time, getopt, threading
00008 import httplib, urllib
00009
00010 import roslib; roslib.load_manifest('axis_camera')
00011 import rospy
00012
00013 from axis_camera.msg import Axis
00014 from geometry_msgs.msg import Twist
00015 import math
00016
00017
00018 class StateThread(threading.Thread):
00019 def __init__(self, axis):
00020 threading.Thread.__init__(self)
00021
00022 self.axis = axis
00023 self.daemon = True
00024
00025 def run(self):
00026 r = rospy.Rate(10)
00027 msg = Axis()
00028
00029 while True:
00030 if not self.axis.timeout and ((rospy.Time.now() - self.axis.last_request).to_sec() > 1.0):
00031 self.axis.timeout = True
00032 self.axis.cmd(Twist(),reset_timeout=False)
00033 conn = httplib.HTTPConnection(self.axis.hostname)
00034 params = { 'query':'position' }
00035 conn.request("GET", "/axis-cgi/com/ptz.cgi?%s" % urllib.urlencode(params))
00036 response = conn.getresponse()
00037 if response.status == 200:
00038 body = response.read()
00039 params = dict([s.split('=',2) for s in body.splitlines()])
00040 msg.pan = float(params['pan'])
00041
00042 if self.axis.flip:
00043 msg.pan = 180 - msg.pan
00044 if msg.pan > 180:
00045 msg.pan -= 360
00046 if msg.pan < -180:
00047 msg.pan += 360
00048 msg.tilt = float(params['tilt'])
00049 msg.zoom = float(params['zoom'])
00050 msg.iris = float(params['iris'])
00051 msg.focus = 0.0
00052 if 'focus' in params:
00053 msg.focus = float(params['focus'])
00054 msg.autofocus = (params['autofocus'] == 'on')
00055 self.axis.pub.publish(msg)
00056 r.sleep()
00057
00058
00059 class AxisPTZ:
00060 def __init__(self, hostname, username, password, flip):
00061 self.hostname = hostname
00062 self.username = username
00063 self.password = password
00064 self.flip = flip
00065
00066 self.st = None
00067 self.timeout = True
00068 self.pub = rospy.Publisher("state", Axis, self)
00069 self.sub = rospy.Subscriber("twist", Twist, self.cmd)
00070 self.cmd(Twist(),reset_timeout=False)
00071
00072 def peer_subscribe(self, topic_name, topic_publish, peer_publish):
00073
00074 if self.st is None:
00075 self.st = StateThread(self)
00076 self.st.start()
00077
00078 def cmd(self, msg, reset_timeout=True):
00079 if reset_timeout:
00080 self.timeout = False
00081 self.last_request = rospy.Time.now()
00082 conn = httplib.HTTPConnection(self.hostname)
00083 pan = int(msg.angular.z * 180./math.pi)
00084 tilt = int(msg.angular.y * 180./math.pi)
00085 if pan>100:
00086 pan=100
00087 if pan<-100:
00088 pan=-100
00089 if tilt>100:
00090 tilt=100
00091 if tilt<-100:
00092 tilt=-100
00093
00094 if self.flip:
00095 pan = -pan
00096 tilt = -tilt
00097 conn.request("GET", "/axis-cgi/com/ptz.cgi?continuouspantiltmove=%d,%d" % (pan,tilt))
00098
00099
00100 def main():
00101 rospy.init_node("axis_twist")
00102
00103 arg_defaults = {
00104 'hostname': '192.168.0.90',
00105 'username': '',
00106 'password': '',
00107 'flip': True,
00108 }
00109 args = {}
00110 for name in arg_defaults:
00111 args[name] = rospy.get_param(rospy.search_param(name), arg_defaults[name])
00112
00113 AxisPTZ(**args)
00114 rospy.spin()
00115
00116
00117 if __name__ == "__main__":
00118 main()