trackpoint_controller.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import math
00004 import numpy
00005 import commands
00006 import sys
00007 
00008 import rospy
00009 import roslib
00010 
00011 roslib.load_manifest('jsk_joy')
00012 
00013 from view_controller_msgs.msg import CameraPlacement
00014 from geometry_msgs.msg import PoseStamped
00015 from sensor_msgs.msg import Joy
00016 import tf.transformations
00017 
00018 sys.path.append(commands.getoutput('rospack find jsk_joy') + '/scripts')
00019 from trackpoint_status import TrackpointStatus
00020 from nanokontrol_status import NanoKONTROL2Status
00021 from nanopad_status import NanoPAD2Status
00022 
00023 INTERACTIVE_MARKER_TOPIC = '/goal_marker'
00024 pre_pose = None
00025 
00026 def nanokontrol_joyCB(msg):
00027     global nanokontrol_st
00028     nanokontrol_st = NanoKONTROL2Status(msg)
00029     
00030 def nanopad_joyCB(msg):
00031     global nanopad_st
00032     nanopad_st = NanoPAD2Status(msg)
00033 
00034 def trackpoint_joyCB(msg):
00035     global pre_view, g_seq_num, pre_pose
00036     global nanokontrol_st, nanopad_st
00037     if not pre_pose:
00038         pre_pose = PoseStamped()
00039     status = TrackpointStatus(msg)
00040 
00041     # rospy.loginfo(status)
00042     # move interactive marker
00043     new_pose = PoseStamped()
00044     new_pose.header.frame_id = '/map'
00045     new_pose.header.stamp = rospy.Time(0.0)
00046     # move in local
00047     local_xy_move = numpy.array((0.0,
00048                                  0.0,
00049                                  0.0,
00050                                  1.0))
00051     z_move = 0
00052     yaw_move = 0
00053     if ( nanopad_st and nanopad_st.buttonL1 ):
00054         scale_xy = 1500.0
00055         scale_z = 1500.0
00056         scale_yaw = 1000.0
00057     else:
00058         scale_xy = 500.0
00059         scale_z = 500.0
00060         scale_yaw = 300.0
00061 
00062     if not ( status.left or status.right or status.middle ):
00063         # if ( nanokontrol_st and nanokontrol_st.S8 ):
00064         #     z_move = status.y / scale
00065         # elif ( nanokontrol_st and nanokontrol_st.R8 ):
00066         #     yaw_move =  status.y / scale
00067         if ( nanopad_st and nanopad_st.buttonU1 ):
00068             z_move = status.y / scale_z
00069         elif ( nanopad_st and nanopad_st.buttonU2 ):
00070             yaw_move =  status.y / scale_yaw
00071         else:
00072             local_xy_move = numpy.array((- status.x / scale_xy,
00073                                          - status.y / scale_xy,
00074                                          0.0,
00075                                          1.0))
00076     q = numpy.array((pre_pose.pose.orientation.x,
00077                      pre_pose.pose.orientation.y,
00078                      pre_pose.pose.orientation.z,
00079                      pre_pose.pose.orientation.w))
00080     # xy_move = numpy.dot(tf.transformations.quaternion_matrix(q),
00081     #                     local_xy_move)
00082     xy_move = local_xy_move
00083 
00084     new_pose.pose.position.x = pre_pose.pose.position.x + xy_move[0]
00085     new_pose.pose.position.y = pre_pose.pose.position.y + xy_move[1]
00086     new_pose.pose.position.z = pre_pose.pose.position.z + z_move
00087     (roll, pitch, yaw) = tf.transformations.euler_from_quaternion(q)
00088     yaw = yaw + yaw_move
00089 
00090     new_q = tf.transformations.quaternion_from_euler(roll, pitch, yaw)
00091     new_pose.pose.orientation.x = new_q[0]
00092     new_pose.pose.orientation.y = new_q[1]
00093     new_pose.pose.orientation.z = new_q[2]
00094     new_pose.pose.orientation.w = new_q[3]
00095     g_interactive_pub.publish(new_pose)
00096     pre_pose = new_pose
00097   
00098     # view update
00099     # view = ViewProperty()
00100     # view.focus = numpy.copy(pre_view.focus)
00101     # view.yaw = pre_view.yaw
00102     # view.pitch = pre_view.pitch
00103     # view.distance = pre_view.distance
00104     # if status.R3:
00105     #   view.distance = view.distance - status.left_analog_y * 0.1
00106     #   # calc camera orietation
00107     #   if status.left:
00108     #     view_x = 1.0
00109     #   elif status.right:
00110     #     view_x = -1.0
00111     #   else:
00112     #     view_x = 0.0
00113     #   if status.up:
00114     #     view_y = 1.0
00115     #   elif status.down:
00116     #     view_y = -1.0
00117     #   else:
00118     #     view_y = 0.0
00119     #   focus_diff = numpy.dot(view.cameraOrientation(),
00120     #                          numpy.array((view_x / 10.0 / view.distance,
00121     #                                       view_y / 10.0 / view.distance,
00122     #                                       0)))
00123     #   view.focus = view.focus + focus_diff
00124     #   if status.L2 and status.R2:           #align to marker
00125     #     view.distance = 1.0
00126     #     view.focus = numpy.array((new_pose.pose.position.x,
00127     #                               new_pose.pose.position.y,
00128     #                               new_pose.pose.position.z))
00129     #     #view.yaw = math.pi
00130     #     view.yaw = yaw + math.pi
00131     #     view.pitch = math.pi / 2.0 - 0.01
00132     # else:
00133     #   view.yaw = view.yaw - 0.5 * status.right_analog_x
00134     #   view.pitch = view.pitch + 0.5 * status.right_analog_y
00135     #   if view.pitch > math.pi / 2.0 - 0.01:
00136     #     view.pitch = math.pi / 2.0 - 0.01
00137     #   elif view.pitch < - math.pi / 2.0 + 0.01:
00138     #     view.pitch = - math.pi / 2.0 + 0.01
00139     # g_camera_pub.publish(view.cameraPlacement())
00140     # pre_view = view
00141 
00142 
00143 def main():
00144     global g_camera_pub, g_interactive_pub
00145     global nanokontrol_st, nanopad_st
00146     rospy.sleep(1)
00147     rospy.init_node('trackpoint_controller')
00148     g_camera_pub = rospy.Publisher('/rviz/camera_placement', CameraPlacement)
00149     g_interactive_pub = rospy.Publisher(INTERACTIVE_MARKER_TOPIC + '/move_marker', PoseStamped)
00150     trackpoint_sub = rospy.Subscriber('/trackpoint/joy', Joy, trackpoint_joyCB)
00151     nanokontrol_sub= rospy.Subscriber('/nanokontrol/joy', Joy, nanokontrol_joyCB)
00152     nanopad_sub= rospy.Subscriber('/nanopad/joy', Joy, nanopad_joyCB)
00153     nanokontrol_st = None
00154     nanopad_st = None
00155         
00156     rospy.spin()
00157         
00158 
00159 if __name__ == '__main__':
00160     main()
00161     


jsk_interactive_test
Author(s): Kei Okada
autogenerated on Wed May 1 2019 02:40:41