/src/morse/sensors/imu.py

https://github.com/peterroelants/morse · Python · 114 lines · 107 code · 2 blank · 5 comment · 0 complexity · 48ad7feffbfae078151066d020b38981 MD5 · raw file

  1. import logging; logger = logging.getLogger("morse." + __name__)
  2. import GameLogic
  3. import math
  4. import morse.core.sensor
  5. class IMUClass(morse.core.sensor.MorseSensorClass):
  6. """ IMU sensor """
  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. logger.info('%s initialization' % obj.name)
  13. # Call the constructor of the parent class
  14. super(self.__class__,self).__init__(obj, parent)
  15. # Variables to store the previous position
  16. self.ppx = 0.0
  17. self.ppy = 0.0
  18. self.ppz = 0.0
  19. # Variables to store the previous angle position
  20. self.pproll = 0.0
  21. self.pppitch = 0.0
  22. self.ppyaw = 0.0
  23. # Variables to store the previous velocity
  24. self.pvx = 0.0
  25. self.pvy = 0.0
  26. self.pvz = 0.0
  27. # Variables to store the previous angle velocity (in rad)
  28. self.pvroll = 0.0
  29. self.pvpitch = 0.0
  30. self.pvyaw = 0.0
  31. # Make a new reference to the sensor position
  32. self.p = self.blender_obj.position
  33. self.v = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # Velocity
  34. self.pv = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # Previous Velocity
  35. self.a = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # Acceleration
  36. # Tick rate is the real measure of time in Blender.
  37. # By default it is set to 60, regardles of the FPS
  38. # If logic tick rate is 60, then: 1 second = 60 ticks
  39. self.ticks = GameLogic.getLogicTicRate()
  40. self.local_data['distance'] = 0.0
  41. self.local_data['velocity'] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  42. self.local_data['acceleration'] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  43. logger.info('Component initialized')
  44. def default_action(self):
  45. """ Compute the speed and accleration of the robot
  46. The speed and acceleration are computed using the blender tics
  47. to measure time.
  48. Since the time in Blender is computed with N ticks equaling a second,
  49. this method will be called N times each second, and so the time
  50. between calls is 1/N.
  51. When computing velocity as v = d / t, and t = 1 / N, then
  52. v = d * N
  53. where N is the number of ticks
  54. """
  55. # Compute the difference in positions with the previous loop
  56. dx = self.p[0] - self.ppx
  57. dy = self.p[1] - self.ppy
  58. dz = self.p[2] - self.ppz
  59. self.local_data['distance'] = math.sqrt(dx**2 + dy**2 + dz**2)
  60. logger.debug("DISTANCE: %.4f" % self.local_data['distance'])
  61. # Compute the difference in angles with the previous loop
  62. droll = self.position_3d.roll - self.pproll
  63. dpitch = self.position_3d.pitch - self.pppitch
  64. dyaw = self.position_3d.yaw - self.ppyaw
  65. # Store the position in this instant
  66. self.ppx = self.p[0]
  67. self.ppy = self.p[1]
  68. self.ppz = self.p[2]
  69. self.pproll = self.position_3d.roll
  70. self.pppitch = self.position_3d.pitch
  71. self.ppyaw = self.position_3d.yaw
  72. # Scale the speeds to the time used by Blender
  73. self.v[0] = dx * self.ticks
  74. self.v[1] = dy * self.ticks
  75. self.v[2] = dz * self.ticks
  76. self.v[3] = droll * self.ticks
  77. self.v[4] = dpitch * self.ticks
  78. self.v[5] = dyaw * self.ticks
  79. logger.debug("SPEED: (%.4f, %.4f, %.4f, %4f, %4f, %4f)" % (self.v[0], self.v[1], self.v[2], self.v[3], self.v[4], self.v[5]))
  80. self.a[0] = (self.v[0] - self.pvx) * self.ticks
  81. self.a[1] = (self.v[1] - self.pvy) * self.ticks
  82. self.a[2] = (self.v[2] - self.pvz) * self.ticks
  83. self.a[3] = (self.v[3] -self.pvroll) * self.ticks
  84. self.a[4] = (self.v[4] -self.pvpitch) * self.ticks
  85. self.a[5] = (self.v[5] -self.pvyaw) * self.ticks
  86. logger.debug("ACCELERATION: (%.4f, %.4f, %.4f, %4f, %4f, %4f)" % (self.a[0], self.a[1], self.a[2], self.a[3], self.a[4], self.a[5]))
  87. # Update the data for the velocity
  88. self.pvx = self.v[0]
  89. self.pvy = self.v[1]
  90. self.pvz = self.v[2]
  91. self.pvroll = self.v[3]
  92. self.pvpitch = self.v[4]
  93. self.pvyaw = self.v[5]
  94. # Store the important data
  95. self.local_data['velocity'] = self.v
  96. self.local_data['acceleration'] = self.a