Go Down

### Topic: BlueCopter - Arduino Quadcopter (Read 239107 times)previous topic - next topic

#### Jenzaah

#90
##### Dec 05, 2013, 06:09 pmLast Edit: Dec 05, 2013, 06:37 pm by Jenzaah Reason: 1
Hi Basel!

For a school Project i'm making a Arduino Based Quadcopter, I'm working with a Arduino Uno so your code doesn't work out of the box for me. I'm making my own code based on yours. Now when the stabilization part begins, i don't understand the PID's.

First you got the angle mode?
Code: [Select]
`if (rateAngleSwitch == 0){    setX=(int)PIDangleX.Compute((float)setX+angles[0],gy_aver,(float)setX/RX_ANGLE_DAMPNING);    setY=(int)PIDangleY.Compute((float)setY-angles[1],gx_aver,(float)setY/RX_ANGLE_DAMPNING);  }`
I don't understand what this part does, Can you explain what each part of the PID does? The confusing part is the gy_aver in the SetX-part and the gx_aver in de SetY part.

Then there is the rate mode?
Code: [Select]
`  int PIDroll_val= (int)PIDroll.Compute((float)setX-gy_aver);  int PIDpitch_val= (int)PIDpitch.Compute((float)setY-gx_aver);  int PIDyaw_val= (int)PIDyaw.Compute((float)setZ-gz_aver);`
Here you calculate the adjustments of the roll/pitch/yaw part, i understand that, but why do you do SetX-gy_aver? and not SetY-gy_aver?

#### baselsw

#91
##### Dec 07, 2013, 07:16 amLast Edit: Dec 07, 2013, 07:18 am by baselsw Reason: 1
Hey,

Well, I don't know if you read the whole thread or not.. But if you go back and look at page 2, you'll see a picture that explains the whole flight control part.. Still I'll try to answer your questions as best as I can..

Now when the stabilization part begins, i don't understand the PID's.

First you got the angle mode?
Code: [Select]
`if (rateAngleSwitch == 0){    setX=(int)PIDangleX.Compute((float)setX+angles[0],gy_aver,(float)setX/RX_ANGLE_DAMPNING);    setY=(int)PIDangleY.Compute((float)setY-angles[1],gx_aver,(float)setY/RX_ANGLE_DAMPNING);  }`
I don't understand what this part does, Can you explain what each part of the PID does? The confusing part is the gy_aver in the SetX-part and the gx_aver in de SetY part.

If you look at the picture on page 2 you'll see that the flight control is made by two loops.. Let us call them the rate loop and the angle loop.. The rate loop is always running and the angle loop is only turned on when you're in angle mode... You can think of it as if the rate PID's are the only ones existing in code and and the set points for these PID's are simply the receiver input.. When you switch over to angle mode you simply turn on two more PID's that auto-pilots your quadcopter (keep it leveled).. So the receiver input will instead be controlling the angles (set point for the angle PID's) and the angle-PID's will in turn control the set-points for the rate PID's.... I hope I didn't confuse you there =), but it's really hard to explain it better without a whiteboard =P... Now the reason for me doing it this way is because of the hardware! Gyro's are accurate at high frequency's (faster updates -> continuous loop) and accelerometers are accurate at low frequency's (slower updates == as slow as the receiver updates == 50Hz)..

The reason for the gy_aver on setX and vice versa is simply the way I've set up my coordinate system and the way I chose to set up the calculations.. See page 6, I've explained it there...

Then there is the rate mode?
Code: [Select]
`  int PIDroll_val= (int)PIDroll.Compute((float)setX-gy_aver);  int PIDpitch_val= (int)PIDpitch.Compute((float)setY-gx_aver);  int PIDyaw_val= (int)PIDyaw.Compute((float)setZ-gz_aver);`
Here you calculate the adjustments of the roll/pitch/yaw part, i understand that, but why do you do SetX-gy_aver? and not SetY-gy_aver?

Because setX is the angular rate of the X axis (Roll) and it corresponds to the gyro's measurement on the Y axis.. I know it's confusing but look at the coordinate system I've attached on page 6 and you'll get it..

//Basel

#### satiro

#92
##### Dec 08, 2013, 04:27 pm
Basel,
tried to fly with him today, but I could not! look in the test cavalote shaped "U" with a degree of freedom, he was responding well! then calibrated it on the floor and went testing in bed! but soon noticed that the speeds quickly changed and increased acceleration when it rose and fell quickly in bed as if a hand was unbalanced! how do I raise the flight! start angle mode or rate mode? you think I should change the values ??of PID?

#### baselsw

#93
##### Dec 08, 2013, 04:34 pmLast Edit: Dec 08, 2013, 04:36 pm by baselsw Reason: 1

Basel,
tried to fly with him today, but I could not! look in the test cavalote shaped "U" with a degree of freedom, he was responding well! then calibrated it on the floor and went testing in bed! but soon noticed that the speeds quickly changed and increased acceleration when it rose and fell quickly in bed as if a hand was unbalanced! how do I raise the flight! start angle mode or rate mode? you think I should change the values ​​of PID?

That sounds like you need to calibrate your PID.. You always start with rate mode (because it's the main loop in code).. If rate mode isn't calibrated correctly you'll never get a stable copter with angle mode.. If you go back to page 3 you'll see a small guide to how to calibrate the PID's.. Anyway here is the same guide:

1 ) Put the quadcopter in rate mode
2 ) Increase the throttle until the quadcopter starts to hover on its own..
3 ) Increase the P-term until you observe oscillations..  Now subtract 10% of the value and set it as your P-term ..
4 ) Increase the D-term until you observe oscillations..  Now subtract 10-20% of the value (this is your D-term now)..
5 ) Now go back and increase the P-term with small steps until you observe oscillations.. Then subtract a couple of steps..
6 ) Increase the I-term until you observe oscillations.. These oscillations will have a lower frequency, so step up the I-term slowly.. And be careful when it starts to oscillate.. Because the P- and D-term will kick in, and increase the frequency of the oscillations.. So be careful! After that, substract approx. 10-15% of the value...
7 ) If you are satisfied with the results, then go out and take it for a spin, and observe how it flies and reacts to the wind etc.. Then go back and fine tune your copter if you observed any types of oscillations/control problems..
8 ) Do the same for Angle Mode..
9 ) Buy me a beer!!!!!

//Basel

#### satiro

#94
##### Dec 08, 2013, 09:17 pmLast Edit: Dec 08, 2013, 09:29 pm by satiro Reason: 1
Basel,
where to start! I reset the values ​​of ki and kd in terms config.h to PID.pitch, PIDRoll, PIDyaw, PIDAngleX, PIDAngleY​​? create a tab to send the data kp, ki and kd the serial to keep up? I find three sets values ​​of kp, ki, and kd correct, since the symmetry guarantees that the values ​​of pitch and roll are the same! the same for the angles X and Y. Look, attached below a picture of my Quad in CAVALOTE shaped "U".

At the exit of the engine, just let m1 = trottle + PIDpitch and m2 = trottle-PIDpitch? and I'm varying the values ​​of P until he started to oscillate as his Palace to palace? I'll start with P = 1, and see if it fluctuates.

#### baselsw

#95
##### Dec 08, 2013, 09:56 pm

Basel,
where to start! I reset the values ??of ki and kd in terms config.h to PID.pitch, PIDRoll, PIDyaw, PIDAngleX, PIDAngleY??? create a tab to send the data kp, ki and kd the serial to keep up? I find three sets values ??of kp, ki, and kd correct, since the symmetry guarantees that the values ??of pitch and roll are the same! the same for the angles X and Y. Look, attached below a picture of my Quad in CAVALOTE shaped "U".

The simplest way is to change the values and re-upload..  The harder way is to open a new tab and write something that will change the values through serial.. Here is an UN-TESTED example:

Create a new tab and name the file: PID_Change_Serial.ino, type this code in the newly created file:
Code: [Select]
`int kp=0;int ki=0;int kd=0;void readAndChange(){  if(Serial.available()>0){    char a = Serial.read();    a = a-'0';    switch (a) {    case 0:      kp=kp+100;      break;    case 1:      kp=kp-100;      break;    case 2:      ki=ki+10;      break;    case 3:      ki=ki-10;      break;    case 4:      kd=kd+1;      break;    case 5:      kd=kd-1;      break;    }    PIDroll.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);    PIDpitch.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,PITCH_PID_MIN,PITCH_PID_MAX);    Serial.print("Kp: ");    Serial.print(kp);    Serial.print('\t');    Serial.print("Ki: ");    Serial.print(ki);    Serial.print('\t');    Serial.print("Kd: ");    Serial.println(kd);  }}`

And in BlueCopter.ino, put this in the loop:

Code: [Select]
`void loop() {  updateSensorVal();  FlightControl();#ifdef DEBUG  debugProcess();  readAndChange()#endif}`

Don't forget to enable DEBUG-mode from the config.h.. Set all the P, I and D values to zero in config.h..

Now hook up your quadcopter through USB, upload the code and turn everything on (power to the motors etc).. Open the Serial-monitor and type the following to change the P, I and D values for the roll and pitch.. When you've got everything stable on these axis we will go on and calibrate the YAW..

Now send the following through the Serial-monitor to adjust the P,I and D values:

Increase P: send a "0"
Decrease P: send a "1"
Increase I: send a "2"
Decrease I: send a "3"
Increase D: send a "4"
Decrease D: send a "5"

For each change the quadcopter will send the current P, I and D values..

//Basel

#### satiro

#96
##### Dec 09, 2013, 02:52 am
Basel , both! In the palace the palace of PID tuning the palace 3 is not equal to the Palace 5 ? And the other in PID_Change_Serial I have done as you indicated , but the figures are more fast ! is not the case for adding the Serial.print PID_Change_Serial the tab next to Debug_Angle ! I can enchergar there ! else doing all values ??equal to " zero " .

# define ROLL_PID_KP 0
# define ROLL_PID_KI 0
# define ROLL_PID_KD 0
# define ROLL_PID_MIN -50.0
# define ROLL_PID_MAX 50.0

# define PITCH_PID_KP 0
# define PITCH_PID_KI 0
# define PITCH_PID_KD 0
# define PITCH_PID_MIN -50.0
# define PITCH_PID_MAX 50.0

# define YAW_PID_KP 0
# define YAW_PID_KI 0
# define YAW_PID_KD 0
# define YAW_PID_MIN -50.0
# define YAW_PID_MAX 50.0

# define ANGLEX_KP 0
# define ANGLEX_KI 0
# define ANGLEX_KD 0
# define ANGLEX_MIN -100.0
# define ANGLEX_MAX 100.0

# define ANGLEY_KP 0
# define ANGLEY_KI 0
# define ANGLEY_KD 0
# define ANGLEY_MIN -100.0
# define ANGLEY_MAX 100.0
No need to disable PID_int ( ) ? void setup there .
I can not understand how as the two PIDs will live together!
You can change my code ! He follows in the Annex !

#### baselsw

#97
##### Dec 09, 2013, 05:14 pm

Basel , both! In the palace the palace of PID tuning the palace 3 is not equal to the Palace 5 ? And the other in PID_Change_Serial I have done as you indicated , but the figures are more fast ! is not the case for adding the Serial.print PID_Change_Serial the tab next to Debug_Angle ! I can enchergar there ! else doing all values ??equal to " zero " .

No need to disable PID_int ( ) ? void setup there .
I can not understand how as the two PIDs will live together!
You can change my code ! He follows in the Annex !

No offence, but I'm having a really hard time to understand what you wrote here.. What is palace/place 3 or 5?

Anyway, you did the right thing to set all the PID's to zero.. While calibrating the PID's you can't debug anything else, like DEBUG_ANGLE and such, that explains why the values disappears so fast.. turn them all off.. Only leave DEBUG mode on... You don't need to touch the PID_init(), leave everything there as is.. Which two PID's do you mean??? Angle and rate PID's? What do you need me to change? Your code looks okay...

//Basel

#### satiro

#98
##### Dec 09, 2013, 06:01 pmLast Edit: Dec 09, 2013, 06:11 pm by satiro Reason: 1
Basel,
sorry for not making myself clear! Look, I can tell you from afar you'd make a great teacher! it is pasciente and can explain things difficult even for those who do not know much! Parabens! But back ... Steps 3 and 5 of PID tuning you repeated twice in the topic seems a repeat! In step 5 you do not put values ​​... Type a percentage% but increase and decrease step ... How to change the P, as in 1, you can no longer get away with the term P?

Basel would not be appropriate to change the PID tab there writing :

PIDroll.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);
// PIDpitch.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,PITCH_PID_MIN,PITCH_PID_MAX);

//PIDyaw.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);
//PIDAngleX.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,PITCH_PID_MIN,PITCH_PID_MAX);
//PIDAngleY.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);
There Degug_Angle write :
Serial.print ( " Kp " ) ;
Serial.print ( (float ) kp/1000.0 ) ;
Serial.print ( '\ t ' ) ;
Serial.print ( " Ki " ) ;
Serial.print ( (float ) ki/1000.0 ) ;
Serial.print ( '\ t ' ) ;
Serial.print ( " KD " ) ;
Serial.println ( (float ) kd/1000.0 ) ;
And Debug_Angle enabled to view real-time adjustment ?

For work with only one degree of freedom , so I need only two opposite motors ! hence the value found is for the two angles of pitch and roll !

Another thing is how will I be able to adjust PID Yaw , it is not necessary that the quad is suspended by a thread ? pos is the movement of yaw direction!

#### baselsw

#99
##### Dec 09, 2013, 06:57 pmLast Edit: Dec 09, 2013, 07:01 pm by baselsw Reason: 1

But back ... Steps 3 and 5 of PID tuning you repeated twice in the topic seems a repeat! In step 5 you do not put values ??... Type a percentage% but increase and decrease step ... How to change the P, as in 1, you can no longer get away with the term P?

The reason that you go back and adjust the P-term in step 5 is that the D-term stabilizes you quadcopter.. So you would be able to increase the P-term a little more...
Well a typical step in BlueCopter firmware is around (for the P-term) is 0.20.. So if your oscillations start at let us say 5.0 then back off two to three steps down to 4.6-4.4..

For the I-term a typical step is 0.05 to 0.10... For the D-term a typical step is 0.002...

Basel would not be appropriate to change the PID tab there writing :

PIDroll.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);
// PIDpitch.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,PITCH_PID_MIN,PITCH_PID_MAX);

//PIDyaw.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);
//PIDAngleX.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,PITCH_PID_MIN,PITCH_PID_MAX);
//PIDAngleY.ChangeParameters((float)kp/1000.0,(float)ki/1000.0,(float)kd/1000.0,ROLL_PID_MIN,ROLL_PID_MAX);

If you're calibrating only one axis at a time then yes, you can change them that way...

There Degug_Angle write :
Serial.print ( " Kp " ) ;
Serial.print ( (float ) kp/1000.0 ) ;
Serial.print ( '\ t ' ) ;
Serial.print ( " Ki " ) ;
Serial.print ( (float ) ki/1000.0 ) ;
Serial.print ( '\ t ' ) ;
Serial.print ( " KD " ) ;
Serial.println ( (float ) kd/1000.0 ) ;
And Debug_Angle enabled to view real-time adjustment ?

Another thing is how will I be able to adjust PID Yaw , it is not necessary that the quad is suspended by a thread ? pos is the movement of yaw direction!

You don't need to view the angle because your calibrating the angle-rate.. Meaning that the Angle won't have any influence on your calibration yet.. When calibrating the Angle-PID's you can enable them, but you'll have some problems seeing the P, I and D values when they change, because the angle's are printed on the serial monitor as fast as possible, whereas the P, I and D terms are only printed when you change them... You don't need to view the angles in real time while adjusting.. If you have a lot of trouble finding optimal P, I and D values then you can enable them to see if there is something wrong...

We will leave the Yaw as the last thing to calibrate.. When you've calibrated the Roll and Pitch then we'll go ahead and calibrate the YAW axis.. To calibrate the YAW axis you'll have to hold the quadcopter in your hand... But to be able to safely do that you need to have a stable quadcopter (meaning calibrate the Roll and Pitch)...

Edit: Here is a video of how to calibrate your quadcopter PID's:

//Basel

#### satiro

#100
##### Dec 10, 2013, 12:32 am
Basel, adjusting the values ??of Kp, Ki and Kd for one degree of freedom you think that the values ??can be inserted at angles of Pitch and Roll? or should I do both at the same time as the video? and another thing, after adjusting for the fly without the Yaw angle! because I'm thinking of adjust it only after adjusting the angles X and Y angle mode. What do you think?

#### SuleymanSahin

#101
##### Dec 10, 2013, 07:31 pm
Hi
I have a question for you.
Can we use their method?

#### satiro

#102
##### Dec 13, 2013, 10:14 pm
Basel ,
I think it took me longer than I'd like ! returning to Quadcopter ! Look, as you indicated I started increasing the value of Kp until it began to wobble as you said , my value was 0.4 , then the term Kp was 0.36 thereafter was weird ! changed in config.h Rescued values ??of 0.36 in each angle , then did the up-load and started changing the Kd values ??as you wrote ! what happened is that the first Kd value that I sent ... the Stcks not respoderam more! is this normal? then kept increasing the value of kd but not seen many swings , so I ask you! I'm on the right track ? still got to step 6 and went with increasing KI values ??of Kp and Kd = 0:38 = 0.0128 , my battery died and I had to recharge it ...
1st question ... I change all three values ??on the same loop or have to change , save and change back to go to next step?

2 What were your initial terms for Kp , Ki and Kd ?

3rd oscillations Kd are as visible as those of Kp ? and fluctuations of Ki are also clearly visible ?

#### krooks86

#103
##### Dec 14, 2013, 10:25 pmLast Edit: Dec 14, 2013, 10:33 pm by krooks86 Reason: 1
Hi Basel,
Excellent article and nice explanation. I had some queries, about the motor control.

int m0_val=throttle+PIDroll_val+PIDpitch_val+PIDyaw_val;
int m1_val=throttle-PIDroll_val+PIDpitch_val-PIDyaw_val;
int m2_val=throttle+PIDroll_val-PIDpitch_val-PIDyaw_val;
int m3_val=throttle-PIDroll_val-PIDpitch_val+PIDyaw_val;

If the quadrotor is in plus mode, then assuming m0 and m3 are on the y axis(controlling pitch) and m1 and m2 are on x axis(controlling roll), then m0 will have a +PIDpitch_val and m3 will have a - PIDPitch_val.
Similarly m1 will have a +PIDroll_val and m2 will have a -PIDroll_val.

For Yaw, the one axis motors will have higher speed, than the other axis motors will have a slower rpm.
So  y axis motors have higher speed and thus, the PIDYaw_val is added to the y axis motors and subtracted from x axis motors.

This is my understanding of how to control the orientation of the vehicle in plus mode.

What I do not understand is, if m0 and m3 are motors on the pitch axis, then why is the roll component PIDroll_val being added and subtracted from their throttle.
Also, if m1 and m2 are motors on the roll axis, then why do we add / subtract the pitch component from the roll axis motors.

Could you explain ? or provide a link to some material.

#### satiro

#104
##### Dec 16, 2013, 10:44 pm
Basel !!!!!!!!!!!!!!!!!!!!! help-me!!!!!!!!!

Go Up

Please enter a valid email to subscribe