File Exchange

image thumbnail

Embedded Coder Support Package for PX4 Autopilots

Automatically build and deploy flight control algorithms to PX4 Autopilots using Simulink and Embedded Coder

108 Downloads

Updated 11 Sep 2019

Using Embedded Coder™ Support Package for PX4® Autopilots, you can generate C++ code, build, and deploy Simulink® models on Pixhawk® flight controllers. The support package uses the PX4 toolchain to compile and deploy algorithms tailored specifically for Pixhawk flight management units and lets you customize algorithms to incorporate onboard sensor data and other PX4-specific services. PX4 is a trademark of Lorenz Meier.

Comments and Ratings (45)

andyfl

Hey, I have the same issue and can't manage to get rid of it. I'm using MATLAb 2018b and Pixhawk 1. I also tried several Baudrates and the command
setenv('MW_PX4_WIN10_BUILDCMD', 'cmd /c start cmd /k "C:/Windows/Sysnative/bash.exe -cli "make $(CMAKEMAKECONFIG) " 2> MWPX4BuildLog.txt & echo MW_BUILD_COMPLETE >> MWPX4BuildLog.txt " ');
The commands you suggested
setenv('MW_PX4_WIN10_BUILDCMD','')
setpref('MW_PX4_WSLBuildPath','SwitchPath', 1)
don't work either.
My px4_read_demo file contains a 'uORB Read'-Block and several 'Display's as sinks. MAVLink's been disabled all time. I tried it on Windows 18362 and Ubuntu 18.04 with the same result.

### Successful completion of build procedure for model: px4_read_demo
### Creating HTML report file px4_read_demo_codegen_rpt.html
Build process completed successfully
host endian mode: Little
Component:Simulink | Category:Model
byte swapping required: false
Component:Simulink | Category:Model
action: EXT_CONNECT
Component:Simulink | Category:Model
Error occurred while executing External Mode MEX-file 'ext_comm':
Timed-out waiting for second connect response packet.
Component:Simulink | Category:Block diagram error
Unable to connect to the 'PX4 Pixhawk 1' target for 'px4_read_demo'.

Could somebody help me please?

Ankur Bose

Hi Yu-Ru chien,
Did you disable MAVLink over USB port?

Yu-Ru chien

I have changed the External setting for using USB port on Hareware Implementatoiin >> External mode
But the error was still occur :

Verify : [ ] 1.0%
Verify : [====================] 100.0%
Rebooting.

### Successful completion of build procedure for model: px4_control_system_ver4
Build process completed successfully
action: EXT_CONNECT
Component:Simulink | Category:Model
Error occurred while executing External Mode MEX-file 'ext_comm':
Failed to connect to the target. A time-out occurred while waiting for the connection response from the target. Possible reasons for the time-out:
a) The target is not switched on.
b) The target is not connected to your host machine.
c) The application for the model is not running on the target. You might have clicked the Stop button. If the Run button is not dimmed, click it. Otherwise, click the Build button, which downloads and runs your application on the target.
Component:Simulink | Category:Block diagram error
Unable to connect to the 'PX4 Pixhawk 1' target for 'px4_control_system_ver4'.

I tried to change baudrate to 9600, 57600, 115200, 921600 on Hardware Implementation >> /dev/ttyACM0 ,but the error still can not fix.
please help

Yu-Ru chien

Hi Ankur Bose,
Beacuse I enabled MAVLink over USB port.
Should I use other port for enabling MAVlink and run External mode on USB port ?

Ankur Bose

Hi Yu-Ru chien,
Can you tell us why you are using Serial 5 on Pixhawk 1 for External mode instead of using the USB port aka /dev/ttyACM0 ?

Yu-Ru chien

I have completed the SD card setup and inserted in Pixhawk 1 flight controller.
But when I run my own firmware on external mode( /dev/ttyS5 aka Serial 5 ), a error occured on connecting the Pixhawk 1 and said the following :

Program: [=================== ] 99.8%
Program: [====================] 100.0%

Verify : [ ] 1.0%
Verify : [====================] 100.0%
Rebooting.

### Successful completion of build procedure for model: px4_control_system_ver4
Build process completed successfully
host endian mode: Little
Component:Simulink | Category:Model
byte swapping required: false
Component:Simulink | Category:Model
action: EXT_CONNECT
Component:Simulink | Category:Model
Error occurred while executing External Mode MEX-file 'ext_comm':
Timed-out waiting for second connect response packet.
Component:Simulink | Category:Block diagram error
Unable to connect to the 'PX4 Pixhawk Series' target for 'px4_control_system_ver4'.

However, I could use putty to connect the controller by serial 5.
Can somebody help me fix the error ?

p.s. I set the baudrate of Serial 5 at 57600

dale

Hi Ankur Bose
thank you for replying to my comments.
The command setpref('MW_PX4_WSLBuildPath','SwitchPath', 1) didn't work .
and setpref('MW_PX4_WSLBuildPath','SwitchPath', 0) won't work either ,because the command i need is 'setenv('MW_PX4_WIN10_BUILDCMD', 'cmd /c start cmd /k "C:/Windows/Sysnative/bash.exe -cli "make $(CMAKEMAKECONFIG) " 2> MWPX4BuildLog.txt & echo MW_BUILD_COMPLETE >> MWPX4BuildLog.txt " ')',,not antything else.
The command 'setpref('MW_PX4_WSLBuildPath','SwitchPath', 1) ' won't help me get the correct setenv command i need.

Ankur Bose

Hi Dale,
Have you looked at the below documentation? This should solve your problem of setting the environment variable every time.
https://in.mathworks.com/help/supportpkg/px4/ug/troubleshooting-px4-build-model-windows.html

dale

I met the same two issues
1、Unable to locate build tool "GNU PX4 Archiver": echo
I found a command to settle the problem temporarily

setenv('MW_PX4_WIN10_BUILDCMD', 'cmd /c start cmd /k "C:/Windows/Sysnative/bash.exe -cli "make $(CMAKEMAKECONFIG) " 2> MWPX4BuildLog.txt & echo MW_BUILD_COMPLETE >> MWPX4BuildLog.txt " ')

you should run the command everytime before you want to build the simulink model,but,anyhow,this method solved my problem.
2.Unable to connect to the 'PX4 Pixhawk 1' target
i have not solve the problem yet.

when i was trying to solve the first problem,i found the command line in the file“C:\ProgramData\MATLAB\SupportPackages\R2018b\toolbox\target\supportpackages\px4\+codertarget\+pixhawk\+internal\setupBuildEnvironment.m”

i read the code ,and i found that there might be something wrong.

i tried every possible setenv('MW_PX4_WIN10_BUILDCMD',‘string’) command in the file to test which is the correct one,
finally i found that only the command i mentioned above is appropriate

Here comes the problem:
my windows version is 18362(i got the windows version after i ran the command “codertarget.pixhawk.internal.getWindows10Version()”),
i also checked getpref('MW_PX4_WindowsToolchain','UseCygwin') command the result is 0
so the 'setenv' command i might get at last is
"setenv('MW_PX4_WIN10_BUILDCMD', 'C:/Windows/Sysnative/bash.exe -cli "make $(CMAKEMAKECONFIG) "') ;"
or
"setenv('MW_PX4_WIN10_BUILDCMD', 'C:/Windows/System32/bash.exe -cli "make $(CMAKEMAKECONFIG) "') ;"
however ,i tried the two command above but they didn't work.

so i guess there might be something wrong in the way we set the MW_PX4_WIN10_BUILDCMD.

Ankur Bose

Hi Daniel,
The mixers are usually defined in the PX4 Firmware for each airframe. In PX4 v1.8.0, the mixers are defined at https://github.com/PX4/Firmware/tree/v1.8.0/ROMFS/px4fmu_common/mixers
The mixer defined in motor_mixer in px4demo_attitudeSystem model is actually a MATLAB function for a hexacopter. If you want to bring a mixer for your airframe in MATLAB, create a MATLAB function for your mixer as listed in the above link.

Regarding the External MOde issue can you please confirm if you have completed the SD card step as described in hardware setup screens and your Pixhawk 1 has an SD card connected inside?

Daniel

Quick question:

Can you point me in a direction to understand how the mixers are implemented in matlab? I don't seem to understand the motor mixer block.

Thanks. Sorry i'm sending many messages. Its so that when you eventually read this, you can just address all of them at once.

Thanks once again.

Daniel

Hi support team,

I have installed the suport package on Ubuntu 16.04. I was able to build the firmware successfully, and upload. However, I am unable to read accelerometer values from the Pixhawk1 (the device I am targeting).

Secondly, when I try to run the example models (i've tried about 3 now), it builds fine, but then gets to a point and says the following (copied from diagnostic window):

Build process completed successfully
host endian mode: Little
Component:Simulink | Category:Model
byte swapping required: false
Component:Simulink | Category:Model
action: EXT_CONNECT
Component:Simulink | Category:Model
Error occurred while executing External Mode MEX-file 'ext_comm':
Timed-out waiting for second connect response packet.
Component:Simulink | Category:Block diagram error
Unable to connect to the 'PX4 Pixhawk 1' target for 'px4demo_Read_Parameter'.

Please help. I tried to do some googling and similar error occured for Arduino users as a result of baudrate issues in non-original boards. I tried to reduce the default baudrate on the 'target hardware resources' in the setup to 57600 which I know to work with 'screen' on DTFI cable. That didn't work. I reduced it further 38400, still didn't work. Please help.

Thank you.

Ankur Bose

Hi John Jahadi,
the wmic command is a windows API and it should be present in all Windows 10 PC. Please check your Windows 10 path environment variable.

john Jahadi

Hi Ankur Bose,
I did it and the response is :
'wmic' is not recognized as an internal or external command,
operable program or batch file.
ans =
1
Now what should I do?
Thanks for responses

Ankur Bose

Hi John Jahadi,
Please execute the below command in MATLAB command window and let us know the output.
system('wmic os get Caption')

john Jahadi

Hi,
I reinstall matlab and Install this pakage too.
I have these error
Error using matlab.hwmgr.internal.hwsetup.launchHardwareSetupApp (line 18)
Error instantiation matlab.hwmgr.internal.hwsetup.register.PX4Workflow. Details Error while checking windows
version

Error in matlabshared.supportpkg.internal.ssi.openHwSetupForBaseCodes

Error using matlab.hwmgr.internal.hwsetup.launchHardwareSetupApp (line 18)
Error instantiation matlab.hwmgr.internal.hwsetup.register.PX4Workflow. Details Error while checking windows
version

Error in matlabshared.supportpkg.internal.ssi.openHwSetupForBaseCodes

com.mathworks.mvm.exec.MvmExecutionException: matlabshared.supportpkg.internal.ssi.openHwSetupForBaseCodes
at com.mathworks.mvm.exec.NativeFutureResult.nativeGet(Native Method)
at com.mathworks.mvm.exec.NativeFutureResult.get(NativeFutureResult.java:62)
at com.mathworks.mvm.exec.FutureResult.getInternal(FutureResult.java:404)
at com.mathworks.mvm.exec.FutureFevalResult.getInternal(FutureFevalResult.java:49)
at com.mathworks.mvm.exec.FutureResult.get(FutureResult.java:262)
at com.mathworks.hwsmanagement.OpenHardwareSetupAction.perform(OpenHardwareSetupAction.java:60)
at com.mathworks.addons.action.PerformAdditionalAction.perform(PerformAdditionalAction.java:28)
at com.mathworks.addons.ActionHandler.dispatch(ActionHandler.java:27)
at com.mathworks.addons.AddonsCommunicator$1.handle(AddonsCommunicator.java:37)
at com.mathworks.connector.message_service.impl.MessageServiceJSONImpl.handlePublishRequest(MessageServiceJSONImpl.java:29)
at com.mathworks.connector.message_service.impl.AbstractMessageService.handlePublishRequest(AbstractMessageService.java:58)
at com.mathworks.connector.message_service.impl.JniMessageServiceAdaptorImpl.doHandlePublish(JniMessageServiceAdaptorImpl.java:59)
at com.mathworks.connector.message_service.api.JniMessageServiceAdaptor.fromNative(JniMessageServiceAdaptor.java:21)
Caused by: com.mathworks.mvm.exec.MvmRuntimeException: Error instantiation matlab.hwmgr.internal.hwsetup.register.PX4Workflow. Details Error while checking windows version
Error in ==> D:\MATLABR18\R2018b\toolbox\shared\hwmanager\hwsetup\launch\+matlab\+hwmgr\+internal\+hwsetup\launchHardwareSetupApp.m>launchHardwareSetupApp at 18
Error in ==> D:\MATLABR18\R2018b\toolbox\shared\supportpkgservices\installservices\+matlabshared\+supportpkg\+internal\+ssi\openHwSetupForBaseCodes.p>openHwSetupForBaseCodes at 0

john Jahadi

Hi Ankur Bose,
I am using Matlab R2018b and my windows version is Win 10
Thanks

Ankur Bose

Hi John Jahadi,
Can you tell us what is the MATLAB version (R2018b or R2019a) that you are using? What is the Windows version(Windows 7, 8.1 or 10) on which you are running MATLAB? Also, can you let us know the output of the below command when you run it in MATLAB command window?
system('wmic os get Caption')

Ankur

john Jahadi

hi,
I am using this toolbox to modify the pix-hawk controller.
in simulink configuration parameters >Hardware implementation > when I click on change CMake configuration
I have this ERROR :
ERROR while checking windows version.
please Help me.
Thanks

Ankur Bose

Hello lulu chen,
Can you please tell us more about the issue? Are you facing it for example models or just for px4demo_PWM?

Yuhui Liu

ge dew

Hello,
After following all instructions, and successfull firmware upload and readout of the accellorator values in hardware setup screen I ran into following problems with the DEMO: 'px4demo_adc'

### Generating code into build folder: C:\Users\root\Documents\MATLAB\px4_SDCard_logging_ert_rtw
### Invoking Target Language Compiler on px4_SDCard_logging.rtw
### Using System Target File: C:\Program Files\MATLAB\R2018b\rtw\c\ert\ert.tlc
### Loading TLC function libraries
......
### Initial pass through model to cache user defined code
..
### Caching model source code
..........................................
### Writing header file px4_SDCard_logging_types.h
### Writing source file px4_SDCard_logging.cpp
### Writing header file px4_SDCard_logging_private.h
### Writing header file px4_SDCard_logging.h
### Writing header file rtwtypes.h
.
### Writing header file multiword_types.h
### Writing source file px4_SDCard_logging_data.cpp
### Writing header file rtmodel.h
### Writing source file ert_main.cpp
### TLC code generation complete.
### Generating TLC interface API.
.............
### Creating data type transition file px4_SDCard_logging_dt.h
.### Evaluating PostCodeGenCommand specified in the model
Removing old px4_simulink_app directory: C:\px4_cygwin\home\Firmware\src\modules\px4_simulink_app.
Build path: C:\Users\root\Documents\MATLAB\px4_SDCard_logging_ert_rtw
### Using toolchain: GNU Tools for PX4 Autopilots v7.2.1 | gmake (64-bit Windows)
### 'C:\Users\root\Documents\MATLAB\px4_SDCard_logging_ert_rtw\px4_SDCard_logging.mk' is up to date
### Building 'px4_SDCard_logging': "C:\PROGRA~1\MATLAB\R2018b\bin\win64\gmake" -f px4_SDCard_logging.mk postdownload_preexecute all

C:\Users\root\Documents\MATLAB\px4_SDCard_logging_ert_rtw>cd .

### Invoking postbuild tool Invoke Make ...
Making Firmware
0 [main] sh 208 sync_with_child: child 12060(0x1D0) died before initialization with status code 0xC0000142
43219 [main] sh 208 sync_with_child: *** child state waiting for longjmp
/usr/bin/sh: fork: Resource temporarily unavailable
gmake: *** [postdownload_preexecute] Error 128
...
C:\Users\root\Documents\MATLAB\px4_SDCard_logging_ert_rtw>echo The make command returned an error of 2
The make command returned an error of 2

C:\Users\root\Documents\MATLAB\px4_SDCard_logging_ert_rtw>An_error_occurred_during_the_call_to_make
'An_error_occurred_during_the_call_to_make' is not recognized as an internal or external command,
operable program or batch file.
### Creating HTML report file px4_SDCard_logging_codegen_rpt.html
Warning: Validation warning(s):
Skipped the existence check for 'GNU PX4 C Compiler'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'GNU PX4 Pre-Linker'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'GNU PX4 C++ Compiler'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo g++'.
Skipped the existence check for 'GNU PX4 C++ Pre-Linker'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'Invoke Make'. Cannot determine the utility that needs to be checked for existence from the build tool command '$(call CALLPX4MK)'.
### Build procedure for model: 'px4_SDCard_logging' aborted due to an error.
Error(s) encountered while building "px4_SDCard_logging":
### Failed to generate all binary outputs.

lulu chen

lulu chen

When I run a sample, the following error occurs.

### Successful completion of build procedure for model: px4demo_PWM
### Creating HTML report file px4demo_PWM_codegen_rpt.html
Build process completed successfully
host endian mode: Little
Component:Simulink | Category:Model
byte swapping required: false
Component:Simulink | Category:Model
action: EXT_CONNECT
Component:Simulink | Category:Model
Error occurred while executing External Mode MEX-file 'ext_comm':
Timed-out waiting for second connect response packet.

Ankur Bose

Hello zjwrj,
The official fix for this issue is now available through support package updates in R2018b. Please update your support package. After the support package is updated, execute the below commands in MATLAB command window and try building the model again.
setenv('MW_PX4_WIN10_BUILDCMD','')
setpref('MW_PX4_WSLBuildPath','SwitchPath', 1)

Ankur Bose

Hello zjwrj,
This was an issue that we have fixed and official bug fix will be available soon. If you wait for some more days, you can upgrade the current support package and get the bug fix. If you need it urgently, please let me know your email-id so that I can reach you and help you in getting the issue resolved.

Thanks,
Ankur

zjwrj

Hello AnKur Bose,Thank you for your response
I did what you said,Still have errors
The error message is as follows:
"### Created: ../px4_uorb.pre.o"
"### Invoking postbuild tool "Invoke Make" ..."
"Making Firmware "
'/run-console_Simulink.bat' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
gmake: *** [postdownload_preexecute] Error 1

C:\Users\EFY\Desktop\px4test\px4_uorb_ert_rtw>echo The make command returned an error of 2
The make command returned an error of 2

C:\Users\EFY\Desktop\px4test\px4_uorb_ert_rtw>An_error_occurred_during_the_call_to_make
'An_error_occurred_during_the_call_to_make' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
### Creating HTML report file px4_uorb_codegen_rpt.html
警告: Validation warning(s):
Skipped the existence check for 'GNU PX4 C Compiler'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'GNU PX4 Pre-Linker'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'GNU PX4 C++ Compiler'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo g++'.
Skipped the existence check for 'GNU PX4 C++ Pre-Linker'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'Invoke Make'. Cannot determine the utility that needs to be checked for existence from the build tool command '$(call CALLPX4MK)'.
### Build procedure for model: 'px4_uorb' aborted due to an error.
Error(s) encountered while building "px4_uorb":
### Failed to generate all binary outputs.
Caused by:
Validation error(s):
### Validating other build tools ...
Unable to locate build tool "GNU PX4 Archiver": echo

Ankur Bose

Hello zjwrj,
Please execute the below two commands in your MATLAB command window to resolve the issue:
setenv('MW_PX4_WIN10_BUILDCMD','')
setpref('MW_PX4_WSLBuildPath','SwitchPath', 1)

After you have execured the above commands in R2018b command window, try building the Simulink model again.

zjwrj

Hello Abhishek GS
1, MATLAB Version :matlab2018b
2, I am using Windows Subsystem for Linux toolchain
3,Hardware Setup Screens is ok
4,An error occurs when run the model
5,The error message is as follows
"### Created: ../px4_uorb.pre.o"
"### Invoking postbuild tool "Invoke Make" ..."
"Making Firmware "
系统找不到指定的路径。
'bash' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
gmake: *** [postdownload_preexecute] Error 1

C:\Users\EFY\Desktop\px4test\px4_uorb_ert_rtw>echo The make command returned an error of 2
The make command returned an error of 2

C:\Users\EFY\Desktop\px4test\px4_uorb_ert_rtw>An_error_occurred_during_the_call_to_make
'An_error_occurred_during_the_call_to_make' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
### Creating HTML report file px4_uorb_codegen_rpt.html
警告: Validation warning(s):
Skipped the existence check for 'GNU PX4 C Compiler'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'GNU PX4 Pre-Linker'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'GNU PX4 C++ Compiler'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo g++'.
Skipped the existence check for 'GNU PX4 C++ Pre-Linker'. Cannot determine the utility that needs to be checked for existence from the build tool command 'echo gcc'.
Skipped the existence check for 'Invoke Make'. Cannot determine the utility that needs to be checked for existence from the build tool command '$(call CALLPX4MK)'.
### Build procedure for model: 'px4_uorb' aborted due to an error.
Error(s) encountered while building "px4_uorb":
### Failed to generate all binary outputs.
Caused by:
Validation error(s):
### Validating other build tools ...
Unable to locate build tool "GNU PX4 Archiver": echo

Abhishek GS

Hello zjwrj,
Can you provide the below information?
1. MATLAB Version
2. Operating system
3. If windows, Can you let us know if you are using the Cygwin toolchain or Windows Subsystem for Linux toolchain?
4. Were you able to successfully complete the Hardware Setup Screens?
5. Complete error log.

You could also reach out via technical support channel.

https://www.mathworks.com/support/contact_us.html

zjwrj

Error(s) encountered while building "px4_readGPS":
### Failed to generate all binary outputs.
Caused by:
Validation error(s):
### Validating other build tools ...
Unable to locate build tool "GNU PX4 Archiver": echo
What is the reason?

Ankur Bose

Hello All,
The Embedded Coder Support Package for PX4 Autopilots is now available in R2018b. If you are working in R2018b, you can try downloading the support package and start working with it.

Abhishek GS

Hello Bart,

You could also reach out via technical support channel.

https://www.mathworks.com/support/contact_us.html

Ankur Bose

Hello Bart Slinger,
Can you please provide more details about the issue you are facing? If you have any use-case or feature that you would like to be present in the support package, you can let us know. If it helps, you can share your email-id and we can follow up with you about the pain you are facing?

Does not work out of the box. Option Prepare to Run not available.

Abhishek GS

Refer the below link to understand the capabilities of the support package.
https://www.mathworks.com/hardware-support/pixhawk.html

Ankur Bose

Abhishek GS

MATLAB Release Compatibility
Created with R2018b
Compatible with R2018b to R2019b
Platform Compatibility
Windows macOS Linux