Skip to content
thebirdgr edited this page Jan 4, 2021 · 11 revisions

Welcome to the NUS ROBOMASTERS PX4-Autopilot wiki!

For our development, Ubuntu is the best environment. It is highly encouraged that all members use Ubuntu as the following instructions were tested successfully on Ubuntu.

This setup is a long process so be patient and follow each command diligently.

Procedure to setup the entire environment for developing

  1. Gperf installation

    sudo apt-get install -y gperf

  2. Install qt5 and qt5 default

    sudo apt-get install qt5-default

  3. From Nuttx apache homepage, follow the installation commands for Ubuntu:

    sudo apt install \ bison flex gettext texinfo libncurses5-dev libncursesw5-dev \ gperf automake libtool pkg-config build-essential gperf genromfs \ libgmp-dev libmpc-dev libmpfr-dev libisl-dev binutils-dev libelf-dev \ libexpat-dev gcc-multilib g++-multilib picocom u-boot-tools util-linux

If the command doesn't work go to this link and copy the same command: link

`sudo apt install kconfig-frontends` -  gives you the interactive frontend for configuration.

`sudo apt install gcc-arm-none-eabi binutils-arm-none-eabi` -  the toolchain to aid our development for ARM architecture.

Now make directory called _nuttx_ anywhere you feel it should be. 

`cd`
`mkdir nuttx`
Move into that directory
`cd nuttx`
Now we are going to close two repositories into the current folder:

`git clone https://github.com/apache/incubator-nuttx.git nuttx`

`git clone https://github.com/apache/incubator-nuttx-apps apps`

`git clone https://bitbucket.org/nuttx/tools.git tools`

You should be able to see three folders inside nuttx : nuttx, apps and tools
Prepare the kconfig-frontends. Run the following commands in sequence:

`cd tools/kconfig-frontends`

`./configure --enable-mconf --disable-nconf --disable-gconf --enable-qconf --prefix=/usr`

 `make`

`sudo make install`

`sudo ldconfig`

`cd ../..` -- you should now be in the nuttx folder.

We now have all the essentials for building the `.bin` file. This file will flash the operating system onto the board.

Move into the nuttx repository. (Yes there are two folders named nuttx).

`cd nuttx`

We are working on the stm32f4discovery board. So we will configure the command shell for that board.

`./tools/configure.sh -l stm32f4discovery:nsh`

Now make the _bin_ file.

`make`

 The build will complete by generating the binary outputs inside nuttx directory. Typically this includes the nuttx ELF file (suitable for debugging using gdb) and a nuttx.bin file that can be flashed to the board.

To clean the build:

`make clean`
  1. To flash the bin file, and future bin files, we'll be using a tool called openocd. To install it:

    sudo apt install openocd

    Move out out the folders and repository before moving on to the next command. cd ../... -- this will move you out of both the nuttx folders. The command ls should only show one nuttx folder

  2. Git clone official px4 github Preparing px4 environment from px4 website. This will take some time, so document every process along the way.

    Downloading the PX4 source code:

    git clone https://github.com/PX4/PX4-Autopilot.git --recursive

    Move into the folder:

    cd PX4-Autopilot

    Then run the following command. It will perform the installation for you.

    bash ./Tools/setup/ubuntu.sh

    Now you'll have to restart your computer.

    Cross-Compiler

    We will now make a cross-compiler. This means that we are making code for a platform different from the one you're currently developing. We'll be using the GCC compiler. Follow the commands below for installation:

    sudo apt-get install -y gcc-8-arm-linux-gnueabihf g++-8-arm-linux-gnueabihf

    sudo apt-get install -y gcc-8-aarch64-linux-gnu g++-8-aarch64-linux-gnu

    sudo update-alternatives --install /usr/bin/aarch64-linux-gnu-gcc aarch64-linux-gnu-gcc /usr/bin/aarch64-linux-gnu-gcc-8 100 --slave /usr/bin/aarch64-linux-gnu-g++ aarch64-linux-gnu-g++ /usr/bin/aarch64-linux-gnu-g++-8

    sudo update-alternatives --config aarch64-linux-gnu-gcc

    If the last command returns saying that no configuration is possible or something, it's fine.

    Clang installation:

    sudo apt-get install clang

    ROS/Gazebo Installation.

    This will install the Robot Operating System and Gazebo simulation.

    To install the development toolchain:

    1. Download the script in a bash shell:

    wget https://raw.githubusercontent.com/PX4/Devguide/master/build_scripts/ubuntu_sim_ros_melodic.sh 2. Run the script:

    bash ubuntu_sim_ros_melodic.sh

    You may need to acknowledge some prompts as the script progresses. Press Enter.

  3. FAST RTPS installation

    Now is the tricky part. If this step goes wrong the debugging of errors is very tedious.

    You might already have the following packages but do install just to make sure.

    sudo apt install cmake g++ python3-pip wget git

    sudo apt install libasio-dev libtinyxml2-dev

    sudo apt install libssl-dev

    pip3 install -U colcon-common-extensions vcstool

    Create a Fast-DDS directory and download the repos file that will be used to install eProsima Fast DDS and its dependencies:

    mkdir ~/Fast-DDS ---- Don't worry about the file location, the '~' character takes care of it

    cd ~/Fast-DDS

    wget https://raw.githubusercontent.com/eProsima/Fast-DDS/master/fastrtps.repos

    mkdir src

    vcs import src < fastrtps.repos

    colcon build

    echo 'source ~/Fast-DDS/install/setup.bash' >> ~/.bashrc ---- this will run a colcon instance every time the terminal is open.

    Fast DDS-Gen installation

    This requires Java version 11 and gradle.

    Installing Java:

    sudo apt install default-jdk

    Configuring it to version 11:

    update-alternatives --config java

    This will bring up an interactive shell. Press the number that corresponds to version 11 and press Enter.

    #Gradle Installation

    Follow the following commands:

    VERSION=6.5.1

    wget https://services.gradle.org/distributions/gradle-${VERSION}-bin.zip -P /tmp

    sudo unzip -d /opt/gradle /tmp/gradle-${VERSION}-bin.zip

    sudo ln -s /opt/gradle/gradle-${VERSION} /opt/gradle/latest

    We will setup the environment variable. We will use a traditional text editor vim. You can use any text editor that you're familiar with too, just take note of the file locations.

    sudo apt install vim

    sudo vim /etc/profile.d/gradle.sh ----- This will take you inside the vim editor interface.

    This editor is very old and can only be operated by keyboard commands.

    Press the key i on your keyboard to enter edit mode.

    Now paste the following configuration:

    export GRADLE_HOME=/opt/gradle/latest export PATH=${GRADLE_HOME}/bin:${PATH}

    Press the Esc key to exit edit mode.

    Now type :wq and hit Enter. This will write and quit the file, meaning that it's saved.

    We give the script execute rights, so that it can be run.

    sudo chmod +x /etc/profile.d/gradle.sh. ---- chmod modifies the permission of files and folders.

    Lastly source the file:

    source /etc/profile.d/gradle.sh

    To verify:

    gradle -v. ---- it should return the version your using.

    Remember that we are still in the Fast DDS-Gen installation.

    Compile Fast DDS-Gen by following the steps below:

    cd ~

    git clone --recursive https://github.com/eProsima/Fast-DDS-Gen.git

    cd Fast-DDS-Gen

    gradle assemble

    ./gradlew assemble

  4. Now we begin to work with our code. Make a directory, ideally 'robomasters' and clone the following directories and perform the following commands.

    cd --- to move to the root directory

    mkdir robomasters

    cd robomasters

    git clone --recursive https://github.com/nusrobomaster/PX4-Autopilot.git

    cd PX4-Autopilot

    git checkout rm2021. --- just hit Tab if you don't want to type the while thing

    The above branch as the bin file.

    Type make and double Tab to list the available boards. You should see something like robomaster-dev-a or something.

    make board-name --- replace board-name with the dev a board. This will make the bin file.

    (If you get an error saying that python is not detected, it’s most likely that it is not linked. So use the following command)

    sudo ln -s /usr/bin/python3 /usr/bin/python

    Move out of the directory before moving onto the next step.
    

    cd ..

    Clone the boatloader repository.

    git clone --recursive https://github.com/nusrobomaster/PX4-Bootloader.git

    cd PX4-Bootloader

    make -- this will make the bootloader.

  5. Lastly, we should be able to upload and flash the board now.

    Move into the PX4-Autopilot directory from where you are.

    cd ..

    cd PX4-Autopilot

    Now connect the board via USB. If it's the first time flashing put it into flashing mode by shorting the circuit. We will give you a demonstration. You should see an LED flashing.

    Enter the following command.

    make robomaster_dev-a upload

    This should do all the work for you. Now install minicom which will let us communicate with shell on the board.

    sudo apt install minicom

    The following command will open a new terminal with the shell running on it.

    minicom -D /dev/ttyACM0

    USB console startup. When using a USB console, the start-up sequence differs a little: In this case, you are required to press ENTER three times. Then NSH prompt will appear as described above. This is required for the following reasons:

    1. This assures that the USB connection is stable. The USB connection may be made, broken, and re-established a few times if the USB cable is not yet fully seated. Waiting for ENTER to be pressed three times assures that the connection is stable.
    
    2. The establishment of the connection is two step process: First, the USB serial connection is made with the host PC. Then the application that uses the serial interface is started on the host. When the serial connection is established on the host, the host operating system may send several AT modem commands to the host depending upon how the host serial port is configured. By waiting for ENTER to be pressed three consecutive times, all of these modem commands will go to the bit-bucket and will not be interpreted as NSH command input.
    
    3. Similarly, in the second step when the applications is started, there may be additional AT modem commands sent out the serial port. Most serial terminal programs will do this unless they are specifically configured to suppress the modem command output. Waiting for the ENTER input eliminates the invalid command errors from both (2) and (3).
    
    4. Finally, if NSH did not wait for some positive indication that the serial terminal program is up and running, then the output of the NSH greeting and initial NSH prompt would be lost. 
    

    (The above numbered list is quoted from Apache NuttX docs.)

    You shell will not be aligned. To align the returns, `CTRL+A`, `Z` and `U`. Your shell should now be aligned.
    
  6. To see the available commands.

    help

    To light-up one of the on-board LEDs,

    gpio write G 2 0 --force

Now you're ready to begin programming.

Tip, for script writers, nsh doesn't use printf(). Use nsh_output(vtbl, "Hello, World!"); instead.