Advertisement
safwan092

Gyro Arduino LEDS Project

Feb 22nd, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.69 KB | None | 0 0
  1. /**
  2.  * Simple implementation false
  3.  *
  4.  * means that for  pitch/yaw there only be
  5.  * a single led for each direction. The led will get more bright
  6.  * as the direction increases
  7.  *
  8.  * Simple implementation true
  9.  * means that for pitch/yaw there will be multiple led's that light up one by one
  10.  *
  11.  * The accelerometer/gyro is a MPU6050, it should be wired to the SDA and SCL pins
  12.  */
  13. #define SIMPLE_IMPLEMENTATION false
  14.  
  15. const int frontLed = 3;
  16. const int bottomLed = 5;
  17. const int rightLed = 6;
  18. const int leftLed = 9;
  19. long int lastPrintTime;
  20.  
  21. typedef struct
  22. {
  23.     byte pin;
  24.     byte positionInsideGroup;    
  25.     char thePosition; // Left, Right, Up, Down
  26.     byte minAngle;
  27.     byte maxAngle;    
  28. } ledConfig;
  29.  
  30. ledConfig leds[] = {
  31.     {3, 1, 'u', 31, 45},  
  32.     {12, 2, 'u', 16, 30},
  33.     {11, 3, 'u', 5, 15},  
  34.     {5, 1, 'd', 5, 15},  
  35.     {6, 2, 'd', 16, 30},
  36.     {7, 3, 'd', 31, 45},  
  37.     {8 , 1, 'r', 5, 23},  
  38.     {9, 2, 'r', 24, 45},
  39.     {10, 1, 'l', 5, 23},  
  40.     {4, 2, 'l', 24, 45},
  41. };
  42.  
  43. #include "I2Cdev.h"
  44. #include "MPU6050_6Axis_MotionApps20.h"
  45. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  46.     #include "Wire.h"
  47. #endif
  48.  
  49. MPU6050 mpu;
  50.  
  51. bool dmpReady = false;  // set true if DMP init was successful
  52. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  53. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  54. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  55. uint16_t fifoCount;     // count of all bytes currently in FIFO
  56. uint8_t fifoBuffer[64]; // FIFO storage buffer
  57.  
  58. // orientation/motion vars
  59. Quaternion q;           // [w, x, y, z]         quaternion container
  60. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  61. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  62. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  63. VectorFloat gravity;    // [x, y, z]            gravity vector
  64. float euler[3];         // [psi, theta, phi]    Euler angle container
  65. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  66. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  67.  
  68. void setup()
  69. {
  70.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  71.         Wire.begin();
  72.         TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  73.     #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  74.         Fastwire::setup(400, true);
  75.     #endif
  76.  
  77.     Serial.begin(9600);
  78.     while (!Serial); // wait for Leonardo enumeration, others continue immediately
  79.     Serial.println(F("Initializing I2C devices..."));
  80.     mpu.initialize();
  81.     Serial.println(F("Testing device connections..."));
  82.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  83.     Serial.println(F("Initializing DMP..."));
  84.     devStatus = mpu.dmpInitialize();
  85.     mpu.setXGyroOffset(220);
  86.     mpu.setYGyroOffset(76);
  87.     mpu.setZGyroOffset(-85);
  88.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  89.     if (devStatus == 0) {
  90.         // turn on the DMP, now that it's ready
  91.         Serial.println(F("Enabling DMP..."));
  92.         mpu.setDMPEnabled(true);
  93.         Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
  94.         attachInterrupt(0, dmpDataReady, RISING);
  95.         mpuIntStatus = mpu.getIntStatus();
  96.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  97.         dmpReady = true;
  98.         packetSize = mpu.dmpGetFIFOPacketSize();
  99.     } else {
  100.         Serial.print(F("DMP Initialization failed (code "));
  101.         Serial.print(devStatus);
  102.         Serial.println(F(")"));
  103.     }
  104.     if (SIMPLE_IMPLEMENTATION) {
  105.         initializeLEDsSimple();
  106.     } else {
  107.         initializeLEDsMultiple();
  108.     }
  109.     lastPrintTime = millis();    
  110. }
  111.  
  112. void loop()
  113. {
  114.     if (!dmpReady) return;
  115.     mpuInterrupt = false;
  116.     mpuIntStatus = mpu.getIntStatus();
  117.     fifoCount = mpu.getFIFOCount();
  118.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  119.         mpu.resetFIFO();
  120.         Serial.println(F("FIFO overflow!"));
  121.     } else if (mpuIntStatus & 0x02) {
  122.         while (fifoCount < packetSize) {
  123.           fifoCount = mpu.getFIFOCount();
  124.         }
  125.         mpu.getFIFOBytes(fifoBuffer, packetSize);        
  126.         fifoCount -= packetSize;
  127.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  128.         mpu.dmpGetGravity(&gravity, &q);
  129.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  130.         int x = ypr[0] * 180/M_PI;
  131.         int y = ypr[1] * 180/M_PI;
  132.         int z = ypr[2] * 180/M_PI;
  133.         Serial.print(y);Serial.print("\t");Serial.println(z);  
  134.         if (SIMPLE_IMPLEMENTATION) {        
  135.             flashLEDsSimple(x, y, z);
  136.         } else {
  137.             flashLEDsMultiple(x, y, z);
  138.         }
  139.     }
  140. }
  141.  
  142. void initializeLEDsSimple()
  143. {
  144.     pinMode(frontLed, OUTPUT);
  145.     pinMode(bottomLed, OUTPUT);    
  146.     pinMode(rightLed, OUTPUT);
  147.     pinMode(leftLed, OUTPUT);
  148. }
  149.  
  150. void initializeLEDsMultiple()
  151. {
  152.     for (int i=0; i<10; i++) {
  153.         Serial.println(leds[i].pin);
  154.         pinMode(leds[i].pin, OUTPUT);
  155.     }
  156.     delay(3000);
  157. }
  158.  
  159. void flashLEDsSimple(int x, int y, int z)
  160. {
  161.     if (y > 0) {
  162.         analogWrite(rightLed, y*4);
  163.         analogWrite(leftLed, 0);          
  164.     } else {
  165.         analogWrite(leftLed, y*4*-1);      
  166.         analogWrite(rightLed, 0);      
  167.     }
  168.     if (z > 0) {
  169.         analogWrite(bottomLed, z*4);
  170.         analogWrite(frontLed, 0);          
  171.     } else {
  172.         analogWrite(frontLed, z*4*-1);      
  173.         analogWrite(bottomLed, 0);      
  174.     }    
  175. }
  176.  
  177. void flashLEDsMultiple(int x, int y, int z)
  178. {
  179.     for (int i=0; i<10; i++) {
  180.         //Serial.print(z);Serial.print(",");Serial.print(leds[i].thePosition);Serial.print(",");Serial.println(leds[i].minAngle);
  181.         bool modified = false;
  182.         if (z < 0 && leds[i].thePosition == 'u' && abs(z) > leds[i].minAngle) {
  183.             digitalWrite(leds[i].pin, HIGH);
  184.             modified = true;
  185.         }
  186.         if (z > 0 && leds[i].thePosition == 'd' && abs(z) > leds[i].minAngle) {
  187.             digitalWrite(leds[i].pin, HIGH);
  188.             modified = true;
  189.         }
  190.         if (y < 0 && leds[i].thePosition == 'l' && abs(y) > leds[i].minAngle) {
  191.             digitalWrite(leds[i].pin, HIGH);
  192.             modified = true;
  193.         }
  194.         if (y > 0 && leds[i].thePosition == 'r' && abs(y) > leds[i].minAngle) {
  195.             digitalWrite(leds[i].pin, HIGH);
  196.             modified = true;
  197.         }
  198.         if (!modified) {
  199.             digitalWrite(leds[i].pin, LOW);
  200.         }
  201.     }
  202. }
  203.  
  204. void dmpDataReady()
  205. {
  206.     mpuInterrupt = true;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement