# Quadrotor Formation Strategies Based on Distributed Consensus and Model Predictive Controls
## 6 Simulation Preparation
### Simulink Environment Setup for the Formation Simulation
The leader's pitch/roll control submodule, _MPC Control(Pitch, Roll)_ in Figure 8, includes the waypoints datasets (orange dashed square), the waypoints update rules (orange square), the block _Preset Constant_ which includes the formation pattern and the parameters for the waypoint update rules, the MPC algorithm _Leader MPC Algorithm_, and the function _Follower ref. Trajectory Generator_ that generates the followers' reference trajectories as shown in Figure 10. The waypoints datasets in the orange dashed square includes the coordinate of the waypoints in frame \(I\), named as _L_wp_, and the number of the waypoints, named as _wp_length_. The waypoint update rules update the waypoint if the distance between the leader and the waypoint, denoted by _R2 wp_, is smaller than \(d_{2}\) and hold at the last waypoint if _R2 wp_ is smaller than \(d_{2}\). Parameter \(d_{1}\) is used under the circumstances that if the user designates the followers to hold a specific position in which the last waypoint is reached. Function _Leader MPC Algorithm_ solves the optimal tracking control input \(M\) by using Equation (29) with the given weighting matrices, the horizontal state of the leader, and the horizontal state-space model. The predicted trajectory for the leader is computed by Equation (23). Note that the horizontal state and the state-space model that are adopted in the _Leader MPC Algorithm_ are the modified state \(\widetilde{\mathbf{x}}_{h}\) and the normalized state-space model in Equation (44). In addition, the control output by the _Leader MPC Algorithm_ is the modified control in Equation (43). Function _Follower ref. Trajectory Generator_ computes the reference trajectories for the followers based on the method in Section 5.1.1 using the leader's predicted trajectory and the formation pattern. The leader's vertical velocity _L_velocity_w_ is input into the function to compute the leader's velocity in frame \(I\) using Equation (8). The control submodule shown in Figure 11 is the control submodule of Follower #1. The follower's control submodule first computes the complete desired formation trajectories, including the information of the velocity, attitude, and angular velocity, using the finite difference method coded in function _FDM Algorithm_. Then, the function _Consensus Algorithm_ computes all the followers' flight paths over the predicted time horizon based on the consensus control (15), modified state \(\widetilde{\mathbf{x}}_{h}\) (43), and the normalized state-space model (44). The signal, labeled as _rN_, is the modified state of the follower under consideration and the signal, labeled as _rNF_all_, contains all the modified states of other followers connected to it in the network structure. In the case shown in Figure 11, _rN_ is the modified state of Follower #1 and _rNF_all_ contains the modified states of Follower #2 and Follower #3. After the flight paths are computed, the function _MPC algorithm_ engages. The _MPC algorithm_ computes the optimal tracking control by using Equation (29) based on the modified state (43), the normalized state-space model (44) of the quadrotor, and the corresponding flight path. Figure 9: Configuration of altitude control of leader (**top**) and follower (**bottom**).