Go to the documentation of this file.00001 from jsk_teleop_joy.joy_plugin import JSKJoyPlugin
00002
00003 import imp
00004 try:
00005 imp.find_module("geometry_msgs")
00006 except:
00007 import roslib; roslib.load_manifest('jsk_teleop_joy')
00008
00009
00010 from geometry_msgs.msg import Twist
00011 import tf
00012 import rospy
00013 import numpy
00014 import math
00015 import tf
00016 import numpy
00017 import time
00018
00019 class JoyCmdVel(JSKJoyPlugin):
00020
00021 def __init__(self, name, args):
00022 JSKJoyPlugin.__init__(self, name, args)
00023 self.cmd_vel = Twist()
00024 self.publish_cmd_vel = self.getArg('publish_cmd_vel', True)
00025 self.max_vel = self.getArg('max_vel', 0.2)
00026 self.max_omega = self.getArg('max_omega', 0.17)
00027 self.orthogonal_axis_mode = self.getArg('orthogonal_axis_mode', True)
00028 self.prev_time = rospy.Time.now()
00029 if self.publish_cmd_vel:
00030 self.twist_pub = rospy.Publisher(self.getArg('cmd_vel', 'cmd_vel'), Twist, queue_size = 1)
00031
00032 def joyCB(self, status, history):
00033 if history.length() > 0:
00034 latest = history.latest()
00035 if status.R3 and status.L2 and status.R2 and not (latest.R3 and latest.L2 and latest.R2):
00036 self.followView(not self.followView())
00037 cmd_vel = Twist()
00038
00039 if not status.R3:
00040
00041 dist = numpy.sqrt(status.left_analog_y * status.left_analog_y + status.left_analog_x * status.left_analog_x)
00042 scale_v = self.max_vel * dist
00043 if self.orthogonal_axis_mode:
00044 if abs(status.left_analog_y) - abs(status.left_analog_x) > 0.2:
00045 x_diff = status.left_analog_y * scale_v
00046 y_diff = 0.0
00047 elif abs(status.left_analog_y) - abs(status.left_analog_x) < -0.2:
00048 x_diff = 0.0
00049 y_diff = status.left_analog_x * scale_v
00050 else:
00051 x_diff = 0.0
00052 y_diff = 0.0
00053 else:
00054 x_diff = status.left_analog_y * scale_v
00055 y_diff = status.left_analog_x * scale_v
00056 else:
00057 x_diff = 0.0
00058 y_diff = 0.0
00059
00060 cmd_vel.linear.x = x_diff
00061 cmd_vel.linear.y = y_diff
00062 cmd_vel.linear.z = 0.0
00063
00064 dyaw = 0.0
00065 if not status.R3:
00066 if status.L1:
00067 dyaw = self.max_omega
00068 elif status.R1:
00069 dyaw = -self.max_omega
00070
00071 cmd_vel.angular.x = 0.0
00072 cmd_vel.angular.y = 0.0
00073 cmd_vel.angular.z = dyaw
00074
00075
00076 if self.publish_cmd_vel:
00077 now = rospy.Time.from_sec(time.time())
00078
00079 if (now - self.prev_time).to_sec() > 1 / 30.0:
00080 self.twist_pub.publish(cmd_vel)
00081 self.prev_time = now