quadcopter dynamics, simulation, and control

Upload: fajar-prima

Post on 04-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    1/18

    Quadcopter Dynamics, Simulation, and Control

    IntroductionA helicopter is a ying vehicle which uses rapidly spinning rotors to push air downwards,thus creating a thrust force keeping the helicopter aloft. Conventional helicopters have tworotors. These can be arranged as two coplanar rotors both providing upwards thrust, butspinning in opposite directions (in order to balance the torques exerted upon the body of the

    helicopter). The two rotors can also be arranged with one main rotor providing thrust and asmaller side rotor oriented laterally and counteracting the torque produced by the main rotor.However, these congurations require complicated machinery to control the direction of mo-tion; a swashplate is used to change the angle of attack on the main rotors. In order to producea torque the angle of attack is modulated by the location of each rotor in each stroke, such thatmore thrust is produced on one side of the rotor plane than the other. The complicated designof the rotor and swashplate mechanism presents some problems, increasing construction costsand design complexity.

    A quadrotor helicopter (quadcopter) is a helicopter which has four equally spaced ro-tors, usually arranged at the corners of a square body. With four independent rotors, the needfor a swashplate mechanism is alleviated. The swashplate mechanism was needed to allowthe helicopter to utilize more degrees of freedom, but the same level of control can be obtained by adding two more rotors.

    The development of quadcopters has stalled until very recently, because controllingfour independent rotors has proven to be incredibly difcult and impossible without elec-tronic assistance. The decreasing cost of modern microprocessors has made electronic andeven completely autonomous control of quadcopters feasible for commercial, military, andeven hobbyist purposes.

    Quadcopter control is a fundamentally difcult and interesting problem. With six de-grees of freedom (three translational and three rotational) and only four independent inputs(rotor speeds), quadcopters are severely underactuated. In order to achieve six degrees of freedom, rotational and translational motion are coupled. The resulting dynamics are highlynonlinear, especially after accounting for the complicated aerodynamic effects. Finally, unlikeground vehicles, helicopters have very little friction to prevent their motion, so they must pro-vide their own damping in order to stop moving and remain stable. Together, these factorscreate a very interesting control problem. We will present a very simplied model of quad-copter dynamics and design controllers for our dynamics to follow a designated trajectory. Wewill then test our controllers with a numerical simulation of a quadcopter in ight.

    1

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    2/18

    Quadcopter DynamicsWe will start deriving quadcopter dynamics by introducing the two frames in which will op-erate. The inertial frame is dened by the ground, with gravity pointing in the negative zdirection. The body frame is dened by the orientation of the quadcopter, with the rotor axespointing in the positive z direction and the arms pointing in the x and y directions.

    Quadcopter Body Frame and Inertial Frame

    KinematicsBefore delving into the physics of quadcopter motion, let us formalize the kinematics in the body and inertial frames. We dene the position and velocity of the quadcopter in the inertialframe as x = ( x, y, z)T and x = ( x, y, z)T , respectively. Similarly, we dene the roll, pitch, andyaw angles in the body frame as = ( , , )T , with corresponding angular velocities equalto = ( , , )T . However, note that the angular velocity vector = . The angular velocityis a vector pointing along the axis of rotation, while is just the time derivative of yaw, pitch,and roll. In order to convert these angular velocities into the angular velocity vector, we canuse the following relation:

    =1 0 s0 c cs0 s cc

    where is the angular velocity vector in the body frame.We can relate the body and inertial frame by a rotation matrix R which goes from the

    body frame to the inertial frame. This matrix is derived by using the ZYZ Euler angle conven-tions and successively undoing the yaw, pitch, and roll.

    R =cc css cs ccs ssccs + cs ccc ss cs

    ss cs c

    For a given vector

    v in the body frame, the corresponding vector is given by R

    v in the inertialframe.

    PhysicsIn order to properly model the dynamics of the system, we need an understanding of thephysical properties that govern it. We will begin with a description of the motors being usedfor our quadcopter, and then use energy considerations to derive the forces and thrusts thatthese motors produce on the entire quadcopter. All motors on the quadcopter are identical, sowe can analyze a single one without loss of generality. Note that adjacent propellers, however,are oriented opposite each other; if a propeller is spinning clockwise, then the two adjacentones will be spinning counter-clockwise, so that torques are balanced if all propellers arespinning at the same rate.

    2

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    3/18

    Motors

    Brushless motors are used for all quadcopter applications. For our electric motors, the torqueproduced is given by

    = K t ( I I 0)

    where is the motor torque, I is the input current, I 0 is the current when there is no load onthe motor, and K t is the torque proportionality constant. The voltage across the motor is thesum of the back-EMF and some resistive loss:

    V = I Rm + K v

    where V is the voltage drop across the motor, Rm is the motor resistance, is the angularvelocity of the motor, and K v is a proportionality constant (indicating back-EMF generatedper RPM). We can use this description of our motor to calculate the power it consumes. Thepower is

    P = IV = ( + K t I 0)( K t I 0Rm + Rm + K tK v)

    K t 2

    For the purposes of our simple model, we will assume a negligible motor resistance. Then, thepower becomes proportional to the angular velocity:

    P ( + K t I 0)K v

    K tFurther simplifying our model, we assume that K t I 0 . This is not altogether unreason-able, since I 0 is the current when there is no load, and is thus rather small. In practice, thisapproximation holds well enough. Thus, we obtain our nal, simplied equation for power:

    P K vK t

    .

    Forces

    The power is used to keep the quadcopter aloft. By conservation of energy, we know thatthe energy the motor expends in a given time period is equal to the force generated on thepropeller times the distance that the air it displaces moves ( P d t = F d x). Equivalently, the power is equal to the thrust times the air velocity ( P = F d xd t ).

    P = Tvh

    We assume vehicle speeds are low, so vh is the air velocity when hovering. We also assumethat the free stream velocity, v , is zero (the air in the surrounding environment is stationaryrelative to the quadcopter). Momentum theory gives us the equation for hover velocity as afunction of thrust,

    vh = T 2 Awhere is the density of the surrounding air and A is the area swept out by the rotor. Usingour simplied equation for power, we can then write

    P = K vK t

    = K vK

    K tT =

    T 32

    2 A.

    Note that in the general case, =

    r

    F; in this case, the torque is proportional to the thrust T by some constant ratio K determined by the blade conguration and parameters. Solving forthe thrust magnitude T , we obtain that thrust is proportional to the square of angular velocityof the motor:

    T =K vK 2 AK t

    2

    = k 2

    3

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    4/18

    where k is some appropriately dimensioned constant. Summing over all the motors, we ndthat the total thrust on the quadcopter (in the body frame) is given by

    T B =4

    i= 1

    T i = k 00

    i2.

    In addition to the thrust force, we will model friction as a force proportional to thelinear velocity in each direction. This is a highly simplied view of uid friction, but will besufcient for our modeling and simulation. Our global drag forces will be modeled by anadditional force term

    FD = k d x k d y k d z

    If additional precision is desired, the constant k d can be separated into three separate frictionconstants, one for each direction of motion. If we were to do this, we would want to modelfriction in the body frame rather than the inertial frame.

    Torques

    Now that we have computed the forces on the quadcopter, we would also like to compute thetorques. Each rotor contributes some torque about the body z axis. This torque is the torquerequired to keep the propeller spinning and providing thrust; it creates the instantaneousangular acceleration and overcomes the frictional drag forces. The drag equation from uiddynamics gives us the frictional force:

    FD = 12 CD Av2.

    where is the surrounding uid density, A is the reference area (propeller cross-section, notarea swept out by the propeller), and CD is a dimensionless constant. This, while only accuratein some in some cases, is good enough for our purposes. This implies that the torque due todrag is given by

    D = 12

    R CD Av2 = 12

    R CD A( R)2 = b2

    where is the angular velocity of the propeller, R is the radius of the propeller, and b is someappropriately dimensioned constant. Note that weve assumed that all the force is applied atthe tip of the propeller, which is certainly inaccurate; however, the only result that matters forour purposes is that the drag torque is proportional to the square of the angular velocity. Wecan then write the complete torque about the z axis for the ith motor:

    z = b2 + I M

    where I M is the moment of inertia about the motor z axis, is the angular acceleration of the propeller, and b is our drag coefcient. Note that in steady state ight (i.e. not takeoff or landing), 0, since most of the time the propellers will be maintaining a constant (oralmost constant) thrust and wont be accelerating. Thus, we ignore this term, simplifying theentire expression to

    z = ( 1) i+ 1b i2.

    where the ( 1) i+ 1 term is positive for the ith propeller if the propeller is spinning clockwiseand negative if it is spinning counterclockwise. The total torque about the z axis is given bythe sum of all the torques from each propeller:

    = b 12 22 + 32 42

    The roll and pitch torques are derived from standard mechanics. We can arbitrarily choose thei = 1 and i = 3 motors to be on the roll axis, so

    = r T = L(k 12 k 32) = Lk (12 32)

    4

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    5/18

    Correspondingly, the pitch torque is given by a similar expression

    = Lk (22 42)

    where L is the distance from the center of the quadcopter to any of the propellers. All together,we nd that the torques in the body frame are

    B =Lk (12 32)Lk (22 42)

    b 12 22 + 32 42

    The model weve derived so far is highly simplied. We ignore a multitude of advancedeffects that contribute to the highly nonlinear dynamics of a quadcopter. We ignore rotationaldrag forces (our rotational velocities are relatively low), blade apping (deformation of pro-peller blades due to high velocities and exible materials), surrounding uid velocities (wind),etc. With that said, we now have all the parts necessary to write out the dynamics of our quad-copter.

    Equations of Motion

    In the inertial frame, the acceleration of the quadcopter is due to thrust, gravity, and linearfriction. We can obtain the thrust vector in the inertial frame by using our rotation matrix R tomap the thrust vector from the body frame to the inertial frame. Thus, the linear motion can be summarized as

    mx =00

    mg+ RT B + FD

    where

    x is the position of the quadcopter, g is the acceleration due to gravity, FD is the dragforce, and T B is the thrust vector in the body frame.

    While it is convenient to have the linear equations of motion in the inertial frame, therotational equations of motion are useful to us in the body frame, so that we can expressrotations about the center of the quadcopter instead of about our inertial center. We derive therotational equations of motion from Eulers equations for rigid body dynamics. Expressed invector form, Eulers equations are written as

    I + ( I ) =

    where is the angular velocity vector, I is the inertia matrix, and is a vector of externaltorques. We can rewrite this as

    =x y z

    = I 1 ( ( I )) .

    We can model our quadcopter as two thin uniform rods crossed at the origin with a point mass(motor) at the end of each. With this in mind, its clear that the symmetries result in a diagonalinertia matrix of the form

    I =

    I xx 0 0

    0 I yy 00 0 I zz.

    Therefore, we obtain our nal result for the body frame rotational equations of motion

    = I xx 1

    I yy 1

    I zz 1

    I yy I zzI xx y z

    I zz I xxI yy x z

    I xx I yyI zz x y

    5

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    6/18

    SimulationNow that we have complete equations of motion describing the dynamics of the system, wecan create a simulation environment in which to test and view the results of various inputs andcontrollers. Although more advanced methods are available, we can quickly write a simulatorwhich utilizes Eulers method for solving differential equations to evolve the system state. InMATLAB, this simulator would be written as follows.

    1 % Simulation times, in seconds.2 start_time = 0;3 end_time = 10;4 dt = 0.005;5 times = start_time:dt:end_time;6

    7 % Number of points in the simulation.8 N = numel (times);9

    10 % Initial simulation state.11 x = [0; 0; 10];12 xdot = zeros (3, 1);13 theta = zeros (3, 1);14

    15 % Simulate some disturbance in the angular velocity.16 % The magnitude of the deviation is in radians / second.17 deviation = 100;18 thetadot = deg2rad(2 * deviation * rand (3, 1) - deviation);19

    20 % Step through the simulation, updating the state.21 for t = times22 % Take input from our controller.23 i = input(t);24

    25 omega = thetadot2omega(thetadot, theta);26

    27 % Compute linear and angular accelerations.28 a = acceleration( i , theta, xdot, m, g, k, kd);29 omegadot = angular_acceleration( i , omega, I, L, b, k);30

    31 omega = omega + dt * omegadot;32 thetadot = omega2thetadot(omega, theta);33 theta = theta + dt * thetadot;34 xdot = xdot + dt * a;35 x = x + dt * xdot;36 end

    We would then need functions to compute all of the physical forces and torques.

    1 % Compute thrust given current inputs and thrust coefficient.2 function T = thrust (inputs, k)3 % Inputs are values for i24 T = [0; 0; k * sum(inputs)];5 end

    6

    7 % Compute torques, given current inputs, length, drag coefficient, and thrust coeffic8 function tau = torques (inputs, L, b, k)9 % Inputs are values for i2

    10 tau = [11 L * k * (inputs(1) - inputs(3))

    6

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    7/18

    12 L * k * (inputs(2) - inputs(4))13 b * (inputs(1) - inputs(2) + inputs(3) - inputs(4))14 ];15 end

    16

    17 function a = acceleration (inputs, angles, xdot, m, g, k, kd)18 gravity = [0; 0; - g];19 R = rotation(angles);20 T = R * thrust(inputs, k);21 Fd = - kd * xdot;22 a = gravity + 1 / m * T + Fd;23 end

    24

    25 function omegadot = angular_acceleration (inputs, omega, I, L, b, k)26 tau = torques(inputs, L, b, k);27 omegaddot = inv(I) * (tau - cross (omega, I * omega));28 end

    We would also need values for all of our physical constants, a function to compute the rotationmatrix R, and functions to convert from an angular velocity vector to the derivatives of roll,pitch, and yaw and vice-versa. These are not shown. We can then draw the quadcopter in a

    three-dimensional visualization which is updated as the simulation is running.

    Quadcopter Simulation. Bars above each propeller represent, roughly, relative thrustmagnitudes.

    Control

    The purpose of deriving a mathematical model of a quadcopter is to assist in developing con-trollers for physical quadcopters. The inputs to our system consist of the angular velocitiesof each rotor, since all we can control is the voltages across the motors. Note that in our sim-plied model, we only use the square of the angular velocities, i2, and never the angularvelocity itself, i. For notational simplicity, let us introduce the inputs i = i2. Since wecan set i, we can clearly set i as well. With this, we can write our system as a rst orderdifferential equation in state space. Let x1 be the position in space of the quadcopter, x2 be thequadcopter linear velocity, x3 be the roll, pitch, and yaw angles, and x4 be the angular velocityvector. (Note that all of these are 3-vectors.) With these being our state, we can write the state

    7

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    8/18

    space equations for the evolution of our state.

    x1 = x2

    x2 =00

    g+ 1

    mRT B +

    1m

    FD

    x3 =1 0 s0 c cs0 s cc

    1

    x4

    x4 = I xx 1

    I yy 1

    I zz 1

    I yy I zzI xx y z

    I zz I xxI yy x z

    I xx I yyI zz x y

    Note that our inputs are not used in these equations directly. However, as we will see, we canchoose values for and T , and then solve for values of i.

    PD Control

    In order to control the quadcopter, we will use a PD control, with a component proportionalto the error between our desired trajectory and the observed trajectory, and a component pro-portional to the derivative of the error. Our quadcopter will only have a gyro, so we will only be able to use the angle derivatives , , and in our controller; these measured values willgive us the derivative of our error, and their integral will provide us with the actual error. Wewould like to stabilize the helicopter in a horizontal position, so our desired velocities andangles will all be zero. Torques are related to our angular velocities by = I , so we wouldlike to set the torques proportional to the output of our controller, with = Iu(t). Thus,

    =

    I xx K d + K p T

    0 d t

    I yy K d + K p T

    0 d t

    I zz K d + K p T

    0 d t

    We have previously derived the relationship between torque and our inputs, so we know that

    B =Lk ( 1 3)Lk ( 2 4)

    b ( 1 2 + 3 4) =

    I xx K d + K p T

    0 d t

    I yy K d + K p T

    0 d t

    I zz K d + K p T

    0 d t

    This gives us a set of three equations with four unknowns. We can constrain this by enforcingthe constraint that our inputs must keep the quadcopter aloft:

    T = mg.

    Note that this equation ignores the fact that the thrust will not be pointed directly up. This

    will limit the applicability of our controller, but should not cause major problems for smalldeviations from stability. If we had a way of determining the current angle accurately, wecould compensate. If our gyro is precise enough, we can integrate the values obtained fromthe gyro to get the angles and . In this case, we can calculate the thrust necessary to keepthe quadcopter aloft by projecting the thrust mg onto the inertial z axis. We nd that

    T proj = mgcos cos

    Therefore, with a precise angle measurement, we can instead enforce the requirement that thethrust be equal to

    T = mgcos cos

    8

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    9/18

    in which case the component of the thrust pointing along the positive z axis will be equal tomg. We know that the thrust is proportional to a weighted sum of the inputs:

    T = mg

    cos cos = k i = i =

    mgk cos cos

    With this extra constraint, we have a set of four linear equations with four unknowns i. Wecan then solve for each i, and obtain the following input values:

    1 = mg

    4k cos cos 2be I xx + e I zzkL

    4bkL

    2 = mg

    4k cos cos +

    e I zz4b

    e I yy2kL

    3 = mg

    4k cos cos

    2be I xx + e I zzkL4bkL

    4 = mg

    4k cos cos +

    e I zz4b

    + e I yy2kL

    This is a complete specication for our PD controller. We can simulate this controllerusing our simulation environment. The controller drives the angular velocities and angles tozero.

    Left: Angular velocities. Right: angular displacements. , , are coded as red, green, and blue.

    However, note that the angles are not completely driven to zero. The average steady stateerror (error after 10 seconds of simulation) is approximately 0.3 . This is a common problemwith using PD controllers for mechanical systems, and can be partially alleviated with a PIDcontroller, as we will discuss in the next section.

    In addition, note that since we are only controlling angular velocities, our positionsand linear velocities do not converge to zero. However, the z position will remain constant,

    9

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    10/18

    because we have constrained the total vertical thrust to be such that it keeps the quadcopterperfectly aloft, without ascending or descending. However, this is really nothing more than acuriosity. With the limited sensing that we have available to us, there is nothing we can do tocontrol the linear position and velocity of the quadcopter. While in theory we could computethe linear velocities and positions from the angular velocities, in practice the values will beso noisy as to be completely useless. Thus, we will restrict ourselves to just stabilizing thequadcopter angle and angular velocity. (Traditionally, navigation is done by a human, andstabilization is there simply to make control easier for the human operator.)

    10

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    11/18

    We have implemented this PD control for use in our simulation. The controller is imple-mented as a function which is given some state (corresponding to controller state, not systemstate) and the sensor inputs, and must compute the inputs i and the updated state. The codefor a PD control follows.

    1 % Compute system inputs and updated state.2 % Note that input = [ 1, . . ., 4]3 function [input, state] = pd_controller (state, thetadot)

    4 % Controller gains, tuned by hand and intuition.5 Kd = 4;6 Kp = 3;7

    8 % Initialize the integral if necessary.9 if isfield(state, integral )

    10 params.integral = zeros (3, 1);11 end

    12

    13 % Compute total thrust14 total = state.m * state.g / state.k / ( cos (state.integral(1)) * cos (state.integra15

    16 % Compute errors17 e = Kd * thetadot + Kp * params.integral;18

    19 % Solve for the inputs, i20 input = error2inputs(params, accels, total);21

    22 % Update the state23 params.integral = params.integral + params.dt . * thetadot;24 end

    PID Control

    PD controllers are nice in their simplicity and ease of implementation, but they areoften inade-

    quate for controllingmechanical systems. Especially in the presence of noise and disturbances,PD controllers will often lead to steady state error. A PID control is a PD control with anotherterm added, which is proportional to the integral of the process variable. Adding an integralterm causes any remaining steady-state error to build up and enact a change, so a PID con-troller should be able to track our trajectory (and stabilize the quadcopter) with a signicantlysmaller steady-state error. The equations remain identical to the ones presented in the PD case, but with an additional term in the error:

    e = K d + K p T

    0d t + K i

    T

    0 T

    0d td t

    e = K d + K p T

    0d t + K i

    T

    0 T

    0d td t

    e =

    K d

    + K p

    T

    0

    d t +

    K i T

    0 T

    0

    d td t

    However, PID controls come with their own shortcomings. One problem that commonly oc-curs with a PID control is known as integral windup.

    11

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    12/18

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    13/18

    With a properly implemented PID, we achieve an error of approximately 0.06 after 10seconds.

    We have implemented this PID control for use in simulation, in the same way as with thePD controller shown earlier. Note that there is an additional parameter to tune in a PID. The

    disturbances used for all the test cases are identical, shown to compare the controllers.

    1 % Compute system inputs and updated state.2 % Note that input = [ 1, . . ., 4]3 function [input, state] = pid_controller (state, thetadot)4 % Controller gains, tuned by hand and intuition.5 Kd = 4;6 Kp = 3;7 Ki = 5.5;8

    9 % Initialize the integral if necessary.10 if isfield(state, integral )11 params.integral = zeros (3, 1);12 params.integral2 = zeros (3, 1);13 end

    14

    15 % Prevent wind-up16 if max( abs (params.integral2)) > 0.0117 params.integral2(:) = 0;18 end

    19

    20 % Compute total thrust21 total = state.m * state.g / state.k / ( cos (state.integral(1)) * cos (state.integra22

    23 % Compute errors

    13

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    14/18

    24 e = Kd * thetadot + Kp * params.integral - Ki * params.integral2;25

    26 % Solve for the inputs, i27 input = error2inputs(params, accels, total);28

    29 % Update the state30 params.integral = params.integral + params.dt . * thetadot;31 params.integral2 = params.integral2 + params.dt . * params.integral;32 end

    14

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    15/18

    Automatic PID Tuning

    Although PID control has the potential to perform very well, it turns out that the quality of thecontroller is highly dependent on the gain parameters. Tuning the parameters by hand may be quite difcult, as the ratios of the parameters is as important as the magnitudes of the pa-rameters themselves; often, tuning parameters requires detailed knowledge of the system andan understanding of the conditions in which the PID control will be used. The parameters wechose previously were tuned by hand for good performance, simply by running simulationswith many possibly disturbances and parameter values, and choosing something that workedreasonably well. This method is clearly suboptimal, not only because it can be very difcultand labor-intensive (and sometimes more or less impossible) but also because the resultinggains are not in any way guaranteed to be optimal or even close to optimal.

    Ideally, we would be able to use an algorithm to analyze a system and output the op-timal PID gains, for some reasonable denition of optimal. This problem has been studiedin depth, and many methods have been proposed. Many of these methods require detailedknowledge of the system being modeled, and some rely on properties of the system, such asstability or linearity. The method we will use for choosing our PID parameters is a methodknown as extremum seeking.

    Extremum seeking works exactly as the name implies. We dene the optimal set of parameters as some vector

    = ( K p, K i, K d) which minimizes some cost function J (

    ). In ourcase, we would like to dene a cost function that penalizes high error and error over extendeddurations of time. One candidate cost function is given by

    J (

    ) = 1

    t f to t f

    t0e(t,

    )2d t

    where e(t,

    ) is the error in following some reference trajectory with some initial disturbanceusing a set of parameters

    . Suppose we were able to somehow compute the gradient of this cost function, J (

    ). In that case, we could iteratively improve our parameter vector bydening a parameter update rule

    (k + 1) =

    (k ) J (

    )

    where

    (k ) is the parameter vector after k iterations and is some step size which dictates howmuch we adjust our parameter vector at each step of the iteration. As k , the cost function J (

    ) will approach a local minimum in the space of PID parameters.The question remains as to how we can estimate J (

    ). By denition,

    J (

    ) = K p

    J (

    ), K i

    J (

    ), K d

    J (

    ) .

    We know how to compute J (

    ). Using this, we can approximate the derivative with respect toany of the gains numerically, simply by computing

    K

    J (

    ) J (

    + uK ) J (

    uK )2

    where uK is the unit vector in the K direction. As 0, this approximation becomes better.Using this approximation, we can minimize our cost function and achieve locally optimal PIDparameters. We can start with randomly initialized positive weights, disturb the system insome set manner, evaluate J (

    ) by simulating the system for different PID parameters, andthen compute the gradient. Then, using the method of gradient descent, we can iterativelyoprtimize our gains until we have some form of convergence.

    The gradient descent method does, however, have several problems. First of all, al-though it nds a local minimum, that minimum is only guaranteed to be a local minimum- there may be other minima which are better global minima. In order to avoid choosingsuboptimal local minima in the cost function, we repeat our optimization several times, andchoose the best result. We initialize our PID parameters randomly, so each time we run theoptimization we will get a different result. In addition, instead of choosing disturbance andthen optimizing the response to that disturbance, we use several random disturbances at each

    15

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    16/18

    iteration and use the average response to compute costs and gradients. This ensures that ourparameters are general and not optimized for a specic disturbance. In addition, we vary thestep size and the number of disturbances to try per iteration, in order to increase the sensitiv-ity of our results as our iteration continues. We stop iterations when we detect a steady state,which we do by computing a linear regression on the most recent costs and iterating until theslope is statistically indistinguishable from zero using a 99% condence interval.

    Using our quadcopter simulation, we can dene a function that computes the cost for agiven set of PID parameters.

    1 function J = cost (theta)2 % Create a controller using the given gains.3 control = controller( pid , theta(1), theta(2), theta(3));4

    5 % Perform a simulation.6 data = simulate(control);7

    8 % Compute the integral, 1t f t0 t f

    t0 e( t)2 dt

    9 t0 = 0;10 tf = 1;11 J = 1 / (tf - t0) * sum(data.theta(data.t >= t0 & data.t

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    17/18

    Top: Angular velocities and angular displacements, using manually tuned PID controller.Bottom: Angular velocities and angular displacements, using automatically tuned PID

    controller.

    The automatically-chosen PID parameters do signicantly better overall. They have signif-icantly smaller swings in value, overshoot signicantly less, and converge faster. However,the error in the angular displacement takes longer to converge to zero with the automaticallytuned parameters than with the manually turned parameters, although the initial convergenceis much better when the parameters are chosen via gradient descent. This is due to the factthat our cost function emphasizes squared error, and thus gives priority to minimizing overallerror magnitude rather than long-term convergence. We could easily modify our cost functionto give higher priority to long-term error, in which case the automatically-tuned parametersare likely to do better.

    17

  • 8/13/2019 Quadcopter Dynamics, Simulation, And Control

    18/18

    ConclusionWe derived equations of motion for a quadcopter, starting with the voltage-torque relationfor the brushless motors and working through the quadcopter kinematics and dynamics. Weignored aerodynamical effects such as blade-apping and non-zero free stream velocity, butincluded air friction as a linear drag force in all directions. We used the equations of motionto create a simulator in which to test and visualize quadcopter control mechanisms.

    We began with a simple PD controller. Although the PD controller worked, it left asignicant steady-state error. In order to decrease the steady-state error, we added an integralterm in order to create a PID controller. We tested the PID controller (with minor modi-cations to prevent integral wind-up) and found that it was better at preventing steady-stateerror than the PD controller when presented with the same disturbances and using the sameproportional and derivative gains. We also found that tuning the PID controller was difcult,and would often lead to an unstable system for unknown reasons. In order to avoid the dif-culty of PID tuning and nd the optimal set of parameters, we used a gradient-descent basedextremum seeking method in order to numerically estimate gradients of a cost function inPID-parameter space and iteratively choose a set of parameters to minimize the cost function.We found that the resulting controller was signicantly better than the one using manuallyturned parameters.