i am building a drone currently.
i have found a way how to get angle values from accelerometer
i have also found a way to control the motors individually. ram using hc-05 bluetooth module as my receiver and my phone as remote control. somebody please help me with an appropriate way to stabilize the drone using accelerometer angle outputs.
my accelerometer x+ direction is at motor 1 and 2 while y+ direction is at motor 2 and 3.
the motors are arranged as top right 2
top left 1
bottom right 3
bottom left 4
Please share what you have "found" so we don't have to reinvent the wheel for you.
Maybe this will help you get started.
#include <PID_v1.h>
const int THROTTLE_MIN = 0;
const int THROTTLE_MAX = 255;
const int MotorPins[4] = {3, 5, 6, 9};
enum MotorIndex {MOTOR_FL_CW, MOTOR_FR_CCW, MOTOR_BL_CCW, MOTOR_BR_CW};
// Set Pitch/Roll/Yaw limits to 10% of throttle range.
const double LIMIT_MIN = -0.1 * (THROTTLE_MAX - THROTTLE_MIN);
const double LIMIT_MAX = 0.1 * (THROTTLE_MAX - THROTTLE_MIN);
const double Kp = 1.0, Ki = 0.0, Kd = 0.0; // Pitch and Roll PID parameters
double PitchSetpoint = 0, PitchInput, PitchOutput;
PID PitchPID(&PitchSetpoint, &PitchInput, &PitchOutput, Kp, Ki, Kd, DIRECT);
double RollSetpoint = 0, RollInput, RollOutput;
PID RollPID(&RollSetpoint, &RollInput, &RollOutput, Kp, Ki, Kd, DIRECT);
void setup()
{
PitchPID.SetOutputLimits(LIMIT_MIN, LIMIT_MAX);
PitchPID.SetMode(AUTOMATIC);
RollPID.SetOutputLimits(LIMIT_MIN, LIMIT_MAX);
RollPID.SetMode(AUTOMATIC);
}
void MotorSpeed(MotorIndex motor, int throttle)
{
throttle = constrain(throttle, THROTTLE_MIN, THROTTLE_MAX);
analogWrite(MotorPins[motor], throttle);
}
void loop()
{
// Read the throttle value from Bluetooth?
int Throttle = 200;
// PitchInput = Accelerometer.pitch();
PitchPID.Compute();
// RollInput = Accelerometer.roll();
RollPID.Compute();
MotorSpeed(MOTOR_FL_CW , Throttle + PitchOutput - RollOutput);
MotorSpeed(MOTOR_FR_CCW, Throttle + PitchOutput + RollOutput);
MotorSpeed(MOTOR_BL_CCW , Throttle - PitchOutput - RollOutput);
MotorSpeed(MOTOR_BR_CW, Throttle - PitchOutput + RollOutput);
}