## 3 Simulation Environment and Approaches
### Software in the Loop  
To ensure the correct functioning and overall performance of the ADCS, both for controller debugging and characterization of its performance under several scenarios, SIL simulations were executed and evaluated using the Monte Carlo analysis to evaluate the controller's efficiency. Moreover, the development of the SIL environment was built upon the MIL environment. Since it is intended that the implementation of the control algorithm be executed through real-time simulation in Simulink, interfaced with an STM32 microcontroller, and taking into account that sensor readings are performed directly by the controller, the Embedded Coder application within Simulink was used to generate and optimize C code for each subsystem. The code generation workflow required aligning the sample times of the simulations with that of the controller to ensure single-rate code generation (avoiding timing functions that are only available for CPUs). The controller processed signals from the plant model and computed control signals within the defined sample interval. The solver configuration was set to fixed step, using the Real-Time ODE1b for environments that includes SimScape models and RK4 for the remaining. The system's target file was configured to err.tlc, and the programming language was set to C99 (ISO) in the Code Generation section in the model setting configuration parameters, which is widely supported by most embedded systems. It is crucial to confirm that both Simulink and STM32 Cube IDE are configured to use the ISO C99 standard [50]. To ensure this configuration in Simulink, the process involved accessing \"Model Settings\", selecting \"Code Generation\", and choosing \"C99 (ISO)\" under \"Language Standard\". Activating C99 in STM32 Cube IDE required navigating through \"File\", then \"Properties\", selecting \"C/C++ Build\", accessing \"Settings\", and choosing \"Toolchain Settings\", with \"GNU99 (ISO C99+gru extensions)(-std=gru99)\" selected under \"Language Standard\". For effective code generation, parameters must be handled as atomic units and the model should be converted to a referenced model. This conversion allows the specification of variables and ensures that the behavior of the referenced model remains consistent, regardless of its position in the model hierarchy. Before initiating code generation, it is essential to establish specific naming rules for auto-generated identifiers, tailored to each subsystem. This necessity arises from the requirement to generate multiple codes within the same Simulink model. In our scenario, three codes were generated: nadir controller, detumbling controllers, and a filter for the magnetometer measurements. Consequently, the name of the file in the \"Code for component\" section was modified accordingly. Upon configuring these settings, the Embedded Coder app could be used to generate the code and build the model. The generated library files were stored in a designated folder, named in the penultimate step of the process. To deploy the C code on the C-caller function (Simulink function for executing \(c\) and _.h_ files) or hardware, the library files were included in the Simulink workspace and also copied into the Inc directory of the STM project. Within the main project file, it was necessary to invoke the model_initialize function and the model_step function to facilitate proper integration and execution of the SIL simulation or hardware deployment. In this way, the ADCS model developed in MIL was deployed in C code, allowing for an optimized code of the entire system. Furthermore, once the model was converted into C code and executed by SIL, it strongly suggested embedding compatibility into an STM32 Nucleo board for further testbed trials to validate the attitude controller. Moreover, to optimize SIL time consumption, and computational demands and to address computational overheads, all of the remaining model functions were turned into subsystems which could then be turned into Matlab S functions.