ptu_action_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import roslib; roslib.load_manifest('ptu46')
3 import rospy
4 import ptu_control.msg
5 import actionlib
6 from sensor_msgs.msg import JointState
7 import threading
8 import numpy as np
9 
10 class PTUControl(object):
11  pan = 0
12  tilt = 0
13  pan_vel = 0
14  tilt_vel = 0
15  state_lock = threading.Lock()
16 
17  def __init__(self):
18  # setup some parameters
19  self.tsmin = rospy.get_param('/ptu/min_tilt_speed', 4.0)
20  self.tsmax = rospy.get_param('/ptu/max_tilt_speed', 140.0)
21  self.psmin = rospy.get_param('/ptu/min_pan_speed' , 4.0)
22  self.psmax = rospy.get_param('/ptu/max_pan_speed' , 140.0)
23  self.pstep = rospy.get_param('/ptu/pan_step', 0.00089759763795882463)
24  self.tstep = rospy.get_param('/ptu/tilt_step', 0.00089759763795882463)
25 
26  # setup the subscribers and publishers
27  rospy.Subscriber('state', JointState, self.cb_ptu_state)
28  self.ptu_pub = rospy.Publisher('cmd', JointState)
29  self.as_goto = actionlib.SimpleActionServer('SetPTUState', \
30  ptu_control.msg.PtuGotoAction, execute_cb=self.cb_goto)
31  self.as_reset = actionlib.SimpleActionServer('ResetPtu', \
32  ptu_control.msg.PtuResetAction, execute_cb=self.cb_reset)
33 
34  def cb_goto(self, msg):
35  pan, tilt, pan_vel, tilt_vel = np.radians((msg.pan, msg.tilt, msg.pan_vel, msg.tilt_vel))
36 
37  if not pan_vel:
38  pan_vel = self.psmax
39  if not tilt_vel:
40  tilt_vel = self.tsmax
41 
42  self._goto(pan, tilt, pan_vel, tilt_vel)
43 
44  result = ptu_control.msg.PtuGotoResult()
45  result.state.position = self._get_state()
46  self.as_goto.set_succeeded(result)
47 
48  def cb_reset(self, msg):
49  self._goto(0,0, self.psmax, self.tsmax)
50  result = ptu_control.msg.PtuResetResult()
51  self.as_reset.set_succeeded(result)
52 
53  def _goto(self, pan, tilt, pan_vel, tilt_vel):
54  rospy.loginfo('going to (%s, %s)' % (pan, tilt))
55  msg_out = JointState()
56  msg_out.header.stamp = rospy.Time.now()
57  msg_out.name = ['head_pan_joint', 'head_tilt_joint']
58  msg_out.position = [pan, tilt]
59  msg_out.velocity = [pan_vel, tilt_vel]
60  self.ptu_pub.publish(msg_out)
61  # wait for it to get there
62  wait_rate = rospy.Rate(10)
63  while not self._at_goal((pan, tilt)) and not rospy.is_shutdown():
64  wait_rate.sleep()
65 
66  def _at_goal(self, goal):
67  return all(np.abs(np.array(goal) - (self.pan, self.tilt)) <= np.degrees((self.pstep, self.tstep)))
68 
69  def cb_ptu_state(self, msg):
70  self.state_lock.acquire()
71  self.pan, self.tilt = msg.position
72  self.state_lock.release()
73 
74  def _get_state(self):
75  self.state_lock.acquire()
76  pt = np.degrees((self.pan, self.tilt))
77  self.state_lock.release()
78  return pt
79 
80 if __name__ == '__main__':
81  rospy.init_node('ptu46_action_server')
82  PTUControl()
83  rospy.spin()


ptu46
Author(s): Erik Karulf, David Lu
autogenerated on Mon Mar 2 2015 01:32:49