How to Write a C/C++ Program for NVIDIA Jetson Nano

In this tutorial, we will write a basic C++ program for NVIDIA Jetson Nano.

Prerequisites

Check Your GCC Version

Open a terminal window, and run the following command to see the version of the GCC C/C++ compiler you have.

gcc --version

If gcc is not installed, follow the instructions in the next section.

Install the GCC/G++ Compilers 

Open a new terminal window, and type:

sudo apt-get update

Now use this command to install a bunch of packages, including GCC, G++, and GNU Make:

sudo apt install build-essential

You might see some sort of error about things being locked if you try the following command. If you do, kill all processes that are using the APT package management tool using this command:

sudo killall apt apt-get

Remove the lock files:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*

Reconfigure the packages and update:

sudo dpkg --configure -a
sudo apt update

Now use this command to install numerous packages, including GCC, G++, and GNU Make:

sudo apt install build-essential

Press Y to continue.

Wait while the whole thing downloads.

Now, install the manual pages about using GNU/Linux for development (note: it might already be installed):

sudo apt-get install manpages-dev

Check to see if both GCC and G++ are installed.

whereis gcc
whereis g++

Check what version you have.

gcc --version
g++ --version

Install the C/C++ Debugger

In this section, we will install the C/C++ debugger. It is called GNU Debugger (GDB) and enables us to detect problems or bugs in the code that we write.

In the terminal window, type the following command:

sudo apt-get install gdb

You will be asked to type your password, and then click Enter.

Type the following command to verify that it is installed:

gdb

Type this command to quit.

quit

Press Enter.

Exit the terminal.

exit

Install Gedit

Install gedit, a text editor that will enable us to write code in C/C++.

Open a terminal window, and type:

sudo apt-get install gedit

Write a Hello World Program in C

Let’s write a program that does nothing but print “Hello Automatic Addison” (i.e. my version of a “Hello World” program) to the screen.

Open a new terminal window, and create a new folder.

mkdir cpp_basics

Move to that folder.

cd cpp_basics

Open a new C program.

gedit hello_automaticaddison_c.c
#include<stdio.h>
int main()
{
printf("Hello Automatic Addison\n");
return 0;
}

Save the file, and close it.

See if your file is in there.

ls

Compile the program.

gcc hello_automaticaddison_c.c -o hello_automaticaddison_c

Run the program.

./hello_automaticaddison_c

Here is what I see.

2021-04-02-160235

Write a Hello World Program in C++

Open a new C++ program.

gedit hello_automaticaddison_cpp.cpp

Write the following code.

// Simple C++ program to display "Hello Automatic Addison"
  
// Header file for input output functions
#include<iostream> 
  
using namespace std;
  
// main function
// where the execution of program begins
int main()
{
    // prints hello world
    cout<<"Hello Automatic Addison";
      
    return 0;
}

Save the file, and close it.

See if your file is in there.

ls

Compile the program. All of this is just a single command.

g++ -o hello_automaticaddison_cpp hello_automaticaddison_cpp.cpp

If you see an error, check your program to see if it is exactly like I wrote it.

Run the program.

./hello_automaticaddison_cpp

Here is what I see.

2021-04-02-160742

That’s it. Keep building!

How to Write a Python Program for NVIDIA Jetson Nano

In this tutorial, we will write a basic Python program for NVIDIA Jetson Nano.

Prerequisites

Install Python

To install Python, open a new terminal window and type:

sudo apt-get install python python3

To find out where the Python interpreter is located, type this command.

which python

You should see:

/usr/bin/python

Install Gedit

Install gedit, a text editor that will enable us to write code in Python.

sudo apt-get install gedit

Install Pip

Let’s begin by installing pip. Pip is a tool that will help us manage software packages for Python.

Software packages are bundles of code written by someone else that are designed to solve a specific problem. Why write code to solve a specific problem from scratch, when someone else has already written code to solve that exact same problem? That is where software packages come into play. They prevent you from having to reinvent the wheel.

Open up a fresh Linux terminal window.

Type the following command to update the list of available packages that can be installed on your system.

sudo apt-get update

Type your password.

Upgrade all the packages. The -y flag in the following command is used to confirm to our computer that we want to upgrade all the packages.

sudo apt-get -y upgrade

Type the following command to check the version of Python you have installed.

python3 --version

My version is 3.6.9. Your version might be different. That’s fine.

Now, let’s install pip.

sudo apt-get install -y python3-pip

If at any point in the future you want to install a Python-related package using pip, you can use the following command:

pip3 install package_name

Create a Virtual Environment

In this section, we will set up a virtual environment. You can think of a virtual environment as an independent workspace with its own set of libraries, settings, packages, and programming language versions installed. 

For example, you might have a project that needs to run using an older version of Python, like Python 2.7. You might have another project that requires Python 3.8. Setting up separate virtual environments for each project will make sure that the projects stay isolated from one another.

Let’s install the virtual environment package. 

sudo apt-get install -y python3-venv

With the software installed, we can now create the virtual environment using the following command. The dot(.) in front of py3venv makes the directory a hidden directory (the dot is optional):

python3 -m venv .py3venv

Type the following command to get a list of all the directories. You should see the .py3venv folder there.

ls -a

List all the contents inside the .py3venv folder.

ls .py3venv/

Now that the virtual environment has been created, we can activate it using the following command:

source ~/.py3venv/bin/activate

Look what happened. There is a prefix on the current line that has the name of the virtual environment we created. This prefix means that the .py3venv virtual environment is currently active.

2021-04-02-155424

When a virtual environment is active that means that when we create software programs here in Python, these programs will use the settings and packages of just this virtual environment. 

Keep your terminal window open. We’re not ready to close it just yet. Move on to the next section so that we can write our first program in Python.

Write a “Hello World” Program

Let’s write a program that does nothing but print “Hello Automatic Addison” (i.e. my version of a “Hello World” program) to the screen.

Create a new folder.

mkdir py_basics

Move to that folder.

cd py_basics

Open a new Python program.

gedit hello_automaticaddison.py

Type the following code in there:

#!/usr/bin/env python 
print("Hello Automatic Addison!")

Save the file, and close it.

See if your file is in there.

ls

Run the program.

python hello_automaticaddison.py
2021-04-02-155716

Deactivate the virtual environment.

deactivate

That’s it. Keep building!

How to Install ROS Melodic on the NVIDIA Jetson Nano

In this tutorial, I will show you everything you need to know to install ROS Melodic on the NVIDIA Jetson Nano. The official tutorial is here and my quick tutorial is here, but I will run through all the steps below.

Prerequisites

You have set up your NVIDIA Jetson Nano Developer Kit.

Directions

In the Start Menu, go to Preferences -> Software & Updates. Then click on it.

Make sure main, universe, restricted, and multiverse are all checked. Then close the window.

2021-04-02-140620

Now open up a new terminal window, and type (or copy and paste) the following command:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

The command above sets your computer up to accept software from packages.ros.org. 

Now we need to set up the secure keys so that our system accepts what we are going to download. 

For the next step, update the package list on your system.

sudo apt update

Now type:

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

Now do a full desktop install of ROS. The command below installs all the software, tools, algorithms, and robot simulators for ROS. After you type the command and press Enter, press Y and hit Enter when asked if you want to continue. It will take a while to download all this stuff, so feel free to take a break while ROS downloads to your system.

sudo apt-get update
sudo apt install ros-melodic-desktop-full

Type Y and press Enter to complete the installation.

Set up the environment variables.

echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

Note that anytime you want to see what environment variables ROS is using, you can type the following command:

export | grep ROS

This command will show you the ROS distribution you are using, the version of Python ROS is using, and a bunch of other stuff.

The two key variables are as follows:

ROS_MASTER_URI: Shows the URL where the roscore is in execution. This is often your own local computer.

ROS_PACKAGE_PATH: Shows the path on your computer where the ROS packages are.

Install some other tools that you will work with in ROS. After you type the command below, press Y and Enter to complete the download process.

sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential

Press Y and hit Enter.

Now initialize rosdep. This is a tool that is required before you can use ROS.

sudo apt install python-rosdep
sudo rosdep init
rosdep update

Here is the last step of the installation process. Check which version of ROS you have installed. If you see your ROS version as the output, congratulations you have successfully installed ROS!

rosversion -d
2021-04-02-144436