Advertisement
Guest User

quadcopter

a guest
Jul 30th, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1. #include <PID_v1.h>12
  2. #include <Servo.h>
  3. #include <FreeSixIMU.h>
  4. #include <FIMU_ADXL345.h>
  5. #include <FIMU_ITG3200.h>
  6.  
  7. #include <Wire.h>
  8.  
  9. #define FRONT_LEFT 0      //FRONT LEFT MOTOR
  10. #define BACK_LEFT 1    //FRONT RIGHT MOTOR
  11. #define BACK_RIGHT 2      //BACK LEFT MOTOR
  12. #define FRONT_RIGHT 3     //BACK RIGHT MOTOR
  13.  
  14. float angles[3]; // yaw pitch roll
  15.  
  16. //basespeed to turn the motors on
  17. int BaseSpeed = 40;
  18.  
  19. //conservative yaw PID
  20. #define consyawKP .25
  21. #define consyawKI 0
  22. #define consyawKD .1
  23. //conservative pitch PID
  24. #define conspitchKP .15
  25. #define conspitchKI 0
  26. #define conspitchKD .05
  27. //Conservative roll PID
  28. #define consrollKP .15
  29. #define consrollKI 0
  30. #define consrollKD .05
  31.  
  32. //setpoints, inputs and output variables for each PID object
  33. double yawSetpoint,yaw_DIRECT_Output,yaw_REVERSE_Output,yawInput;
  34. double pitchSetpoint,pitch_DIRECT_Output,pitch_REVERSE_Output,pitchInput;
  35. double rollSetpoint,roll_DIRECT_Output,roll_REVERSE_Output,rollInput;
  36.  
  37. // Set the FreeSixIMU object
  38. FreeSixIMU sixDOF = FreeSixIMU();
  39.  
  40.  
  41. //Setup PID object
  42. PID yaw_DIRECT_PID(&yawInput,&yaw_DIRECT_Output,&yawSetpoint,consyawKP,consyawKI,consyawKD,DIRECT); //PID for yaw
  43. PID yaw_REVERSE_PID(&yawInput,&yaw_REVERSE_Output,&yawSetpoint,consyawKP,consyawKI,consyawKD,REVERSE); //PID for yaw
  44. PID pitch_DIRECT_PID(&pitchInput,&pitch_DIRECT_Output,&pitchSetpoint,conspitchKP,conspitchKI,conspitchKD,DIRECT); //PID for pitch
  45. PID pitch_REVERSE_PID(&pitchInput,&pitch_REVERSE_Output,&pitchSetpoint,conspitchKP,conspitchKI,conspitchKD,REVERSE); //PID for pitch
  46. PID roll_DIRECT_PID(&rollInput,&roll_DIRECT_Output,&rollSetpoint,consrollKP,consrollKI,consrollKD,DIRECT); //PID for roll
  47. PID roll_REVERSE_PID(&rollInput,&roll_REVERSE_Output,&rollSetpoint,consrollKP,consrollKI,consrollKD,REVERSE); //PID for roll
  48.  
  49. //Declare Servo array
  50. Servo motors[4];
  51.  
  52. //attach motors to appropriate pins
  53. void init_motors()
  54. {
  55.   motors[0].attach(8);
  56.   motors[1].attach(9);
  57.   motors[2].attach(10);
  58.   motors[3].attach(11);
  59.   Serial.println("Motors initialized."); // let us know when you're done initializing motors
  60. }
  61.  
  62. //arm the motors (turn them on)
  63. void armAll(){
  64.   // arm the speed controller, modify as necessary for your ESC  
  65.   setSpeed(0,0);
  66.   setSpeed(0,1);
  67.   setSpeed(0,2);
  68.   setSpeed(0,3);
  69.   delay(1000); //delay 1 second,  some speed controllers may need longer
  70.  
  71. }
  72. //set the desired speed with params of speed and id of motor to set
  73. void setSpeed(int speed,int id){
  74.   // speed is from 0 to 100 where 0 is off and 100 is maximum speed
  75.   //the following maps speed values of 0-100 to angles from 0-180,
  76.   // some speed controllers may need different values, see the ESC instructions
  77.   Serial.print("Motor: ");
  78.   Serial.print(id,DEC);
  79.   Serial.print(" Speed changed to: ");
  80.   Serial.print(speed,DEC);
  81.   Serial.println();
  82.   int angle = map(speed, 0, 100, 0, 180);
  83.   motors[id].write(angle);
  84. //  Serial.print("Motor: ");
  85. //  Serial.print(id,DEC);
  86. //  Serial.print(" Speed: ");
  87. //  Serial.print(speed,DEC);
  88. //  Serial.println();  
  89.  
  90. }
  91. void setup() {
  92.   Serial.begin(115200);  //initialize serial port
  93.   Wire.begin();          //start I2C bus
  94.  
  95.   init_motors(); //attach pins to motors
  96.   armAll();      //initialize all motor speeds to 0
  97.  
  98.   sixDOF.init();  //initialize 6dof
  99.   sixDOF.getEuler(angles);  //initial reading of IMU
  100.  
  101.   yawInput = angles[0];
  102.   pitchInput = angles[1];  //assign input value to pitch
  103.   rollInput  = angles[2];  //assign input value to roll
  104.  
  105.   yawSetpoint = 0;
  106.   pitchSetpoint = 0;    //we dont want any pitch ideally
  107.   rollSetpoint = 0;    //same for roll
  108.  
  109.   yaw_DIRECT_PID.SetMode(AUTOMATIC);
  110.   yaw_REVERSE_PID.SetMode(AUTOMATIC);    
  111.   pitch_DIRECT_PID.SetMode(AUTOMATIC);      //set PID modes to AUTOMATIC
  112.   pitch_REVERSE_PID.SetMode(AUTOMATIC);
  113.   roll_DIRECT_PID.SetMode(AUTOMATIC);
  114.   roll_REVERSE_PID.SetMode(AUTOMATIC);
  115.  
  116.   Serial.print("pitch P: ");
  117.   Serial.print(pitch_DIRECT_PID.GetKp());
  118.   Serial.print(" I: ");
  119.   Serial.print(pitch_DIRECT_PID.GetKi());
  120.   Serial.print (" D: ");
  121.   Serial.print(pitch_DIRECT_PID.GetKd());
  122.   Serial.println();
  123. }
  124.  
  125. //helper function to print out double data types
  126.  
  127. //we want hover mode enabled for right now
  128. boolean hover = true;
  129. //correct the pitch and roll
  130. void correctYawPitchRoll(double adjusted_DIRECT_Yaw,double adjusted_REVERSE_Yaw,double adjusted_DIRECT_Pitch,double adjusted_REVERSE_Pitch,double adjusted_DIRECT_Roll,double adjusted_REVERSE_Roll,double Basespeed)
  131. {  
  132.  double CommandYaw;
  133.  double CommandPitch;
  134.  double CommandRoll;
  135.  
  136.  CommandYaw = adjusted_REVERSE_Yaw - adjusted_DIRECT_Yaw;
  137.  CommandPitch = adjusted_REVERSE_Pitch - adjusted_DIRECT_Pitch;
  138.  CommandRoll = adjusted_REVERSE_Roll - (1.1*adjusted_DIRECT_Roll);
  139.  
  140.  setSpeed(constrain(Basespeed - CommandPitch + CommandRoll + CommandYaw,.8*Basespeed,1.2*Basespeed), FRONT_LEFT);
  141.  setSpeed(constrain(Basespeed - CommandPitch - CommandRoll - CommandYaw,.8*Basespeed,1.2*Basespeed), FRONT_RIGHT);
  142.  setSpeed(constrain(Basespeed + CommandPitch + CommandRoll - CommandYaw,.8*Basespeed,1.2*Basespeed), BACK_LEFT);
  143.  setSpeed(constrain(Basespeed + CommandPitch - CommandRoll + CommandYaw,.8*Basespeed,1.2*Basespeed), BACK_RIGHT);
  144.  
  145. }
  146.  
  147. //main loop
  148. void loop() {
  149.          
  150.   //if hover mode disengaged, set speed to 30 (shuts off) and hold
  151.   if(hover == false)
  152.   {
  153.     setSpeed(30,0);
  154.     setSpeed(30,1);
  155.     setSpeed(30,2);
  156.     setSpeed(30,3);
  157.   }
  158.   else
  159.   {
  160.     //otherwise initialize to basespeed on all
  161.     setSpeed(BaseSpeed,FRONT_LEFT);
  162.     setSpeed(BaseSpeed,FRONT_RIGHT);
  163.     setSpeed(BaseSpeed,BACK_LEFT);
  164.     setSpeed(BaseSpeed,BACK_RIGHT);
  165.     //enter loop after speed set
  166.     while(hover)
  167.     {
  168.       //if we get a message, parse the number      
  169.     if(Serial.available() == 3)
  170.       {
  171.           int hundreds = Serial.read() - '0'; // read hundreds place
  172.           int tens = Serial.read() - '0';      //read tens place
  173.           int ones = Serial.read() - '0';      //read ones
  174.           int input = ((ones) + (tens*10) + hundreds*100);
  175.          //if input matches kill signal, kill
  176.            BaseSpeed = input;
  177.           if(input == 123)
  178.           {  
  179.             Serial.println("Kill Sequence Initiated...Powering down...");
  180.             hover = false; //break loop and hold
  181.           }
  182.          // else
  183.          // {
  184.          //   BaseSpeed = input;
  185.          // }
  186.          
  187.       }
  188.       else
  189.       {
  190.         //otherwise, read sensors and compensate
  191.        
  192.         sixDOF.getEuler(angles);    //read 6dof
  193.        
  194.         yawInput = angles[0];
  195.         pitchInput = angles[1];     //assign new readings appropriately
  196.         rollInput  = angles[2];
  197.  
  198.         yaw_DIRECT_PID.Compute();
  199.         yaw_REVERSE_PID.Compute();
  200.         pitch_DIRECT_PID.Compute();
  201.         pitch_REVERSE_PID.Compute();
  202.         roll_DIRECT_PID.Compute();
  203.         roll_REVERSE_PID.Compute();
  204.        
  205.         correctYawPitchRoll(yaw_DIRECT_Output, yaw_REVERSE_Output,pitch_DIRECT_Output,pitch_REVERSE_Output,roll_DIRECT_Output,roll_REVERSE_Output,BaseSpeed);
  206.       }
  207.     }
  208.   }
  209.  
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement