Advertisement
pleasedontcode

"Sensor Control" rev_14

Jun 23rd, 2024
423
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 Control"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-24 04:53:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* take input from 4 hall sensor for each wheel */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* take input pwm signal and send out to 4 brushless */
  23.     /* esc for each wheel. change the motor speed to */
  24.     /* maintain traction */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <MPU6050_6Axis_MotionApps20.h> //https://github.com/jrowberg/i2cdevlib
  30. #include <Servo.h> // Library for controlling ESCs
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void dmpDataReady(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
  39. const uint8_t hallSensorPins[4] = {3, 4, 5, 6}; // Pins for hall sensors
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t escPins[4] = {7, 8, 9, 10}; // Pins for ESCs
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
  46. const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
  47. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. MPU6050 mpu;
  51. Servo escs[4]; // Array to hold ESC objects
  52.  
  53. /****** GLOBAL VARIABLES *****/
  54. bool dmpReady = false;  // set true if DMP init was successful
  55. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  56. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  57. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  58. uint16_t fifoCount;     // count of all bytes currently in FIFO
  59. uint8_t fifoBuffer[64]; // FIFO storage buffer
  60.  
  61. // orientation/motion vars
  62. Quaternion q;           // [w, x, y, z]         quaternion container
  63. VectorFloat gravity;    // [x, y, z]            gravity vector
  64. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  65.  
  66. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  67.  
  68. void dmpDataReady() {
  69.     mpuInterrupt = true;
  70. }
  71.  
  72. void setup(void)
  73. {
  74.     // put your setup code here, to run once:
  75.  
  76.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  77.  
  78.     // Initialize hall sensor pins
  79.     for (int i = 0; i < 4; i++) {
  80.         pinMode(hallSensorPins[i], INPUT);
  81.     }
  82.  
  83.     // Initialize ESCs
  84.     for (int i = 0; i < 4; i++) {
  85.         escs[i].attach(escPins[i]);
  86.         escs[i].writeMicroseconds(1500); // Neutral position for ESCs
  87.     }
  88.  
  89.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  90.         Wire.begin();
  91.         Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  92.     #endif
  93.  
  94.     Serial.begin(115200);
  95.     while (!Serial);
  96.  
  97.     Serial.println(F("Initializing I2C devices..."));
  98.     mpu.initialize();
  99.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  100.  
  101.     Serial.println(F("Testing device connections..."));
  102.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  103.  
  104.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  105.     while (!Serial.available());
  106.     while (Serial.available() && Serial.read());
  107.  
  108.     Serial.println(F("Initializing DMP..."));
  109.     devStatus = mpu.dmpInitialize();
  110.  
  111.     mpu.setXGyroOffset(220);
  112.     mpu.setYGyroOffset(76);
  113.     mpu.setZGyroOffset(-85);
  114.     mpu.setZAccelOffset(1788);
  115.  
  116.     if (devStatus == 0) {
  117.         mpu.CalibrateAccel(6);
  118.         mpu.CalibrateGyro(6);
  119.         mpu.PrintActiveOffsets();
  120.         Serial.println(F("Enabling DMP..."));
  121.         mpu.setDMPEnabled(true);
  122.  
  123.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  124.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  125.         Serial.println(F(")..."));
  126.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  127.         mpuIntStatus = mpu.getIntStatus();
  128.  
  129.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  130.         dmpReady = true;
  131.         packetSize = mpu.dmpGetFIFOPacketSize();
  132.     } else {
  133.         Serial.print(F("DMP Initialization failed (code "));
  134.         Serial.print(devStatus);
  135.         Serial.println(F(")"));
  136.     }
  137.  
  138.     pinMode(LED_BUILTIN, OUTPUT);
  139. }
  140.  
  141. void loop(void)
  142. {
  143.     // put your main code here, to run repeatedly:
  144.  
  145.     if (!dmpReady) return;
  146.  
  147.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  148.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  149.         mpu.dmpGetGravity(&gravity, &q);
  150.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  151.         Serial.print("ypr\t");
  152.         Serial.print(ypr[0] * 180/M_PI);
  153.         Serial.print("\t");
  154.         Serial.print(ypr[1] * 180/M_PI);
  155.         Serial.print("\t");
  156.         Serial.println(ypr[2] * 180/M_PI);
  157.  
  158.         digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  159.     }
  160.  
  161.     // Read hall sensor inputs
  162.     int hallSensorValues[4];
  163.     for (int i = 0; i < 4; i++) {
  164.         hallSensorValues[i] = digitalRead(hallSensorPins[i]);
  165.     }
  166.  
  167.     // Control ESCs based on hall sensor inputs
  168.     for (int i = 0; i < 4; i++) {
  169.         int pwmValue = map(hallSensorValues[i], 0, 1, 1000, 2000); // Example mapping
  170.         escs[i].writeMicroseconds(pwmValue);
  171.     }
  172. }
  173.  
  174. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement