Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 import roslib;
00009 import rospy
00010 from sensor_msgs.msg import Joy
00011 from geometry_msgs.msg import Twist
00012 from lizi.msg import lizi_pan_tilt
00013
00014 def joy_callback(data):
00015 global msg
00016 global msg_pt
00017 global max_vel
00018 global max_rot
00019 global msg_pt
00020 global max_pan
00021 global max_tilt
00022 global pan_vel
00023 global tilt_vel
00024 global pan_dir
00025 global tilt_dir
00026 global scroll_btn
00027 global current_robot
00028 global num_robots
00029 global joy_for
00030 global joy_rot
00031 global joy_pan
00032 global joy_tilt
00033 global joy_scroll_btn
00034 global joy_slow_btn
00035 global slow
00036 global slow_btn
00037 global joy_center_btn
00038 global center_btn
00039 global center
00040 msg.linear.x=data.axes[joy_for]*max_vel*slow
00041 msg.angular.z=data.axes[joy_rot]*max_rot*slow
00042 if (data.axes[joy_pan]>0.5 and msg_pt.pan_angle<max_pan):
00043 pan_dir=1
00044 elif (data.axes[joy_pan]<-0.5 and msg_pt.pan_angle>-max_pan):
00045 pan_dir=-1
00046 else:
00047 pan_dir=0
00048 if (data.axes[joy_tilt]>0.5 and msg_pt.tilt_angle<max_tilt):
00049 tilt_dir=1
00050 elif (data.axes[joy_tilt]<-0.5 and msg_pt.tilt_angle>-max_tilt):
00051 tilt_dir=-1
00052 else:
00053 tilt_dir=0
00054 if (data.buttons[joy_scroll_btn]==1):
00055 if (scroll_btn==0):
00056 current_robot=current_robot+1
00057 scroll_btn=1
00058 if (current_robot>num_robots):
00059 current_robot=1
00060 rospy.loginfo("Controlling Lizi %d",current_robot)
00061 if (data.buttons[joy_scroll_btn]==0):
00062 scroll_btn=0
00063 if (data.buttons[joy_slow_btn]==1):
00064 if (slow_btn==0):
00065 if (slow==0.5):
00066 slow=1.0
00067 else:
00068 slow=0.5
00069 slow_btn=1
00070 rospy.loginfo("Max velocity is %f m/s",max_vel*slow)
00071 if (data.buttons[joy_slow_btn]==0):
00072 slow_btn=0
00073 if (data.buttons[joy_center_btn]==1):
00074 if (center_btn==0):
00075 msg_pt.pan_angle=0
00076 msg_pt.tilt_angle=0
00077 center=0
00078 center_btn=1
00079 rospy.loginfo("Centering pan tilt system")
00080 if (data.buttons[joy_center_btn]==0):
00081 center_btn=0
00082
00083 def joy_cam():
00084 global pub
00085 global pub_pt
00086 global msg
00087 global msg_pt
00088 global max_pan
00089 global max_tilt
00090 global pan_vel
00091 global tilt_vel
00092 global pan_dir
00093 global tilt_dir
00094 global max_vel
00095 global max_rot
00096 global num_robots
00097 global scroll_btn
00098 global current_robot
00099 global joy_for
00100 global joy_rot
00101 global joy_pan
00102 global joy_tilt
00103 global joy_scroll_btn
00104 global joy_slow_btn
00105 global slow
00106 global slow_btn
00107 global joy_center_btn
00108 global center_btn
00109 global center
00110 joy_for = rospy.get_param("joy_for")
00111 joy_rot = rospy.get_param("joy_rot")
00112 joy_pan = rospy.get_param("joy_pan")
00113 joy_tilt = rospy.get_param("joy_tilt")
00114 joy_scroll_btn = rospy.get_param("joy_scroll_btn")
00115 joy_slow_btn = rospy.get_param("joy_slow_btn")
00116 joy_center_btn = rospy.get_param("joy_center_btn")
00117 max_vel = rospy.get_param("max_vel")
00118 max_rot = rospy.get_param("max_rot")
00119 max_pan = rospy.get_param("max_pan")
00120 max_tilt = rospy.get_param("max_tilt")
00121 num_robots = rospy.get_param("lizi_n")
00122 center=10
00123 slow=1
00124 slow_btn=0
00125 center_btn=0
00126 pan_dir=0
00127 tilt_dir=0
00128 pan_vel=0.02
00129 tilt_vel=0.02
00130 pub=range(num_robots)
00131 pub_pt=range(num_robots)
00132 scroll_btn=0
00133 current_robot=1
00134 msg = Twist()
00135 msg.linear.x=0
00136 msg.linear.y=0
00137 msg.linear.z=0
00138 msg.angular.x=0
00139 msg.angular.y=0
00140 msg.angular.z=0
00141 msg_pt = lizi_pan_tilt()
00142 msg_pt.pan_angle=0
00143 msg_pt.tilt_angle=0
00144 rospy.init_node('joy_cam', anonymous=True)
00145 rospy.Subscriber('joy', Joy, joy_callback)
00146 robot_name_global="lizi_"
00147 rospy.loginfo("Waiting for robots to load")
00148 for i in range(1, num_robots+1):
00149 robot_name=robot_name_global+str(i)+"/cmd_vel"
00150
00151 pub[i-1] = rospy.Publisher(robot_name, Twist)
00152 pub[i-1].publish(msg)
00153 rospy.sleep(0.1)
00154 robot_name_pt=robot_name_global+str(i)+"/pan_tilt"
00155 pub_pt[i-1] = rospy.Publisher(robot_name_pt, lizi_pan_tilt)
00156 pub_pt[i-1].publish(msg_pt)
00157 rospy.sleep(0.1)
00158 rospy.sleep(15)
00159 rospy.loginfo("Ready to control, Controlling Lizi %d",current_robot)
00160 rospy.loginfo("Max velocity is %f m/s",max_vel*slow)
00161 while not rospy.is_shutdown():
00162 pub[current_robot-1].publish(msg)
00163 msg_pt.pan_angle=msg_pt.pan_angle+pan_dir*pan_vel
00164 msg_pt.tilt_angle=msg_pt.tilt_angle+tilt_dir*tilt_vel
00165 if (msg_pt.pan_angle>max_pan):
00166 msg_pt.pan_angle=max_pan
00167 if (msg_pt.pan_angle<-max_pan):
00168 msg_pt.pan_angle=-max_pan
00169 if (msg_pt.tilt_angle>max_tilt):
00170 msg_pt.tilt_angle=max_tilt
00171 if (msg_pt.tilt_angle<-max_tilt):
00172 msg_pt.tilt_angle=-max_tilt
00173 if (pan_dir<>0 or tilt_dir<>0 or center<10):
00174 pub_pt[current_robot-1].publish(msg_pt)
00175 center=center+1
00176
00177 rospy.sleep(0.15)
00178
00179 if __name__ == '__main__':
00180 rospy.loginfo("Starting multi_lizi_control.py node")
00181 joy_cam()