Advertisement
pleasedontcode

**Sensor Data** rev_01

Nov 7th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Sensor Data**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-11-07 23:14:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read angle and trun on led while recive reading */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <MPU6050.h>    //https://github.com/electroniccats/mpu6050
  27. #include "I2Cdev.h"
  28. #include "MPU6050_6Axis_MotionApps20.h"
  29. #include "Wire.h"
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void dmpDataReady(); // Added prototype for interrupt function
  35. void readAngles();    // Added prototype for reading angles
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. // class default I2C address is 0x68
  39. MPU6050 mpu;
  40. //MPU6050 mpu(0x69); // <-- use for AD0 high (commented out because we are using the default address)
  41.  
  42. #define LED_PIN 2 // (Arduino is 11, Teensy is 11, Teensy++ is 6)
  43. bool blinkState = false;
  44.  
  45. // MPU control/status vars
  46. bool dmpReady = false;  // set true if DMP init was successful
  47. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  48. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  49. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  50. uint16_t fifoCount;     // count of all bytes currently in FIFO
  51. uint8_t fifoBuffer[64]; // FIFO storage buffer
  52.  
  53. // orientation/motion vars
  54. Quaternion q;           // [w, x, y, z]         quaternion container
  55. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  56. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  57. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  58. VectorFloat gravity;    // [x, y, z]            gravity vector
  59. float euler[3];         // [psi, theta, phi]    Euler angle container
  60. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  61.  
  62. // packet structure for InvenSense teapot demo
  63. uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };
  64.  
  65. unsigned long currentMillis;
  66.  
  67. long previousMillis = 0;    // set up timers
  68. long interval = 20;        // time constant for timers
  69. unsigned long count;
  70.  
  71. int IMUdataReady = 0;
  72.  
  73. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  74.  
  75. // ****************** SETUP ******************************
  76.  
  77. void setup() {
  78.     // join I2C bus (I2Cdev library doesn't do this automatically)
  79.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  80.         Wire.begin();
  81.     #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  82.         Fastwire::setup(400, true);
  83.     #endif
  84.  
  85.     // initialize serial communication
  86.     Serial.begin(115200);
  87.  
  88.     // initialize device
  89.     mpu.initialize();
  90.     devStatus = mpu.dmpInitialize();
  91.  
  92.     // supply your own gyro offsets here, scaled for min sensitivity
  93.     mpu.setXGyroOffset(113);
  94.     mpu.setYGyroOffset(-22);
  95.     mpu.setZGyroOffset(10);
  96.     mpu.setXAccelOffset(-1315);
  97.     mpu.setYAccelOffset(-544);
  98.     mpu.setZAccelOffset(1119);
  99.  
  100.     // make sure it worked (returns 0 if so)
  101.     if (devStatus == 0) {
  102.         // turn on the DMP, now that it's ready
  103.         mpu.setDMPEnabled(true);
  104.  
  105.         // enable Arduino interrupt detection
  106.         attachInterrupt(23, dmpDataReady, RISING); // Ensure pin 23 is available on ESP32
  107.         mpuIntStatus = mpu.getIntStatus();
  108.  
  109.         // get expected DMP packet size for later comparison
  110.         packetSize = mpu.dmpGetFIFOPacketSize();
  111.     } else {
  112.         // ERROR!
  113.         Serial.print(F("DMP Initialization failed (code "));
  114.         Serial.print(devStatus);
  115.         Serial.println(F(")"));
  116.     }
  117.  
  118.     // configure LED for output
  119.     pinMode(LED_PIN, OUTPUT);
  120. }
  121.  
  122. // ********************* MAIN LOOP *******************************
  123.  
  124. void loop() {  
  125.     currentMillis = millis();
  126.     if (currentMillis - previousMillis >= interval) {  // start timed event
  127.         previousMillis = currentMillis;
  128.  
  129.         if (IMUdataReady == 1) {
  130.             readAngles(); // Ensure this function is defined
  131.         }
  132.  
  133.         Serial.print(ypr[1] * 180/M_PI); // Print pitch angle
  134.         Serial.print('\n');
  135.  
  136.         // Turn on LED while receiving data
  137.         digitalWrite(LED_PIN, HIGH);
  138.     } else {
  139.         // Turn off LED when not receiving data
  140.         digitalWrite(LED_PIN, LOW);
  141.     } // end of timed event          
  142. } // end of main loop
  143.  
  144. // Function to read angles from the MPU6050
  145. void readAngles() {
  146.     // Get the quaternion values
  147.     mpu.dmpGetQuaternion(&q, fifoBuffer);
  148.     // Get the Euler angles
  149.     mpu.dmpGetEuler(euler, &q);
  150.     // Convert to yaw, pitch, roll
  151.     ypr[0] = euler[0]; // yaw
  152.     ypr[1] = euler[1]; // pitch
  153.     ypr[2] = euler[2]; // roll
  154.     IMUdataReady = 0; // Reset data ready flag
  155. }
  156.  
  157. // Interrupt function to signal data ready
  158. void dmpDataReady() {
  159.     IMUdataReady = 1; // Set data ready flag
  160. }
  161.  
  162. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement