## 4 HIL Simulation
### Sensor Details  
To simulate the inclination sensor, a gazebo sensor plugin for an inertial measurement unit (IMU) is used and placed, using a fixed joint, on the load part. This sensor allows the measurement of the inclination angles of the load, relative to their fixation point on the quadrotor and their rates. This data is published on both a Gazebo link state and a separate sensor ROS topic, so it can be later processed. Below is the code snippet for the sensor and defined parameters that are appended to the SDF. The sensor adds a Gaussian noise to simulate real-life performance and has a switch to turn on or off, in case it is needed to simulate a failsafe. The update and publish rate is also variable and can be easily adjusted to different rates to suit the performance needs.  
Figure 10: Flying quadrotor with the suspended load.  
Figure 9: Quadrotor with the suspended load on the ground.  
```
``` <bodyName>base_footprint</bodyName> <topicName>imu</topicName> <serviceName>imu_service</serviceName> <gaussianNoise>0.0</gaussianNoise> <updateRate>20.0</updateRate> </plugin> </gazebo>
```

This plugin publishes its reading to a ROS topic defined by the parameter \"<topicName>\", this data is then synchronized with the PX4 using a Mavlink message through MAVROS then sent to the control algorithm.

The plugin approach can be replicated by reading the link state directly from gazebo, but it is better to use external plugins to measure drone behavior on the readings.

### MAVROS Plugin

MAVROS is a communication node between ROS and the Mavlink protocol to facilitate algorithm and application development. The Mavlink system sends data in a structured format called \"messages\", where the message is instantiated as an object and its contents are parameters to that object. The code below shows the structure of the \"swing_angles\" message that was created specifically for this work.

```
uint64 timestamp#timesincesystemstart(microSeconds)
```

float32 ph#Phiwingangle float32 theta#Thetawingangle float32 ph#Phiwinganglerateofchange float32 theta#Thetawinganglerateofchange float32 theta#Thetawinganglerateofchange The message contains a timestamp header to ensure synchronization with the main IMU in the Pixhawk. The plugin performs two main functions: during simulation, it is used as a relay for the simulated sensor from the Gazebo to the controller and the PX4 module through Mavlink; meanwhile, in real testing, the plugin receives the sensor message from the PX4 and relays it back to the companion computer and then publishes it through ROS for the controller. The first function is useful in determining the suitable update rates that can function correctly with the PX4 firmware and various Pixhawk boards, without overloading the processor with a large data stream.

### Mavlink Message Definition

The Mavlink message sent by Mavros should be defined in the Mavlink headers of both the sender and receiver; thus, the Mavlink headers in both Mavros and PX4 are regenerated with the newly enumerated message so it can be transmitted. This enumeration follows the Mavlink message template definition so it can be implemented to any Mavlink-supported flight stack. The message definition snippet is shown below.

``` <messageid=\"228\"name=\"SWINGANGLES\"> <description>TransmitswingsingsingsfromsuspendedpayloadsensortoPixhawk </description> <fieldtype=\"uint64t\"name=\"time_usec\"units=\"us\">Timestamp(syncedtoUNIXtimeorsincesystemboot).</field> <fieldtype=\"float\"name=\"phi\">Swinganglephi</field> <fieldtype=\"float\"name=\"theta\">Swingangletheta</field> <fieldtype=\"float\"name=\"phi\">SwingangleratePhi</field> <fieldtype=\"float\"name=\"theta\">Swingangleratetheta</field> <fieldtype=\"float\"name=\"theta\">Swingangleratetheta</field> <fieldtype=\"float\"name=\"theta\">Swingangleratetheta</field> <fieldtype=\"float\"name=\"theta\">Swingangleangleratetheta</field> <fieldtype=\"float\"name=\"theta\"