Go to the documentation of this file.00001 from joy_rviz_view_controller import RVizViewController
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 PoseStamped
00011 import tf
00012 import rospy
00013 import numpy
00014 import math
00015 import tf
00016 import numpy
00017 import time
00018
00019 def signedSquare(val):
00020 if val > 0:
00021 sign = 1
00022 else:
00023 sign = -1
00024 return val * val * sign
00025
00026 class JoyPose6D(RVizViewController):
00027
00028 def __init__(self, name, args):
00029 RVizViewController.__init__(self, name, args)
00030 self.pre_pose = PoseStamped()
00031 self.pre_pose.pose.orientation.w = 1
00032 self.prev_time = rospy.Time.from_sec(time.time())
00033 self.publish_pose = self.getArg('publish_pose', True)
00034 self.frame_id = self.getArg('frame_id', '/map')
00035 if self.publish_pose:
00036 self.pose_pub = rospy.Publisher(self.getArg('pose', 'pose'),
00037 PoseStamped)
00038 self.supportFollowView(True)
00039
00040 self.puse_sub = rospy.Subscriber(self.getArg('set_pose', 'set_pose'), PoseStamped, self.setPoseCB)
00041 self.frame_id = rospy.get_param('~frame_id', '/map')
00042 self.tf_listener = tf.TransformListener()
00043
00044 def setPoseCB(self, pose):
00045 pose.header.stamp = rospy.Time(0)
00046 self.pre_pose = self.tf_listener.transformPose(self.frame_id, pose)
00047
00048 if self.publish_pose:
00049 self.pose_pub.publish(self.pre_pose)
00050
00051 def joyCB(self, status, history):
00052 pre_pose = self.pre_pose
00053 if history.length() > 0:
00054 latest = history.latest()
00055 if status.R3 and status.L2 and status.R2 and not (latest.R3 and latest.L2 and latest.R2):
00056 self.followView(not self.followView())
00057 RVizViewController.joyCB(self, status, history)
00058 new_pose = PoseStamped()
00059 new_pose.header.frame_id = self.frame_id
00060 new_pose.header.stamp = rospy.Time(0.0)
00061
00062 if not status.R3:
00063
00064 if status.square:
00065 scale = 10.0
00066 else:
00067 dist = status.left_analog_y * status.left_analog_y + status.left_analog_x * status.left_analog_x
00068 if dist > 0.9:
00069 scale = 20.0
00070 else:
00071 scale = 60.0
00072 x_diff = signedSquare(status.left_analog_y) / scale
00073 y_diff = signedSquare(status.left_analog_x) / scale
00074
00075 if status.L2:
00076 z_diff = 0.005
00077 elif status.R2:
00078 z_diff = -0.005
00079 else:
00080 z_diff = 0.0
00081 if status.square:
00082 z_scale = 10.0
00083 elif history.all(lambda s: s.L2) or history.all(lambda s: s.R2):
00084 z_scale = 4.0
00085 else:
00086 z_scale = 2.0
00087 local_move = numpy.array((x_diff, y_diff,
00088 z_diff * z_scale,
00089 1.0))
00090 else:
00091 local_move = numpy.array((0.0, 0.0, 0.0, 1.0))
00092 q = numpy.array((pre_pose.pose.orientation.x,
00093 pre_pose.pose.orientation.y,
00094 pre_pose.pose.orientation.z,
00095 pre_pose.pose.orientation.w))
00096 xyz_move = numpy.dot(tf.transformations.quaternion_matrix(q),
00097 local_move)
00098 new_pose.pose.position.x = pre_pose.pose.position.x + xyz_move[0]
00099 new_pose.pose.position.y = pre_pose.pose.position.y + xyz_move[1]
00100 new_pose.pose.position.z = pre_pose.pose.position.z + xyz_move[2]
00101 roll = 0.0
00102 pitch = 0.0
00103 yaw = 0.0
00104 DTHETA = 0.02
00105 if not status.R3:
00106 if status.L1:
00107 if status.square:
00108 yaw = yaw + DTHETA * 5
00109 elif history.all(lambda s: s.L1):
00110 yaw = yaw + DTHETA * 2
00111 else:
00112 yaw = yaw + DTHETA
00113 elif status.R1:
00114 if status.square:
00115 yaw = yaw - DTHETA * 5
00116 elif history.all(lambda s: s.R1):
00117 yaw = yaw - DTHETA * 2
00118 else:
00119 yaw = yaw - DTHETA
00120 if status.up:
00121 if status.square:
00122 pitch = pitch + DTHETA * 5
00123 elif history.all(lambda s: s.up):
00124 pitch = pitch + DTHETA * 2
00125 else:
00126 pitch = pitch + DTHETA
00127 elif status.down:
00128 if status.square:
00129 pitch = pitch - DTHETA * 5
00130 elif history.all(lambda s: s.down):
00131 pitch = pitch - DTHETA * 2
00132 else:
00133 pitch = pitch - DTHETA
00134 if status.right:
00135 if status.square:
00136 roll = roll + DTHETA * 5
00137 elif history.all(lambda s: s.right):
00138 roll = roll + DTHETA * 2
00139 else:
00140 roll = roll + DTHETA
00141 elif status.left:
00142 if status.square:
00143 roll = roll - DTHETA * 5
00144 elif history.all(lambda s: s.left):
00145 roll = roll - DTHETA * 2
00146 else:
00147 roll = roll - DTHETA
00148 diff_q = tf.transformations.quaternion_from_euler(roll, pitch, yaw)
00149 new_q = tf.transformations.quaternion_multiply(q, diff_q)
00150 new_pose.pose.orientation.x = new_q[0]
00151 new_pose.pose.orientation.y = new_q[1]
00152 new_pose.pose.orientation.z = new_q[2]
00153 new_pose.pose.orientation.w = new_q[3]
00154
00155
00156 if self.publish_pose:
00157 now = rospy.Time.from_sec(time.time())
00158
00159 if (now - self.prev_time).to_sec() > 1 / 30.0:
00160 self.pose_pub.publish(new_pose)
00161 self.prev_time = now
00162
00163 self.pre_pose = new_pose