rviz_view_controller_singleton.py
Go to the documentation of this file.
00001 from jsk_teleop_joy.camera_view import CameraView
00002 
00003 import imp
00004 try:
00005   imp.find_module("view_controller_msgs")
00006 except:
00007   import roslib; roslib.load_manifest('jsk_teleop_joy')
00008 
00009 from view_controller_msgs.msg import CameraPlacement
00010 
00011 import tf
00012 import rospy
00013 
00014 import numpy
00015 import math
00016 
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 
00027 class RVizViewControllerManager():
00028   '''
00029 Description:
00030   This class is used as a singleton instance named RVizViewControllerManagerSingleton.
00031   Plugin class using this class is RVizViewController.
00032   Use TabletViewController(jsk_rviz_plugin) in rviz (selected at 'Views' Panel)
00033   '''
00034   def __init__(self):
00035     self.camera_pub = rospy.Publisher('/rviz/camera_placement', CameraPlacement, queue_size = 1)
00036     self.camera_sub = rospy.Subscriber('/rviz/current_camera_placement', CameraPlacement,
00037                                        self.cameraCB, queue_size = 1)
00038     self.pre_view = CameraView()
00039     self.follow_view = False
00040     self.counter = 0
00041     self.prev_time = rospy.Time.from_sec(time.time())
00042   def cameraCB(self, msg):
00043     self.pre_view = CameraView.createFromCameraPlacement(msg)
00044   def joyCB(self, status, history, pre_pose):
00045     self.counter = self.counter + 1
00046     if self.counter > 1024:
00047       self.counter = 0
00048     pre_view = self.pre_view
00049     view = CameraView()
00050     view.focus = numpy.copy(pre_view.focus)
00051     view.yaw = pre_view.yaw
00052     view.pitch = pre_view.pitch
00053     view.distance = pre_view.distance
00054     view_updated = False
00055     if status.R3:
00056       if status.L3:
00057         ## reset camera pose
00058         v = CameraView()
00059         self.publishView(v)
00060         return
00061       if status.L1:
00062         ## focus to pose
00063         view.focus = numpy.array((pre_pose.pose.position.x,
00064                                   pre_pose.pose.position.y,
00065                                   pre_pose.pose.position.z))
00066         self.publishView(view)
00067         return
00068       ## calc distance
00069       if not status.left_analog_y == 0.0:
00070         view.distance = view.distance - signedSquare(status.left_analog_y) * 0.05
00071         view_updated = True
00072       # calc camera orietation(x)
00073       if status.left:
00074         view_updated = True
00075         view_x = 1.0
00076       elif status.right:
00077         view_updated = True
00078         view_x = -1.0
00079       else:
00080         view_x = 0.0
00081       # calc camera orietation(y)
00082       if status.up:
00083         view_updated = True
00084         view_y = 1.0
00085       elif status.down:
00086         view_updated = True
00087         view_y = -1.0
00088       else:
00089         view_y = 0.0
00090 
00091       if not status.left_analog_x == 0.0:
00092         view_updated = True
00093 
00094       focus_diff = numpy.dot(view.cameraOrientation(),
00095                              numpy.array((view_x / 7.0 / view.distance,
00096                                           view_y / 7.0 / view.distance,
00097                                           - status.left_analog_x / 7.0 / view.distance)))
00098       view.focus = view.focus + focus_diff
00099     else:
00100       if status.right_analog_x != 0.0:
00101         view_updated = True
00102       if status.right_analog_y != 0.0:
00103         view_updated = True
00104 
00105       view.yaw = view.yaw - 0.05 * signedSquare(status.right_analog_x)
00106       view.pitch = view.pitch + 0.05 * signedSquare(status.right_analog_y)
00107       ## invert z up
00108       if (int(math.floor((2 * math.fabs(view.pitch))/math.pi)) + 1) % 4 > 1:
00109         view.z_up[2] = -1
00110 
00111     ## follow view mode
00112     if self.follow_view and self.support_follow_view:
00113       view_updated = True
00114       view.focus = numpy.array((pre_pose.pose.position.x,
00115                                 pre_pose.pose.position.y,
00116                                 pre_pose.pose.position.z))
00117       #view.yaw = math.pi
00118       q = numpy.array((pre_pose.pose.orientation.x,
00119                        pre_pose.pose.orientation.y,
00120                        pre_pose.pose.orientation.z,
00121                        pre_pose.pose.orientation.w))
00122       mat = tf.transformations.quaternion_matrix(q)
00123       camera_local_pos = numpy.dot(mat, numpy.array((0, 0, 1, 1)))[:3]
00124       pitch = math.asin(camera_local_pos[2])
00125       # calc pitch quadrant
00126       if camera_local_pos[1] < 0:
00127         pitch = math.pi - pitch
00128       if math.fabs(math.cos(pitch)) < 0.01:
00129         yaw = 0.0
00130       else:
00131         cos_value = camera_local_pos[0] / math.cos(pitch)
00132         if cos_value > 1.0:
00133           cos_value = 1.0
00134         elif cos_value < -1.0:
00135           cos_value = -1.0
00136         yaw = math.acos(cos_value)
00137       view.pitch = pitch
00138       view.yaw = yaw
00139       z_up = numpy.dot(mat, numpy.array((1, 0, 0, 1)))
00140       view.z_up = z_up[:3]
00141     if view_updated:
00142       self.publishView(view)
00143 
00144   def publishView(self, view):
00145     now = rospy.Time.from_sec(time.time())
00146     placement = view.cameraPlacement()
00147     placement.time_from_start = now - self.prev_time
00148     if (now - self.prev_time).to_sec() > 1 / 10.0:
00149       self.camera_pub.publish(placement)
00150       self.prev_time = now
00151     self.pre_view = view
00152 
00153 RVizViewControllerManagerSingleton = RVizViewControllerManager()


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Wed Jul 19 2017 02:54:50