找回密码
 注册

QQ登录

只需一步,快速开始

搜索

MPU-6050使用Python上位机监控实时动作

[复制链接]
coolice 发表于 2020-1-20 19:52:49 | 显示全部楼层 |阅读模式
单片机:STM32F4_MD6
上位机:Python
MPU-6050-Python上位机.zip (19.63 MB, 售价: 1 E币)
  1. #ifdef COMPASS_ENABLED
  2. void send_status_compass() {
  3.         long data[3] = { 0 };
  4.         int8_t accuracy = { 0 };
  5.         unsigned long timestamp;
  6.         inv_get_compass_set(data, &accuracy, (inv_time_t*) &timestamp);
  7.         MPL_LOGI("Compass: %7.4f %7.4f %7.4f ",
  8.                         data[0]/65536.f, data[1]/65536.f, data[2]/65536.f);
  9.         MPL_LOGI("Accuracy= %d\r\n", accuracy);

  10. }
  11. #endif

  12. /* Handle sensor on/off combinations. */
  13. static void setup_gyro(void)
  14. {
  15.     unsigned char mask = 0, lp_accel_was_on = 0;
  16.     if (hal.sensors & ACCEL_ON)
  17.         mask |= INV_XYZ_ACCEL;
  18.     if (hal.sensors & GYRO_ON) {
  19.         mask |= INV_XYZ_GYRO;
  20.         lp_accel_was_on |= hal.lp_accel_mode;
  21.     }
  22. #ifdef COMPASS_ENABLED
  23.     if (hal.sensors & COMPASS_ON) {
  24.         mask |= INV_XYZ_COMPASS;
  25.         lp_accel_was_on |= hal.lp_accel_mode;
  26.     }
  27. #endif
  28.     /* If you need a power transition, this function should be called with a
  29.      * mask of the sensors still enabled. The driver turns off any sensors
  30.      * excluded from this mask.
  31.      */
  32.     mpu_set_sensors(mask);
  33.     mpu_configure_fifo(mask);
  34.     if (lp_accel_was_on) {
  35.         unsigned short rate;
  36.         hal.lp_accel_mode = 0;
  37.         /* Switching out of LP accel, notify MPL of new accel sampling rate. */
  38.         mpu_get_sample_rate(&rate);
  39.         inv_set_accel_sample_rate(1000000L / rate);
  40.     }
  41. }

  42. static void tap_cb(unsigned char direction, unsigned char count)
  43. {
  44.     switch (direction) {
  45.     case TAP_X_UP:
  46.         MPL_LOGI("Tap X+ ");
  47.         break;
  48.     case TAP_X_DOWN:
  49.         MPL_LOGI("Tap X- ");
  50.         break;
  51.     case TAP_Y_UP:
  52.         MPL_LOGI("Tap Y+ ");
  53.         break;
  54.     case TAP_Y_DOWN:
  55.         MPL_LOGI("Tap Y- ");
  56.         break;
  57.     case TAP_Z_UP:
  58.         MPL_LOGI("Tap Z+ ");
  59.         break;
  60.     case TAP_Z_DOWN:
  61.         MPL_LOGI("Tap Z- ");
  62.         break;
  63.     default:
  64.         return;
  65.     }
  66.     MPL_LOGI("x%d\n", count);
  67.     return;
  68. }

  69. static void android_orient_cb(unsigned char orientation)
  70. {
  71.         switch (orientation) {
  72.         case ANDROID_ORIENT_PORTRAIT:
  73.         MPL_LOGI("Portrait\n");
  74.         break;
  75.         case ANDROID_ORIENT_LANDSCAPE:
  76.         MPL_LOGI("Landscape\n");
  77.         break;
  78.         case ANDROID_ORIENT_REVERSE_PORTRAIT:
  79.         MPL_LOGI("Reverse Portrait\n");
  80.         break;
  81.         case ANDROID_ORIENT_REVERSE_LANDSCAPE:
  82.         MPL_LOGI("Reverse Landscape\n");
  83.         break;
  84.         default:
  85.                 return;
  86.         }
  87. }


  88. static inline void run_self_test(void)
  89. {
  90.     int result;
  91.     long gyro[3], accel[3];

  92. #if defined (MPU6500) || defined (MPU9250)
  93.     result = mpu_run_6500_self_test(gyro, accel, 0);
  94. #elif defined (MPU6050) || defined (MPU9150)
  95.     result = mpu_run_self_test(gyro, accel);
  96. #endif
  97.     if (result == 0x7) {
  98.         MPL_LOGI("Passed!\n");
  99.         MPL_LOGI("accel: %7.4f %7.4f %7.4f\n",
  100.                     accel[0]/65536.f,
  101.                     accel[1]/65536.f,
  102.                     accel[2]/65536.f);
  103.         MPL_LOGI("gyro: %7.4f %7.4f %7.4f\n",
  104.                     gyro[0]/65536.f,
  105.                     gyro[1]/65536.f,
  106.                     gyro[2]/65536.f);
  107.         /* Test passed. We can trust the gyro data here, so now we need to update calibrated data*/

  108. #ifdef USE_CAL_HW_REGISTERS
  109.         /*
  110.          * This portion of the code uses the HW offset registers that are in the MPUxxxx devices
  111.          * instead of pushing the cal data to the MPL software library
  112.          */
  113.         unsigned char i = 0;

  114.         for(i = 0; i<3; i++) {
  115.                 gyro[i] = (long)(gyro[i] * 32.8f); //convert to +-1000dps
  116.                 accel[i] *= 2048.f; //convert to +-16G
  117.                 accel[i] = accel[i] >> 16;
  118.                 gyro[i] = (long)(gyro[i] >> 16);
  119.         }

  120.         mpu_set_gyro_bias_reg(gyro);

  121. #if defined (MPU6500) || defined (MPU9250)
  122.         mpu_set_accel_bias_6500_reg(accel);
  123. #elif defined (MPU6050) || defined (MPU9150)
  124.         mpu_set_accel_bias_6050_reg(accel);
  125. #endif
  126. #else
  127.         /* Push the calibrated data to the MPL library.
  128.          *
  129.          * MPL expects biases in hardware units << 16, but self test returns
  130.                  * biases in g's << 16.
  131.                  */
  132.             unsigned short accel_sens;
  133.             float gyro_sens;

  134.                 mpu_get_accel_sens(&accel_sens);
  135.                 accel[0] *= accel_sens;
  136.                 accel[1] *= accel_sens;
  137.                 accel[2] *= accel_sens;
  138.                 inv_set_accel_bias(accel, 3);
  139.                 mpu_get_gyro_sens(&gyro_sens);
  140.                 gyro[0] = (long) (gyro[0] * gyro_sens);
  141.                 gyro[1] = (long) (gyro[1] * gyro_sens);
  142.                 gyro[2] = (long) (gyro[2] * gyro_sens);
  143.                 inv_set_gyro_bias(gyro, 3);
  144. #endif
  145.     }
  146.     else {
  147.             if (!(result & 0x1))
  148.                 MPL_LOGE("Gyro failed.\n");
  149.             if (!(result & 0x2))
  150.                 MPL_LOGE("Accel failed.\n");
  151.             if (!(result & 0x4))
  152.                 MPL_LOGE("Compass failed.\n");
  153.      }

  154. }

复制代码



095549ud5y9z5fqfooa9u5.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-26 14:40

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表