/src/morse/sensors/jido_posture.py

https://github.com/peterroelants/morse · Python · 159 lines · 152 code · 2 blank · 5 comment · 0 complexity · 20f937f8be14d1c9a016ba5d2e49fde9 MD5 · raw file

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