Category Archives: PX4

Running Lua under nuttx on PX4 flight control board

The PX4FMU is a powerful little flight control board based on the STM32F4 chip. The PX4 development environment currently uses the NuttX embedded RTOS.  In order to build and run apps in the PX4 environment, a developer must rebuild the entire nuttxOS, which has obvious drawbacks.

NuttX does include a simple shell, nsh, which supports a basic scripting language.  However, if you wish to develop complex app logic, nsh is probably not the right tool.

One idea I had was to port a high-level interpreted language to nuttx, to allow developers to script and modify flight control behaviors at runtime, without the need to rebuild nuttx.   So I took the compact Lua interpreter code and ported it to the PX4 environment.

You can find this under my PX4Firmware repository on github.  I have a sample app “pizzahat” that provides a script to the lua interpreter.  The current version is a proof-of-concept that causes Lua to run one of the built-in apps.  This demonstrates that Lua can be used to script all the nuttx builtin applications.

Note that I needed to hack Lua itself significantly to get it running under nuttx: you can find the hacked version (lua-nutt) here.

This is very early-stage, but shows potential.

Edit: I’ve had a couple people ask how big the Lua binary is.  A quick test shows that the px4default .px4 file is 115kB larger with lua-nutt (and the pizzahat test module).  

PX4FMU and PX4IO setup with APM

Ok, I was trying to setup my PX4IO board with the latest firmware.  This took way longer than it should, possibly because the instructions spread out on the various pixhawk/diydrones sites are outdated and rambling. Here are the steps that worked for me.

  1. Disconnect the PX4IO from the PX4FMU, if it’s already connected. I was never able to get this to work smoothly with the two already attached. 
  2. Flash the standalone PX4FMU with the latest APM firmware. I used the qupgrade tool with the PX4FMU board connected via USB.
  3. Place the latest “px4io.bin” file onto a microSD card, in the root directory.
  4. Insert the microSD card into the PX4FMU. Remove the PX4FMU from any source of power (e.g. USB)
  5. Attach the safety switch to the PX4IO.
  6. While holding down the safety switch, attach external power to the PX4IO. In my case I used an old NiMH battery pack attached to the power input directly.  
  7. The PX4IO should power up into bootloader mode. The red/amber LED will be pulsing rapidly,  the green light will be solid, and the blue light will be off as detailed here. IF the LEDs aren’t lit properly to indicate bootloader mode, remove power and try again. 
  8. Release the safety switch.
  9. With the PX4IO board still powered, insert the expansion bus pins from the PX4IO board into the PX4FMU.  The good news here is that you don’t need to hold down the safety switch anymore, once you get the PX4IO into bootloader mode. 
  10. Since you’ve already flashed the PX4FMU with the APM firmware, the APM startup script will notice the px4io.bin file on the microSD card and install that on the PX4IO. 
  11. To verify that the PX4IO updated properly, you can look for /APM/px4io_update.log on the microSD card. It should say something like “Loaded /fs/microsd/px4io.bin OK” at the end.
  12. Note that you may need to reboot after this.  You can either use the reset button on the PX4FMU board, or use eg UART1 to issue the “reboot” command in nsh.
That’s it…good luck!