I am actually coding for a Quad. its a bit long and forum does't allow me to post because its exceeding the maximum allowed length. I am uploading it in 2 parts.
here is my code:
// ================================================================
// === Sensor Int. ===
// ================================================================
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 mpu;
// MPU control/status vars
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 euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
float AA,BB;
// packet structure for InvenSense teapot demo
uint8_t teapotPacket[14] = { '
, 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}
//------------------------------------------------------------------------------------------\
////////////////// OTHER Init. //////////////////////////
#include <Wire.h>
#include <PID_v1.h>
int esc1= 5; int esc2= 6; int esc3= 7; int esc4= 8 ;
double a,b,c,d;
float dpitch,droll;
float pitch,roll;
volatile double throttle;
float pkp =0.92, pki= 0, pkd= 0;
float rkp =0.92,rki =0,rkd= 0;
/////////////////////////////////////////////////////////////
PID p0utput(&AA,&pitch, &dpitch,pkp, pki, pkd, DIRECT);
PID r0utput(&BB,&roll, &droll, rkp, rki, rkd, DIRECT);
///////////////////////////////////////////////
/////////////////////////////////////////////////
#define PID_ROLL_INFLUENCE 7
////////////////////////PID DATA END//////////////
//--------------------------------------------------------------\
////////////////////////RC_RX Init.///////////////////////////
#define CH1_int_th 0 // Channel 1 interrupt throttle
#define CH1_pin_th 2 // Respective channel Hardware interrupt pin number
#define CH2_int_pt 1 // Channel 2 interrupt pitch
#define CH2_pin_pt 3 // Respective channel Hardware interrupt pin number
#define CH3_int_r1 4 // Channel 3 interrupt roll
#define CH3_pin_r1 19 // Respective channel Hardware interrupt pin number
#define valid_pulse_limit 3000 // [uS] Valid output high pulse time limit for RC controller
#define max_high_time 1895 // [uS] Maximum expected high time
#define min_high_time 1090 // [uS] Minimum expected high time
unsigned long t=0;
int counter;
void init_rc_rx();
volatile unsigned long CH1_t_th=0, CH1_delta_th=0, CH2_t_pt=0, CH2_delta_pt=0, CH3_t_rl=0, CH3_delta_rl=0 ;
// Interrupt ISRs
void CH1_int_ISR()
{
if ((micros()-CH1_t_th) < valid_pulse_limit){
CH1_delta_th = micros()-CH1_t_th;
}
CH1_t_th = micros();
}
void CH2_int_ISR()
{
if ((micros()-CH2_t_pt) < valid_pulse_limit){
CH2_delta_pt = micros()-CH2_t_pt;
}
CH2_t_pt = micros();
}
void CH3_int_ISR()
{
if ((micros()-CH3_t_rl) < valid_pulse_limit){
CH3_delta_rl = micros()-CH3_t_rl;
}
CH3_t_rl = micros();
}
// Initialization\
void init_rc_rx(){
//MAKING PINS INPUT//
pinMode(CH1_pin_th, INPUT);
pinMode(CH2_pin_pt, INPUT);
pinMode(CH3_pin_r1, INPUT);
//ENABLING INTERRUPTS//
attachInterrupt(CH1_int_th, CH1_int_ISR, CHANGE);
attachInterrupt(CH2_int_pt, CH2_int_ISR, CHANGE);
attachInterrupt(CH3_int_r1, CH3_int_ISR, CHANGE);
}
void pid()
{
r0utput.SetMode(AUTOMATIC);//set pid on may be
r0utput.SetOutputLimits(-PID_ROLL_INFLUENCE, PID_ROLL_INFLUENCE);
r0utput.Compute();
p0utput.SetMode(AUTOMATIC);
p0utput.SetOutputLimits(-PID_ROLL_INFLUENCE, PID_ROLL_INFLUENCE);
p0utput.Compute();
}
// ================================================================
// === Sensor ===
// ================================================================
void sensor_temp() {
// if programming failed, don't try to do anything
if (!dmpReady) return;
// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {
}
// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
// get current FIFO count
fifoCount = mpu.getFIFOCount();
// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));
// otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);
// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
AA=ypr[1] * 180/M_PI;
BB=ypr[2] * 180/M_PI;
}
}