Advertisement
pleasedontcode

"MPU6050 Servo" rev_01

Jul 8th, 2024
179
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: "MPU6050 Servo"
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-07-09 00:13:52
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn on the servo for 3 seconds when the mpu6050 */
  21.     /* deflects by 40 degrees in either x axis or y axis */
  22.     /* and shut it off when */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* When the MPU6050 detects a deflection of 40 */
  25.     /* degrees or more on either the X or Y axis, */
  26.     /* activate the servo motor connected to pin D5 for 3 */
  27.     /* seconds, then turn it off. Use the MPU6050 */
  28.     /* interrupt on pin D3 to trigger the action. */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <Wire.h>
  33. #include <MPU6050_6Axis_MotionApps20.h> //https://github.com/jrowberg/i2cdevlib
  34. #include <Servo.h> // Include the Servo library
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs(void);
  40. void dmpDataReady(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t mpu_6050_MPU6050_Interrupt_PIN_D3 = 3;
  44.  
  45. /***** DEFINITION OF PWM OUTPUT PINS *****/
  46. const uint8_t servo_Servomotor_PWMSignal_PIN_D5 = 5;
  47.  
  48. /***** DEFINITION OF I2C PINS *****/
  49. const uint8_t mpu_6050_MPU6050_I2C_PIN_SDA_A4 = A4;
  50. const uint8_t mpu_6050_MPU6050_I2C_PIN_SCL_A5 = A5;
  51. const uint8_t mpu_6050_MPU6050_I2C_SLAVE_ADDRESS = 0x68; // MPU6050 I2C address
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. /***** used to store raw data *****/
  55. uint8_t servo_Servomotor_PWMSignal_PIN_D5_rawData = 0;
  56.  
  57. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  58. /***** used to store data after characteristic curve transformation *****/
  59. float servo_Servomotor_PWMSignal_PIN_D5_phyData = 0.0;
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. MPU6050 mpu(mpu_6050_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 object with I2C address
  63. Servo myservo; // Initialize Servo object
  64.  
  65. /****** GLOBAL VARIABLES *****/
  66. bool dmpReady = false;  // set true if DMP init was successful
  67. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  68. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  69. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  70. uint16_t fifoCount;     // count of all bytes currently in FIFO
  71. uint8_t fifoBuffer[64]; // FIFO storage buffer
  72.  
  73. Quaternion q;           // [w, x, y, z]         quaternion container
  74. VectorFloat gravity;    // [x, y, z]            gravity vector
  75. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  76.  
  77. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  78.  
  79. void dmpDataReady() {
  80.     mpuInterrupt = true;
  81. }
  82.  
  83. void setup(void) {
  84.     // put your setup code here, to run once:
  85.     Wire.begin();
  86.     Wire.setClock(400000); // 400kHz I2C clock
  87.  
  88.     Serial.begin(115200);
  89.     while (!Serial);
  90.  
  91.     Serial.println(F("Initializing I2C devices..."));
  92.     mpu.initialize();
  93.     pinMode(mpu_6050_MPU6050_Interrupt_PIN_D3, INPUT);
  94.  
  95.     Serial.println(F("Testing device connections..."));
  96.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  97.  
  98.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  99.     while (!Serial.available());
  100.     while (Serial.available() && Serial.read());
  101.  
  102.     Serial.println(F("Initializing DMP..."));
  103.     devStatus = mpu.dmpInitialize();
  104.  
  105.     mpu.setXGyroOffset(220);
  106.     mpu.setYGyroOffset(76);
  107.     mpu.setZGyroOffset(-85);
  108.     mpu.setZAccelOffset(1788);
  109.  
  110.     if (devStatus == 0) {
  111.         mpu.CalibrateAccel(6);
  112.         mpu.CalibrateGyro(6);
  113.         mpu.PrintActiveOffsets();
  114.         Serial.println(F("Enabling DMP..."));
  115.         mpu.setDMPEnabled(true);
  116.  
  117.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  118.         Serial.print(digitalPinToInterrupt(mpu_6050_MPU6050_Interrupt_PIN_D3));
  119.         Serial.println(F(")..."));
  120.         attachInterrupt(digitalPinToInterrupt(mpu_6050_MPU6050_Interrupt_PIN_D3), dmpDataReady, RISING);
  121.         mpuIntStatus = mpu.getIntStatus();
  122.  
  123.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  124.         dmpReady = true;
  125.         packetSize = mpu.dmpGetFIFOPacketSize();
  126.     } else {
  127.         Serial.print(F("DMP Initialization failed (code "));
  128.         Serial.print(devStatus);
  129.         Serial.println(F(")"));
  130.     }
  131.  
  132.     pinMode(servo_Servomotor_PWMSignal_PIN_D5, OUTPUT);
  133.     myservo.attach(servo_Servomotor_PWMSignal_PIN_D5); // Attach the servo to the PWM pin
  134. }
  135.  
  136. void loop(void) {
  137.     // put your main code here, to run repeatedly:
  138.     if (!dmpReady) return;
  139.  
  140.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  141.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  142.         mpu.dmpGetGravity(&gravity, &q);
  143.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  144.         Serial.print("ypr\t");
  145.         Serial.print(ypr[0] * 180 / M_PI);
  146.         Serial.print("\t");
  147.         Serial.print(ypr[1] * 180 / M_PI);
  148.         Serial.print("\t");
  149.         Serial.println(ypr[2] * 180 / M_PI);
  150.  
  151.         // Check if deflection is greater than 40 degrees on either X or Y axis
  152.         if (abs(ypr[1] * 180 / M_PI) >= 40 || abs(ypr[2] * 180 / M_PI) >= 40) {
  153.             myservo.write(90); // Activate the servo motor
  154.             delay(3000); // Keep the servo on for 3 seconds
  155.             myservo.write(0); // Deactivate the servo motor
  156.         }
  157.     }
  158. }
  159.  
  160. void updateOutputs() {
  161.     myservo.write(servo_Servomotor_PWMSignal_PIN_D5_rawData); // Write the mapped value to the servo
  162. }
  163.  
  164. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement