/src/morse/sensors/jido_posture.py

https://github.com/kargm/morse · Python · 158 lines · 99 code · 27 blank · 32 comment · 19 complexity · 1810e5d5f0a74077b4c3138c75472200 MD5 · raw file

  1. import math
  2. import morse.core.sensor
  3. import mathutils
  4. import morse.helpers.math as morse_math
  5. class JidoPostureClass(morse.core.sensor.MorseSensorClass):
  6. """ Jido posture sensor. Currently working with PTU and KUKA arm """
  7. def __init__(self, obj, parent=None):
  8. """ Constructor method.
  9. Receives the reference to the Blender object.
  10. The second parameter should be the name of the object's parent.
  11. """
  12. print ("######## Jido POSTURE EXPORTER '%s' INITIALIZING ########" % obj.name)
  13. # Call the constructor of the parent class
  14. super(self.__class__,self).__init__(obj, parent)
  15. # Object position (maybe delete later)
  16. self.local_data['x'] = 0.0
  17. self.local_data['y'] = 0.0
  18. self.local_data['z'] = 0.0
  19. self.local_data['yaw'] = 0.0
  20. self.local_data['pitch'] = 0.0
  21. self.local_data['roll'] = 0.0
  22. # joints of kuka-arm
  23. self.local_data['seg0'] = 0.0
  24. self.local_data['seg1'] = 0.0
  25. self.local_data['seg2'] = 0.0
  26. self.local_data['seg3'] = 0.0
  27. self.local_data['seg4'] = 0.0
  28. self.local_data['seg5'] = 0.0
  29. self.local_data['seg6'] = 0.0
  30. # joints of PTU-unit
  31. self.local_data['pan'] = 0.0
  32. self.local_data['tilt'] = 0.0
  33. print ('######## Jido POSTURE EXPORTER INITIALIZED ########')
  34. ##################### PTU joints ##################
  35. # Check if robot parent has a child named "PTUname"
  36. for child in self.robot_parent.blender_obj.children:
  37. if str(child) == self.blender_obj['PTUname']:
  38. self._ptu_obj = child
  39. # Get the references to the childen object and
  40. # store a transformation3d structure for their position
  41. for child in self._ptu_obj.childrenRecursive:
  42. if 'PanBase' in child.name:
  43. self._pan_base = child
  44. self._pan_position_3d = morse.helpers.transformation.Transformation3d(child)
  45. elif 'TiltBase' in child.name:
  46. self._tilt_base = child
  47. self._tilt_position_3d = morse.helpers.transformation.Transformation3d(child)
  48. # Check the bases were found, or exit with a message
  49. try:
  50. print ("Using PTU: '%s'" % self._ptu_obj.name)
  51. print ("Using pan base: '%s'" % self._pan_base.name)
  52. print ("Using tilt base: '%s'" % self._tilt_base.name)
  53. except AttributeError as detail:
  54. print ("ERROR: Platine is missing the pan and tilt bases. Module will not work!")
  55. ###################### KUKA joints ##################
  56. # Gather information about all segments of the kuka-arm
  57. self._segments = []
  58. self.kuka_obj = 0
  59. # Check if robot parent has a child named "kuka_base"
  60. for child in self.robot_parent.blender_obj.children:
  61. if str(child) == self.blender_obj['KUKAname']:
  62. self.kuka_obj = child
  63. try:
  64. print ("Using KUKA arm: '%s'" % self.kuka_obj.name)
  65. except AttributeError as detail:
  66. print ("ERROR: Kuka arm is missing. Module will not work!")
  67. # The axis along which the different segments of the kuka armrotate
  68. # Considering the rotation of the arm as installed in Jido
  69. self._dofs = ['z', '-y', 'z', 'y', 'z', '-y', 'z']
  70. def default_action(self):
  71. """ Get the x, y, z, yaw, pitch and roll of the blender object. """
  72. x = self.position_3d.x
  73. y = self.position_3d.y
  74. z = self.position_3d.z
  75. yaw = self.position_3d.yaw
  76. pitch = self.position_3d.pitch
  77. roll = self.position_3d.roll
  78. ############################# PTU joints ##############################
  79. # Reset movement variables
  80. rx, ry, rz = 0.0, 0.0, 0.0
  81. # Update the postition of the base platforms
  82. try:
  83. self._pan_position_3d.update(self._pan_base)
  84. self._tilt_position_3d.update(self._tilt_base)
  85. except AttributeError as detail:
  86. print ("ERROR: Platine is missing the pan and tilt bases. Platine does not work!")
  87. return
  88. current_pan = self._pan_position_3d.yaw
  89. current_tilt = self._tilt_position_3d.pitch
  90. #print ("Platine: pan=%.4f, tilt=%.4f" % (current_pan, current_tilt))
  91. ############################# KUKA joints ##############################
  92. segment = self.kuka_obj.children[0]
  93. self._angles = []
  94. # Gather all the children of the object which are the segments of the kuka-arm
  95. for i in range(len(self._dofs)):
  96. self._segments.append(segment)
  97. # Extract the angles
  98. rot_matrix = segment.localOrientation
  99. segment_matrix = mathutils.Matrix(rot_matrix[0], rot_matrix[1], rot_matrix[2])
  100. segment_euler = segment_matrix.to_euler()
  101. # Use the corresponding direction for each rotation
  102. if self._dofs[i] == 'y':
  103. self._angles.append(segment_euler[1])
  104. elif self._dofs[i] == '-y':
  105. self._angles.append(-segment_euler[1])
  106. elif self._dofs[i] == 'z':
  107. self._angles.append(segment_euler[2])
  108. try:
  109. segment = segment.children[0]
  110. # Exit when there are no more children
  111. except IndexError as detail:
  112. break
  113. ############################# Hand data over to middleware ##############################
  114. self.local_data['x'] = float(x)
  115. self.local_data['y'] = float(y)
  116. self.local_data['z'] = float(z)
  117. self.local_data['yaw'] = float(yaw)
  118. self.local_data['pitch'] = float(pitch)
  119. self.local_data['roll'] = float(roll)
  120. # KUKA arm
  121. self.local_data['seg0'] = self._angles[0]
  122. self.local_data['seg1'] = self._angles[1]
  123. self.local_data['seg2'] = self._angles[2]
  124. self.local_data['seg3'] = self._angles[3]
  125. self.local_data['seg4'] = self._angles[4]
  126. self.local_data['seg5'] = self._angles[5]
  127. self.local_data['seg6'] = self._angles[6]
  128. # PTU
  129. self.local_data['pan'] = float(current_pan)
  130. self.local_data['tilt'] = float(current_tilt)