Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.04 KB | None | 0 0
  1. //TO-DO
  2.  
  3. #include <SdFat.h>
  4. #include "I2Cdev.h"
  5. #include "MPU6050_6Axis_MotionApps20.h"
  6. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  7.     #include "Wire.h"
  8. #endif
  9.  
  10. MPU6050 mpu;
  11.  
  12. #define OUTPUT_READABLE_WORLDACCEL
  13.  
  14. // MPU control/status vars
  15. bool dmpReady = false;  // set true if DMP init was successful
  16. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  17. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  18. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  19. uint16_t fifoCount;     // count of all bytes currently in FIFO
  20. uint8_t fifoBuffer[64]; // FIFO storage buffer
  21.  
  22. // orientation/motion vars
  23. Quaternion q;           // [w, x, y, z]         quaternion container
  24. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  25. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  26. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  27. VectorFloat gravity;    // [x, y, z]            gravity vector
  28.  
  29. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  30.  
  31. SdFat sd;
  32. SdFile myFile;
  33.  
  34. const int chipSelect = 3;
  35.  
  36. //const int ledRecordPin = 7;
  37. const int  redLed= 7;
  38. const int greenLed = 5;
  39. const int blueLed = 6;
  40.  
  41. int startButtonState = 0;
  42. const int startButtonPin = 8;
  43. signed short int isRecording = 1; // if odd, then it is not recording; if even it is.
  44. boolean isStartButtontHigh = false; // to prevent repeated HIGH detecttion for the button pin
  45. int accelX;
  46. int accelY;
  47. int accelZ;
  48. char data[30]; //MPU data to be written to SD card
  49.  
  50. char dataTime[60]; // A separator for millis() data
  51. unsigned long ellapsedTime; //Contais millis() data
  52. signed short int ledTime = 0; //Contais an increment for LED
  53. signed short int ledInterval = 500; //How much time the red led will be HIGH
  54.  
  55. boolean fifoOverflow = false; //Red light.
  56. boolean canRecord = false; //Blue light. Timer will confirm if it is ok to start recording
  57. //isRecording is green light
  58.  
  59. void setup() {
  60.   Serial.begin(115200);
  61.   pinMode(10,OUTPUT); //SPI requirements
  62.   digitalWrite(10,HIGH); // Idem
  63.   pinMode(blueLed,OUTPUT);  
  64.   pinMode(greenLed,OUTPUT);  
  65.   pinMode(redLed,OUTPUT);
  66.   pinMode(startButtonPin,INPUT);
  67.   ledControl(); //Warning users they should wait, as data will not be recorded
  68.  
  69.   #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  70.       Wire.begin();
  71.       TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Comment this line if having compilation difficulties with TWBR.
  72.   #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  73.       Fastwire::setup(100, true);
  74.   #endif
  75.  
  76.   mpu.initialize();
  77.   mpu.testConnection();
  78.  
  79.   devStatus = mpu.dmpInitialize();
  80.  
  81.   // supply your own gyro offsets here, scaled for min sensitivity
  82.   mpu.setXGyroOffset(93);
  83.   mpu.setYGyroOffset(-37);
  84.   mpu.setZGyroOffset(22);
  85.  
  86.   mpu.setXAccelOffset(-4654);
  87.   mpu.setYAccelOffset(1446);
  88.   mpu.setZAccelOffset(1389);
  89.  
  90.   if (devStatus == 0) {
  91.       mpu.setDMPEnabled(true);
  92.  
  93.       attachInterrupt(0, dmpDataReady, RISING);
  94.       mpuIntStatus = mpu.getIntStatus();
  95.  
  96.       dmpReady = true;
  97.  
  98.       packetSize = mpu.dmpGetFIFOPacketSize();
  99.   } else {
  100.       // ERROR!
  101.       // 1 = initial memory load failed
  102.       // 2 = DMP configuration updates failed
  103.       // (if it's going to break, usually the code will be 1)
  104.       Serial.print(F("DMP Initialization failed (code "));
  105.       Serial.print(devStatus);
  106.       Serial.println(F(")"));
  107.   }
  108. }
  109.  
  110. void loop() {
  111.     ellapsedTime = millis();
  112.     ledTime++; //Start incrementing ledTime
  113.    
  114.     if(ellapsedTime > 5000) { //Wait until 60 seconds for MPU data stabilize
  115.       canRecord=true; //Now data can be recorded  
  116.       ledControl();    
  117.     }
  118.  
  119.     if( ledTime > ledInterval){
  120.       fifoOverflow = false;
  121.       ledControl();    
  122.     }
  123.  
  124.     //MPU CODE STARTS HERE
  125.     if (!dmpReady) return;
  126.  
  127.     while (!mpuInterrupt && fifoCount < packetSize) {} //////Muitas vezes programa pode travar aqui
  128.  
  129.     mpuInterrupt = false;
  130.     mpuIntStatus = mpu.getIntStatus();
  131.  
  132.     fifoCount = mpu.getFIFOCount();
  133.  
  134.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  135.         mpu.resetFIFO();
  136.        
  137.         fifoOverflow = true;
  138.         ledTime=0;
  139.         ledControl();
  140.         sprintf(data,"Overflow: %lu",ellapsedTime);
  141.         writteToFile(isRecording, data);
  142.        
  143.         Serial.println("OVERFLOW");
  144.        
  145.  
  146.     } else if (mpuIntStatus & 0x02) {
  147.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  148.  
  149.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  150.        
  151.         fifoCount -= packetSize;
  152.  
  153.         #ifdef OUTPUT_READABLE_WORLDACCEL
  154.             mpu.dmpGetQuaternion(&q, fifoBuffer);
  155.             mpu.dmpGetAccel(&aa, fifoBuffer);
  156.             mpu.dmpGetGravity(&gravity, &q);
  157.             mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
  158.             mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
  159.         #endif
  160.  
  161.       if( canRecord ){ //do not record until at least 60 seconds are passed
  162.            startButtonState = digitalRead(startButtonPin);
  163.            
  164.           if (startButtonState == HIGH && isStartButtontHigh) {
  165.               isStartButtontHigh = false;
  166.               isRecording += 1;  
  167.               Serial.println("GRAVANDO");
  168.               handleFile(isRecording);    
  169.            }
  170.            else if (startButtonState == LOW) {
  171.                isStartButtontHigh = true;
  172.            }
  173.    
  174.            accelX = aaWorld.x;
  175.            accelY = aaWorld.y;
  176.            accelZ = aaWorld.z;
  177.            
  178.            sprintf(data,"%d\t%d\t%d\t%lu",accelX, accelY, accelZ, ellapsedTime);
  179.          
  180.            writteToFile(isRecording, data);
  181.            ledControl();
  182.         }
  183.     }    
  184. }
  185.  
  186. void dmpDataReady() {
  187.     mpuInterrupt = true;
  188. }
  189.  
  190. void ledControl(){
  191.   //Turn all leds off
  192.   digitalWrite(greenLed,LOW);
  193.   digitalWrite(redLed,LOW);
  194.   digitalWrite(blueLed,LOW);
  195.  
  196.   //Check what should be on
  197.   if( !canRecord ){
  198.     digitalWrite(blueLed,HIGH); //Wait for MPU data to stabilize. Nothing is being recorded.
  199.   }
  200.  
  201.   if( fifoOverflow ){
  202.     digitalWrite(redLed,HIGH); //FIFO overflow!
  203.   }
  204.  
  205.   if( !fifoOverflow ){
  206.     digitalWrite(redLed,LOW);
  207.   }
  208.  
  209.   if( !fifoOverflow && canRecord &&  isRecording % 2 == 0 ){
  210.     digitalWrite(greenLed,HIGH); //Recording
  211.   }
  212.  
  213. }
  214.  
  215. void handleFile(int isRecording){
  216.    
  217.   //if isRecording is even
  218.   if(isRecording % 2 == 0){  
  219.  
  220.       if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  221.  
  222.       if (!myFile.open("MPU.txt", O_RDWR | O_CREAT | O_AT_END)) {
  223.           sd.errorHalt("opening for write failed");
  224.          
  225.        }
  226.        sprintf(dataTime,"Starting record: %lu",ellapsedTime);
  227.        myFile.println( dataTime );
  228.   }else{  
  229.  
  230.       sprintf(dataTime,"Stopping record: %lu",ellapsedTime);      
  231.       myFile.println( dataTime );
  232.       myFile.close();
  233.       mpu.resetFIFO();
  234.   }
  235. }
  236.  
  237. void writteToFile(int isRecording, char* data){
  238.   if(isRecording % 2 == 0){
  239.     myFile.println(data);    
  240.   }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement