twist_to_jog_frame.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Take spacemouse twist. Republish them as JogFrame
4 
5 import rospy
6 from jog_msgs.msg import JogFrame
7 from geometry_msgs.msg import Twist
8 import numpy as np
9 
11 
12  def __init__(self):
13  self.scale_linear = rospy.get_param('~scale_linear', {'x': 0.05, 'y': 0.05, 'z': 0.05})
14  self.scale_angular = rospy.get_param('~scales_angular', {'x': 0.05, 'y': 0.05, 'z': 0.05})
15  self.rotation_matrix = np.matrix(rospy.get_param('~rotation_matrix', [[1,0,0],[0,1,0],[0,0,1]]))
16  self.pub = rospy.Publisher('jog_frame', JogFrame, queue_size=1)
17 
18  # Analyze a Twist_msg and return only the dominant axis
19  def dominantAxisMode(self, twist_msg):
20  axes = {twist_msg.linear.x, twist_msg.linear.y, twist_msg.linear.z, twist_msg.angular.x, twist_msg.angular.y, twist_msg.angular.z}
21  axes = self.hasDuplicate(axes)
22  for axis in axes:
23  abs(axis)
24  highest = axes.index(max(axes))
25 
26  dominantTwist = Twist()
27  # Add the dominant value to the dominant axis
28  if highest == 0:
29  dominantTwist.linear.x = twist_msg.linear.x
30  elif highest == 1:
31  dominantTwist.linear.y = twist_msg.linear.y
32  elif highest == 2:
33  dominantTwist.linear.z = twist_msg.linear.z
34  elif highest == 3:
35  dominantTwist.angular.x = twist_msg.angular.x
36  elif highest == 4:
37  dominantTwist.angular.y = twist_msg.angular.y
38  elif highest == 5:
39  dominantTwist.angular.z = twist_msg.angular.z
40 
41  return dominantTwist
42 
43  def axesRemap(self, twist_msg):
44  # Computed axis remap
45  linear_mat = [twist_msg.linear.x, twist_msg.linear.y, twist_msg.linear.z] * self.rotation_matrix
46  angular_mat = [twist_msg.angular.x, twist_msg.angular.y, twist_msg.angular.z] * self.rotation_matrix
47 
48  # Remapped values allocation
49  remappedTwist = Twist()
50  remappedTwist.linear.x = linear_mat[0,0]
51  remappedTwist.linear.y = linear_mat[0,1]
52  remappedTwist.linear.z = linear_mat[0,2]
53  remappedTwist.angular.x = angular_mat[0,0]
54  remappedTwist.angular.y = angular_mat[0,1]
55  remappedTwist.angular.z = angular_mat[0,2]
56 
57  return remappedTwist
58 
59  # Check if two axis are identical
60  # return a list with all members set to zero if true
61  def hasDuplicate(self, duplicate):
62  final_list = []
63  for num in duplicate:
64  if num not in final_list:
65  final_list.append(num)
66 
67  if len(final_list) != len(duplicate):
68  final_list = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
69  return final_list
70 
71  # Convert to JogFrame and republish
72  def callback(self, twist):
73 
74  msg = JogFrame()
75 
76  msg.header.stamp = rospy.Time.now()
77  msg.header.frame_id = rospy.get_param('~frame_id', 'base_link')
78  msg.group_name = rospy.get_param('~group_name', 'manipulator')
79  msg.link_name = rospy.get_param('~link_name', 'tool0')
80 
81  if rospy.get_param('~dominant_mode', True):
82  twist = self.dominantAxisMode(twist)
83 
84  if rospy.get_param('~axes_remap', True):
85  twist = self.axesRemap(twist)
86 
87  msg.angular_delta.x = self.scale_angular['x']*twist.angular.x
88  msg.angular_delta.y = self.scale_angular['y']*twist.angular.y
89  msg.angular_delta.z = self.scale_angular['z']*twist.angular.z
90 
91  msg.linear_delta.x = self.scale_linear['x']*twist.linear.x
92  msg.linear_delta.y = self.scale_linear['y']*twist.linear.y
93  msg.linear_delta.z = self.scale_linear['z']*twist.linear.z
94 
95  msg.avoid_collisions = True
96 
97  self.pub.publish(msg)
98 
99  def republish(self):
100  rospy.Subscriber(rospy.get_param('~sub_topic', 'spacenav/twist'), Twist, self.callback)
101 
102  rospy.spin()
103 
104 if __name__ == '__main__':
105  rospy.init_node('twist_to_jog_frame', anonymous=True)
106  republisher = twist_to_jog_frame()
107  republisher.republish()


jog_controller
Author(s): Ryosuke Tajima
autogenerated on Sun May 17 2020 03:25:01