/src/morse/sensors/accelerometer.py

https://github.com/kargm/morse · Python · 112 lines · 63 code · 15 blank · 34 comment · 0 complexity · 67cb00757f91e126f1a431c9f3d52a2f MD5 · raw file

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