How to run Simulink model at start-up?

Hi guys,

I have created a custom control loop in simulink and it works successfully on the pixhawk if I’m connected to Simulink.

How can I run this software independent from my PC as soon as I plug-in the power?


On the memory card you can add a startup file: etc/rc.txt. This startup is a bunch of commands run in nuttx at startup. Add your control app at the end:

... some commands ...
rgbled start
usleep 1000
your_simulink_app start
... maybe more commands ...

Hi glennb,

First of all, thank you very much for your answer. Unfortunately it still doesn’t seem to work.

I copied the changed the etc/rc.txt file like this:

#sercon - in the latest release sercon already gets called
usleep 1000
uorb start
usleep 1000
#nshterm /dev/ttyACM0 & #Disable the USB serial console
usleep 1000
px4io start
usleep 1000
#commander start
#usleep 1000
#mavlink start -d /dev/ttyS1 -b 115200
#usleep 5000
#dataman start
#usleep 1000
#navigator start
#usleep 1000
sh /etc/init.d/rc.sensors
usleep 1000
#sh /etc/init.d/rc.logging
#usleep 1000
#gps start
#usleep 1000
#attitude_estimator_ekf start - the latest release does not use this anymore
ekf_att_pos_estimator start
usleep 1000
#attitude_estimator_so3 start
#usleep 1000
#mavlink start -d /dev/ttyS3 -b 115200
mtd start
usleep 1000
param load /fs/mtd_params
usleep 1000
rgbled start
usleep 1000
px4_simulink_app start #disable automatic starting up of the model
nshterm /dev/ttyS6 & #this will make ttyS6 the serial console

As you see, I uncommented “px4_simulink_app start”

But the program seems not to run after unplugging it from my PC and plugging it into a battery.

I tried it with the “px4demo_attitude_system_multi_task” example provided from Matlab.

Any ideas what I did wrong?

What are your indications that the module does not work? Are you communicating with it through the shell or QGroundControl?

Also, I’m wondering about why you have the exit command at the end. Maybe it shuts down the pixhawk? (Edit: never mind, I see I have that as well.)

Hey glennb,

When the pixhawk is connected to Simulink (COM3 on PX4 and COM5 on serial FTDI) then I just need to press the SWITCH and my servo connected to MAINOUT is moving.

As soon as I disconnect the pixhawk from the PC and power it up with a battery, the servo won’t move at all and the RGB led is off all the time (even when I press the SWITCH).

When you say COM3 on PX4, what is the port name written by the connector on the actual hardware? TELEM 2, TELEM 1, SERIAL 4/5, or the USB? This page has the following table:

/dev/ttyS0 UART1 (NSH console) IO DEBUG (RX only)
/dev/ttyS1 UART2 TELEM1 (USART1)
/dev/ttyS2 UART5 TELEM2 (USART2)
/dev/ttyS3 UART6 GPS (UARTX)
/dev/ttyS4 N/A N/A (UART5, IO link)
/dev/ttyS5 N/A SERIAL5 (UART7, NSH console do only use as console)
/dev/ttyS6 N/A SERIAL4 (UART8)

Which of these

What I mean is that you should try to connect to the terminal named /dev/ttyS6 (which would be SERIAL 4 connection), and see if it outputs any errors. Here you can also try to get the status of the simulink app.

COM3 is the mini-USB on my pixhawk.
COM5 is a serial connection (FTDI) on SERIAL4 of the pixhawk (/dev/ttyS0 UART1 (NSH console) IO DEBUG (RX only))

Everything works great as soon as the pixhawk is connected to Simulink. But it won’t work without simulink.
When the pixhawk is disconnected from the PC, it will boot-up but the RGBled won’t light up. Just the SWITCH blinks…

I’ve tried to remove “exit” from the rc.txt file, same problem…

I’m curious if you run the Simulink in External mode?

Yes, I run Simulink on External mode (I try the example provided from matlab: “px4demo_attitude_system_multi_task”)

I do this tutorial:

Which Data Exchange Serial Port did you select?

Here is a screenshot (I use Matlab 2015b):

I’m sorry, I can’t see without closer inspection what may be the issue. What I would do is to move the line

nshterm /dev/ttyS6 &

At the top of the rc.txt file, such as this:

usleep 1000
uorb start
usleep 1000
nshterm /dev/ttyS6 &

Then I would connect the FTDI to the console output (the Pixhawk ttyS6 connector (according to it should be SERIAL 4)).

I would then run a Putty terminal on the FTDI to monitor the nuttx shell (nshterm), and see if there were any error messages. If not, I would try to run the px4_app from the terminal, like this:

px4_simulink_app start

And see what happens. If you do this and cannot come to a conclusion, you may post it here and I can see if I can help you further.

If I disconnect the Pixhawk from the USB and power it up again with the battery, I get this answer via SERIAL4:

nsh> px4_simulink_app start
px4_simulink_app: model is already running

In Simulink I always had to press “RUN” to make the model running, so I wonder how to do that if the pixhawk is not connected to the computer.

If nothing is connected as in external mode, the model should just run by itself.

This is strange. If the model is already running, I find it strange that it’s not doing what you are expecting. Maybe it’s some issue with the external mode options.

Has anyone tried to run a Simulink model sucessfully without any connection to the PC?

I wonder if the pixhawk might be “armed” somehow?

Or is there are other way to upload the generated code from Simulink and run it independent from my PC?

Ok guys, I think I know where the problem is:

It works if the pixhawk IS NOT set-up for external mode !!!

So if you use the examples for the “Normal”-mode, it works!!!
So if you want to develope a new control system in simulink it just works without the PC if it is setup in “Normal-mode”.

1 Like