============================= PSP Toolbox ============================= .. figure:: /images/Quan-ch3-Fig3.13.jpg :align: center Fig. 3.13 Relationship between Simulink and Pixhawk autopilot code generation Figure 3.13 shows the relationship among the PSP toolbox, the PX4 software, and the Pixhawk hardware. The main features of the toolbox are summarized below. (1). The toolbox can simulate and test different multicopter models and flight control algorithms in Simulink and then automatically deploy the algorithms to the Pixhawk autopilot. (2). The toolbox provides many practical examples, including LED control, RC data process, and attitude controller. (3). The toolbox provides many interface modules to access the Pixhawk hardware and software components. (4). It automatically records flight data from sensors, actuators, and controllers deployed by themselves. (5). It can subscribe and publish uORB topic messages. All messages in the PX4 autopilot software are temporarily stored in a uORB message pool. The subscription function can read topics of interest from the message pool, and the publishing function can publish specific topics to the message pool for other modules. The relationship between the code generated by Simulink and the Pixhawk autopilot system is summarized below. (1). The structure of a Pixhawk autopilot system includes two parts, namely the Pixhawk hardware (similar to the computer hardware) and the PX4 software (similar to the operating system and applications running on a computer). (2) The PX4 software system can be further divided into several small modules, which run independently in parallel multi-thread. Each module exchanges data with other modules through the subscription and publication of uORB messages. (3). After the algorithm code is generated by the PSP toolbox, it is embedded into the PX4 software system. This will not affect the operation of the native control modules in the PX4 software. Instead, a new independent module (with an independent thread) named “px4_simulink_app” will be created to run in parallel with other modules. (4). As shown in Fig. 3.13, the whole code generation and deployment procedures are presented as follows. 1). The PSP toolbox generates the C/C++ code from the control algorithm designed in Simulink. 2). The obtained algorithm code is imported into the PX4 source code to generate a “px4_simulink_app” independent of other modules. 3). The PSP toolbox calls the compiling toolchain (Win10WSL, Msys2, or Cygwin) to compile all the code into a “.px4” PX4 firmware file (similar to a software installation package). 4). Upload the obtained firmware file to the Pixhawk hardware; then, the Pixhawk autopilot can execute the PX4 software with the generated algorithm code. (5). The native modules in the PX4 software may assess the same hardware outputs as the generated “px4_simulink_app” module, which may cause read and write conflicts. Therefore, in the one-key installation script, the hardware output accessing codes of the PX4 native modules have been blocked in the last option shown in Fig. 2.4. This will ensure that only the “px4_simulink_app” module can send motor control signals. .. figure:: /images/Quan-ch3-Fig3.14.jpg :align: center Fig. 3.14 Relationship between PX4 native modules and module generated by Simulink (6). The generated Simulink code can also be used to replace some of the native modules (sensors, filters, attitude controllers, etc.) of the PX4 software shown in Fig. 3.13. However, the PX4 software code needs to be manually modified to block the output interface of the corresponding native module (Another feasible way is to block the module in the startup script “Firmware\ROMFS\px4fmu_common\init.d\rcS”). For example, if readers want to use Simulink to replace the filter module (input sensor data, output filter data) of the PX4 software, they should manually block uORB publishing code of the PX4 “Position and Attitude Estimator” module in Fig. 3.14 to prevent it from publishing estimate data. The detailed steps are described next. 1). Open the “Firmware\ src\modules\ekf2\ekf2_main.cpp” file (corresponding to the code of the extended Kalman filter module). 2). Search for the text “orb_publish_auto(ORB_ID(vehicle_attitude)” and comment out the related code. Simulink Pixhawk Target Blocks Library of PSP Toolbox -------------------------------------------------------- As shown in Fig. 3.15, after installing the PSP toolbox, a “Pixhawk Target Blocks” interface module library can be found in the Simulink library browser. These modules provide interfaces to access the Pixhawk hardware I/Os and the PX4 internal messages. The “Pixhawk Target Blocks” library consists of four sub-libraries, namely “ADC and Serial library”, “Miscellaneous Utility Blocks” library, “Sensors and Actuators” library, and “uORB Read and Write” library. .. figure:: /images/Quan-ch3-Fig3.15.jpg :align: center Fig. 3.15 Simulink PSP module library Figure 3.16 presents several key I/O interface modules in the “Sensors and Actuators” library. These modules make it easy to acquire the sensor data or estimate data for designing flight controllers that compute output signals for the motors, LEDs, and buzzers. .. figure:: /images/Quan-ch3-Fig3.16.jpg :align: center Fig. 3.16 Schematic diagram of PSP toolbox sensor and actuator interface library As shown in Fig. 3.17, the PSP toolbox also provides many examples (see folder `e0\2.PSPOfficialExps `_ ) with an official manual (see document `e0\2.PSPOfficialExps\Pixhawk_Pilot_Support_Package.pdf `_ for details) for readers to be quickly familiar with functions and usage methods of PSP toolbox. .. figure:: /images/Quan-ch3-Fig3.17.jpg :align: center Fig. 3.17 Official examples and manuals for PSP toolbox Instructions for Modules in PSP Toolbox ------------------------------------------ (1). RC input module Figure 3.18 presents the RC input module and its parameter setting box. It is convenient to select RC channels and other information to be used by Simulink. The definition and application of each option can be viewed by clicking the “help” button of the box or by consulting the official PDF document. The PSP toolbox also provides an example ( see file `e0\2.PSPOfficialExps\px4demo_input_rc. slx `_) to show how to use this module. .. figure:: /images/Quan-ch3-Fig3.18.jpg :align: center Fig. 3.18 RC input module and its parameter setting box (2). PWM output module Figure 3.19 depicts the PWM output module, which is used to send PWM signals to PX4IO ports to control the motor. The PWM update frequency and the number of output channels can be configured in the setting box. .. figure:: /images/Quan-ch3-Fig3.19.jpg :align: center Fig. 3.19 PWM output module and its parameter setting box (3). FMU output module Figure 3.20 presents the FMU output module, which is used to send PWM signals to PX4FMU ports to control the servo deflection. The PWM update frequency and the number of output channels can be configured in the setting box. .. figure:: /images/Quan-ch3-Fig3.20.jpg :align: center Fig. 3.20 FMU output module and its parameter setting box (4). Buzzer module Figure 3.21 presents the Buzzer module, which is used when the buzzer is required to make a warning sound. There is an example (see file `e0\2.PSPOfficialExps\px4demo_tune.slx `_) for detailed information. .. figure:: /images/Quan-ch3-Fig3.21.jpg :align: center Fig. 3.21 Buzzer module and its parameter setting box (5). RGB_LED module This module can control the blink mode and color of the LED on Pixhawk. As shown in Fig. 3.22, the module receives two inputs, namely “Mode” and “Color” representing the mode and color of the LED. The PSP toolbox provides an example (see file `e0\2.PSPOfficialExps\px4demo_rgbled.slx `_) to study this module. .. figure:: /images/Quan-ch3-Fig3.22.jpg :align: center Fig. 3.22 LED light module and its parameter setting box (6). Sensor combination module This module can access the sensor data available in the Pixhawk autopilot, which can then be used for controller design in Simulink. Available sensor data include magnetometers, accelerometers, gyroscopes, barometers, and timestamps. As shown in Fig. 3.23, the sample rate and the required sensor data can be configured in the parameter setting box. The PSP toolbox also provides an example (see file `e0\2.PSPOfficialExps\px4demo_attitude_control.slx `_) to study this module. .. figure:: /images/Quan-ch3-Fig3.23.jpg :align: center Fig. 3.23 Sensor combination module and its parameter setting box (7). Attitude data module As shown in Fig. 3.24, the attitude data module provides an interface to access the attitude estimate (Euler angles and quaternion). The PSP toolbox also provides an example (see file `e0\2.PSPOfficialExps\px4demo_attitude_control.slx `_) to study this module. .. figure:: /images/Quan-ch3-Fig3.24.jpg :align: center Fig. 3.24 Attitude data module and its parameter setting box (8). GPS data module This module, shown in Fig. 3.25, can be used to access the Pixhawk GPS data, which are achieved by subscribing to the uORB topic “vehicle_gps”. Therefore, in practical operation, it is necessary to ensure that the GPS module is inserted into the Pixhawk hardware and then works. The PSP toolbox also provides an example (see file `e0\2.PSPOfficialExps\px4demo_gps.slx `_) to study this module. .. figure:: /images/Quan-ch3-Fig3.25.jpg :align: center Fig. 3.25 GPS data module and its parameter setting box (9). Battery data module This module, shown in 3.26, can be used to obtain the real-time status of the battery. It is implemented by subscribing to the uORB topic “battery_status”. Therefore, in practical operation, it is necessary to ensure that the power module is inserted into the Pixhawk hardware and then works correctly. .. figure:: /images/Quan-ch3-Fig3.26.jpg :align: center Fig. 3.26 Battery data module and its parameter setting box (10). uORB modules These modules, presented in Fig. 3.27, are used to read or write uORB messages from the PX4 autopilot software. All the uORB message topics supported by the PX4 autopilot are listed in the directory “Firmware\msg” of the software package installation directory (configured as in Fig. 2.4; the default directory is “C:\PX4PSP”). .. figure:: /images/Quan-ch3-Fig3.27.jpg :align: center Fig. 3.27 uORB modules for message reading and writing Double-click the “uORB Write” module in Fig. 3.27, then the obtained parameter setting box of the “uORB Write” module is presented in Fig. 3.28, where the uORB topic name and the message variables to be sent can be configured. .. figure:: /images/Quan-ch3-Fig3.28.jpg :align: center Fig. 3.28 “uORB Write” module parameter setting box Clicking the “Open .msg file” button in Fig. 3.28 yields the content of the select “.msg” file (see Fig. 3.29), and clicking the “Open .msg folder” button yields the list of all supported uORB messages (See Fig. 3.30). .. figure:: /images/Quan-ch3-Fig3.29.jpg :align: center Fig. 3.29 uORB message file .. figure:: /images/Quan-ch3-Fig3.30.jpg :align: center Fig. 3.30 Pop-up window of “Open .msg folder” button There are two advanced “uORB Write” modules presented in Fig. 3.31, which provide more convenient ways to send uORB messages. .. figure:: /images/Quan-ch3-Fig3.31.jpg :align: center Fig. 3.31 Advanced “uORB Write” modules and difference between them In fact, all modules (PWM output, RGB_LED, etc.) mentioned in this section are implemented at the underlying code by reading and writing uORB messages. Theoretically, by using the “uORB Read and Write” modules, all messages and intermediate variables used in the PX4 autopilot can be accessed by Simulink. This simplifies the implementation of more advanced functions for controller design. The PSP toolbox also provides two examples (see file `e0\2.PSPOfficialExps\px4demo_fcn_call_uorb_example.slx `_, and file `e0\2.PSPOfficialExps\px4demo_write_uorb_example.slx `_) to study this module. In the PX4 development website, there are detailed documents for creating a `new uORB message `_ and receiving a `new MAVLink message `_ to communicate with external devices. In addition to the uORB modules presented in Fig. 3.27, it is convenient for the Simulink controller “px4_simulink_app” to exchange data with external devices, such as cameras, sensors, and host computers. (11). Accessing PX4 internal parameters For the sake of convenience for controller parameter tuning in flight tests, the PSP Toolbox also provides interfaces to access the PX4 internal parameters. In this way, the parameters of the controller generated by Simulink can be tuned online in the GCS software, instead of modifying the controller parameters in Simulink, generating code, and uploading the firmware file again. As shown in Fig. 3.32, an example of how to access the PX4 internal parameters is presented in file `e0\2.PSPOfficialExps\px4demo_Parameter_CSC_example.slx `_. .. figure:: /images/Quan-ch3-Fig3.32.jpg :align: center Fig. 3.32 Example of PX4 internal parameter reading .. figure:: /images/Quan-ch3-Fig3.33.jpg :align: center Fig. 3.33 Simulink initialization script for accessing PX4 parameters PX4 internal parameter access is realized by using the function “Pixhawk_CSC.Parameter( {`*`, `*`})”, which needs to be called in the Simulink initialization function ( click “Simple”—“Model Properties”—“Callbacks”—“InitFcn” in the Simulink menu bar). For the example shown in Fig. 3.32, the corresponding parameter initialization script is shown in Fig. 3.33. Simulink Configuration for Code Generation of PSP Toolbox ----------------------------------------------------------- (1). Preparation of the Simulink controller for code generation The preparation procedure is described below. 1). As shown in Fig. 3.7, design a controller in Simulink and verify it with SIL simulations. 2). Copy the verified controller to a new Simulink file. 3). Connect the input and output ports of the controller subsystem with the input (e.g., combined sensor module and RC input module) and output (e.g., PWM module and uORB modules) interface modules in the PSP module library presented in Fig. 3.15. 4). An example of the obtained Simulink controller file is presented in Fig. 3.34. The example file is available in `e0\2.PSPOfficialExps\px4demo_attitude_system.slx `_. .. figure:: /images/Quan-ch3-Fig3.34.jpg :align: center Fig. 3.34 Example of Simulink controller connecting with PSP modules (2). Open the Simulink setting panel The new created Simulink file must be configured to support the code generation function of the PSP toolbox. First of all, as shown in Fig. 3.35, the Simulink setting panel can be opened by clicking “Simulation”—“Model Configuration Parameters” in the Simulink menu bar. .. figure:: /images/Quan-ch3-Fig3.35.jpg :align: center Fig. 3.35 Simulink “Settings” button for different MATLAB versions (3). Setting for PSP code generation As indicated in Fig. 3.36, go to the “Hardware Implementation” tab and select the “Pixhawk PX4” item in the pull-down menu of the “Hardware board” option. Then, all necessary parameter setting for PSP code generation is automatically configured. .. figure:: /images/Quan-ch3-Fig3.36.jpg :align: center Fig. 3.36 Selecting target hardware (4). Source code compilation and firmware generation Click the “Build” button in Fig. 3.37 to convert the Simulink controller into C/C++ code and then compile it into the PX4 firmware. As shown in Fig. 3.38, the code generation and compiling process can also be observed by clicking the “Diagnostics” button on Simulink. .. figure:: /images/Quan-ch3-Fig3.37.jpg :align: center Fig. 3.37 Simulink “Build” button for different MATLAB versions .. figure:: /images/Quan-ch3-Fig3.38.jpg :align: center Fig. 3.38 Simulink “Diagnostics” button for different MATLAB versions A successful compiling process in the “Diagnostic Viewer” dialog is shown in Fig. 3.39, where the compiling process is finished with the following text “Successfully generated all binary outputs”. It can also be observed in Fig. 3.39 that a “Code Generation Report” document will pop up after the compiling process is finished. .. figure:: /images/Quan-ch3-Fig3.39.jpg :align: center Fig. 3.39 Display dialog of code generation and firmware compiling (5). Upload PX4 firmware to Pixhawk hardware Use the one-key upload function provided by the PSP toolbox to upload and burn the firmware to the Pixhawk hardware. The specific steps are described below. 1). Use a USB cable to connect the MicroUSB port (on the side of the Pixhawk hardware) with the USB port on the computer. 2). As shown in Fig. 3.40, for MATLAB 2017b–2019a, click “Code”—“PX4 PSP: Upload code to Px4FMU” on the Simulink menu bar, then the firmware will be automatically uploaded to the Pixhawk autopilot; for MATLAB 2019b and above, since the Simulink menu is deprecated, readers can input the “PX4Upload” command in the “Command Window” of the MATLAB interface to upload the firmware . .. figure:: /images/Quan-ch3-Fig3.40.jpg :align: center Fig. 3.40 Firmware upload methods for different MATLAB versions 3). Check the pop-up window carefully; sometimes, the Pixhawk autopilot has to be re-plugged to start the firmware uploading process. After completing the above steps, the controller designed in Simulink has been run on the Pixhawk autopilot.