motion.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Code generator for the test 2 blocks.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Python.motion');
  10. goog.require('Blockly.Python');
  11. Blockly.Python['motion_setup'] = function(block) {
  12. var motion_include = '#include "I2Cdev.h"\n' +
  13. '#include "MPU6050_6Axis_MotionApps20.h"\n' +
  14. '#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE\n' +
  15. '#include "Wire.h"\n' +
  16. '#endif\n';
  17. Blockly.Python.addInclude('motion_include', motion_include);
  18. var motion_declaration = '' +
  19. 'MPU6050 mpu\n' +
  20. '\n' +
  21. '#define _INTERRUPT_PIN 2\n' +
  22. '#define _LED_PIN 13\n' +
  23. 'bool _blinkState = false\n' +
  24. '\n' +
  25. 'bool dmpReady = false; // set true if DMP init was successful\n' +
  26. 'uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU\n' +
  27. 'uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)\n' +
  28. 'uint16_t packetSize; // expected DMP packet size (default is 42 bytes)\n' +
  29. 'uint16_t fifoCount; // count of all bytes currently in FIFO\n' +
  30. 'uint8_t fifoBuffer[64]; // FIFO storage buffer\n' +
  31. '\n' +
  32. 'Quaternion q; // [w, x, y, z] quaternion container\n' +
  33. 'VectorInt16 aa; // [x, y, z] accel sensor measurements\n' +
  34. 'VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements\n' +
  35. 'VectorFloat gravity; // [x, y, z] gravity vector\n' +
  36. 'float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector\n' +
  37. '\n' +
  38. 'volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high\n' +
  39. '\n' +
  40. 'float _motionGetYaw, _motionGetPitch, _motionGetRoll\n' +
  41. 'int _directionState = 0\n' +
  42. 'float _directionValue = 0.00\n' +
  43. 'void dmpDataReady() {\n' +
  44. 'mpuInterrupt = true\n' +
  45. '}\n' +
  46. '';
  47. Blockly.Python.addDeclaration('motion_declaration', motion_declaration);
  48. var motion_Setup = '' +
  49. 'void _motionSensorSetup() {\n' +
  50. '#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE\n' +
  51. 'Wire.begin()\n' +
  52. 'Wire.setClock(400000)\n' +
  53. '#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE\n' +
  54. 'Fastwire::setup(400, true)\n' +
  55. '#endif\n' +
  56. 'Serial.begin(115200)\n' +
  57. '// while (!Serial)\n' +
  58. 'Serial.println(F("Initializing I2C devices..."))\n' +
  59. 'mpu.initialize()\n' +
  60. 'pinMode(_INTERRUPT_PIN, INPUT)\n' +
  61. 'Serial.println(F("Testing device connections..."))\n' +
  62. 'Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"))\n' +
  63. 'Serial.println("Waiting for calibrating... 2s")\n' +
  64. 'delay(1000)\n' +
  65. 'Serial.println("Waiting for calibrating... 1s")\n' +
  66. 'delay(1000)\n' +
  67. 'Serial.println(F("Initializing DMP..."))\n' +
  68. 'devStatus = mpu.dmpInitialize()\n' +
  69. 'mpu.setXGyroOffset(220)\n' +
  70. 'mpu.setYGyroOffset(76)\n' +
  71. 'mpu.setZGyroOffset(-85)\n' +
  72. 'mpu.setZAccelOffset(1788)\n' +
  73. 'if (devStatus == 0) {\n' +
  74. 'mpu.CalibrateAccel(6)\n' +
  75. 'mpu.CalibrateGyro(6)\n' +
  76. 'mpu.PrintActiveOffsets()\n' +
  77. 'Serial.println(F("Enabling DMP..."))\n' +
  78. 'mpu.setDMPEnabled(true)\n' +
  79. 'Serial.print(F("Enabling interrupt detection (Arduino external interrupt "))\n' +
  80. 'Serial.print(digitalPinToInterrupt(_INTERRUPT_PIN))\n' +
  81. 'Serial.println(F(")..."))\n' +
  82. 'attachInterrupt(digitalPinToInterrupt(_INTERRUPT_PIN), dmpDataReady, RISING)\n' +
  83. 'mpuIntStatus = mpu.getIntStatus()\n' +
  84. 'Serial.println(F("DMP ready! Waiting for first interrupt..."))\n' +
  85. 'dmpReady = true\n' +
  86. 'packetSize = mpu.dmpGetFIFOPacketSize()\n' +
  87. '} else {\n' +
  88. 'Serial.print(F("DMP Initialization failed (code "))\n' +
  89. 'Serial.print(devStatus)\n' +
  90. 'Serial.println(F(")"))\n' +
  91. '}\n' +
  92. 'pinMode(_LED_PIN, OUTPUT)\n' +
  93. '}\n' +
  94. '';
  95. Blockly.Python.addFunction('motion_SetupFunc', motion_Setup);
  96. var motion_Loop = '' +
  97. 'void _motionSensorMain() {\n' +
  98. 'if (!dmpReady) return\n' +
  99. 'while (!mpuInterrupt && fifoCount < packetSize) {\n' +
  100. 'if (mpuInterrupt && fifoCount < packetSize) {\n' +
  101. 'fifoCount = mpu.getFIFOCount()\n' +
  102. '}\n' +
  103. '}\n' +
  104. 'mpuInterrupt = false\n' +
  105. 'mpuIntStatus = mpu.getIntStatus()\n' +
  106. 'fifoCount = mpu.getFIFOCount()\n' +
  107. 'if (fifoCount < packetSize) {\n' +
  108. '}\n' +
  109. 'else if ((mpuIntStatus & _BV(MPU6050_INTERRUPT_FIFO_OFLOW_BIT)) || fifoCount >= 1024) {\n' +
  110. ' mpu.resetFIFO()\n' +
  111. 'Serial.println(F("FIFO overflow!"))\n' +
  112. '} else if (mpuIntStatus & _BV(MPU6050_INTERRUPT_DMP_INT_BIT)) {\n' +
  113. 'while (fifoCount >= packetSize) {\n' +
  114. 'mpu.getFIFOBytes(fifoBuffer, packetSize)\n' +
  115. 'fifoCount -= packetSize\n' +
  116. '}\n' +
  117. 'mpu.dmpGetQuaternion(&q, fifoBuffer)\n' +
  118. 'mpu.dmpGetGravity(&gravity, &q)\n' +
  119. 'mpu.dmpGetYawPitchRoll(ypr, &q, &gravity)\n' +
  120. '_motionGetYaw = ypr[0] * 180 / M_PI\n' +
  121. '_motionGetPitch = ypr[1] * 180 / M_PI\n' +
  122. '_motionGetRoll = ypr[2] * 180 / M_PI\n' +
  123. 'mpu.dmpGetQuaternion(&q, fifoBuffer)\n' +
  124. 'mpu.dmpGetAccel(&aa, fifoBuffer)\n' +
  125. 'mpu.dmpGetGravity(&gravity, &q)\n' +
  126. 'mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity)\n' +
  127. '_blinkState = !_blinkState\n' +
  128. 'digitalWrite(_LED_PIN, _blinkState)\n' +
  129. '}\n' +
  130. '}\n' +
  131. '';
  132. Blockly.Python.addFunction('motion_LoopFunc', motion_Loop);
  133. Blockly.Python.addSetup("motion_setup", "_motionSensorSetup()\n");
  134. var code = "_motionSensorMain()\n"
  135. return code;
  136. };
  137. Blockly.Python['motion_onshake'] = function(block) {
  138. var onshake_func = '' +
  139. 'boolean _motionOnShake() {\n' +
  140. 'if (aaReal.x < -5000 || aaReal.x > 5000 || aaReal.y < -5000 || aaReal.y > 5000 || aaReal.z < -5000 || aaReal.z > 5000) {\n' +
  141. 'return true\n' +
  142. '} else {\n' +
  143. 'return false\n' +
  144. '}\n' +
  145. '}\n' +
  146. '';
  147. Blockly.Python.addFunction("onShake_function", onshake_func);
  148. var code = "_motionOnShake()";
  149. return [code, Blockly.Python.ORDER_ATOMIC];
  150. }
  151. Blockly.Python['motion_onDirection'] = function(block) {
  152. var direction = block.getFieldValue("DIRECTION");
  153. var ondirection_func = "int _motionOnTilting(int direction) {\n" +
  154. " _directionState = 0\n" +
  155. " switch (direction) {\n" +
  156. " case 1:\n" +
  157. " if (ypr[2] * 180 / M_PI < -20) {\n" +
  158. " _directionState = 1\n" +
  159. " }\n" +
  160. " break\n" +
  161. " case 2:\n" +
  162. " if (ypr[2] * 180 / M_PI > 20) {\n" +
  163. " _directionState = 1\n" +
  164. " }\n" +
  165. " break\n" +
  166. " case 3:\n" +
  167. " if (ypr[1] * 180 / M_PI < -20) {\n" +
  168. " _directionState = 1\n" +
  169. " }\n" +
  170. " break\n" +
  171. " case 4:\n" +
  172. " if (ypr[1] * 180 / M_PI > 20) {\n" +
  173. " _directionState = 1\n" +
  174. " }\n" +
  175. " break\n" +
  176. " case 5:\n" +
  177. " if (ypr[0] * 180 / M_PI +180 - _directionValue > 0) {\n" +
  178. " _directionState = 1\n" +
  179. " }\n" +
  180. " break\n" +
  181. " case 6:\n" +
  182. " if (ypr[0] * 180 / M_PI +180 - _directionValue < 0) {\n" +
  183. " _directionState = 1\n" +
  184. " }\n" +
  185. " break\n" +
  186. " default:\n" +
  187. " break\n" +
  188. " }\n" +
  189. " _directionValue = ypr[0] * 180 / M_PI +180\n" +
  190. " return _directionState\n" +
  191. "}\n";
  192. Blockly.Python.addFunction("onDirection_function", ondirection_func);
  193. var direction_tag
  194. switch (direction) {
  195. case "left":
  196. direction_tag = 1;
  197. break;
  198. case "right":
  199. direction_tag = 2;
  200. break;
  201. case "front":
  202. direction_tag = 3;
  203. break;
  204. case "back":
  205. direction_tag = 4;
  206. break;
  207. case "clockwise":
  208. direction_tag = 5;
  209. break;
  210. case "counterClockwise":
  211. direction_tag = 6;
  212. break;
  213. default:
  214. break;
  215. }
  216. var code = "_motionOnTilting(" + direction_tag + ")";
  217. return [code, Blockly.Python.ORDER_ATOMIC];
  218. }
  219. Blockly.Python['motion_getRotation_ypr'] = function(block) {
  220. var rotation = block.getFieldValue("Rotation");
  221. var code = "ypr[" + rotation + "] * 180 / M_PI";
  222. return [code, Blockly.Python.ORDER_ATOMIC];
  223. }
  224. Blockly.Python['motion_getAcceleration'] = function(block) {
  225. var acc = block.getFieldValue("Acceleration");
  226. var code = "aaReal." + acc;
  227. return [code, Blockly.Python.ORDER_ATOMIC];
  228. }