NOTE: This project is gradually improving. It is fully functional at this point, but it could be better.

**Considerations:**

The vehicle model is simplified as a box shape with forward speed equalling zero. A gimbal is
attached to the body of the vehicle, so the vehicle and gyroscope are considered as two separate
rigid bodies that are kinematically constrained. Additionally, only consider the roll motion in YZ
plane and the rotating speed of flywheel is constant, see figure below.

- a-Frame: The earth-fixed inertial reference frame;
- b-Frame: Fixed to the vehicle body;
- c-Frame: Fixed to the Control Moment Gyroscope (CMG) casing
- rho: Roll angle of the vehicle b-Frame about the 1-axis with respect to the a-Frame (degrees);
- theta: Gimbal angle of the CMG c-Frame about the 2-axis with respect to the b-Frame (degrees);
- omega: Rotation speed of the CMG wheel (RPM).

**Model:**

The equation of motion in roll is derived as a nonlinear 4th order differential equation, wich states all the torques on the vehicle about rho axis. The left side is an inertial term due to the masses of the bicycle and gyro, which is the angular acceleration times the moment of inertia. The first term on the right is Coriolis term since it involves the product of the two rates and . These terms arise when dealing with systems having multiple rotating reference frames. Second term is righting torque generated by gyro and the last term represents the heeling moment due to gravity.

Once the equation describing the system was obtained, the model was then
tested by means of computer simulation. First, the system was described in
state-space form x* = Ax + Bu:

And linearized at upright position, which means rho*=theta*=rho = 0

**Control:**

After defining all the physical parameters and response speed, with the help of state-space form,
the control gains K are calculated from the linear approximation model by using Ackermann's
formula (in MATLAB the function name is Acker) which is a solution for pole assignment method.
Pole assignment is a method to adjust the poles of a given system by adjusting the feedback gains
from state variables.

The gain factor K is a PD controller to calculate theinput precession angular speed (theta*):

The controller gain K guarantees the motor properly generate necessary torque to tilt the gyro and thus keep the vehicle stable dynamically.

**Signal processing:**

To estimate the vehicle angle, was used a low pss filter with cutoff frequency of 2Hz in the accelerometer data acquired in X,Y and Z by the function discretized in 250Hz:

Then, the filtered acceleration was converted to absolute angle using the equation:

Which passes through a moving average of 50 samples and the angular velocity that comes from the gyroscope passes through a moving average of 35 samples.

Since the actuator input is angle and the calculated control input is angular acceleration, the control must be integrated to actually actuate on the system with the MG90S.

**Prototype demonstration:**