Advertisement
Guest User

New Code

a guest
Jul 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.68 KB | None | 0 0
  1. #include "I2Cdev.h"
  2. #include "MPU6050.h"
  3. #include <Wire.h>
  4. #include <SD.h>
  5. int pinCS = 10;
  6. File myFile ;
  7. // FIND MEMORY SPACE ! ! !
  8. /////////////////////////////////// CONFIGURATION /////////////////////////////
  9. //Change this 3 variables if you want to fine tune the skecth to your needs.
  10. int buffersize = 1000; //Amount of readings used to average, make it higher to get more precision but sketch will be slower (default:1000)
  11. int acel_deadzone = 8; //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge (default:8)
  12. int giro_deadzone = 1; //Giro error allowed, make it lower to get more precision, but sketch may not converge (default:1)
  13.  
  14. // default I2C address is 0x68
  15. // specific I2C addresses may be passed as a parameter here
  16. // AD0 low = 0x68 (default for InvenSense evaluation board)
  17. // AD0 high = 0x69
  18. //MPU6050 accelgyro;
  19. MPU6050 accelgyro(0x68); // <-- use for AD0 high int cnt = 0; // for counting up to 100 , then reading the SD card.
  20. int cnt=0;
  21. int cnt2 =0; // for counting the number of calibration steps.
  22. int16_t ax, ay, az, gx, gy, gz;
  23.  
  24. int mean_ax, mean_ay, mean_az, mean_gx, mean_gy, mean_gz, state = 0;
  25. int ax_offset, ay_offset, az_offset, gx_offset, gy_offset, gz_offset;
  26. int ready = 0;
  27.  
  28. long accelX, accelY, accelZ;
  29. float gForceX, gForceY, gForceZ;
  30.  
  31. long gyroX, gyroY, gyroZ;
  32. float rotX, rotY, rotZ;
  33.  
  34. void setup() {
  35.  
  36. pinMode(pinCS, OUTPUT);
  37. Serial.begin(9600);
  38. if (SD.begin())
  39. {
  40. Serial.println("SD card is ready to use.");
  41. } else
  42. {
  43. Serial.println("SD card initialization failed");
  44. return;
  45. }
  46.  
  47.  
  48. // join I2C bus (I2Cdev library doesn't do this automatically)
  49. Wire.begin();
  50.  
  51. // start message
  52. Serial.println("Starting 5 seconds timer to give time for the new program to upload: ");
  53. delay(5000);
  54. Serial.println("\nMPU6050 Calibration Sketch");
  55. delay(2000);
  56. Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  57. delay(3000);
  58.  
  59. // verify connection
  60. Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  61. delay(1000);
  62.  
  63. // reset offsets
  64. accelgyro.setXAccelOffset(0);
  65. accelgyro.setYAccelOffset(0);
  66. accelgyro.setZAccelOffset(0);
  67. accelgyro.setXGyroOffset(0);
  68. accelgyro.setYGyroOffset(0);
  69. accelgyro.setZGyroOffset(0);
  70.  
  71. setupMPU();
  72.  
  73. if (state == 0) {
  74. Serial.println(F("\nReading sensors for first time..."));
  75. meansensors();// why is this function called 2 times ? TODO ! ! ! <-----------------------------------------------------------------------------////////////////////////////////
  76. state++;
  77. delay(1000);
  78. }
  79.  
  80. if (state == 1) {
  81. Serial.println(F("\nCalculating offsets..."));
  82. calibration();
  83. state++;
  84. delay(1000);
  85. }
  86.  
  87. if (state == 2) {
  88. meansensors();
  89. Serial.println(F("\nFINISHED!"));
  90. Serial.println(F("Number of calibration steps: \t ")); Serial.print(cnt2);
  91. Serial.print(F("\nSensor readings with offsets:\t"));
  92. Serial.print(mean_ax);
  93. Serial.print("\t");
  94. Serial.print(mean_ay);
  95. Serial.print("\t");
  96. Serial.print(mean_az);
  97. Serial.print("\t");
  98. Serial.print(mean_gx);
  99. Serial.print("\t");
  100. Serial.print(mean_gy);
  101. Serial.print("\t");
  102. Serial.print(mean_gz);
  103. Serial.print(F("Your offsets:\t"));
  104. Serial.print(ax_offset);
  105. Serial.print("\t");
  106. Serial.print(ay_offset);
  107. Serial.print("\t");
  108. Serial.print(az_offset);
  109. Serial.print("\t");
  110. Serial.print(gx_offset);
  111. Serial.print("\t");
  112. Serial.print(gy_offset);
  113. Serial.print("\t");
  114. Serial.println(gz_offset);
  115. Serial.println(F("\nData is printed as: acelX acelY acelZ giroX giroY giroZ"));
  116. Serial.println(F("Check that your sensor readings are close to 0 0 16384 0 0 0"));
  117. Serial.println(F(" --------------------------------- "));
  118.  
  119. myFile = SD.open("test.txt", FILE_WRITE);
  120. //Serial.println(F("If this value is 0, you've fucked something up :D")); always gives 0 ?
  121. Serial.print(myFile);
  122. if (myFile) {
  123. Serial.println("Yaaay, we are writing to the file :D");
  124. myFile.print("The offsets are: \n ");
  125. myFile.print(ax_offset); myFile.print("\t");
  126.  
  127. myFile.println(ay_offset); myFile.print("\t");
  128.  
  129. myFile.println(az_offset); myFile.print("\t");
  130.  
  131.  
  132. myFile.close(); // close the file
  133. }
  134. else {
  135. Serial.println(F("error opening test.txt"));
  136. }
  137. delay(1000);
  138.  
  139. }
  140.  
  141. }
  142.  
  143.  
  144. void loop() {
  145. // the offsets were in the loop func
  146. // myFile = SD.open("test.txt", FILE_WRITE);
  147. // //Serial.println(F("If this value is 0, you've fucked something up :D")); always gives 0 ?
  148. // Serial.print(myFile);
  149. // if (myFile) {
  150. // Serial.println("Yaaay, we are writing to the file :D");
  151. // myFile.println(" are ");
  152. // myFile.print(ax_offset); myFile.print("\t");
  153. //
  154. // myFile.println(ay_offset); myFile.print("\t");
  155. //
  156. // myFile.println(az_offset); myFile.print("\t");
  157. //
  158. //
  159. // myFile.close(); // close the file
  160. // }
  161. // if the file didn't open, print an error:
  162. // else {
  163. // Serial.println(F("error opening test.txt"));
  164. // }
  165. // delay(1000);
  166.  
  167. recordAccelRegisters();
  168. recordGyroRegisters();
  169. printData();
  170. Serial.println(F("After this sentence, it should start writing the Gforces on the SD card, so yeah, we did something: \t"));
  171. myFile = SD.open("test.txt", FILE_WRITE);
  172. {
  173. // new line , today, 17 . 07
  174. myFile.print(gForceX); myFile.print("\t"); // change to printLN ? 17.07
  175.  
  176. myFile.print(gForceY); myFile.print("\t");
  177.  
  178. myFile.print(gForceZ); myFile.print("\t");
  179.  
  180.  
  181. cnt++; // increase the counter after writing X times to the file
  182. myFile.close();
  183. }
  184.  
  185. delay(1000);
  186.  
  187.  
  188. Serial.println(F("This is just for checking the counter status ___ "));
  189. Serial.print(cnt);
  190. if (cnt == 10) // increase the counter number for larger data
  191. {
  192. myFile = SD.open("test.txt");
  193. while (myFile.available())
  194. {
  195. Serial.write(myFile.read());
  196. }
  197.  
  198. Serial.println(F("File was closed, exiting"));
  199. myFile.close();
  200.  
  201. delay(50000);
  202. }
  203. }
  204.  
  205.  
  206. void meansensors() {
  207. long i = 0, buff_ax = 0, buff_ay = 0, buff_az = 0, buff_gx = 0, buff_gy = 0, buff_gz = 0;
  208.  
  209. while (i < (buffersize + 101)) {
  210. // read raw accel/gyro measurements from device
  211. accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  212.  
  213. if (i > 100 && i <= (buffersize + 100)) { //First 100 measures are discarded
  214. buff_ax = buff_ax + ax;
  215. buff_ay = buff_ay + ay;
  216. buff_az = buff_az + az;
  217. buff_gx = buff_gx + gx;
  218. buff_gy = buff_gy + gy;
  219. buff_gz = buff_gz + gz;
  220. }
  221. if (i == (buffersize + 100)) {
  222. mean_ax = buff_ax / buffersize;
  223. mean_ay = buff_ay / buffersize;
  224. mean_az = buff_az / buffersize;
  225. mean_gx = buff_gx / buffersize;
  226. mean_gy = buff_gy / buffersize;
  227. mean_gz = buff_gz / buffersize;
  228. }
  229. i++;
  230. delay(2); //Needed so we don't get repeated measures
  231. }
  232. }
  233.  
  234. void calibration() {
  235. ax_offset = -mean_ax / 8;
  236. ay_offset = -mean_ay / 8;
  237. az_offset = (8192 - mean_az) / 8;
  238.  
  239. gx_offset = -mean_gx / 4;
  240. gy_offset = -mean_gy / 4;
  241. gz_offset = -mean_gz / 4;
  242. while (ready < 6) {
  243. int ready = 0;
  244. accelgyro.setXAccelOffset(ax_offset);
  245. accelgyro.setYAccelOffset(ay_offset);
  246. accelgyro.setZAccelOffset(az_offset);
  247.  
  248. accelgyro.setXGyroOffset(gx_offset);
  249. accelgyro.setYGyroOffset(gy_offset);
  250. accelgyro.setZGyroOffset(gz_offset);
  251.  
  252. meansensors();
  253. Serial.println("...");
  254. cnt2++;
  255.  
  256. if (abs(mean_ax) <= acel_deadzone) ready++;
  257. else ax_offset = ax_offset - mean_ax / acel_deadzone;
  258.  
  259. if (abs(mean_ay) <= acel_deadzone) ready++;
  260. else ay_offset = ay_offset - mean_ay / acel_deadzone;
  261.  
  262. if (abs(8192 - mean_az) <= acel_deadzone) ready++;
  263. else az_offset = az_offset + (8192 - mean_az) / acel_deadzone;
  264.  
  265. if (abs(mean_gx) <= giro_deadzone) ready++;
  266. else gx_offset = gx_offset - mean_gx / (giro_deadzone + 1);
  267.  
  268. if (abs(mean_gy) <= giro_deadzone) ready++;
  269. else gy_offset = gy_offset - mean_gy / (giro_deadzone + 1);
  270.  
  271. if (abs(mean_gz) <= giro_deadzone) ready++;
  272. else gz_offset = gz_offset - mean_gz / (giro_deadzone + 1);
  273.  
  274. if (ready == 6) break;
  275.  
  276. }
  277. }
  278.  
  279. void setupMPU() {
  280. Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2), we use the LOW , a.k.a. ADO is connected to GND .
  281. Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
  282. Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
  283. Wire.endTransmission();
  284. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  285. Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
  286. Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
  287. Wire.endTransmission();
  288. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  289. Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
  290. Wire.write(0b00001000); //Setting the accel to +/- 4g
  291. Wire.endTransmission();
  292. }
  293.  
  294. void recordAccelRegisters() {
  295. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  296. Wire.write(0x3B); //Starting register for Accel Readings
  297. Wire.endTransmission();
  298. Wire.requestFrom(0b1101000, 6); //Request Accel Registers (3B - 40)
  299. while (Wire.available() < 6);
  300. accelX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
  301. accelY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
  302. accelZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
  303. processAccelData();
  304. }
  305.  
  306.  
  307. void processAccelData() {
  308. gForceX = accelX / 8192.0;
  309. gForceY = accelY / 8192.0;
  310. gForceZ = accelZ / 8192.0;
  311. }
  312.  
  313. void recordGyroRegisters() {
  314. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  315. Wire.write(0x43); //Starting register for Gyro Readings
  316. Wire.endTransmission();
  317. Wire.requestFrom(0b1101000, 6); //Request Gyro Registers (43 - 48)
  318. while (Wire.available() < 6);
  319. gyroX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
  320. gyroY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
  321. gyroZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
  322. processGyroData();
  323. }
  324.  
  325. void processGyroData() {
  326. rotX = gyroX / 131.0;
  327. rotY = gyroY / 131.0;
  328. rotZ = gyroZ / 131.0;
  329. }
  330.  
  331. void printData() {
  332.  
  333. Serial.println(F("Gyro (deg)"));
  334. Serial.println(F(" X="));
  335. Serial.print(rotX);
  336. // if (rotX >= 100) { // <<<<<<<<<<<<<<--------- DELETE THIS FUCKERS, WE DON'T HAVE MEMORY !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!, NVM FOUND IT :D
  337. // blinkLED();
  338. // }
  339. // if (rotX <= -90) {
  340. // blinkLED();
  341. // }
  342. // Serial.print(" Y=");
  343. // Serial.print(rotY);
  344. // if (rotY >= 90) {
  345. // blinkLED();
  346. // }
  347. // if (rotY <= -90) {
  348. // blinkLED();
  349. // }
  350. Serial.println(F(" Z="));
  351. Serial.print(rotZ);
  352. Serial.println(F(" Accel (g)"));
  353. Serial.print(F(" X="));
  354. Serial.print(gForceX);
  355. // if (gForceX >= 0.75) {
  356. // blinkLED();
  357. // }
  358. // if (gForceX <= -0.75) {
  359. // blinkLED();
  360. // }
  361. Serial.print(F(" Y="));
  362. Serial.print(gForceY);
  363. // if (gForceY >= 0.75) {
  364. // blinkLED();
  365. // }s
  366. // if (gForceY <= -0.75) {
  367. // blinkLED();
  368. // }
  369. // Serial.print(" Z=");
  370. // Serial.println(gForceZ);
  371. // if (gForceZ >= 3.5) {
  372. // blinkLED();
  373. // }
  374. //}
  375. //void blinkLED() {
  376. // digitalWrite(LED_BUILTIN, HIGH);
  377. // delay(5000);
  378. // digitalWrite(LED_BUILTIN, LOW);
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement