/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "Sensor Control"
- Source Code NOT compiled for: Arduino Mega
- Source Code created on: 2024-06-24 04:53:25
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* take input from 4 hall sensor for each wheel */
/****** SYSTEM REQUIREMENT 2 *****/
/* take input pwm signal and send out to 4 brushless */
/* esc for each wheel. change the motor speed to */
/* maintain traction */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include //https://github.com/jrowberg/i2cdevlib
#include // Library for controlling ESCs
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void dmpDataReady(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
const uint8_t hallSensorPins[4] = {3, 4, 5, 6}; // Pins for hall sensors
/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t escPins[4] = {7, 8, 9, 10}; // Pins for ESCs
/***** DEFINITION OF I2C PINS *****/
const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
MPU6050 mpu;
Servo escs[4]; // Array to hold ESC objects
/****** GLOBAL VARIABLES *****/
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}
void setup(void)
{
// put your setup code here, to run once:
pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
// Initialize hall sensor pins
for (int i = 0; i < 4; i++) {
pinMode(hallSensorPins[i], INPUT);
}
// Initialize ESCs
for (int i = 0; i < 4; i++) {
escs[i].attach(escPins[i]);
escs[i].writeMicroseconds(1500); // Neutral position for ESCs
}
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#endif
Serial.begin(115200);
while (!Serial);
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
Serial.println(F("\nSend any character to begin DMP programming and demo: "));
while (!Serial.available());
while (Serial.available() && Serial.read());
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();
mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
mpu.setZAccelOffset(1788);
if (devStatus == 0) {
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
mpu.PrintActiveOffsets();
Serial.println(F("Enabling DMP..."));
mpu.setDMPEnabled(true);
Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
Serial.println(F(")..."));
attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(void)
{
// put your main code here, to run repeatedly:
if (!dmpReady) return;
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
Serial.print("ypr\t");
Serial.print(ypr[0] * 180/M_PI);
Serial.print("\t");
Serial.print(ypr[1] * 180/M_PI);
Serial.print("\t");
Serial.println(ypr[2] * 180/M_PI);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
// Read hall sensor inputs
int hallSensorValues[4];
for (int i = 0; i < 4; i++) {
hallSensorValues[i] = digitalRead(hallSensorPins[i]);
}
// Control ESCs based on hall sensor inputs
for (int i = 0; i < 4; i++) {
int pwmValue = map(hallSensorValues[i], 0, 1, 1000, 2000); // Example mapping
escs[i].writeMicroseconds(pwmValue);
}
}
/* END CODE */