Getting Started
Throughout this guide we will focus on completing a specific example task. This task will involve setting up the codebase, running the fake/keyboardwalk
binary (which uses simulated hardware) and seeing the robot move in NUsight.
Prerequisites
Git
For our example, we will need Git to get the codebase later on in this guide.
Git is a version control tool. We use it to track changes to the codebase and work together on the same codebase without getting in each other's way.
To work on the NUbots code, you'll need to know the basics of using Git, specifically:
- how to clone remote repositories onto your computer
- how to create branches
- how to add and commit changes
- how to fetch, pull, and push changes
You can do all the above using Git in the terminal, or you can use a Git GUI tool such as GitKraken or GitHub Desktop. There is a guide to using Git in NUbots here.
Docker
Docker is a container system that allows you to install development tools and run builds in an isolated environment that won't affect the rest of your system. We use it to set up the NUbots build system and run builds and binaries.
You will need to perform the steps correctly to set up Docker for your operating system to complete the example.
Ubuntu
To install on Ubuntu, use the official Docker instructions. These instructions should work for Ubuntu-like Linux distributions, such as Pop!_OS. There are multiple ways to install Docker, as detailed on the website. NUbots recommends the following for installing Docker
Navigate to the section for installing using the Apt repository. Complete the three steps under this section.
Install python3 and pip.
sudo apt-get install python3 python3-pipAdd the current user to the Docker group, so you can run Docker commands without
sudo
.sudo usermod -aG docker "${USER}"Reboot to make the group change take effect.
MacOS
Install Homebrew by following the instructions at brew.sh.
Install Python 3 and Docker using Homebrew.
brew install python3brew cask install dockerRun the Docker Desktop app that was installed and ensure that Docker is running by checking that it shows a green dot in the app menu bar.
Go to Advanced settings in Docker Desktop's Preferences and increase the memory available to containers to at least 4GB. By default Docker will allocate 2GB of memory to containers, which is not enough to build the NUbots docker image.
Windows
Ensure your Windows build is at least 2004.
- Press WIN + R.
- Type
winver
and hit Enter. - Inspect the version to verify it is at least 2004. Upgrade Windows if not.
Enable WSL 2
- Open Windows Powershell in administrator mode.
- Run the following to install WSL:dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
- Run the following to enable the Virtual Machine Platform feature needed for WSL:dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
- Restart your computer.
Go to the Microsoft Store. Search for 'Ubuntu 20.04 LTS' and install it. Launch Ubuntu.
Ensure Ubuntu is using WSL 2.
- In Powershell, run:wsl --list --verbose
- If the version is not 2, run:wsl --set-version Ubuntu-20.04 2
Note that 'ubuntu' in the last command may need to be capitalised. Check the exact spelling from the list output in the first command of this step.If you have issues with the Linux kernel needing to be updated, get the update from the Microsoft page.
- In Powershell, run:
Download and install Docker for Windows. Choose the latest stable version. Ensure you enable WSL 2 features.
Configure Docker settings.
- In the system tray, right-click the Docker icon and select Settings.
- In General, enable the Use the WSL 2 based engine option.
- In Resources: WSL Integration enable Ubuntu.
In Ubuntu run
sudo apt update
followed bysudo apt upgrade
.Ensure you have pip3 by running
sudo apt install python3-pip
When you clone/download the code, you should do so in the WSL Ubuntu terminal, from your home folder. Run cd ~
to navigate to your home folder in Ubuntu.
Outside of WSL, the path to the cloned NUbots code folder will be of the form \\wsl$\Ubuntu\home\<USERNAME>\NUbots
, where <USERNAME>
is the username you set up for Ubuntu. This path should be the one you use to open the code in text editors such as Visual Studio Code.
GitKraken will not allow you to open the repository outside of WSL. Use the steps described in this article to set up your system to use GitKraken in WSL.
To be able to copy and paste into the Ubuntu terminal, right-click on the bar at the top of the Ubuntu window. Then click on Properties, and enable Use Ctrl+Shift+C/V as Copy/Paste in the Options tab. Note that this is Ctrl+Shift+C/V not Ctrl+C/V.
A Text Editor
This section is not required for our example, but it is a good step to do in case you need to edit code in the future.
You'll need a text editor to make changes to the code. We recommend Visual Studio Code, but feel free to use your favourite editor.
To ensure a consistent code style, NUbots uses clang-format, a tool that reformats code files in the repository to have the same style, even if they're written by different people. For convenience, you can install the clang-format
extension in Visual Studio Code to automatically format the files when you make changes.
Another useful tool for editing NUbots code in Visual Studio Code is the Remote Development extension pack. This pack allows Visual Studio Code to work from within the Docker container, using the tools and libraries installed there.
Getting the Code
With all the prerequisites in place, you can now clone the NUbots repository from GitHub to build it and run the code. This is an important step in our example.
Clone the repo by running the following command in a terminal.
git clone --recursive https://github.com/NUbots/NUbots.gitThe repo will be cloned into a
NUbots
directory in the current working directory.Change into the cloned directory and install the required Python dependencies.
cd NUbotssudo -H pip3 install -r tools/requirements.txt
Building the Code
Run all these commands for our example, and pay special attention to the instructions in the second step relating to our example.
To build the code, open a terminal and change into the NUbots code directory. Then do the following:
Select the platform (type of computer) that the built binaries will run on and create the Docker image. You do this by running the command:
./b target <PLATFORM>Replace
<PLATFORM>
with your desired platform. The available platforms are:generic
- used to build the code for a "generic" computer. This will enable you to run the built binaries on the computer you're building the code on.nuc12wshi7
- used to build the code for the platform that the robots run on. This will build binaries that are optimised for the NUgus robots.
If you are following the example, use
generic
.With the target platform selected, choose the binaries (called "roles") that you want to build. You do this by running the following command:
./b configure./b configure -iNote that you will need to do
configure
without-i
the very first time you do this. Using-i
will show a menu that will allow you to choose the roles to build. Enable the roles you want to build and disable the ones you don't by highlighting them using the ↑ and ↓ arrow keys and pressing the spacebar to toggle on and off.If you are following the
fake/keyboardwalk
example, you will need to make sureROLE_fake-keyboardwalk
is set toON
. Find the list of items that are prefixed withROLE_
and set all toOFF
except forfake-keyboardwalk
, which should beON
. You do not need to change any other build options in this example.Press c to configure. Once configuration has finished, press g to generate the build list. Press e to exit if it has not automatically returned you to the terminal.
Build the code by running:
./b build
Some built roles use data from .yaml
configuration files. If you change a configuration file, you will need to run ./b build
again.
Running the Code
With the code built, you can run the code on a real robot or on your computer using a simulated robot. If you are following the example in this guide, go to 'Running with a Simulated Robot' below, and skip 'Running on a Real Robot'.
Running with a Simulated Robot
If you are not running the code on a robot, you will need to use a simulated robot. If, for example, you want to run a role called "test", you would run
./b run test
Roles run this way generally contain platform::HardwareSimulator
, a module that simulates robot hardware input and output, or platform::Webots
, a module that communicates with the simulated robot in Webots.
If you are following the example, run ./b run fake/keyboardwalk
.
Running on a Real Robot
You can skip this section if you are following the example.
To run the code on a robot, you need to make sure the robot is powered on, install the built roles onto the robot, and then ssh
into the robot to run the installed roles.
Install the built roles onto a robot by running:
./b install [options] <robot>Where:
[options]
corresponds to zero, one, or multiple options as detailed in the Build System page<robot>
corresponds to the name of the robot such asn1
ornugus1
, or an IP address
Next, open a new terminal and
ssh
into the robot by running:ssh nubots@<address>Where
<address>
is the IP address of the robot, in the form10.1.1.x
, withx
being the specific robot's number, i.e.1
,2
, or3
.Now you can run any role you have built and installed onto the robot. For example, if you built the
scriptrunner
role and want to make the robot stand up, you would run:./scriptrunner Stand.yamlThis will start the binary to run the stand script, but the robot won't move until you press the green middle button on its back.
In addition to the stand script, the
scriptrunner
role can run other scripts. These can be found in thescripts
folder. See Configuration and Script System for details.You can also run other roles that are not
scriptrunner
. For example, to run therobocup
role enter the command:./robocup
Get, Build and Run NUsight
NUsight is a visual debugging web application used at NUbots that visually represents what the robot is doing. It can be used to view output from any platform, whether that is a real NUgus robot or a simulation. Read more on the capabilities of NUsight on the NUsight page.
NUsight is needed to see the robot walking when we run fake/keyboardwalk
.
Open up a new terminal to run NUsight and NUbots code side by side.
Get the dependencies for NUsight by running
./b yarnBuild NUsight by running
./b yarn buildFind the IP address of the Docker container.
In Linux, run
ifconfigIn Windows WSL, make sure you are running a binary (eg
fake/keyboardwalk
) in your original terminal, and in your second terminal rundocker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps | grep nubots:selected | cut -d " " -f1)There will be an entry called
docker0
, or similar. Find the address after the wordbroadcast
. We will refer to this address as<ADDRESS>
.Run the following command to run NUsight
./b yarn prod --address <ADDRESS>If you are on the NUbots lab network
epsilon-z
you do not need the--address
argument. Just run./b yarn prod
.The terminal will output a link - localhost:9090. Visit it to view NUsight.
Example
In this section we will finish off our example of setting up the codebase, running the fake/keyboardwalk binary and seeing the robot walk in NUsight.
By now you will have gone through this page and have everything set up to make the robot walk. Ensure you have all the following steps complete
Prerequisite: Git
Prerequisite: Docker
Getting the Code
Building the Code
Running the Code: Running with a Simulated Robot
Build and Run NUsight
With the fake/keyboardwalk
binary running, and with NUsight running and your browser open to localhost:9090, do the following
Select the 'localisation' tab in NUsight.
You will see a stationary robot. To focus on the robot, click inside the window and press space to change perspective.
To make the robot walk, go into the terminal that is running
fake/keyboardwalk
and hit e to enable walking. Use w, a, s and d to make the robot move. Note that this is a basic simulation that does not use gravity, so the robot will move its limbs but may not move in the world.