Mode Switch and PID Tuning

Firmware/software/electronics/mechanics
Post Reply
pHysiX
Beginner
Posts: 8
Joined: Thu May 16, 2013 4:03 am

Mode Switch and PID Tuning

Post by pHysiX » Thu May 16, 2013 5:07 am

Hi everyone!

Loving my crazyflie here :D

Just a quick question for the people that have experience with the code:

Background:
I am currently trying to create a tutorial tune the PID settings. I have played around with the parameters in the PC client, and I definitely notice better performance.

Each crazyflie will have some deviation from the ideal settings, so it'll really help new fliers level-up faster.
Also, for when the pressure sensors become functional, having tuned PID settings will meant better altitude hold.

My setup:
I currently run my crazyflie using both a PS3 controller (USB and Bluetooth), and xBox 360 (wireless) on win 7 64-bit and the Xubuntu VM.

I have experience with the C/C++ code (firmware), but still have a lot to learn for the python side (Client).

Question:
:arrow: Does anyone know the code/section of code in either the firmware or the client that can put the crazyflie into Rate and Attitude mode?

I know by default the crazyflie is put into attitude mode. I've made changes to the client using QT Designer on the VM, but I am not convinced that this is enough to set the crazyflie firmware to fly in rate mode.

I've checked the parameter outputs, and I think it will be useful to have one extra output to tell if it's rate or attitude mode (based on the PID controller implemented, attitude mode includes a rate controller :) ).

Perhaps we can even extend the client and allow people to change the flight mode using the controller pad 8-)

Hope to hear from you guys soon.

Ian

EDIT: Changed title to reflect information I am posting here =]
Last edited by pHysiX on Sat May 18, 2013 2:12 pm, edited 1 time in total.



tobias
Bitcraze
Posts: 1914
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Attitude and Rate Mode Switch

Post by tobias » Thu May 16, 2013 9:54 am

Hi Ian!

This is what we eventually want to do and we have done some brief testing. The problem flying in rate mode is that the gamepad sensitivity needs to be changed and preferable to a none linear response. This could be done in the client.

The rate mode setting is located in the commander.c and is currently hard coded.
Adding a parameter as a setting would be a first easy step to configure it on the fly.

pHysiX
Beginner
Posts: 8
Joined: Thu May 16, 2013 4:03 am

Re: Mode Switch and PID Tuning

Post by pHysiX » Sat May 18, 2013 2:54 pm

tobias wrote:Hi Ian!

This is what we eventually want to do and we have done some brief testing. The problem flying in rate mode is that the gamepad sensitivity needs to be changed and preferable to a none linear response. This could be done in the client.

The rate mode setting is located in the commander.c and is currently hard-coded.
Adding a parameter as a setting would be a first easy step to configure it on the fly.
Thanks heaps Tobias for your response!!!

Response 1: Mode Switcher
I have editted commander.c so that I just need to change one definition to switch between rate and attitude mode:

Code: Select all

if (!flightType) {
		*rollType = RATE;
		*yawType = RATE;
	}
	else {
		*rollType  = ANGLE;
		*pitchType = ANGLE;
	}
Rate = 0, Attitude = 1.

I don't think we have to modify the client since the mode is hard-coded anyway. I will work on some code to switch this on-the-fly using the client once i finish my assignments.

Response 2: PID Tuning
To do PID Tuning, I made a simple rig. Basically, just attach two strings on into the holes in the corner of the CF, and then attached that thread to two steady posts. I have attached a photo for reference.

Why do the tuning? Every CF will be slightly different. While out of the box it does fly very well, I noticed that I lacked a lot of control (partly because I am a n00b flyer :P). The CF was slow to react and stabilisation could definitely have improved.
This also allows me to fly in both Rate and Attitude mode very well!

:!: Note that I fly in X-Mode, so make sure your PC Client has "Client X-mode" enabled! :!:

So to actually do the tuning, we need to isolate the Rate and Attitude mode.

First, here are the default values in case you ever lose or want to revert:
Axis Mode: [KP, KI, KD, iLimit]

Pitch Rate: [70.0, 0.0, 0.0, 100.0]
Roll Rate: [70.0, 0.0, 0.0, 100.0]
Yaw Rate: [50.0, 25.0, 0.0, 500.0]
Pitch Attitude: [3.5, 2.0, 0.0, 20.0]
Roll Attitude: [3.5, 2.0, 0.0, 20.0]
Yaw Attitude: [0.0, 0.0, 0.0, 360.0]

To PID tune Rate:
1. Tune the Rate mode first. This will mean hard-coding the firmware to make sure roll and pitch input are RATE.
2. In the code (pid.h), set KP, KI and KD to 0.0 for all the roll, pitch and yaw ATTITUDE. We only want Rate. (PID_PITCH_KP etc)
3. In the same code (pid.h) set KI and KD to 0.0 for all roll, pitch and yaw for RATE. We only want to tune the proportional gain first. (PID_PITCH_RATE_KP etc)
4. Flash this to the Crazyflie.

5. Now is the fun part. Load the client, and connect to the CF normally. Under View, go to Tabs, then choose Parameters. You can view your current PID settings here, and also update them live!!! I love that feature so much. Just remember that if you turn your CF off, then values set in the client are not saved.

:!: 6. Make sure your CF is secured to your rig. PID tuning will get VIOLENT because we want to find the gain limits!!!
7. To tune the KP for roll, pitch and yaw, gradually increase roll and pitch KP until you notice oscillations. This means that the CF is overshooting lots and has reached instability. Physically, the CF will wobble a lot and rock around.
8. Once you reach the point of instability, tune it down 5-10 points. That shall be your proportional gain. You can repeat the same process for yaw as well. :D

9. Now we will tune KI. Tuning KI will allow your CF to hold an angle better because naturally with KP only, the CF will have a tendency to revert back to zero-attitude due to gravity/wind effects etc. From experience with tuning KK2.0 etc, a good start is to set KI to half of KP, and work your way up and down from there.
10. When KI causes instability, the CF will wobble and rock around, but on a lower frequency compared to KP instability. This is your critical KI, and so tune down 5-10 points.
11. Repeat for Yaw axis, except KI is usually around 80%+ of KP.

12. Note down all these values, and hard-code it into the pid.h.

Once this is done, pat yourself on the back! :D Rate mode successfully tuned. Have a quick flying session to see how it goes.
NOTE: If you notice that your remote is not providing enough input to the CF, change the client to Advanced mode, and set a higher "Max angle/rate". I have mine at 200, and it is beautiful. Each person's control and settings will be different, so please be careful! Increase the max rate in the client slowly!


To PID tune Attitude:
1. First tune Rate.
2. Now head back into commander.c and change back to ANGLE type. That is, set the CF back into attitude mode.
3. In pid.h, change KI and KD for roll and pitch to 0.0 for ATTITUDE. Also, make sure all attitude KP KI KD for Yaw is 0.0 (PID_PITCH_KP etc)
4. Flash this to the CF and then load up the client again to begin tuning.

5. Again, please make sure the CF is securely attached to your rig.
6. Start by having the KP for roll and pitch attitude at 3.5. Look for any instability such as oscillations. Keep increasing KP until you hit the limit.
7. If you find that KP is causes the CF to be unstable for anything higher than 4, consider slightly lowering the RATE KP and KI by 5-10 points. This allows you a bit more "freedom" with tuning attitude mode.
8. To tune the KI, again slowly increase KI for roll and pitch attitude. Instability again is the low-frequency oscillations.

In perfect tuning, the CF will level itself quickly, respond quickly, and will not wobble/oscillate. You may tolerate a tiny bit of oscillation, but that can quickly lead to flying disaster. Just personal preference here tbh.

Once done, hard-code the values and be proud! Your CF should be much easier to control and is much easier to fly now. :D :lol: :lol: :D

Here are my settings as a reference if anyone is interested:
Rate:
Pitch: [110.0, 50.0, 0.0, 100.0]
Roll: [110.0, 50,0, 0.0, 100.0]
Yaw: [100.0, 90.0, 0.0, 200.0]

Attitude:
Pitch: [3.5, 1.5, 0.0, 20.0]
Roll: [3.5, 1.5, 0.0, 20.0]
Yaw: [0.0, 0.0, 0.0, 360.0]

PC Client:
Flight mode: Advanced
Roll trim: 0
Pitch trim: 0
Client X-Mode enabled

When in Rate mode:
Max angle/rate: 200
May Yaw angle/rate: 200
Max thrust: 90.0
Min thrust: 0.00
SlewLimit: 50
Thrust lowering slew rate: 35

When in Attitude mode:
Max angle/rate: 60
May Yaw angle/rate: 200
Max thrust: 90.0
Min thrust: 0.00
SlewLimit: 50
Thrust lowering slew rate: 35

Sorry for the long post. :oops: I just love the CF so much that I want everyone to be able to take full advantage of every feature the CF has to offer <3
Attachments
Tuning rig.jpg
My simple tuning rig with purple thread
Tuning rig.jpg (211.54 KiB) Viewed 13596 times

pHysiX
Beginner
Posts: 8
Joined: Thu May 16, 2013 4:03 am

Re: Mode Switch and PID Tuning

Post by pHysiX » Sun May 19, 2013 2:27 pm

Further little update:

New Tuning:
The previous post, I gave the tuning I came to.

Since then, I have added little foam pads on the base of the motors, and so have changed the dynamics.

With the previous settings, you will notice some wobbling but that will allow for more aggressive flying.

I've broken 2 motors now, so I want to still have aggressive flying but definitely less wobble.

Here is what I have found:
Pitch Rate: [100 0 0 100]
Roll Rate: [100 0 0 100]
Yaw Rate: [100 0 0 100]

Pitch Att: [4 0 0 100]
Roll Att: [4 0 0 100]
Yaw Att: [4 0 0 100]

The new settings only rely on proportional gain. If the environment is windy, consider adding in a bit of KI for Rate (<50.0) and small KI for Attitude (<2.0).
Be mindful that over tuning will introduce those wobbles from overshoot and low-frequency oscillations.

Tuning Tips:
The developers have uploaded a schematic on the wiki to explain the PID implementation. Basically it is two PID controllers in cascade: Att PID --> Rate PID.

Effects of Tuning KP KI KD
For Yaw: Tuning KP KI KD for Attitude yaw is useless. Yaw is in Rate mode, and so the numbers have no use in attitude.
In Rate, Yaw is good when your CF can yaw without losing or gaining altitude. Yaw PID is meant to keep the thrust constant when the CF is changing its torque to rotate.

For Pitch and Roll:KP KI KD in Rate and Attitude have different effects.
KD is not used at all. KD in theory helps reduce settling time and decrease overshoot. Only problem is in reality, it re-introduces noise! That is why electricity plants avoid using derivative error in their PID! :lol:

KP for Rate: This is the proportional gain for the Inner loop. Refer to the schematic in the wiki. Having this high will allow the quad to react faster (i.e. reach the rate faster). Too high and you will see fast oscillations.
KI for Rate: This is the integral gain for the Inner loop. This is to help the CF hold the angle in rate mode. Too much KI will cause the settling time to be slow, increase overshoot and introduces small oscillations.

Use KI when your CF is unbalanced or when the environment is windy for example. To be honest, you do not need any KI on the CF! 8-)

KP for Attitude: This is the proportional gain for the Outer loop. High gain here is to help the CF reach the attitude faster. Too high KP for Attitude will cause low-frequency oscillations and you will lose control fast.
KI for Attitude: This is the integral gain for the Outer loop. Same principles as above for KI Rate. However, the KI here is MORE SENSITIVE, and will cause a lot more overshoot compared to tuning KI Rate.

A good video to watch is the OpenPilot tutorial. It is very detailed with string-tuning, flight-tuning, and simulation. Amazingly enough, whatever I had learnt from control theory in university matches and is exactly like what the CF does. Thank you Bitcraze so much for the amazing experience!!! <3

tobias
Bitcraze
Posts: 1914
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Mode Switch and PID Tuning

Post by tobias » Thu May 30, 2013 1:43 pm

Great explaining pHysiX!
One cool and great thing would be if we could make the values auto adjust to the propeller balancing test. :D ;)

daikii
Beginner
Posts: 1
Joined: Tue Feb 17, 2015 10:03 am

Re: Mode Switch and PID Tuning

Post by daikii » Tue Feb 17, 2015 10:31 am

Thank you for great documentation!

A quick nooby question - what is X-Mode in the client? What does it specifically do? I couldn't find any specific documentation on this.

Thanks again!

poizone
Member
Posts: 59
Joined: Thu Feb 05, 2015 3:59 am
Location: Ohio, USA

Re: Mode Switch and PID Tuning

Post by poizone » Tue Feb 17, 2015 11:12 pm

X-mode makes your "Forward" direction the red light on the CF1. I believe it turns the edge of the board between M1 and M4 into "Forward" on the CF2. It's more controllable in that you have two motors' thrust increasing or decreasing for a given rotation instead of one in the pitch and roll dimensions.

Woo, PID tuning! I'm glad this topic got pulled up from the depths, because implementing automatic PID tuning is something I'm interested in once my CF is back in the air. I love working with optimization algorithms, and the CF 1 and 2 have more than enough processing power left to implement some PSO at launch to retune from the built in settings, along with adjusting them during flight as needed. Between this and implementing the magnetometer code to help with gyro drift, we should be able to have our flies stable and without drift in hover mode.
Last edited by poizone on Sun Mar 22, 2015 5:47 am, edited 1 time in total.
One day our flies will drown out the sun in an autonomous Skynet of whirring motors and blinking lights.

arnaud
Bitcraze
Posts: 2204
Joined: Tue Feb 06, 2007 12:36 pm

Re: Mode Switch and PID Tuning

Post by arnaud » Wed Feb 18, 2015 10:20 am

Hi,

More precisely the client X-Mode is rotating the pitch and roll setpoint 45 degree on the left (https://github.com/bitcraze/crazyflie-c ... der.py#L67), for Crazyflie 1 it allowed to fly in X mode and for Crazyflie 2 (which is already in X mode) it will make it fly in + mode instead (M4 becomes front). This is a feature that should be implemented in the firmware in the future.

Youjin
Beginner
Posts: 3
Joined: Wed Jul 03, 2013 6:01 am

Re: Mode Switch and PID Tuning

Post by Youjin » Fri Jun 12, 2015 12:09 am

Great documentation.

Your doc gives me a happiness to get the stability of my hobby life.

Post Reply