Advertisement
pleasedontcode

"MPU6050 Servo" rev_02

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