Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3.  
  4. const int chipSelect = 4;
  5.  
  6. #include "I2Cdev.h"
  7. #include "MPU6050.h"
  8. #include "Wire.h"
  9.  
  10.  
  11.  
  12.  
  13.  
  14. #define LED_R 2
  15. #define LED_G 5
  16. #define LED_B 6
  17.  
  18. const int MPU=0x68;
  19.  
  20. float AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
  21. /////////////////////////////////// CONFIGURATION /////////////////////////////
  22.  
  23. int buffersize=1000;
  24. int acel_deadzone=8;
  25. int giro_deadzone=1;
  26.  
  27.  
  28. MPU6050 accelgyro(0x68);
  29.  
  30. int16_t ax, ay, az,gx, gy, gz,x,y,z;
  31.  
  32. int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
  33. int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;
  34. const long interval = 1000;
  35. /////////////////////////////////// SETUP ////////////////////////////////////
  36.  
  37. int ledState = LOW;
  38. unsigned long previousMillis = 0;
  39. int pushButton = 8;
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. void setup() {
  47.  
  48.  
  49.  
  50.  
  51. pinMode(LED_R, OUTPUT);
  52. pinMode(LED_G, OUTPUT);
  53. pinMode(LED_B, OUTPUT);
  54. pinMode(pushButton, INPUT);
  55.  
  56.  
  57. Wire.begin();
  58. Wire.beginTransmission(MPU);
  59. Wire.write(0x69);
  60. Wire.write(0);
  61. Wire.endTransmission(true);
  62.  
  63.  
  64.  
  65. TWBR = 24;
  66.  
  67.  
  68.  
  69. Serial.begin(9600);
  70.  
  71. while (!Serial) {
  72. ;
  73. }
  74. Serial.print(F("Initializing SD card..."));
  75.  
  76.  
  77. if (!SD.begin(chipSelect)) {
  78. Serial.println(F("Card failed, or not present"));
  79.  
  80. return;
  81. }
  82. Serial.println(F("card initialized."));
  83.  
  84.  
  85. accelgyro.initialize();
  86.  
  87. Serial.println(F("\nMPU6050 Calibration Sketch"));
  88. delay(2000);
  89. 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");
  90. delay(3000);
  91.  
  92. Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed" );
  93. delay(1000);
  94. if (!accelgyro.testConnection())
  95. digitalWrite(LED_R,HIGH);
  96. if (accelgyro.testConnection())
  97. digitalWrite(LED_G,HIGH);
  98.  
  99.  
  100. accelgyro.setXAccelOffset(0);
  101. accelgyro.setYAccelOffset(0);
  102. accelgyro.setZAccelOffset(0);
  103. accelgyro.setXGyroOffset(0);
  104. accelgyro.setYGyroOffset(0);
  105. accelgyro.setZGyroOffset(0);
  106.  
  107. if (state==0){
  108. Serial.println(F("\nReading sensors for first time..."));
  109. meansensors();
  110. state++;
  111. delay(1000);
  112. }
  113.  
  114. if (state==1) {
  115. Serial.println(F("\nCalculating offsets..."));
  116. calibration();
  117. state++;
  118. delay(1000);
  119. }
  120.  
  121. if (state==2) {
  122. meansensors();
  123. Serial.println(F("\nFINISHED!"));
  124.  
  125. }
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134. /////////////////////////////////// LOOP ////////////////////////////////////
  135. void loop() {
  136.  
  137.  
  138.  
  139. digitalWrite(LED_B, HIGH);
  140. int buttonState = digitalRead(pushButton);
  141.  
  142. if( buttonState == HIGH){
  143. digitalWrite(LED_B,LOW);
  144.  
  145. unsigned long currentMillis = millis();
  146. if (currentMillis - previousMillis >= interval) {
  147.  
  148. previousMillis = currentMillis;
  149.  
  150.  
  151. if (ledState == LOW) {
  152. ledState = HIGH;
  153. } else {
  154. ledState = LOW;
  155. }
  156.  
  157.  
  158. digitalWrite(LED_G, ledState);
  159. }
  160.  
  161. Wire.beginTransmission(MPU);
  162. Wire.write(0x3B);
  163. Wire.endTransmission(false);
  164. Wire.requestFrom(MPU,14,true);
  165. AcX=Wire.read()<<8|Wire.read();
  166. AcY=Wire.read()<<8|Wire.read();
  167. AcZ=Wire.read()<<8|Wire.read();
  168. Tmp=Wire.read()<<8|Wire.read();
  169. GyX=Wire.read()<<8|Wire.read();
  170. GyY=Wire.read()<<8|Wire.read();
  171. GyZ=Wire.read()<<8|Wire.read();
  172.  
  173. Serial.print(F("sta savlando"));
  174. File dataFile = SD.open("datalog.txt", FILE_WRITE);
  175.  
  176. if (dataFile) {
  177. dataFile.print(AcX/mean_az);
  178. dataFile.print(F(" "));
  179. dataFile.print(AcY/mean_az);
  180. dataFile.print(F(" "));
  181. dataFile.print(AcZ/mean_az);
  182. dataFile.print(F(" "));
  183. dataFile.println(Tmp/340.00+36.53);
  184. dataFile.close();
  185.  
  186.  
  187.  
  188. }
  189.  
  190. else {
  191. Serial.println(F("error opening datalog.txt"));
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. digitalWrite(LED_G,LOW);
  201.  
  202.  
  203.  
  204. }
  205. }
  206.  
  207. /////////////////////////////////// FUNCTIONS ////////////////////////////////////
  208. void meansensors(){
  209. long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;
  210.  
  211. while (i<(buffersize+101)){
  212.  
  213. accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  214.  
  215. if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
  216. buff_ax=buff_ax+ax;
  217. buff_ay=buff_ay+ay;
  218. buff_az=buff_az+az;
  219. buff_gx=buff_gx+gx;
  220. buff_gy=buff_gy+gy;
  221. buff_gz=buff_gz+gz;
  222. }
  223. if (i==(buffersize+100)){
  224. mean_ax=buff_ax/buffersize;
  225. mean_ay=buff_ay/buffersize;
  226. mean_az=buff_az/buffersize;
  227. mean_gx=buff_gx/buffersize;
  228. mean_gy=buff_gy/buffersize;
  229. mean_gz=buff_gz/buffersize;
  230. }
  231. i++;
  232. delay(2);
  233. }
  234. }
  235.  
  236. void calibration(){
  237. ax_offset=-mean_ax/8;
  238. ay_offset=-mean_ay/8;
  239. az_offset=(16384-mean_az)/8;
  240.  
  241. gx_offset=-mean_gx/4;
  242. gy_offset=-mean_gy/4;
  243. gz_offset=-mean_gz/4;
  244. while (1){
  245. int ready=0;
  246. accelgyro.setXAccelOffset(ax_offset);
  247. accelgyro.setYAccelOffset(ay_offset);
  248. accelgyro.setZAccelOffset(az_offset);
  249.  
  250. accelgyro.setXGyroOffset(gx_offset);
  251. accelgyro.setYGyroOffset(gy_offset);
  252. accelgyro.setZGyroOffset(gz_offset);
  253.  
  254. meansensors();
  255. Serial.println(F("..."));
  256. digitalWrite(LED_G,LOW);
  257. unsigned long currentMillis = millis();
  258. if (currentMillis - previousMillis >= interval) {
  259.  
  260. previousMillis = currentMillis;
  261.  
  262.  
  263. if (ledState == LOW) {
  264. ledState = HIGH;
  265. } else {
  266. ledState = LOW;
  267. }
  268.  
  269.  
  270. digitalWrite(LED_B, ledState);
  271. }
  272. if (abs(mean_ax)<=acel_deadzone) ready++;
  273. else ax_offset=ax_offset-mean_ax/acel_deadzone;
  274.  
  275. if (abs(mean_ay)<=acel_deadzone) ready++;
  276. else ay_offset=ay_offset-mean_ay/acel_deadzone;
  277.  
  278. if (abs(16384-mean_az)<=acel_deadzone) ready++;
  279. else az_offset=az_offset+(16384-mean_az)/acel_deadzone;
  280.  
  281. if (abs(mean_gx)<=giro_deadzone) ready++;
  282. else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);
  283.  
  284. if (abs(mean_gy)<=giro_deadzone) ready++;
  285. else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);
  286.  
  287. if (abs(mean_gz)<=giro_deadzone) ready++;
  288. else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);
  289.  
  290. if (ready==6) break;
  291. }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement