You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. float pitch, roll;
  2. float gx, gy, gz;
  3. int16_t mx, my, mz;
  4. uint16_t xc, yc;
  5. uint8_t isTouch;
  6. float ax, ay, az;
  7. int16_t xc_old, yc_old;
  8. /* Decode 12-bit signed value (assuming two's complement) */
  9. #define TWELVE_BIT_SIGNED(x) (((x) & 0x800)?(-(((~(x)) & 0xFFF) + 1)):(x))
  10. #define Model_ZCM1 1
  11. //#define Model_ZCM2 0
  12. void printAngles(){
  13. //test function calls
  14. float gx, gy, gz;
  15. getAccel(ax, ay, az);
  16. Serial.printf("Accel-g's: %f, %f, %f\n", ax, ay, az);
  17. getGyro(gx, gy, gz);
  18. Serial.printf("Gyro-deg/sec: %f, %f, %f\n", gx, gy, gz);
  19. getAngles(pitch, roll);
  20. Serial.printf("Pitch/Roll: %f, %f\n", pitch, roll);
  21. getCoords(xc, yc, isTouch);
  22. }
  23. void getCoords(uint16_t &xc, uint16_t &yc, uint8_t &isTouch){
  24. //uint8_t finger = 0; //only getting finger 1
  25. // Trackpad touch 1: id, active, x, y
  26. xc = ((psAxis[37] & 0x0f) << 8) | psAxis[36];
  27. yc = psAxis[38] << 4 | ((psAxis[37] & 0xf0) >> 4),
  28. isTouch = psAxis[35] >> 7;
  29. if(xc != xc_old || yc != yc_old){
  30. Serial.printf("Touch: %d, %d, %d, %d\n", psAxis[33], isTouch, xc, yc);
  31. xc_old = xc;
  32. yc_old = yc;
  33. }
  34. }
  35. void getAccel( float &ax, float &ay, float &az){
  36. int accelx = (int16_t)(psAxis[20]<<8) | psAxis[19];
  37. int accelz = (int16_t)(psAxis[22]<<8) | psAxis[21];
  38. int accely = (int16_t)(psAxis[24]<<8) | psAxis[23];
  39. ax = (float) accelx/8192;
  40. ay = (float) accely/8192;
  41. az = (float) accelz/8192;
  42. }
  43. void getAngles(float &p, float &r){
  44. getAccel( ax, ay, az);
  45. p = (atan2f(ay, az) + PI) * RAD_TO_DEG;
  46. r = (atan2f(ax, az) + PI) * RAD_TO_DEG;
  47. }
  48. void getGyro(float &gx, float &gy, float &gz){
  49. int gyroy = (int16_t)(psAxis[14]<<8) | psAxis[13];
  50. int gyroz = (int16_t)(psAxis[16]<<8) | psAxis[15];
  51. int gyrox = (int16_t)(psAxis[18]<<8) | psAxis[17];
  52. gx = (float) gyrox * RAD_TO_DEG/1024;
  53. gy = (float) gyroy * RAD_TO_DEG/1024;
  54. gz = (float) gyroz * RAD_TO_DEG/1024;
  55. }
  56. void printPS3MotionAngles(){
  57. //test function calls
  58. float gx, gy, gz;
  59. getPS3MotionAccel(ax, ay, az);
  60. Serial.printf("Accel-g's: %f, %f, %f\n", ax, ay, az);
  61. getPS3MotionGyro(gx, gy, gz);
  62. Serial.printf("Gyro-deg/sec: %f, %f, %f\n", gx, gy, gz);
  63. getPS3MotionMag(mx, my, mz);
  64. Serial.printf("Mag: %d, %d, %d\n", mx, my, mz);
  65. getPS3MotionAngles(pitch, roll);
  66. Serial.printf("Pitch/Roll: %f, %f\n", pitch, roll);
  67. }
  68. void getPS3MotionAccel( float &ax, float &ay, float &az){
  69. int accelx = (psAxis[15]<<8 | psAxis[14]);
  70. int accely = (psAxis[17]<<8 | psAxis[16]);
  71. int accelz = (psAxis[19]<<8 | psAxis[18]);
  72. #if defined(Model_ZCM1)
  73. accelx = accelx-0x8000;
  74. accely = accely-0x8000;
  75. accelz = accelz-0x8000;
  76. #elif defined(Model_ZCM2)
  77. accelx = (accelx & 0x8000) ? (-(~accelx & 0xFFFF) + 1) : accelx;
  78. accely = (accely & 0x8000) ? (-(~accely & 0xFFFF) + 1) : accely;
  79. accelz = (accelz & 0x8000) ? (-(~accelz & 0xFFFF) + 1) : accelz;
  80. #endif
  81. ax = (float) accelx/4096;
  82. ay = (float) accely/4096;
  83. az = (float) accelz/4096;
  84. }
  85. void getPS3MotionAngles(float &p, float &r){
  86. getAccel( ax, ay, az);
  87. p = (atan2f(ay, az) + PI) * RAD_TO_DEG;
  88. r = (atan2f(ax, az) + PI) * RAD_TO_DEG;
  89. }
  90. void getPS3MotionGyro(float &gx, float &gy, float &gz){
  91. int gyrox = (psAxis[21]<<8 | psAxis[20]);
  92. int gyroy = (psAxis[23]<<8 | psAxis[22]);
  93. int gyroz = (psAxis[25]<<8 | psAxis[24]);
  94. #if defined(Model_ZCM1)
  95. gyrox = gyrox-0x8000;
  96. gyroy = gyroy-0x8000;
  97. gyroz = gyroz-0x8000;
  98. #elif defined(Model_ZCM2)
  99. gyrox = (gyrox & 0x8000) ? (-(~gyrox & 0xFFFF) + 1) : gyrox;
  100. gyroy = (gyroy & 0x8000) ? (-(~gyroy & 0xFFFF) + 1) : gyroy;
  101. gyroz = (gyroz & 0x8000) ? (-(~gyroz & 0xFFFF) + 1) : gyroz;
  102. #endif
  103. gx = (float) gyrox * RAD_TO_DEG/1024;
  104. gy = (float) gyroy * RAD_TO_DEG/1024;
  105. gz = (float) gyroz * RAD_TO_DEG/1024;
  106. }
  107. void getPS3MotionMag(int16_t &mx, int16_t &my, int16_t &mz){
  108. #if defined(Model_ZCM1)
  109. mx = TWELVE_BIT_SIGNED(((psAxis[33] & 0x0F) << 8) |
  110. psAxis[34]);
  111. my = TWELVE_BIT_SIGNED((psAxis[35] << 4) |
  112. (psAxis[36] & 0xF0) >> 4);
  113. mz = TWELVE_BIT_SIGNED(((psAxis[36] & 0x0F) << 8) |
  114. psAxis[37]);
  115. #elif defined(Model_ZCM2)
  116. // NOTE: This model does not have magnetometers
  117. Serial.println("Not avail for ZCM2!");
  118. #endif
  119. }