# State vector for a Quad-copter?

Whatever literature I've read on quad-copter dynamics, the state of a quad copter is define as a 12x1 vector, containing the coordinate positions (x,y,z) and velocity (xdot, ydot, zdot) in Earth frame, euler angles (theta, phi, psi) and euler angle derivatives (thetadot, phidot, psidot). However, this code that I am reading seems to have modeled the state vector as a 13x1 vector. It's modeled as : [x,y,z,xdot,ydot,zdot,qw,qx,qy,qz,p,q,r]. I am lost at these last 7 variables. In the initialization module, (qw,qx,qy,qz) are initialized as Quat(1),Quat(2),Quat(3),Quat(4) respectively, and (p,q,r) are all zero. I'm guessing the (p,q,r) are derivatives hence are set to zero during initialization. The Quat() is supposed to be the vector of quaternions, which is again something I haven't found in most literature on modeling of quad-copter dynamics. What is this and why is it needed? I have no prior background in robotics or control systems!

Representing the orientation through Euler angles limits the maneuvers (essentially angle of rotation). For example: won't be able to execute a 360-degree flip. Using quaternions to represent the dynamics avoids that. A better alternative is to use rotational matrices (known also as direction cosine matrices). Rotational matrices are 3x3 matrices that uniquely represent the orientation.

Also, note that the relation between quaternion $Q =[q_v, q_0]$ and the "angular velocity" is given by

$$\frac{d}{dt}[q_v,q_0] = \frac{1}{2}[q_0 \omega + q_v\times\omega, - q_v\cdot \omega]$$

Here $q_v, \omega$ are three-dimensional vectors and $q_0$ is a scalar.

Quaternions are just another way of modelling space orientation of a body. They are used in the same way you would use Euler angles. You can find algorithms (check Wikipedia, I found some there written in several programming languages) to go from quaternions to Euler and reverse. There is no real reason to use them instead of Euler angles, I prefer using the latter, but in some projects I encountered quaternions and simply made the conversion.

• The base of your answer is correct but there are very good reason's to use quaternions: they avoid gimbal lock and are less ambiguous than Euler angles. While not unique, given a quaternion you know exactly what orientation the body is in (the reverse is not true). Given Euler angles, you also need to know what order to apply the rotations in (roll then pitch then yaw? is that about local axes or global axes?) It's very common to use quaternions for control/state purposes and then just convert to Euler angles for presentation to humans. – ryan0270 Sep 18 '17 at 12:02
• Quaternions can also be interpolated much easier and nicer, which can be an advantage when generating reference trajectories. – fibonatic Sep 19 '17 at 6:48

Quaternion are used to avoid gimbal Lock. As Euler-angle suffer from singularity configuration and the gimbal lock needs to be treated explicitly in your algorithm. Quaternion only suffer from a slight singularity in the sense that you express the rotation by a vector (3d) and a rotation around this vector (1d), so one rotation can also be represented by the opposite vector and the opposite angle (ie kind of multiplying the quaternion by -1) . The best is to use rotation matrices to be on the clean side, but it's usually heavier to implement. Using rotation matrices a nice paper is DOI: 0.1109/CDC.2010.5717652

Another point in favor of rotation matrices or quaternion is that the number of convention is sensibly lower from more than 12 for Euler-angles, to 2 for quaternions and 1 for matrices. This is especially good when you work with other people/other software to be sure to be easily interfaced. See more on rotation in general https://www.astro.rug.nl/software/kapteyn/_downloads/attitude.pdf

A last remark, in general for quadrotors the translational dynamics is expressed in world frame and the rotational dynamic is expressed in body frame, which simplifies expression of the control matrix. So naturally [p,q,r] are the angular velocities along the main axis of the body frame.