Pixhawk - Pilot - Support - Package V 3.04
Pixhawk - Pilot - Support - Package V 3.04
Pixhawk - Pilot - Support - Package V 3.04
User Guide
Version 3.04
MathWorks
The PSP provides the ability to build and download to a PX4 board unit. It does not provide exact
function behavior blocks for other services running on the Pixhawk (e.g. Attitude Estimation using
EFK or SOF). The user will need to use blocks from the base Simulink or possibly the Aerospace
blockset for simulating their flight control system model. Once the flight control system (FCS) has
been successfully modeled, simulated and verified, the Pixhawk Target can be used to deploy the
control system onto the PX4 hardware.
The Pixhawk Simulink blocks allows users to access sensor data and other calculations available
to be used in their Simulink model at runtime. Generated code can then be compiled using the
PX4 CMake build system.
The Pixhawk Pilot Support Package is based off a forked version of the official Pixhawk
Firmware. This forked version can be found here.
https://github.com/mathworks/PX4-Firmware/tree/PixhawkPSP_v3.0.3
During the PSP installation process, a download script will automatically clone this repository.
This forked version is roughly based off of the 1.6.5 tag
https://github.com/PX4/Firmware/releases/tag/v1.6.5
A NuttX application called “px4_simulink_app” is created using this PSP and code generation
tools. This application follows the same code structure and format depicted here.
http://dev.px4.io/tutorial-hello-sky.html
This Pilot Support Package has been tested with the Pixhawk (px4fmu-v2) and the Pixhawk
Mini (px4fmu-v3) which can be configured to run different CMake configurations through the
installation process. We have tested the “default” configuration but we also allow you to specify
your own custom CMake configuration.
Please ensure that you select the correct CMake option which matches the board you are
targeting:
https://dev.px4.io/en/setup/building_px4.html
For example:
Pixhawk 1: make px4fmu-v2_default
Pixhawk Mini: make px4fmu-v3_default
Since this package is generating code for a Simulink PX4 module, our PSP adapts the Simulink
code generation and compilation process to fit into the Pixhawk build environment by making
use of CMake. A CMake command is executed compile the Pixhawk Firmware to invoke
compilation.
Ideally, one should be familiar with the embedded software environment of the PX4 platform
prior to using this Pilot Support Package. For more information on this, refer to the later sections
that go into details about the code generation process as well as the PSP installation section.
1.2 Acronyms/Definitions
Pixhawk (PX4) – the Flight Controller Unit providing various sensor value inputs and PWM
outputs as well as an ARM Cortex-M4 microprocessor for flight control and management.
PSP – Pilot Support Package. MathWorks software offering customized feature development
or updates that are not yet available in the officially released version of MATLAB/Simulink.
TLC – Target Language Compiler
BTI – Built Tool Integration
FMU – Flight Management Unit
PWM – Pulse Width Modulation
RC – Radio Control
Tx/Rx – Transmitter/Receiver
ESC – Electronic Speed Controller
NED – North-East-Down
2 System Requirements
2.1 MATLAB/Simulink Toolboxes
To generate code from a Simulink model, the following products are needed:
• MATLAB R2017a / R2017b
o Note: This Pilot Support Package has not been tested on R2018a
• Simulink
• Simulink Coder
• Embedded Coder
• Aerospace Blockset is needed for some of the example models
• Instrument Control Toolbox is needed for some data acquisition examples
Once this has been done, the next step is to install the necessary PX4 dependencies
(Cmake, python, cross-compilers) for the Windows-Ubuntu bash environment. A shell script
has been provided to download and set this up.
2.3 Required (Linux)
• arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 5.4
• Python
• CMake (tested with 3.5.1)
NOTE (Linux): Compilation of the firmware can actually fail on newer versions of the arm-
none-eabi-gcc. All tests with the Pixhawk PSP were conducted on version arm-gcc 5.4
A shell script has been provided to download the necessary PX4 dependencies. More detail
on this will be provided below.
3 Installation
This PSP is supported on Win64 and Linux platforms. By running the MLTBX installer it will
copy/paste files to your MATLAB Add-ons folder which you configure under your MATLAB
preferences.
NOTE: This section assumes you have NOT setup your Ubuntu bash for Windows with the
necessary PX4 build environment dependencies
After you have setup and installed the Ubuntu-Linux bash shell, the next step is to setup this bash
terminal with the correct cross-compiler and other dependencies. A script has been provided in
<Location of installed PSP>\PX4PSP\code\px4\Win10bash_shell_setup\windows_bash_nuttx.sh
This script was originally based off a script provided by the PX4 developers:
https://dev.px4.io/en/setup/dev_env_windows.html
The only major difference between the above script and what is provided is that we omitted the
downloading of the PX4 Firmware. This is done at a different step.
NOTE: This section assumes you have NOT setup your Linux with the necessary PX4 build
environment dependencies.
1) Install gcc-arm-none-eabi 5.4. There are numerous ways to do this - here is one approach
which uses the exact same tool-chain used in the Windows 10 Bash
wget https://github.com/SolinGuo/arm-none-eabi-bash-on-win10-/raw/master/gcc-arm-none-
eabi-5_4-2017q2-20170512-linux.tar.bz2
tar -jxf gcc-arm-none-eabi-5_4-2017q2-20170512-linux.tar.bz2
exportline="export PATH=$HOME/gcc-arm-none-eabi-5_4-2017q2/bin:\$PATH"
if grep -Fxq "$exportline" ~/.bashrc; then echo " GCC path already set." ; else echo
$exportline >> ~/.bashrc; fi
. ~/.bashrc
2) Run install shell script to setup build tools, CMAKE, python, other dependencies
The ‘ubuntu_sim_common_deps.bash’ script does this for you. This is originally based on the
bash script provided here:
https://raw.githubusercontent.com/PX4/Devguide/master/build_scripts/ubuntu_sim_common_deps.sh
PixhawkPSP('<Location of Firmware>')
Where <Location of Firmare> represents the folder path of where the PX4 firmware will
be. Note that this folder must exist – you can select a folder with existing firmware so long as that
firmware originated from
https://github.com/mathworks/PX4-Firmware/tree/PixhawkPSP_v3.0.3
If you get the above error you may need to use “Sysnative” rather than “System32” which can be
specified in the “Windows 10 bash” text field.
Download Firmware
The PX4 firmware which is forked on the MathWorks GitHub will be cloned to the string
argument you passed in to the ‘PixhawkPSP’ function. The other option is to manually
clone the firmware with git commands and then point to the parent folder where the firmware
exists using the PixhawkPSP('<folder location command>').
This command will open up a Windows 10 bash terminal and run a git clone firmware
command. You can use the Validate Firmware button to confirm that the firmware which was
cloned contains the PX4 Simulink module
Cmake Configuration
Next, select the CMAKE firmware. If you're targeting the Pixhawk Mini and the Pixhawk 2.1
Cube, we’ll want to select v3. If you plan to target other PX4 platforms, you can click on the
"custom" option and enter in the name of the make-file. Note that you will need to make the
necessary modifications to the CMAKE / src files to add the PX4 Simulink App. To see which
make file corresponds to the correct hardware platform, please see
https://dev.px4.io/en/setup/building_px4.html
So for instance, if you wanted to support Pixhawk 3 Pro you would use the “specify”
checkbox and then type
“px4fmu-v4pro_default”
Build Firmware
Next, build the firmware. This is a step will build most of the firmware such that when it comes
to compiling the generated code the time taken on the first build will not build from the very
beginning. Building the firmware will also create some necessary files for various header files
needed by the build process.
You can launch a serial terminal program like TerraTerm or PuTTY and connect to the PX4
and manually run the built-in commands using the nuttx shell. NuttX is the OS that is
delivered with the Pixhawk toolchain and will be used for running the code generated from
your Simulink models.
You can find out which “Builtin” Apps your firmware has by typing “?” at the nuttx shell prompt
“nsh>”
nsh> ?
help usage: help [-v] [<cmd>]
df kill mkrd rm unset ? echo losetup mh rmdir usleep
cat exec ls mount set xd cd exit mb mv sh cp free
mkdir mw sleep cmp help mkfatfs ps test dd hexdump
mkfifo pwd umount
Builtin Apps:
sercon
serdis
adc
attitude_estimator_ekf
bl_update
blinkm
boardinfo
commander
…
4) top – will list all the NuttX processes running at the time (press ‘q’ to quit)
Processes: 11 total, 2 running, 9 sleeping
CPU usage: 37.36% tasks, 0.48% sched, 62.16% idle
Uptime: 904.233s total, 561.039s idle
The script’s filename is rc.txt. It should be copied to the SD-card directory /etc. A script
sample has been provided by the PSP installation and can be found in your Pixhawk
Toolchain installation directory: <Selected Firmware Location>\example_rctxt\rc.txt. By
copying this file to your SD-card in the folder /etc the Pixhawk will execute the
px4_simulink_app at system startup. This app is built into the firmware that is flashed onto
your PX4 hardware at Simulink Model build time. Simply renaming this file (e.g. rc.txt to
rc.txt.simulink) on the SD-card will allow boot-up of the default flight control software.
NOTE: Newer release of the Pixhawk firmware has changed how the boot-up tone is played
by moving it to the ‘commander’ application. Because we are not using the commander
application and instead running our own boot sequence from the SD card, you will not hear
the boot-up sound. You can manually add a tone alarm sequence in the rc.txt file to indicate
successful boot-up.
In order for the firmware to properly function, the uorb task must be executed upon startup
(uorb start). Many of the Simulink Blocks that generate code interacting with the PX4
hardware rely on the uORB mechanism.
The next section will talk more about how the Pixhawk Pilot Support Package creates the
px4_simulink_app application from generated code.
The Pixhawk target uses MathWorks Build Tool Integration (BTI) to allow MATLAB to invoke
the ARM-GCC compiler to build px4_simulink_app. The system target file needs to be ert.tlc
(Embedded Real-Time) which is available with Embedded Coder. The user is then able to
choose the hardware and toolchain. If the target hardware is set to ‘Pixhawk’, then the
appropriate toolchain (Pixhawk) will be chosen automatically.
The Pixhawk firmware now uses a CMake build process. We have adapted the PSP to take
advantage of this. This is separated into different parts:
Simulink Settings
For the model to target the PX4 hardware, the Simulink model must be configured to use the
appropriate code generation options. Go to the Hardware Implementation page and select
Pixhawk PX4 to do this. Note that in previous releases this selection was done in the Code
Generation panel, but nowit has moved into Hardware Implementation. The code generation
panel should automatically update the labeled items one through four (1-4) to select the
correct compiler and build configurations.
There are a few other settings which are required for this version of the PSP. These are:
1) Solver Type should be set for Fixed-Step (for embedded code generation)
2) Model Optimization Option Inline Params must be 'on' for Pixhawk code generation
Inline parameters setting is highly recommended due to the limited resources in global
memory and constraints on the Pixhawk target. Inline parameters places all model
parameters (ie: gains) as “inline” constants or variables on the function stack rather. You
will receive an error if this setting is not adjusted in your model.
Under the Hardware Implementation pane there are several Target Hardware Resource
Options. These are explained in detail below.
4) External Mode Options – Please see the external mode chapter documentation.
These options configure which serial port settings to use to setup external mode
communication.
5) Uploading Options (Windows Only) – For uploading to the Pixhawk FMU, we can
either force it to connect to a port manually or we can tell MATLAB to search for the
correct COM port and connect automatically. Once MATLAB determines the COM
port it will continue using it without having to search again or until the COM port value
changes for connecting to the PX4 FMU.
Building the Firmware
The firmware for model can be generated by pressing the ‘Build’ icon on the toolbar:
The firmware will then start to build, starting with the generated code then along with the
rest of the Pixhawk Firmware using CMake. In Windows 10, the bash terminal will open
shortly and begin cross compilation. In Linux, the build will occur within
MATLAB/Simulink.
If the “Build, Download and Run” option was selected in the hardware implementation
panel then the next chain of events will occur after the build process is completed:
The Diagnostic Window will show the progress of the build process. When the firmware
is ready and the ‘Build, Load, Run” option is selected, the user will be promoted to make
sure that the pixhawk is NOT currently plugged into the computer USB port (see pop-up
dialog below). Press OK on this pop-up dialog, then plug in the pixhawk into the USB
port. This will start the flashing process. When the process is complete, the PX4 will re-
boot and you should hear the start-up tune.
A successful upload using the “Build, Load and Run” option looks something like this in
the Simulink Diagnostic Viewer
4.3.3.2 Build Only and Manual Download (Windows)
NOTE: please ensure putty or any connection to the Nutshell terminal is closed before
attempting an upload!
The build button will open a bash terminal. Wait for the compilation to reach 100%.
To upload the firmware, is found under the Code menu > PX4 PSP: Upload code to
Px4FMU. Ensure that the PX4 device is plugged in during this time.
Starting the PX4 Simulink Application
To start the application you can call the command
px4_simulink_app start
If you have modified your rc.txt file you can automatically start the generated application
as soon as the board powers up. The app can be stopped using
px4_simulink_app stop
When the application has ended these threads will terminate along with the
“PX4_Simulink _Tasks”.
The source file nuttxinitialize.c and PX4_TaskControl.c is responsible for spawning these
threads, semaphores and so forth to execute the generated code at the specified sample
rates in the Simulink model. This source file can be found in \psp\pixhawk\src
In the previous versions of the Pixhawk PSP we would spawn a thread called
schedlerTask which would setup a semaphore that waits on a POSIX timer using
functions such as timer_create. Using this method, it was observed that there was jitter in
the pace of execution. While this jitter was not enough to cause instability in the system, it
was enough to warrant an update. We now employ a High-Resolution Timer (HRT) which
was observed to have less jitter to post the base-rate semaphore which is used to set the
execution pace of the base-rate thread. To read more about this go here:
https://pixhawk.org/dev/accurately_timed_operations
Hard Real-Time Constraints
It is highly recommended to NOT use this during flight tests as there is a chance the system
will auto-shut down in midflight.
In the R2016a/R2016b PSP release, a new feature has been added in here to allow users to
determine if the flight algorithm is able to meet scheduling deadlines by examining task over-
run occurrences. By definition, task over-run means that the generated code was not able to
complete a call complete it’s task in the specified sample time set by the Simulink model. This
is illustrated below:
When enabling Hard Real-Time constraints, the generated code will auto-shut down and
report to the Nuttshell terminal when task over-run crosses a certain threshold.
This threshold is dictated by the semaphore water-mark. In the above settings, we allow task
over-run to occur at a maximum of 20 times before the application shuts down. The water-
mark is here to account for more flexibility in instances where model initialization may have
taken longer than a single sample period and causes brief semaphore wind-up but the
generated code step function is still able to meet the scheduling deadline.
• Use this to help determine if your flight algorithm does not perform in real time and be
able to quantify the severity
• It is highly recommended to NOT use this during flight tests as there is a chance the
system will auto-shut down in midflight. You will be warned prior to compiling the Simulink
model that this option has been enabled
QGroundControl is a utility which can interact with your Pixhawk FMU through calibration
routines, mission planning and parameter adjustments. This utility uses Mavlink serial
connection to communicate back to the host computer. For more information on
QGroundControl please refer to their website:
http://qgroundcontrol.com/
QGroundControl has had many different releases which may or may not work with the
Pixhawk PSP. When running px4_simulink_app we recommend disabling several
applications such as the commander and mavlink. This is done because
• We currently do not generate code to interact with Mavlink. We also sometimes require
the serial port to be free to access for other things (ie: generic UART communication)
• The commander application has full control over the motors/actuators. Because
px4_simulink_app was intended to replace the commander application as the main flight
controls system, we disable this app. This unfortunately means that QGroundControl
cannot be used simultaneously while px4_simulink_app is running if the commander
application is disabled.
If you wish run through a calibration routine with QGroundControl, you can follow these steps.
To read more on each block, consult the documentation in the MATLAB help guide:
For some of these examples, you will need to establish a serial terminal connection to the PX4
hardware with a program such as TerraTerm or PuTTY to examine stdout print statements. This
can be done by running the command:
nsh> px4_simulink_app start
If have edited the rc.txt boot script to start px4_simulink_app at boot-time, then you will need to
stop it, then re-start it with these commands (since there is no stdout console available at boot-up
time the printf statements in the code can’t output any text):
Then
nsh> px4_simulink_app stop
px4demo_ADC_example.slx
Select the different ADC channels through the options in the block. This block was written as
a system object.
System objects are another alternate method of block authoring. The source code is written
as MATLAB class. To view the source code, a link is provided in the block description.
px4demo_input_rc.slx
This model will test the RC transmitter block. Use the RC Transmitter to control the color and
mode of the RGB LED on the pixhawk. Channel 3 is typically the “Thrust” or the left vertical
joystick control. Channel 4 is typically the “Yaw” or the right horizontal joystick control.
px4demo_rgbled.slx
A simple model that show how to program the RGB_LED library block. Every second the
RGB LED changes from blinking-fast blue color to “breathing” red color.
Note: enumerations for LEDs are as follows (in MATLAB) :
SL_MODE_OFF (0)
SL_MODE_ON (1)
SL_MODE_DISABLED (2)
SL_MODE_BLINK_SLOW (3)
SL_MODE_BLINK_NORMAL (4)
SL_MODE_BLINK_FAST (5)
SL_MODE_BREATHE (6)
px4demo_tune.slx
To test various tunes, this model plays all the pre-defined tunes plus a user-custom tune
cycling every 10 seconds.
px4demo_gps.slx
A test model has been provided to test out the GPS Block. This model will print out
information to a terminal window once a second and the RGB LED will “breathe” Green.
px4demo_attitude_control.slx
This model contains the heart of the attitude flight control model. It should have the identical
configuration parameters as the parent model.
px4demo_attitude_system.slx
After the flight control system has been successfully simulated, it can be used in an
“implementation” model that the user can use to generate code and deploy to the Pixhawk
PX4 hardware.
Here is the same Control Model referenced in a system model for deployment. The
RED/GREEN colors indicate the different sample rates of the model (RED = 250Hz, GREEN
= 2Hz).
Note that in Firmware v1.6.5 and beyond the vehicle_attitude uORB topic only outputs in
quaternion.
To convert to euler angles this model contains a block which can convert quaternion to euler
angles. The code is based off this:
https://github.com/PX4/Matrix/blob/471e96ff6f5f22018b782441c6a8df19d8294181/matrix/Eul
er.hpp#L132
px4demo_Parameter_CSC_example.slx
px4demo_ParameterUpdate_CSC_example.slx
The Pixhawk Px4FMUv2 uses many parameters to store and access during various
operations. Much of these include sensor/actuator calibration data and are stored in flash
memory which is accessible by MTD via NuttX.
The Pixhawk PSP allows you to access these parameters using Embedded Coder's
Custom Storage Class feature. A parameter is first defined in the MATLAB workspace with
specific parameter properties which is then accessed in the generated code.
Pixhawk_CSC.Parameter( CELL_ARRAY )
Where CELL_ARRAY is a MATLAB cell array composed of a value (int32 or single) and a
string of the parameter. For instance:
If parameters are changing and the model requires to use these newly updated
parameters, then adding this block in your model can enable such capabilities.
You can use this block to auto-populate a list of storage classes used by the model. You
can pick and choose which parameters require updating at run-time.
A version of the above model with the update block can be found here:
px4demo_ParameterUpdate_CSC_example.slx
This model is to be run without external mode. Printf statements can be viewed by running
px4_simulink_app.
px4demo_write_uorb_example.slx
This model demonstrates how one can write data to uORB topics. The uORB write block
writes to the struct elements 'lat','lon' and 'timestamp' to the GPS topic. The GPS block then
outputs the same value we are writing to by first advertising the GPS topic and then
publishing data. You can define whatever topic to write to and its individual struct elements
1) Define LAT, LON and TIME in the MATLAB work-space with assigned values. Ensure they
are matching data types to what the block expects.
2) Run in external mode
3) Tune values LAT, LON and TIME and watch the values change in the display from the
output of the GPS block
The “uORB Write Advance” block is used which allows you to write to the entire data
structure of a uORB topic. Use the UI by double clicking on the block and select which uORB
topic struct element to write to. Note that data type and dimenions have been resolved.The
older uORB Write block has a limited set but is still accessible in the Simulink PX4 Library
Serial Communication
Two models have been provided to demonstrate how to setup serial communication
px4demo_HostSerial_TxRx.slx
This model does not undergo code generation, it resides on the host PC and is responsible
for sending/receiving data to the Pixhawk Px4FMU over serial. The scopes will show
accelerometer and gyro readings. A loopback display block is used to show the value that we
send to the Pixhawk is sent back.
px4demo_Serial_TxRx.slx
This model is the one that will be deployed the Pixhawk FMU. It will fetch data from a uORB
topic and send it off over serial (ttyS6). Loopback data is received and sent back into the
serial send block.
Included within this are demos which allow for parameter tuning and sending debug messages
over QGroundControl (QGC)
px4demo_QGC_tune.slx
<Selected PX4 Firmware Directory>\examples\qgc_tune_parameter\
This model contains defines two parameters, “SL_MSG” and “SL_TEST”. In Simulink, they are
assigned as custom storage classes. To adhere to PX4’s firmware parameter definition scheme,
we also need to include an additional source file px4_simulink_app_params.c.
/**
* Sample Simulink Param
*
* <longer description, can be multi-line>
*
* @unit number
* @min 0
* @max 100
* @decimal 0
* @increment 1
* @reboot_required false
* @group simulink
*/
PARAM_DEFINE_INT32(SL_MSG, 10);
As for the model itself, the ‘ParamUpdate’ block was used to ensure parameters are updated
when QGC tunes these parameters over Mavlink:
1) Before compiling the model, you may need to delete parameters.xml within your build
folder. This is to force re-generation of this XML file which will contain newly defined
parameters from the model.
<Firmware Location>\Firmware\build_<fimware_variant>\parameters.xml
Where <firmware_variant> could be px4fmu-v3_default or px4fmu-v2_default, etc
You will want to make sure that no other application is using the same serial port.
5) Start up QGC – connection should be established in a few seconds. This has been tested
on QGC v3.2.4
6) If your px4_simulink_app is not running you can start it up by going to the Mavlink
console
8) To trigger refresh of the parameters you first select a different parameter group other
than Default and then clicked on Default again. SL_MSG and SL_TEST should now show
up
The PX4 application will begin printing values to the screen. You should be able to tune values
within here and watch the values change accordingly.
px4demo_QGC_tune.slx
This example model uses rc-controller input and appends it to a message defined within the
“mavlink_text_msg” block. This string is then passed on to a uORB write block which writes to the
“mavlink_log” topic. This topic is then viewable within QGroundControl as shown below
Like the previous example, MAVLINK must be enabled to establish this communication. You can
experiment with trying different “severity” levels to match the expected messaging behavior that’s
specific to QGC.
By enabling the “Publish Outputs” this block will also publish data to the uORB topic
“actuator_outputs”.
5 Building your own custom Simulink Block
There are several reasons you may want to consider building your own Simulink block. The
most common reason is the need to interface generated code with custom hand-code. This
could be to interface with driver code which talks to various sensors/actuators or to send data
over to another interface. Whatever the reason may be, MATLAB and Simulink offer many
ways to accomplish this.
S-Function Approach
All the blocks in this Pilot Support Package were created by writing S-functions with TLC and
System Objects.
There are many ways to create S-Functions and the accompanying TLC code.
We have provided an example in this version of the PSP (C-MEX and TLC) for users to learn
from. Please See:
<PSP install>\px4\examples\BlockCreation\
Apply the MEX command on the sfun_px4_battery_example.cpp block to generate a valid MEX
file for the S-function. Use this block as an example as to how to create blocks with S-Functions.
The logging block was written using a MATLAB Function block. MATLAB Coder syntax is used
to describe the interface to hand-code. Please examine the contents of this block for more
information on how this was accomplished
Another method that exists is using System Objects. These types of blocks make use of
MATLAB Coder’s capability of transforming MATLAB Code into C-code. System Objects are
written using an object-oriented approach. Please see the ADC and Serial blocks as examples
of how to write such blocks.
Click on the ‘Source code’ hyperlink to open up the MATLAB System Object code for these
types of blocks.
For more information on MATLAB Coder and System Objects, please also see:
https://www.mathworks.com/help/simulink/ug/creating-an-example-model-that-uses-a-matlab-
function-block.html
https://www.mathworks.com/help/simulink/slref/coder.ceval.html
https://www.mathworks.com/help/simulink/system-objects.html
6 Limitations
The supplied Simulink blocks do not support any simulation behavior. These are merely
there to provide code generation to interface the control system to the actual hardware
drivers necessary in the firmware. It is advised that you use Model Referencing to
separate your control system so that you can re-use the model in your simulation as well
as the implementation model (used for code generation).
Currently, the optimization option “Inline Parameters” must be turned on. This
eliminates the use of global data being created which has shown to cause compilation
errors due to limited global memory space.
We currently do not support interactions with HIL or Mavlink with the px4_simulink_app.
This is something we wish to investigate in the future and will require significant changes /
updates to the way we generate code for this application. Additional code will need to be
added to each of the blocks to allow routing of signals when in a HIL environment. If you
have suggestions or contributions to help in this area, please feel free to reach out to
MathWorks Pilot Engineering.
The current uORB read block is only able to convert uORB messages into Simulink bus
objects if the topic is not treated as a C++ object. Several messages are treated as a C++
object where the data structure will not be compatible in C. At the moment, we only
generate C code for px4_simulink_app. This means that thing such as memcopies or
memory layout cannot be assumed to be contagious. Here’s an example of a uORB topic
that uses C++ notation.
The header file which gets generated looks something like this:
C:\px4\Firmware\build_px4fmu-v2_default\src\modules\uORB\topics\battery_status.h
#ifdef __cplusplus
struct __EXPORT battery_status_s {
#else
struct battery_status_s {
#endif
uint64_t timestamp;
float voltage_v;
float voltage_filtered_v;
float current_a;
float current_filtered_a;
float discharged_mah;
float remaining;
int32_t cell_count;
bool connected;
uint8_t warning;
#ifdef __cplusplus
static const uint8_t BATTERY_WARNING_NONE = 0;
static const uint8_t BATTERY_WARNING_LOW = 1;
static const uint8_t BATTERY_WARNING_CRITICAL = 2;
#endif
};
The battery_status uORB topic was written with non-C struct notation:
BATTERY_WARNING_NONE,
BATTERY_WARNING_LOW,
BATTERY_WARNING_CRITICAL.
Because the PixhawkPSP generates C code we cannot instantiate this struct and copy
data elements such as BATTERY_WARNING_NONE over like a normal C struct without
getting a compiler error. The “Battery_Status” uORB data structure must be treated as a
‘singleton’ or global since it contains global data.
We have provided a block which is like the uORB read block in the current library but
supports the ability to access struct elements from a C++ uORB object, however, the
ability to actually read the global elements is not supported. Please look at the example
inside: \px4\SampleSFcn
Example:
However, attempting to do this:
Will result in compiler error. Therefore, the official shipped version of the uORB read block
that uses bus objects will reject these types of data structures completely while this version
of the block will still allow you to use any data structure.
If you wish to support the C++ global member variables such as the one in the example
above, you will probably need to write your own block that does a copy of data from a C++
struct into local C variables. This function/code could live inside a C++ source file and gets
used only if the topic will contain C++ data-structures. Consult the previous chapter on
block creation for more tips on how to do this as well. This may be addressed in a future
version of the PSP. Alternatively, you can try editing the .msg file and commenting out the
“constants” used. This will work only if you aren’t running other software that may rely on
these to be defined as part of the topic structure (e.g. commander).
If you run into any issues with the use of this PSP please contact your MathWorks sales
representative or Pilot Engineering group directly. Do not go through technical support
for issues with this PSP. Do go through technical support for issues related to
MATLAB/Simulink outside the scope of this PSP.