motion_old.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /*
  9. 'use strict';
  10. goog.provide('Blockly.Python.motion');
  11. goog.require('Blockly.Python');
  12. Blockly.Python['motion_setup'] = function() {
  13. var motion_include =' #include <RobotIRremote.h>\n' +
  14. ' #include <RobotIRremoteInt.h>\n' +
  15. ' #include <RobotIRremoteTools.h>\n\n' +
  16. ' #include <RobotIRremote.h>\n' +
  17. ' #include <RobotIRremoteInt.h>\n' +
  18. ' #include <RobotIRremoteTools.h>\n\n' +
  19. ' #include "I2Cdev.h"\n' +
  20. ' #include "MPU6050_6Axis_MotionApps20.h"\n' +
  21. ' #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE\n' +
  22. ' #include "Wire.h"\n' +
  23. ' #endif\n\n';
  24. Blockly.Python.addInclude('motion_include',motion_include);
  25. var motion_declaration_A =' MPU6050 mpu;\n' +
  26. ' #define OUTPUT_READABLE_QUATERNION\n\n' +
  27. ' #define OUTPUT_READABLE_WORLDACCEL\n' +
  28. ' #define OUTPUT_READABLE_REALACCEL\n' +
  29. ' '
  30. ' bool dmpReady = false; // set true if DMP init was successful\n' +
  31. ' uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU\n' +
  32. ' uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)\n' +
  33. ' uint16_t packetSize; // expected DMP packet size (default is 42 bytes)\n'+
  34. ' uint16_t fifoCount; // count of all bytes currently in FIFO\n' +
  35. ' uint8_t fifoBuffer[64]; // FIFO storage buffer\n\n' +
  36. ' Quaternion q; // [w, x, y, z] quaternion container\n' +
  37. ' VectorInt16 aa; // [x, y, z] accel sensor measurements\n' +
  38. ' VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements\n'+
  39. ' VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements\n'+
  40. ' VectorFloat gravity; // [x, y, z] gravity vector\n' +
  41. ' float euler[3]; // [psi, theta, phi] Euler angle container\n' +
  42. ' float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector\n\n'+
  43. ' volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high\n';
  44. Blockly.Python.addDeclaration('motion_declaration_A', motion_declaration_A);
  45. //var motion_declaration_B = ' const int MPU_addr=0x68; // I2C address of the MPU-6050\n' +
  46. // ' int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;\n';
  47. //Blockly.Python.addDeclaration('motion_declaration_B', motion_declaration_B);
  48. var dmpDataReady = ' void dmpDataReady() {\n' +
  49. ' mpuInterrupt = true;\n' +
  50. ' }\n';
  51. Blockly.Python.addFunction('motion_dmpDateReady_function', dmpDataReady);
  52. var motion_setup =' #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE\n' +
  53. ' Wire.begin();\n' +
  54. ' TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)\n' +
  55. ' #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE\n' +
  56. ' Fastwire::setup(400, true);\n' +
  57. ' #endif\n' +
  58. ' mpu.initialize();\n' +
  59. ' devStatus = mpu.dmpInitialize();\n' +
  60. ' mpu.setXGyroOffset(220);\n' +
  61. ' mpu.setYGyroOffset(76);\n' +
  62. ' mpu.setZGyroOffset(-85);\n' +
  63. ' mpu.setZAccelOffset(1788);\n' +
  64. ' if (devStatus == 0) {\n' +
  65. ' mpu.setDMPEnabled(true);\n'+
  66. ' attachInterrupt(0, dmpDataReady, RISING);\n' +
  67. ' mpuIntStatus = mpu.getIntStatus();\n' +
  68. ' dmpReady = true;\n' +
  69. ' packetSize = mpu.dmpGetFIFOPacketSize();\n' +
  70. ' } else {\n' +
  71. ' }\n';
  72. Blockly.Python.addSetup('motion_setup',motion_setup);
  73. var motion_code = 'if (!dmpReady) return;\n' +
  74. 'if (mpu.getFIFOCount() >= packetSize) {\n' +
  75. 'mpu.getFIFOBytes(fifoBuffer, packetSize);\n' +
  76. 'mpu.dmpGetQuaternion(&q, fifoBuffer);\n' +
  77. 'mpu.dmpGetEuler(euler, &q);\n' +
  78. '}\n' +
  79. 'mpuInterrupt = false;\n' +
  80. 'mpuIntStatus = mpu.getIntStatus();\n' +
  81. 'fifoCount = mpu.getFIFOCount();\n' +
  82. 'if ((mpuIntStatus & 0x10) || fifoCount == 1024) {\n' +
  83. // reset so we can continue cleanly
  84. ' mpu.resetFIFO();\n' +
  85. '} else if (mpuIntStatus & 0x02) {\n' +
  86. ' while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();\n' +
  87. ' mpu.getFIFOBytes(fifoBuffer, packetSize);\n' +
  88. ' fifoCount -= packetSize;\n' +
  89. // display quaternion values in easy matrix form: w x y z
  90. ' mpu.dmpGetQuaternion(&q, fifoBuffer);\n' +
  91. ' mpu.dmpGetAccel(&aa, fifoBuffer);\n' +
  92. ' mpu.dmpGetGravity(&gravity, &q);\n' +
  93. ' mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);\n' +
  94. ' mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);\n'+
  95. '}\n';
  96. return motion_code;
  97. };
  98. Blockly.Python['motion_getRawData'] = function() {
  99. var dropdown_rawdatatype = block.getFieldValue('rawdatatype');
  100. var dropdown_rawdatalocate = block.getFieldValue('rawdatalocate');
  101. var code ='';
  102. if (dropdown_rawdatatype == 'motion_rawdata_accelerate') {
  103. code = 'aa';
  104. } else if (dropdown_rawdatatype == 'motion_rawdata_gyroscope') {
  105. code = '';
  106. }
  107. if (dropdown_rawdatalocate == 'motion_rawdata_x') {
  108. code +='.x\n';
  109. } else if (dropdown_rawdatalocate == 'motion_rawdata_y') {
  110. code +='.y\n';
  111. } else if (dropdown_rawdatalocate == 'motion_rawdata_z') {
  112. code +='.z\n';
  113. }
  114. return [code,Blockly.Python.ORDER_ATOMIC];
  115. };
  116. */