bexus_mpu.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "bexus_mpu.h"
  2. int32_t *rawTemp;
  3. double accelScale = 16384;
  4. double gyroScale = 131.072;
  5. double *tempMpu;
  6. struct vector_Uint *rawAccel, *rawGyro;
  7. struct vector *hMeas;
  8. struct vector *gSens;
  9. void Mpu_Init() {
  10. if(HAL_I2C_IsDeviceReady(&hi2c2,MPU_ADDR,3,20)==HAL_OK){
  11. //HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,1);
  12. }
  13. //Sleepmode Beenden
  14. ConfigMpu(0x6B, 0x01);
  15. //Konfigurieren
  16. // ConfigMpu(0x1A, 0x05);//Low-Pass-Filter Config
  17. // ConfigMpu(0x1B, 0x00<<3);//Gyros Config
  18. // ConfigMpu(0x1C, 0x00<<3);//Achsel Config
  19. ConfigMpu(0x1C, 0x00 << 3);//Achsel Config 2G
  20. ConfigMpu(0x1B, 0x00 << 3);//Gyros Config 250°/s
  21. ConfigMpu(0x1A, 0x04);//Low-Pass-Filter Config 20Hz LowPass
  22. }
  23. int Bexus_Mpu_Read() {
  24. rawAccel = &Dataset_Master[buff_sensor_data_index].accel_raw;
  25. rawGyro = &Dataset_Master[buff_sensor_data_index].gyro_raw;
  26. rawTemp = &Dataset_Master[buff_sensor_data_index].temperature_mpu_raw;
  27. hMeas = &Dataset_Master[buff_sensor_data_index].accel;
  28. gSens = &Dataset_Master[buff_sensor_data_index].gyro;
  29. tempMpu = &Dataset_Master[buff_sensor_data_index].temperature_mpu;
  30. //Auslesen
  31. //Beschleunigungssensor
  32. rawAccel->x = recvIntFromMpu(MPU_ADDR_ACCEL);
  33. rawAccel->y = recvIntFromMpu(MPU_ADDR_ACCEL+2);
  34. rawAccel->z = recvIntFromMpu(MPU_ADDR_ACCEL+4);
  35. //Gyrosensor
  36. rawGyro->x = recvIntFromMpu(MPU_ADDR_GYRO);
  37. rawGyro->y = recvIntFromMpu(MPU_ADDR_GYRO+2);
  38. rawGyro->z = recvIntFromMpu(MPU_ADDR_GYRO+4);
  39. //Temperatur
  40. *rawTemp=recvIntFromMpu(0x41);
  41. //*tempMpu = ((*rawTemp-0xFFFF0000)/340+36.53)/10;
  42. *tempMpu = (double)((*rawTemp*1.)/340+36.53);
  43. //berechnen
  44. /*
  45. accel->x = RAD_TO_DEG * atan(rawAccel->x / sqrt(pow(rawAccel->y, 2) + pow(rawAccel->z, 2)));
  46. accel->y = RAD_TO_DEG * atan(rawAccel->y / sqrt(pow(rawAccel->x, 2) + pow(rawAccel->z, 2)));
  47. accel->z = RAD_TO_DEG * atan(sqrt(pow(rawAccel->y,2) + pow(rawAccel->x,2)) / rawAccel->z);
  48. */
  49. #ifdef BOARD_HAS_ETHERNET //vertical
  50. hMeas->x = - rawAccel->x / accelScale + 0.0326;
  51. hMeas->y = - rawAccel->z / accelScale - 0.13;
  52. hMeas->z = - rawAccel->y / accelScale + 0.;
  53. gSens->x = - rawGyro->x/gyroScale - 3.0823; // °/s
  54. gSens->y = - rawGyro->z/gyroScale + 0.0824;
  55. gSens->z = - rawGyro->y/gyroScale + 0.336;
  56. #else //horizontal
  57. hMeas->x = - rawAccel->x / accelScale + 0.03;
  58. hMeas->y = - rawAccel->y / accelScale - 0.0;
  59. hMeas->z = + rawAccel->z / accelScale + 0.126;
  60. gSens->x = - rawGyro->x/gyroScale - 3.014; // °/s
  61. gSens->y = - rawGyro->y/gyroScale + 0.633;
  62. gSens->z = + rawGyro->z/gyroScale - 0.496;
  63. #endif
  64. return HAL_OK;
  65. }
  66. int sendByteToMpu(uint8_t* data, uint8_t len) {
  67. if(HAL_OK != HAL_I2C_Master_Transmit(&hi2c2,MPU_ADDR,data,len,25)) return HAL_ERROR;
  68. return HAL_OK;
  69. }
  70. void ConfigMpu(uint8_t reg, uint8_t data) {
  71. uint8_t butt_temp[2]={reg,data};
  72. sendByteToMpu(butt_temp, 2);
  73. //sendByteToMpu(&data, 1);
  74. }
  75. uint8_t recvByteFromMpu(uint8_t reg) {
  76. uint8_t buf[1];
  77. if (HAL_OK ==sendByteToMpu(&reg, 1))
  78. HAL_I2C_Master_Receive(&hi2c2,MPU_ADDR,buf,1,20);
  79. return buf[0];
  80. }
  81. int16_t recvIntFromMpu(uint8_t reg) {
  82. uint8_t buf[2];
  83. if (HAL_OK ==sendByteToMpu(&reg, 1))
  84. HAL_I2C_Master_Receive(&hi2c2,MPU_ADDR,&buf[0],1,25);
  85. reg++;
  86. if (HAL_OK ==sendByteToMpu(&reg, 1))
  87. HAL_I2C_Master_Receive(&hi2c2,MPU_ADDR,&buf[1],1,25);
  88. return (int16_t)(buf[1] | (buf[0] << 8));
  89. }