This article is rather a logical continuation of my article about the balancer: “Creating a balancer robot on an arduino” . It will very briefly highlight: a simple model of quadrocopter angular stabilization using quaternions, linearization, building control for an object and checking it in Matlab Simulink, as well as checking on a real object. Crazyflie 1.0 will be the test subject.
Now it flies like this (at the time of filming, I didn’t set the controls very well):
Building a dynamic system
We introduce 2 coordinate systems: local, tied to the ground, and the second, associated with the copter. ')
The rotation of the body is more convenient to represent using quaternions, due to the smaller amount of necessary calculations. About them many articles are written, including on Habré. I recommend reading the book “Branets V.N., Shmyglevsky I.P. Using quaternions in orientation tasks ”, thanks to Slovak from the MathWorks Competence Center for the hint.
We use the basic law of the dynamics of rotational motion:
where - moments acting on the body, I is the inertia tensor, and - angular velocities along the main axes (in a related coordinate system). In this way: .
By virtue of the theorem on reducing the inertia tensor to the principal axes, the inertia tensor can be represented as: .
We define external moments through controls: where
Thus, the equations of angular velocity in a related coordinate system:
I would note that if we took into account the position of the copter, it would be possible not to introduce separate control functions, but to immediately use as a force traction, which is more convenient and faster in the calculations. In this case, the stabilization system does not have any data on the required amount of thrust forces, so it is necessary to use just such controls ...
The propeller thrust can be roughly described as . Then the equations can be written through the angular frequencies of the propellers, if you can control the frequency of the motors directly and know the concrete b: Where - Euler angles I note that the selection of the coefficient b I have done manually, a simple selection.
You also need to write the equation for the rotation quaternion. From the properties of quaternions it follows that where are the angular velocities in the coordinate system associated with the aircraft, in it the gyroscopes measure the angular velocity [1].
Let's try to stabilize only the angles and angular velocities:
Or more
We introduce the state space vector: . It should be noted that if a component is included in the space vector the system ceases to be managed. However, we can assume that and remove it from the state vector, thereby reducing the number of coordinates [2].
Control Vector: ,
The system is presented in the standard form.
.
In our case
, but
Linearization and control construction
By linearizing the system near the origin, we obtain the following matrices A and B:
,
Like last time, we use a linear-quadratic regulator. Let me remind the Matlab team to calculate it:
[K,S,e]=lqr(A,B,Q,R)
Matrices Q and R are weight matrices. Q penalizes for deviation from zero, and R for power consumption by control. As a result, we got the matrix K. In my coefficient matrix, all non-diagonal elements were very small (about 10 ^ -4) and I did not take them into account. Let me remind you that in order to obtain control, it is necessary to multiply the matrix K by the vector X. Of course, in the code, you can omit the concept of a matrix and simply multiply each coordinate by a certain factor for speed.
Model checking
To check the results, a model was created in Matlab Simulink. Run it with non-zero initial conditions.
The first graph shows how the angular velocities behave, the second - the change in the components of the quaternion. Notice that the scalar magnitude of the quaternion comes in unit, despite the fact that it does not enter into the equations of the linearized system. As can be seen from the graphs - the model is stabilized.
Code
Crazyflie uses the Free RTOS system, where all the code is divided into modules, we are interested in the code sensfusion6.c and stabilizer.c. Fortunately, the accelerometer and gyroscope filtering is done in quaternions, the problem is that the sensors on the copter are located for the + circuit. The model I expected for the X scheme. The only difference is in the choice of controls U1 and U2.
You must add a quaternion acquisition code to sensfusion6.c:
I did not add a separate module for the LQR controller, instead I changed stabilizer.c. Yes, perhaps this is not the most intelligent way, but it will be suitable for testing the model.
You should start by adding the variables of the current and the desired position of the device, as well as controls:
The selection of coefficients was made empirically, since it was not possible to find out the relationship between the team sent to the motors and the force given by the motor unit.
I also changed the power distribution function of the motors:
Based on the fact that the copter stabilizes its corners, we can conclude that the mathematical model is designed correctly. Unfortunately, it is not yet possible to get your coordinates and speeds (integrating an accelerometer makes a huge mistake), so the copter does not dampen the initial speed and does not return to the initial position. To solve this problem, MIT, for example, uses cameras and tags on its copters.
Additional materials and sources
Branets V.N., Shmyglevsky I.P. "The use of quaternions in orientation problems"
Yaguang Yang "Analytic LQR Design for Spacecraft Control System Based on Quaternion Model"
PS Unfortunately, I can not share the model, as well as talk about the extended model with autopilot and coordinate stabilization due to the fact that this is part of my future diploma, and all diplomas are now checked for novelty and anti-plagiarism. PPS I publish this article on Habré, and not on the new GT due to the fact that the rest of my articles of similar subjects remained exactly on Habré.