Quadcopter PID question

Hi!

I am making a quadcopter, I have just got the gyro to read proper values (MPU6050) and now it's time to go to the next step to make the actuall drone stabilize smoothly. Should I just slightly increase and decrease throttle with some for loop or should I calculate the PID and adjust the throttle that way?

Thanks beforehand!
Best regards Max

1 Like

@lordmax2 Hi Max,

There are probably numerous ways to do this, but this is how I did it:

  1. Calculate the roll, pitch and yaw from the pilot input and map it to minimum and maximum stick rates of around ±250°/s, (although you could also use radians/s). This means with the sticks centred, the pilot input will be around 0°/s for each axis.

  2. Calculate the corresponding rotational roll, pitch and yaw rates for the gyroscope in degrees/s.

  3. Create 3 floating point rate PID control loops for each axis with the pilot input as the setpoint and the gyroscope and the input, (both setpoint and input using the same units in degrees/s).

  4. In the motor mixer section take the raw throttle value (the value that will be sent out to the motor ESCs) and superimpose the PID outputs on to it. This is dependent on aircraft configuration, for example for a quadcopter X configuration:

motor1 = throttlePulsewidth + rollRateOut + pitchRateOut + yawRateOut;
motor2 = throttlePulsewidth - rollRateOut + pitchRateOut - yawRateOut;
motor3 = throttlePulsewidth - rollRateOut - pitchRateOut + yawRateOut;
motor4 = throttlePulsewidth + rollRateOut - pitchRateOut - yawRateOut;

The thing to note here, is that the PID control loops' outputs don't have any units as such. In this case, they are used to add and subract from the raw throttle value position. The amount of correction adjustment is controlled by the control loops' P, I and D gain. (Your pluses and minuses my vary depending on which motors you designate as 1, 2, 3, 4, etc..., the orientation of the gyrscope on your FC board, or if any of your RC receiver channels are reversed).

Note that I also constained the outputs to the minimum and maximum values expected by the ESCs, using the Ardino constrain() function.

I initially test flew the quadcopter using a the P terms (set to around 1.0f) only and subsequently introduced the I and D terms.

Hi, thanks for your response!

I have mapped 2 joysticks to currently +-180 but what do you mean with the mapped value to degrees/s?

Right now I get the angles in degrees from the gyro, like Roll: -45° Pitch: -32°.

The floating point are the ones I don't really understand at all, why take the derivate and integral?

Thanks beforehand!
Best regards Max

The Integral term of the PID makes sure that the Input eventually settles on the Setpoint, even when the error is very low. If the error is small, the Proportional output is very low and not always enough to actually change the position.

The Derivative term makes sure the Input doesn't shoot too far past the Setpoint. It damps down the Output when the Input is changing rapidly.

@lordmax2 Hi Max,

On a basic flight controller there are two main modes of operation, there's what's known as acro/gyro/rate mode and stabilise/auto-level mode.

In acro mode the sticks control the rate of rotation of the aircraft. This mode requires only a gyro sensor, that's why this mode is known by various names.

Acro mode is essentially how most aircraft joysticks work and is the mode preferred by most experienced pilots, since it allows for free flowing expressive control. However, it does require some practice to fly, as the pilot has to control all aspects of the flight envelope.

A 3-axis gyroscope measures roll, pitch and yaw rate of rotation, this can be in degrees/s or radian/s.

Implementing acro mode is relatively straightforward.

Auto-level mode allows the roll and pitch sticks to control the aircraft's angle in degrees or radians, say ±45°, rather than its rate (in degrees/s or radians/s). When the pilot lets go of the sticks and allows them to return to 0°, the aircraft will return to level flight, hence the name auto-level or stabilise. Auto-level is often used by beginners, as it's much easier to fly in this mode, since the flight computer is controlling rate of motion.

Auto-level is where things become much more mathematically complex, firstly because it's necessary to track the aircraft's orientaion (or angles) relative to a fixed world frame. Also, the roll, pitch and yaw are interdependent on each other, so for example if you pitch up 45° and yaw 90,° your aircraft is now in a roll at a 45° angle. This is much more complicated than it first appears. Secondly, because it's necessary to use sensor fusion to combine the gyroscope with the accelerometer, to provide a roll and pitch angle estimation. This employs sensor fusion techniques such as the complementary or (extended) Kalman filter.

Keeping track of the aircraft's rotation in 3D space is traditionally done using either a Direct Cosine Matrix (DCM) or Quaternions (4-dimensional complex numbers). Although, it's also possible to simplify the mathematics somewhat by using Euler angles together yaw cross-coupling between roll and pitch, in other word feeding some of the yaw rotation into the roll and pitch angles.

Using the MPU6050's Digital Motion Processor (DMP) combines both the aircraft orientation and sensor fusion, to generate the angles without having to implement everything yourself.

1 Like

But flying in acro mode is relatively not!

@lordmax2 between your initial post and your responses to the several longer posts providing details about how to do quadcopter software it is evident that you are in very deep water.

And it’s not clear you know how to swim.

I do mean to discourage you. There are many many projects of various levels of sophistication that address all the aspects of flight control.

I suggest you find them, study them, use them with real vehicles and get a feel for all that is involved.

Rolling your own from scratch is just going to be a waste of your time and possibly quite dangerous.

Write your own after learning a crap-ton more about this from end to end.

google

betaflight

and

ardupilot

and

silverware flight control

for three variously capable and mature flight control software “ecosystems”.

a7

1 Like

Open source Ardupilot and DIYDrones are great resources. They have been around for well over a decade and many people have contributed to software and hardware development.

Spend some time on those sites. There is plenty of work to be done, once you are up to speed.

1 Like

@lordmax2 Hi Max,

I don't know your experience level, but @alto777 and @jremington are right. Using an existing flight controller or system is the best way to start out. It builds familiarity with how drone/multi-rotors operate in general. There's a lot of reading up about equipment and watching build videos, just to get started. There's a lot of online support out there.

I wouldn't wish to put you off, as building your own flight controller is a very rewarding experience, but also it requires knowledge of the safe handling of Lipo batteries, power distribution from the battery to the ESCs and motors, as well as the connections to the flight controller itself.

In addition, if you do choose to create your own flight controller, it's an absolute necessity to incorporate some form of interlock, in order to arm and disarm the motors. It allows the quad to be handled safely with the battery still attached and prevents an inadvertent nudge on the throttle from causing an accidental uncontrolled launch. Brushless DC motors operate at very high speeds.

Whatever flight controller you're using, for safety always remove the propellers during testing and keep a safe distance during flights.

It's also necessary to check the regulations in your country, as nowadays many require anyone piloting drones over 250g to be registered with the national aviation authority: FAA (US), CAA (UK), etc...

3 Likes

This is something I really want to learn, I know it is a big step but I feel that I've got onto the train, I am really looking forward to learn thos from the bottom. I know I'm in deep waters but I want to try it, I feel that this can educate me a lot and now that I feel that I have the drive for it, I will go for it, thanks everyone for your responses and if you have any good sources I would gladly check them out :slight_smile:

I have permanent scars from drone "software glitches". When doing development, its important to use underrated props. (That may fail in flight) Because they may save your fingers.

-jim lee

This is something I really want to learn

Excellent! But don't start with a drone. Pretty much the same sensor, 3D orientation, navigation, motor control, programming and power considerations are required to develop and implement a semiautonomous boat or land vehicle, with vastly less risk of personal injury and property damage.

Once you have a 2D vehicle working well, then start thinking about flight.

1 Like

You should never be in circumstances where it is an “underrated prop” that would save you a finger or an eye…

a7

Well you don't try to be. But I bet you have auto insurance.

-jim lee

@jimLee What scars have you got from your project? Dont lose all ope in me but I was to lazy to build a mound so I accidently got the propeller into two of my fingers, but it just cut through about 1/3 or 1/4th of the finger. I was lucky the propeller broke.

I got sliced open on the back of my hand between my pinky and ring fingers.

Nasty!

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.