Monday, May 25, 2015

PX4 + MKBL ESC: PX4 software stack

Mikrokopter i2c ESC - Electronic speed controller, i2c bus


PX4 with native software and MKBL ESC

Here will be summary of how to enable PX4 i2c ESC support. (well, at least we will try. Master Yoda would not appreciate it, but never the less). 


Disclaimer

All information in this blog is for personal use only.
I do not deny it's use in educational purposes, but be aware, that you are using all the information found here on your own risk!



Power up PWM outputs on FMU and load mixer

To power up PWM outputs on FMU unit when we have PX4IO connected, we should run (it will also free up USART2 pass though to PX4IO board):

fmu mode_pwm

And then load mixer with:
mixer load /dev/pwm_output /etc/mixers/FMU_quad_x.mix

Connecting to i2c

As soon as i2c is parallel bus, all we need is to connect all ESC SDA and SCL lines to SDA and SCL outputs of PX4FMU controller (I tried connecting to i2c3 output of PX4IO, but it have not detected anything, so I let it bee for now).

Well, that mostly it. With all lines connected and all equipment powered from the same battery, i.e. having common ground (can be checked with tester) and SDA/SCL lines connected, there is nothing much to be done for wiring.

I assume, that you have already set up i2c addresses on ESCs as it is written here

Now, to start your mkblctrl driver automatically you should create etc/config.txt file on SD card with following contents:


set USE_IO yes
set OUTPUT_MODE mkblctrl
IMO it will be wonder if it starts working right away, so you will probably need debugging instrument. i2ctool as long as it's readme can be found here. Downside is that you have to do "build" thing to make it up and running.


Customizing

I'll be writing this notes for ubuntu linux x64 version, but there are also manuals for Windows and Mac on official website.
INFO! All the actions are conducted with unprivileged user. If privilege elevation needed sudo directive is used.

Installing toolchain and setup environment

  • Update repository
    sudo apt-get update
  • Install dependencies
    sudo apt-get install python-serial python-argparse openocd flex bison libncurses5-dev autoconf texinfo build-essential libftdi-dev libtool zlib1g-dev genromfs git-core wget zip python-empy
  • Install 32-bit dependencies
    sudo apt-get install libc6:i386 libgcc1:i386 gcc-4.6-base:i386 libstdc++5:i386 libstdc++6:i386

  • Add user to "dialout" group, so we have access to "modem" devices, which includes /dev/ttyACM0
    sudo usermod -a -G dialout $USER
  • Make git "look pretty"git config --global color.branch auto
    git config --global color.diff auto
    git config --global color.interactive auto
    git config --global color.status auto

  • Make file "updateUdev.sh"
    #!/bin/bash
    cat > $HOME/rule.tmp <<_eof br="">
    # All 3D Robotics (includes PX4) devicesSUBSYSTEM=="usb", ATTR{idVendor}=="26AC", GROUP="plugdev"# FTDI (and Black Magic Probe) DevicesSUBSYSTEM=="usb", ATTR{idVendor}=="0483", GROUP="plugdev"# Olimex DevicesSUBSYSTEM=="usb",  ATTR{idVendor}=="15ba", GROUP="plugdev"_EOFsudo mv $HOME/rule.tmp /etc/udev/rules.d/10-px4.rulessudo restart udev
    > Chmod +x updateUdev.sh
    > ./updateUdev.sh

  • Add user to "plugdev" group
    sudo usermod -a -G plugdev $USER

  • Make updateBashrc.sh file
    #!/bin/bash
    pushd .cd ~wget -c https://launchpadlibrarian.net/186124160/gcc-arm-none-eabi-4_8-2014q3-20140805-linux.tar.bz2tar -jxf gcc-arm-none-eabi-4_8-2014q3-20140805-linux.tar.bz2exportline="export PATH=$HOME/gcc-arm-none-eabi-4_8-2014q3/bin:\$PATH"if grep -Fxq "$exportline" ~/.bashrc; then echo nothing to do ; else echo $exportline >> ~/.bashrc; fi. ~/.bashrcpopd;> Chmod +x updateBashrc.sh> ./updateBashrc.sh

Getting project sources

Let's get the code. Not just code. The code! :)
For sake of copy-pasting convenience I'll omit shell # but all commands should be executed in shell.

mkdir ~/src
cd ~/src
git clone https://github.com/PX4/Firmware
cd Firmware
git submodule init
git submodule update
cd ~/src/Firmware
make distclean

Well, that's basically it. At this point we should have nice clean source distribution.

Compiling i2ctool into NuttX kernel

Now that we need to debug i2c we have to enable i2ctool in NuttX kernel.
In Frimware folder we need to
  • Copy NuttX config for your controller (in my case it's PX4FMU v1) to default NuttX config which will be editedcp nuttx-configs/px4fmu-v1/nsh/defconfig NuttX/nuttx/.config
  • Go to nuttx foldercd NuttX/nuttx
  • Issue configuration command and make changes we wantmake menuconfig
  • i2c tool is in -> Application Configuration -> System Libraries and NSH Add-Ons
  • Quit and choose "save" to save configuration
  • Make backup of initial defconfig
    mv ../../nuttx-configs/px4fmu-v1/nsh/defconfig ../../nuttx-configs/px4fmu-v1/nsh/defconfig.backup
  • Copy our newly made config file in placecp .config ../../nuttx-configs/px4fmu-v1/nsh/defconfig 
  • Now it's build time. First of all we need to build NuttX. I'd not recommend use -j* parameter at this pointcd ../../
    make archives
  • next step will be to build firmware make px4fmu-v1_default
  • finally we need to upload firmware to our board.
    Unplug your board from USB, issue command
    px4fmu-v1_default uploadand plug in board when prompted.
Special thanks to LibraeL for help with this section.



No comments:

VIM cheat sheet

Basic basics :) i - start editing, current symbol a - start editing, next symbol Esc - stop editing :w - write to disk :w <filename> -...