PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/morse/sensors/human_posture.py

https://github.com/davidhodo/morse
Python | 223 lines | 181 code | 22 blank | 20 comment | 0 complexity | 9aeb5ac574a3a093d8b5c551b3e4c4b1 MD5 | raw file
  1. import logging; logger = logging.getLogger("morse." + __name__)
  2. import math
  3. import morse.core.sensor
  4. from morse.helpers.components import add_data
  5. class HumanPosture(morse.core.sensor.Sensor):
  6. """
  7. This sensor collects the positions of the bones in the human armature
  8. for the file ``$MORSE_ROOT/data/robots/human.blend``.
  9. It stores the position and orientation of the general armature
  10. object, as well as the local rotation of each individual bone. The
  11. rotation angles are given in radians.
  12. This sensor will only work for the ``human.blend`` model, as it
  13. uses a specific naming convention for each of the bones.
  14. You can also check to general documentation of the :doc:`human
  15. component <../others/human>`.
  16. .. image:: ../../../media/human_joints.png
  17. :align: center
  18. :width: 600
  19. """
  20. _name = "Human Posture"
  21. add_data('x', 0.0, "float",
  22. 'global X position of the armature in the scene, in meter')
  23. add_data('y', 0.0, "float",
  24. 'global Y position of the armature in the scene, in meter')
  25. add_data('z', 0.0, "float",
  26. 'global Z position of the armature in the scene, in meter')
  27. add_data('yaw', 0.0, "float",
  28. 'rotation angle with respect to the Z axis, in radian')
  29. add_data('pitch', 0.0, "float",
  30. 'rotation angle with respect to the Y axis, in radian')
  31. add_data('roll', 0.0, "float",
  32. 'rotation angle with respect to the X axis, in radian')
  33. add_data('empty1', 0.0, "float")
  34. add_data('empty2', 0.0, "float")
  35. add_data('empty3', 0.0, "float")
  36. add_data('empty4', 0.0, "float")
  37. add_data('empty5', 0.0, "float")
  38. add_data('empty6', 0.0, "float")
  39. add_data('dof_12', 0.0, 'float',
  40. 'rotation around the X axis for the torso, in radian')
  41. add_data('dof_13', 0.0, 'float',
  42. 'rotation around the Y axis for the torso, in radian')
  43. add_data('dof_14', 0.0, 'float',
  44. 'rotation around the Z axis for the torso, in radian')
  45. add_data('dof_15', 0.0, 'float',
  46. 'rotation around the Z axis for the head, in radian')
  47. add_data('dof_16', 0.0, 'float',
  48. 'rotation around the Y axis for the head, in radian')
  49. add_data('dof_17', 0.0, 'float',
  50. 'rotation around the X axis for the head, in radian')
  51. add_data('dof_18', 0.0, 'float',
  52. 'rotation around the X axis for the right shoulder, in radian')
  53. add_data('dof_19', 0.0, 'float',
  54. 'rotation around the Y axis for the right shoulder, in radian')
  55. add_data('dof_20', 0.0, 'float',
  56. 'rotation around the Z axis for the right shoulder, in radian')
  57. add_data('dof_21', 0.0, 'float',
  58. 'elongation of the right arm, in meter')
  59. add_data('dof_22', 0.0, 'float',
  60. 'rotation around the Z axis for the right elbow, in radian')
  61. add_data('dof_23', 0.0, 'float', 'R_POINT')
  62. add_data('dof_24', 0.0, 'float',
  63. 'rotation around the X axis for the right wrist, in radian')
  64. add_data('dof_25', 0.0, 'float',
  65. 'rotation around the Y axis for the right wrist, in radian')
  66. add_data('dof_26', 0.0, 'float',
  67. 'rotation around the Z axis for the right wrist, in radian')
  68. add_data('dof_27', 0.0, 'float',
  69. 'rotation around the X axis for the left shoulder, in radian')
  70. add_data('dof_28', 0.0, 'float',
  71. 'rotation around the Y axis for the left shoulder, in radian')
  72. add_data('dof_29', 0.0, 'float',
  73. 'rotation around the Z axis for the left shoulder, in radian')
  74. add_data('dof_30', 0.0, 'float',
  75. 'elongation of the left arm, in meter')
  76. add_data('dof_31', 0.0, 'float',
  77. 'rotation around the Z axis for the left elbow, in radian')
  78. add_data('dof_32', 0.0, 'float', 'L_POINT')
  79. add_data('dof_33', 0.0, 'float',
  80. 'rotation around the X axis for the left wrist, in radian')
  81. add_data('dof_34', 0.0, 'float',
  82. 'rotation around the Y axis for the left wrist, in radian')
  83. add_data('dof_35', 0.0, 'float',
  84. 'rotation around the Z axis for the left wrist, in radian')
  85. add_data('dof_36', 0.0, 'float',
  86. 'rotation around the X axis for the right hip, in radian')
  87. add_data('dof_37', 0.0, 'float',
  88. 'rotation around the Y axis for the right hip, in radian')
  89. add_data('dof_38', 0.0, 'float',
  90. 'rotation around the Z axis for the right hip, in radian')
  91. add_data('dof_39', 0.0, 'float',
  92. 'rotation around the Z axis for the right knee, in radian')
  93. add_data('dof_40', 0.0, 'float',
  94. 'rotation around the X axis for the right ankle, in radian')
  95. add_data('dof_41', 0.0, 'float',
  96. 'rotation around the Y axis for the right ankle, in radian')
  97. add_data('dof_42', 0.0, 'float',
  98. 'rotation around the Z axis for the right ankle, in radian')
  99. add_data('dof_43', 0.0, 'float',
  100. 'rotation around the X axis for the left hip, in radian')
  101. add_data('dof_44', 0.0, 'float',
  102. 'rotation around the Y axis for the left hip, in radian')
  103. add_data('dof_45', 0.0, 'float',
  104. 'rotation around the Z axis for the left hip, in radian')
  105. add_data('dof_46', 0.0, 'float',
  106. 'rotation around the Z axis for the left knee, in radian')
  107. add_data('dof_47', 0.0, 'float',
  108. 'rotation around the X axis for the left ankle, in radian')
  109. add_data('dof_48', 0.0, 'float',
  110. 'rotation around the Y axis for the left ankle, in radian')
  111. add_data('dof_49', 0.0, 'float',
  112. 'rotation around the Z axis for the left ankle, in radian')
  113. def __init__(self, obj, parent=None):
  114. """ Constructor method.
  115. Receives the reference to the Blender object.
  116. The second parameter should be the name of the object's parent. """
  117. logger.info('%s initialization' % obj.name)
  118. # Call the constructor of the parent class
  119. super(self.__class__, self).__init__(obj, parent)
  120. logger.info('Component initialized, runs at %.2f Hz', self.frequency)
  121. def _read_pose(self, armature):
  122. for channel in armature.channels:
  123. if 'X_' not in channel.name:
  124. logger.debug("\tChannel '%s': (%.4f, %.4f, %.4f)" %
  125. (channel,
  126. channel.joint_rotation[0],
  127. channel.joint_rotation[1],
  128. channel.joint_rotation[2]))
  129. if channel.name == 'Chest':
  130. self.local_data['dof_12'] = channel.joint_rotation[2] #y
  131. self.local_data['dof_13'] = channel.joint_rotation[0] #x
  132. self.local_data['dof_14'] = channel.joint_rotation[1] #z
  133. if channel.name == 'Head':
  134. self.local_data['dof_15'] = - channel.joint_rotation[0] #z axis
  135. self.local_data['dof_16'] = channel.joint_rotation[2] #x axis
  136. self.local_data['dof_17'] = channel.joint_rotation[1] #y axis
  137. if channel.name == 'UpArm.R':
  138. self.local_data['dof_18'] = - channel.joint_rotation[1]
  139. self.local_data['dof_19'] = - channel.joint_rotation[0]
  140. self.local_data['dof_20'] = channel.joint_rotation[2]
  141. if channel.name == 'ForeArm.R':
  142. self.local_data['dof_21'] = channel.joint_rotation[2]
  143. if channel.name == 'Hand.R':
  144. self.local_data['dof_22'] = channel.joint_rotation[0]
  145. self.local_data['dof_23'] = channel.joint_rotation[1]
  146. self.local_data['dof_24'] = channel.joint_rotation[2]
  147. if channel.name == 'UpArm.L':
  148. self.local_data['dof_25'] = - channel.joint_rotation[1]
  149. self.local_data['dof_26'] = - channel.joint_rotation[0]
  150. self.local_data['dof_27'] = channel.joint_rotation[2]
  151. if channel.name == 'ForeArm.L':
  152. self.local_data['dof_28'] = channel.joint_rotation[2]
  153. if channel.name == 'Hand.L':
  154. self.local_data['dof_29'] = channel.joint_rotation[0]
  155. self.local_data['dof_30'] = channel.joint_rotation[1]
  156. self.local_data['dof_31'] = channel.joint_rotation[2]
  157. if channel.name == 'UpLeg.R' :
  158. self.local_data['dof_32'] = channel.joint_rotation[1]
  159. self.local_data['dof_33'] = channel.joint_rotation[0]
  160. self.local_data['dof_34'] = - channel.joint_rotation[2]
  161. if channel.name == 'LoLeg.R':
  162. self.local_data['dof_35'] = channel.joint_rotation[0]
  163. if channel.name == 'Foot.R':
  164. self.local_data['dof_36'] = channel.joint_rotation[0]
  165. self.local_data['dof_37'] = channel.joint_rotation[1]
  166. self.local_data['dof_38'] = channel.joint_rotation[2]
  167. if channel.name == 'UpLeg.L':
  168. self.local_data['dof_39'] = channel.joint_rotation[1]
  169. self.local_data['dof_40'] = channel.joint_rotation[0]
  170. self.local_data['dof_41'] = - channel.joint_rotation[2]
  171. if channel.name == 'LoLeg.L':
  172. self.local_data['dof_42'] = channel.joint_rotation[0]
  173. if channel.name == 'Foot.L':
  174. self.local_data['dof_43'] = channel.joint_rotation[0]
  175. self.local_data['dof_44'] = channel.joint_rotation[1]
  176. self.local_data['dof_45'] = channel.joint_rotation[2]
  177. def default_action(self):
  178. """ Extract the human posture """
  179. self.local_data['x'] = float(self.position_3d.x)
  180. self.local_data['y'] = float(self.position_3d.y)
  181. self.local_data['z'] = float(self.position_3d.z)
  182. self.local_data['yaw'] = float(self.position_3d.yaw) - (math.pi/2)
  183. self.local_data['pitch'] = float(self.position_3d.pitch)
  184. self.local_data['roll'] = float(self.position_3d.roll)
  185. self._read_pose(self.bge_object)
  186. logger.debug("LOCAL_DATA: ", self.local_data)