Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AP_Characterise, a KF for learning aerodynamic coefficients #11766

Open
wants to merge 7 commits into
base: master
from

Conversation

@IamPete1
Copy link
Contributor

commented Jul 10, 2019

This adds a Kalman filter that learns the aerodynamic coefficients of the air-frame and propeller. These can then then be used to estimate the battery drain to travel a certain distance, this would allow a dynamic RTL battery fail-safe that would always get you back home with x% battery remaining no mater how far you were from home when the fail-safe triggered. (this is yet to be implemented).

Further to this it is hoped that the coefficients maybe able to give some insight into aircraft setup and design. For example prop X is more efficient at airspeed Y.

So far this has only be tested in Realflight, it would be great to get as many logs as possible so we can select the optimal parameters for the KF, in particular if you have a idea what the aerodynamic parameters for your airfraime should be.

The KF learns 11 states and then uses these states to predict the X(trust - drag) and Z(lift) body frame accelerations and the power draw. In order to converge to a correct solution some states are subject to constants. The states are:

  1. Cl0 - the zero angle of attack lift coefficient
  2. Cla - the change in lift coefficient with angle of attack - this must be positive, lift must increase with angle of attack
  3. Cd0 - the zero angle of attack drag coefficient - this must be positive
  4. Cda - the change in drag coefficient with angle of attack
  5. Cdaa - the change in drag coefficient with angle of attack squared - this must be positive, drag must increase with angle of attack squared
  6. Ct0 - the zero advance ratio propeller thrust coefficient - this must be positive, must be some static thrust
    7.Ctj - the change in thrust coefficient with advance ratio
  7. Ctjj - the change in thrust coefficient with advance ratio squared - this must be negative, thrust must decrease as the airspeed increases
  8. Cp0 - the zero advance ratio propeller power coefficient - this must be positive, must use some power at zero airspeed
  9. Cpj - the change in power coefficient with advance ratio
  10. Cpjj - the change in power coefficient with advance ratio squared, this must be negative, power must decrease at the airspeed increases

In order to learn these states accurately you must give the KF some information about your aircraft, its mass, wing area, propeller diameter and number of props. Note that if you don't care about the values of the learned coefficients but just want to get the battery percentage predictions they can be left at the default but all the coefficients will be none-dimensionalised wrong.

You must however have a battery power sensor(s) and a prop rpm sensor(s). The code can use both battery and rpm feedback from BlHeli-telem. If you have multiple props the average of the rpm sensors will be taken, the KF assumes that all the props are identical. The KF also relys on knowing the airspeed so a airspeed sensor is a good idea too.

This is data from a example flight using a beaver model in realflight. More testing is required to see if this is something that could be left running all the time or if it would be better to tune it up once with a plane autotune type flight. This is me flying a autotune style flight, i had to remove the last bit of the data as the KF got abit confused once it had landed.
inputs
These are the inputs to the KF

states
This is the KF states over time, the red lines are the average values used for plotting the later graphs. This is abit wired, sort of like filtering a filter, any better ideas?

cov
This is the state covariances, note the thrust J and JJ are more or less continually increasing, this may be a problem if the KF were to run for a longer period of time.

lift and drag
This is the lift and drag coefficients vs angle of attack. The black shows the predictions over time, red is the average. Note that we only consider the linear lift region, no stall effects.

prop
Propeller thrust and power coefficient vs advance ratio. (airspeed / (prop rotation rate * diameter))

eff
From these curves we can then plot some more insightful things. The lift over drag ratio, this shows that this air-frame is most efficient at a angle of attack of ~3 deg with a best L/D ratio of 0.77. The propeller efficiency gives a peek efficiency of 51% at a advance ratio of 0.2. Note that this is actually the whole power system efficient as we measure in input electrical power rather than shaft power to the propeller. This is the efficiency of the prop * motor efficiency * esc efficiency.

drag
This is the drag vs airspeed, for this aircraft minimum drag is achieved at ~18m/s.

If we assume straight level un-accelerated flight (SLUF) we can go further.

power

Power draw vs airspeed, if you want to use little power fly slowly. Note that this assume small angles, that is that the propeller does not contribute to lift, no prop hanging. To do a dynamic battery fail safe we only need to get the power correct at the RLT airspeed.

So all in all for this example the graphs look quite plausible but it would be nice to do some tests to try and find out how much we can really trust these graphs.

If they seem quite trust worthy we could do some more things such as automatically choosing the airspeed to give the best flight time or minimum (power / ground speed) in a particular direction. (this depends on the wind speed)

@IamPete1 IamPete1 requested review from tridge and rmackay9 Jul 10, 2019

@rmackay9 rmackay9 changed the title Add AP_Characterise, a KF for leaning aerodynamic coefficients Add AP_Characterise, a KF for learning aerodynamic coefficients Jul 10, 2019

@priseborough

This comment has been minimized.

Copy link
Contributor

commented Jul 16, 2019

The way to introduce this capability is to have the EKF run as a python or C program that can be run log data so that data can be analysed off line and the algorithm tested using logs provided by users. Otherwise we will not get sufficient diversity of testing.

@IamPete1

This comment has been minimized.

Copy link
Contributor Author

commented Jul 17, 2019

@priseborough Thanks for taking a look, I have such a code, but it is in Matlab. Currently it uses data logged by this library but I guess I could extended it to use data that is already logged. I have been testing this using realflight, but I have found that and change in my process noise pramiters, Q, gives a change in the output. I think this is because my simple KF does not properly account for the noise of the inputs, perhaps moving to a full EKF with the input values also as states would help? If you have any ideas I would love to hear them!

@samuelctabor

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2019

Nice project. I've done something similar in the past using an EKF to estimate just two parameters, CD0 and B from the drag polar form CD=CD0+B*CL^2. I used airspeed and sink rate measurements only, for glider applications.

My observation was that convergence was good but only when input data was strictly filtered to enforce SLUF, and when a large range of airspeeds was used to allow the effects of the two parameters to be distinguished. This indicates that the EKF formulation may be essential at least for this case.

Looking forward to hearing more progress. For using MATLAB with logs I use https://github.com/Georacer/ardupilog, it's very convenient.

@priseborough

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2019

Do you have a method to get flight logs into your Matlab? I have experimented with using EKF's to estimate aero derivatives in the past with limited success dependent on having a reasonable initial guess and the correct manoeuvre inputs. I had to turn off estimation for parts of the flight when the vehicle was flying straight and level. Otherwise noise (mostly gust inputs) degraded estimates to worse than the initial guess.

@IamPete1

This comment has been minimized.

Copy link
Contributor Author

commented Jul 19, 2019

Mission planner has a convert to .mat button that works quite well. I had seemingly quite good success using fmincon to apply the constraints with minimum change to the states as weighted with the inverse of there covariance. Not much hope of running that in real time on board tho. This allowed much lower process noise Q. With hard min max constraints it needed larger Q to stop it going unstable.

I have put the matlab code and some test date here. Code is a bit of a mess, sorry. and I think it has some low pass stuff i was playing with, that i think is probably not correct.

It has its moments, seems to work quite well at times, I think it should be possible with the right formulation. Currently this does not take into account the covariance of the inputs used to calculate the forces and power, ie the airspeed, AoA, RPM and air density.

The solution is probably to includes these as states, Makes the equations quite hard to formulate tho, I was having trouble with badly scaled matrix's, decided to do some other stuff for abit can come back to this later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.