Im working around with a simulation of a quadrotor.
So i've read articles, did the math to derive the set of equation using lagrangian approach. So far so good.
Then i want to control the system. so i did another round of math to linearize (with the help of the paper but also other:https://ieeexplore.ieee.org/document/6417914) and i implement a lqr controller.
So the lqr controller give me a set of command (for the thrust and each moment), that if feed directly in the model work well to reach a command. Great !
My problem is that i want to have something more 'realistic', since i want to control the speed of the motor directly. So using thrust mixing describe in many papers, i transform the set of command given by the lqr to a set of command for the motor. And then everything falls apart.
As i want to keep a certain touch of realism, i dont want to have negative command for the motor (meaning that they will have to spin backward so not possible) nor have then spin too fast. The problem is with negative command.
So, i tried to clip the motor command but it doesnt work, the system is unable to reach a command. I tried to rewrite the state space equation so instead of the traditional command i have directly motor command and then redo a lqr from there but it didnt work (i may have done some mistake).
I feel like i'm missing something but i dont know what. Any advice ?
Edit: So after applying lagrangian mechanics we end up with:
$$ \ddot{q} = \begin{pmatrix} u1/m(\cos(\phi)\sin(\theta)\cos(\psi)+\sin(\psi)\sin(\phi))-A_xu/m \\ u1/m (\sin(\psi)*\sin(\theta)*\cos(\phi) - \cos(\psi)*\sin(\phi)) - A_yv/m \\ u1/m \cos(\theta)*\cos(\phi) - g - A_zw/m \\ \dot{\psi}\dot{\theta}(I_y-I_z)/I_x + lu_2/I_x + J/I_x\dot{\theta}W \\ \dot{\phi}\dot{\psi}(I_z-I_x)/I_y + lu_3/I_y - J/I_y\dot{\phi}W \\ \dot{\phi}\dot{\theta}(I_x-I_y)/I_z + u_4/I_z \end{pmatrix} $$ where q is {x, y,z, $\phi, \theta, \psi$} the position and attitude of the quadcopter, $\ddot{q}$ the translational and rotational acceleration, m is the masse of the quadcopter, l the length of the arm, $I_X,I_y,I_z$ the moment of inertia, u v w the velocity, A drag coefficient J drag moment $u_1 = \Sigma b\Omega_i^2$, $\Omega$ the motor rpm, b the thrust coefficient $u_2= lb(-\Omega_2^2 +\Omega_4^2)$ $u_3 = lb(\Omega_3^2 - \Omega_1^2)$ $u_4 = -d\Omega_1^2 +d\Omega_2^2 -d\Omega_3^2 + d\Omega_4^2$
this what i call the reconstruct command, as i reconstruct command using the motor speed
i linearize around equilibrium (hoovering) which give $\Omega_i^2=mg/4b$ and everything else at 0.
LQR design $$ A = \begin{bmatrix} 0& 1& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& g& 0& 0& 0\\ 0& 0& 0& 1& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& -g& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 1& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 1\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ \end{bmatrix}$$
$$ B = \begin{bmatrix} 0& 0& 0& 0\\ 0& 0& 0& 0\\ 0& 0& 0& 0\\ 0& 0& 0& 0\\ 0& 0& 0& 0\\ 1/m& 0& 0& 0\\ 0& 0& 0& 0\\ 0& 1/Ix& 0& 0\\ 0& 0& 0& 0\\ 0& 0& 1/Iy& 0\\ 0& 0& 0& 0\\ 0& 0& 0& 1/Iz \end{bmatrix} $$ $$ Q= \begin{bmatrix} 1& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 1& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 1& 0& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 1& 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 5& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 2& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 5& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 2& 0& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 5& 0\\ 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 0& 2\\ \end{bmatrix} $$ $$ R= \begin{bmatrix} 10& 0& 0& 0\\ 0& 10& 0& 0\\ 0& 0& 10& 0\\ 0& 0& 0& 10 \end{bmatrix} $$
i use the classic U = - K x to get the command (K the lqr gain and x the state that i define as q in the physics part) and i mixed them as: $$ \Omega_1^2 = u_1/4b +u_3/2b + u_4/4d\\ \Omega_2^2 = u_1/4b -u_2/2b - u_4/4d\\ \Omega_3^2 = u_1/4b -u_3/2b + u_4/4d\\ \Omega_4^2 = u_1/4b +u_2/2b - u_4/4d $$ to get the motor command. i take the signed square root and add the result to the $\sqrt{mg/4b}$ that have arise during the linearization to have everything in term of $\Omega$ and not $\Omega^2$
if i fed the Omegas to get the command my simulation failed, especially when my altitude is higher than my command bc of negative command that cant be transcribe ($100^2 = -100^2$ so it fails), if i fed directly the result from the U=-Kx operation as $u_1, u_2, u_3, u_4$ in the model part it works and i dont understand why.Hope its clearer
Solution: In the end, my error reside in how i code the $u$
i code the thrust of each propeller in a list, and then use this list to compute the $u$. so i had: $F = [b\Omega_1, b\Omega_2, b\Omega_3, b\Omega_4]$
then $u_1$ is the sum of F and $u_2 = l*(Ft[3]-Ft[1])$ etc.
but i mistankenly wrote $u_2 = bl*(Ft[3]-Ft[1])$ so i had a b in excess which cause the whole system to misbehave. I probably had in mind the expression of $u_2$ in term of $\Omega$ while in code the $u$ are in term of thrust.