Advertisement
Guest User

BROKKING lsm6

a guest
Mar 18th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define DEBUG
  4.  
  5.  
  6. const float RefreshFrequency = 250.00, // CONFIG
  7. PololuRefreshFrequency = 208.00;
  8.  
  9. unsigned short RefreshRate = round((float)1 / RefreshFrequency * 1000),
  10. SensorRefreshRate = round((float)1 / PololuRefreshFrequency * 1000); // frequencies in miiliseconds
  11.  
  12. unsigned long update_timer = 0,
  13. sensor_update = 0,
  14. print_timer = 0;
  15.  
  16. const short CALIBRATION_PROBES = 100;
  17.  
  18. const float GYRO_SENSITIVITY = 0.0175, // mdps/LSB -> dps/LSB
  19. COMPLIMENTARY_FACTOR = 0.7; // < 1
  20.  
  21. long C[6] = {0,0,0,0,0,0};
  22.  
  23. long gx, gy, gz,
  24. ax, ay, az;
  25.  
  26. const float RateDeltaTime = GYRO_SENSITIVITY / RefreshFrequency,
  27. RateDeltaTimeDegress = RateDeltaTime * PI / 180,
  28. GRAVITY = 256.0;
  29.  
  30. bool calibration_finished = false;
  31.  
  32. float g_pitch, g_roll, g_yaw,
  33. angle_pitch, angle_roll,
  34. acc_vector, a_pitch, a_roll;
  35.  
  36. void setup()
  37. {
  38. Wire.begin();
  39. Wire.setClock(400000L);
  40.  
  41. #ifdef DEBUG
  42. Serial.begin(115200);
  43. #endif
  44.  
  45. PCICR |= (1 << PCIE0);
  46. PCMSK0 |= (1 << PCINT4); // PIN 10
  47. PCMSK0 |= (1 << PCINT5); // PIN 11
  48. PCMSK0 |= (1 << PCINT6); // PIN 12
  49. PCMSK0 |= (1 << PCINT7); // PIN 13
  50.  
  51.  
  52. // GA.init();
  53. WriteReg(0x10, 0b01101100); // 208Hz refresh rate, +-8g anti-aliasing 200Hz
  54. WriteReg(0x11, 0b01101100); // 208Hz refresh rate, +- 2k dps, full-scale at 125dps off
  55. WriteReg(0x12, 0b00000100);
  56.  
  57. sensor_update = millis();
  58. for(short i = 0; i < CALIBRATION_PROBES; i++)
  59. {
  60. Read();
  61.  
  62. #ifdef DEBUG
  63. if(i % 50 == 0) Serial.print(".");
  64. #endif
  65.  
  66. C[0] += gx;
  67. C[1] += gy;
  68. C[2] += gz;
  69.  
  70. C[3] += ax;
  71. C[4] += ay;
  72. C[5] += az;
  73.  
  74. delay(5);
  75. }
  76.  
  77. for(byte i = 0; i < 6; i++)
  78. C[i] /= CALIBRATION_PROBES;
  79.  
  80. C[5] -= GRAVITY;
  81.  
  82. calibration_finished = true;
  83.  
  84. update_timer = print_timer = millis();
  85.  
  86. #ifdef DEBUG
  87. Serial.print("RefreshRate: "); Serial.println(RefreshRate);
  88. #endif
  89.  
  90. }
  91.  
  92. void WriteReg(uint8_t reg, uint8_t val)
  93. {
  94. Wire.beginTransmission(0b1101011);
  95. Wire.write(reg);
  96. Wire.write(val);
  97. Wire.endTransmission();
  98. }
  99.  
  100.  
  101. bool Read()
  102. {
  103. unsigned long current_timer = millis();
  104.  
  105. if(current_timer - sensor_update < SensorRefreshRate)
  106. return false;
  107.  
  108. sensor_update = current_timer;
  109.  
  110. Wire.beginTransmission(0b1101011);
  111. Wire.write(0x28); // OUTX_L_XL
  112. Wire.endTransmission();
  113. Wire.requestFrom(0b1101011, (uint8_t) 6);
  114.  
  115. uint8_t axl = Wire.read(), axh = Wire.read(),
  116. ayl = Wire.read(), ayh = Wire.read(),
  117. azl = Wire.read(), azh = Wire.read();
  118.  
  119. ax = (long)(axh << 8 | axl) >> 4; // mg
  120. ay = (long)(ayh << 8 | ayl) >> 4; // mg
  121. az = (long)(azh << 8 | azl) >> 4; // mg
  122.  
  123. if(calibration_finished)
  124. {
  125. ax -= C[3];
  126. ay -= C[4];
  127. az -= C[5];
  128. }
  129.  
  130. acc_vector = sqrt(ax * ax + ay * ay + az * az);
  131. // acc_vector /= GRAVITY;
  132.  
  133. Wire.beginTransmission(0b1101011);
  134. Wire.write(0x22); // OUTX_L_G
  135. Wire.endTransmission();
  136. Wire.requestFrom(0b1101011, (uint8_t) 6);
  137.  
  138. uint8_t gxl = Wire.read(), gxh = Wire.read(),
  139. gyl = Wire.read(), gyh = Wire.read(),
  140. gzl = Wire.read(), gzh = Wire.read();
  141.  
  142. gx = (long)(gxh << 8 | gxl);
  143. gy = (long)(gyh << 8 | gyl);
  144. gz = (long)(gzh << 8 | gzl);
  145.  
  146. if(calibration_finished)
  147. {
  148. gx -= C[0];
  149. gy -= C[1];
  150. gz -= C[2];
  151. }
  152.  
  153. #ifdef BROKKING_ALGO
  154. g_pitch = (g_pitch * COMPLIMENTARY_FACTOR) + ((float)gx * GYRO_SENSITIVITY) * (1 - COMPLIMENTARY_FACTOR);
  155. g_roll = (g_roll * COMPLIMENTARY_FACTOR) + ((float)gy * GYRO_SENSITIVITY) * (1 - COMPLIMENTARY_FACTOR);
  156. g_yaw = (g_yaw * COMPLIMENTARY_FACTOR) + ((float)gz * GYRO_SENSITIVITY) * (1 - COMPLIMENTARY_FACTOR);
  157. #endif
  158.  
  159. return true;
  160. }
  161.  
  162. void loop()
  163. {
  164.  
  165. unsigned long current_clock = millis();
  166. Read();
  167.  
  168. angle_pitch += (float)gx * RateDeltaTime;
  169. angle_roll += (float)gy * RateDeltaTime;
  170.  
  171. angle_pitch -= angle_roll * sin(gz * RateDeltaTimeDegress);
  172. angle_roll += angle_pitch * sin(gz * RateDeltaTimeDegress);
  173.  
  174. if(abs(ay) < acc_vector)
  175. a_pitch = asin((float)ay / acc_vector) * 57.296;
  176.  
  177. if(abs(ax) < acc_vector)
  178. a_roll = asin((float)ax / acc_vector) * -57.296;
  179.  
  180. angle_pitch = angle_pitch * 0.9996 + a_pitch * 0.0004;
  181. angle_roll = angle_roll * 0.9996 + a_roll * 0.0004;
  182. // current_clock = micros();
  183.  
  184. #ifdef DEBUG
  185. //if(current_clock - print_timer >= 500)
  186. {
  187. Serial.print("ACC X:" ); Serial.print(ax); Serial.print(" Y:" ); Serial.print(ay); Serial.print(" Z:" ); Serial.println(az);
  188. Serial.print("GYRO X:" ); Serial.print(gx); Serial.print(" Y:" ); Serial.print(gy); Serial.print(" Z:" ); Serial.println(gz);
  189. // Serial.print("PITCH: "); Serial.print(round(angle_pitch)); Serial.print(" ROLL: "); Serial.println(round(angle_roll));
  190. print_timer = current_clock;
  191. }
  192. #endif
  193.  
  194. while(millis() - update_timer < 4);
  195. update_timer = millis();
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement