How to Install Docker on Ubuntu

In this blog post, I will explain the step-by-step process for installing Docker on an Ubuntu Linux system. As a reference for you, here are the official instructions.

At the end, I will explain in detail how Docker is used in robotics.

Uninstall Old Versions of Docker

The first thing you need to do is to uninstall any outdated versions of Docker that might be lurking on your computer.

Open a terminal window, and type the following command:

for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

You might get a message back saying that you have none of those packages installed. That is just fine.

Set Up the Apt Repository

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Install Docker

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose

Verify that Docker Installed

sudo service docker start
sudo docker run hello-world

Post-Installation Configuration

Here are some recommended steps to take after you have successfully installed Docker.

Create the docker group.

sudo groupadd docker

Add your user to the docker group.

sudo usermod -aG docker $USER

Reboot your computer.

sudo reboot

Or you could have activated changes to groups instead of rebooting:

newgrp docker

To test that you can run Docker without using “sudo”, type the following command:

docker run hello-world

Now configure Docker to start on boot:

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

You can stop this startup on boot at any time, by typing:

sudo systemctl disable docker.service
sudo systemctl disable containerd.service

Now you’re all set.

How is Docker Used in Robotics?

Docker is a tool that allows you to package your software, including all its dependencies, into a standalone unit called a “container.” You can then deploy this container on any machine that has Docker installed, regardless of the underlying computer that is being used.

Docker is used in robotics for a number of reasons, including:

  • Reproducibility: Docker containers allow you to create a reproducible development environment for your robotics project. This means that you can be confident that your project will run the same way on any machine that has Docker installed.
  • Portability: Docker containers are portable, meaning that they can be easily moved from one machine to another. This is useful for developing and deploying robotics applications on a variety of platforms, including embedded devices, cloud servers, and robots.
  • Scalability: Docker containers are lightweight and efficient, making them ideal for scaling robotics applications. You can easily start and stop multiple containers on a single machine, or deploy multiple containers across a cluster of machines.

Here are some specific examples of how Docker is used in robotics:

  • Developing and testing robotics software: Docker can be used to create isolated development environments for robotics software. This allows developers to work on different components of a robotics project in parallel, without worrying about conflicts with other dependencies. Docker can also be used to test robotics software on different platforms, without having to install all of the necessary dependencies on each platform.
  • Deploying robotics applications: Docker can be used to deploy robotics applications on a variety of platforms, including embedded devices, cloud servers, and robots. This makes it easy to scale robotics applications and deploy them to production environments.
  • Running ROS 2: Docker is often used to run ROS 2. You can find the official ROS 2 Docker repository here on DockerHub.

If you are looking to simplify your development and deployment of software on to robots, I highly recommend you learn Docker.

That’s it. Keep building!

Obstacle Avoiding Robot Using a DC Motor and Arduino

In this tutorial, I will show you my setup for an obstacle avoiding robot that has two DC motors and uses Arduino as its “brain”.

Here is the motor I am working with, but you can use any motor that looks like this one.

1_dc_motor_encoder_wheels-1

Real-World Applications

This project has a number of real-world applications: 

  • Indoor Delivery Robots
  • Robot Vacuums
  • Factories

Prerequisites

You Will Need

In addition to the parts listed in the article I linked to in the Prerequisites, you will need the following components (#ad).

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Set Up the HC-SR04 Ultrasonic Distance Sensor

The first thing we need to do is set up the hardware. Here is the wiring diagram:

hcsr04_ultrasonic_distance_sensor_bb
  • VCC on the sensor connects to 5V on the Arduino
  • Echo on the sensor connects to Digital Pin 12 on the Arduino
  • Trig (stands for trigger) on the sensor connects to Digital Pin 11 on the Arduino
  • GND (stands for Ground) on the sensor connects to GND on the Arduino

Test the Ultrasonic Distance Sensor

Now, upload the following sketch to the Arduino to test the ultrasonic sensor. I named my program test_ultrasonic_distance_sensor.ino.

/**
 *  This program tests the ultrasonic
 *  distance sensor
 * 
 * @author Addison Sears-Collins
 * @version 2.0 2021-04-11
 */
 
/* Give a name to a constant value before
 * the program is compiled. The compiler will 
 * replace references to Trigger and Echo with 
 * 11 and 12, respectively, at compile time.
 * These defined constants don't take up 
 * memory space on the Arduino.
 */
#define Trigger 11
#define Echo 12
 
/*   
 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
 */
void setup(){
 
  // Set the baud rate to 9600. 9600 means that 
  // the serial port is capable of transferring 
  // a maximum of 9600 bits per second.
  Serial.begin(9600);
 
  // Define each pin as an input or output.
  pinMode(Echo, INPUT);
  pinMode(Trigger, OUTPUT);
}
 
void loop(){
 
  // Make the Trigger LOW (0 volts) 
  // for 2 microseconds
  digitalWrite(Trigger, LOW);
  delayMicroseconds(2);
 
  // Emit high frequency 40kHz sound pulse
  // (i.e. pull the Trigger) 
  // by making Trigger HIGH (5 volts) 
  // for 10 microseconds
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trigger, LOW); 
 
  // Detect a pulse on the Echo pin 8. 
  // pulseIn() measures the time in 
  // microseconds until the sound pulse
  // returns back to the sensor.
  int distance = pulseIn(Echo, HIGH);
 
  // Speed of sound is:
  // 13511.811023622 inches per second
  // 13511.811023622/10^6 inches per microsecond
  // 0.013511811 inches per microsecond
  // Taking the reciprocal, we have:
  // 74.00932414 microseconds per inch 
  // Below, we convert microseconds to inches by 
  // dividing by 74 and then dividing by 2
  // to account for the roundtrip time.
  distance = distance / 74 / 2;
 
  // Print the distance in inches
  Serial.println(distance);
 
  // Pause for 100 milliseconds
  delay(100);
}

As soon as uploading is finished and with the USB cable still connected to the Arduino, click on the green magnifying glass in the upper right of the IDE to open the Serial Monitor.

Make sure you have the following settings:

  • Autoscroll: selected
  • Line ending: No Line ending
  • Baud: 9600 baud

Place any object in front of the sensor and move it back and forth. You should see the readings on the Serial Monitor change accordingly.

1_test_ultrasonic_sensor

Code for Obstacle Avoidance

Now, upload the following sketch to your Arduino. I named my program obstacle_avoiding_robot_l298n.ino

/**
 * This robot avoids obstacles 
 * using an ultrasonic sensor.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2021-04-11
 */
 
// Motor A connections
const int enA = 9;
const int in1 = 5;
const int in2 = 6;

// Motor B connections
const int enB = 10;
const int in3 = 7;
const int in4 = 8;

// Set the speed (0 = off and 255 = max speed)
// If your wheels are not moving, check your connections,
// or increase the speed.
const int motorSpeed = 80;
 
/* Give a name to a constant value before
 * the program is compiled. The compiler will 
 * replace references to Trigger and Echo with 
 * 11 and 12, respectively, at compile time.
 * These defined constants don't take up 
 * memory space on the Arduino.
 */
#define Trigger 11
#define Echo 12
 
/*   
 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
 */
void setup(){
   
  // Set the baud rate to 9600. 9600 means that 
  // the serial port is capable of transferring 
  // a maximum of 9600 bits per second.
  //Serial.begin(9600);
 
  // Motor control pins are outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // Set the motor speed
  analogWrite(enA, motorSpeed); 
  analogWrite(enB, motorSpeed); 
 
  // Define each pin as an input or output.
  pinMode(Echo, INPUT);
  pinMode(Trigger, OUTPUT);
 
  // Initializes the pseudo-random number generator
  // Needed for the robot to wander around the room
  randomSeed(analogRead(3));
 
  delay(200);     // Pause 200 milliseconds               
  go_forward();   // Go forward
}
 
/*
 * This is the main code that runs again and again while
 * the Arduino is connected to power.
 */
void loop(){
  int distance = doPing();
 
  // If obstacle <= 16 inches away
  if (distance >= 0 && distance <= 16) {  
      
    //Serial.println("Obstacle detected ahead");  
    go_backwards();   // Move in reverse
    delay(2000);
 
    /* Go left or right to avoid the obstacle*/
    if (random(2) == 0) {  // Generates 0 or 1, randomly        
      go_right();  // Turn right
    }
    else {
      go_left();  // Turn left
    }
    delay(3000);
    go_forward();  // Move forward
  }
  delay(50); // Wait 50 milliseconds before pinging again
}
 
/*
 * Returns the distance to the obstacle as an integer
 */
int doPing () {
  int distance = 0;
  int average = 0;
 
  // Grab four measurements of distance and calculate
  // the average.
  for (int i = 0; i < 4; i++) {
 
    // Make the Trigger LOW (0 volts) 
    // for 2 microseconds
    digitalWrite(Trigger, LOW);
    delayMicroseconds(2);
 
     
    // Emit high frequency 40kHz sound pulse
    // (i.e. pull the Trigger) 
    // by making Trigger HIGH (5 volts) 
    // for 10 microseconds
    digitalWrite(Trigger, HIGH);
    delayMicroseconds(10);
    digitalWrite(Trigger, LOW);
      
    // Detect a pulse on the Echo pin 8. 
    // pulseIn() measures the time in 
    // microseconds until the sound pulse
    // returns back to the sensor.    
    distance = pulseIn(Echo, HIGH);
 
    // Speed of sound is:
    // 13511.811023622 inches per second
    // 13511.811023622/10^6 inches per microsecond
    // 0.013511811 inches per microsecond
    // Taking the reciprocal, we have:
    // 74.00932414 microseconds per inch 
    // Below, we convert microseconds to inches by 
    // dividing by 74 and then dividing by 2
    // to account for the roundtrip time.
    distance = distance / 74 / 2;
 
    // Compute running sum
    average += distance;
 
    // Wait 10 milliseconds between pings
    delay(10);
  }
 
  // Return the average of the four distance 
  // measurements
  return (average / 4);
}
 
/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void go_backwards() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_right() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void stop_all() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Disconnect the USB cable from the Arduino.

Place your robot on the floor.

Turn the power ON for the motors.

Plug in the Arduino’s power.

Watch the Obstacle Avoiding Robot move!

If your wheels are not moving, check your connections, or increase the speed.

Related Articles

How to Connect DC Motors to Arduino and the L298N

In this tutorial, we learn how to connect DC motors to Arduino and the L298N motor driver.  

Here is the motor we will work with, but you can use any motor that looks like this one.

1_dc_motor_encoder_wheels-1

Prerequisites

You have the Arduino IDE (Integrated Development Environment) installed on your PC (Windows, MacOS, or Linux).

You Will Need

This section is the complete list of components you will need for this project (#ad).

OR

  • Self-Balancing Car Kit (which includes everything above and more…Elegoo and Osoyoo are good brands you can find on Amazon.com)

I also bought the following:

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Set Up the Hardware

The first thing we need to do is set up the hardware. Below is the wiring diagram, and here is the pdf version so you can see the pin numbers better:

connect_dc_motor_l298n_bb

On my motors, the outer pins are the ones that drive the motors. These pins need to be connected to the L298N OUT1-OUT4 pins.

Remove the two black jumpers that are on the board that cover the ENA and ENB pins. Do not remove the other jumper that is near the power input pins.

Write the Code

Now let’s write the code. You will need to:

  • Load this code to your Arduino from your PC.
  • Remove the Arduino from your PC.
  • Turn on your Arduino using the 9V battery.
/*
 * Author: Automatic Addison
 * Website: https://automaticaddison.com
 * Description: Controls the speed and direction of two DC motors.
 */

// Motor A connections
const int enA = 9;
const int in1 = 5;
const int in2 = 6;

// Motor B connections
const int enB = 10; 
const int in3 = 7;
const int in4 = 8;

// Set the speed (0 = off and 255 = max speed)
const int motorSpeed = 128;

void setup() {
  
  // Motor control pins are outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // Set the motor speed
  analogWrite(enA, motorSpeed); 
  analogWrite(enB, motorSpeed); 
}

void loop() {

  // Go forwards
  go_forward();
  delay(3000);

  // Go backwards
  go_backwards();
  delay(3000);

  // Go right
  go_right();
  delay(3000);

  // Go left
  go_left();
  delay(3000);

  // Stop
  stop_all();
  delay(3000);
}

/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void go_backwards() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_right() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void go_left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void stop_all() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

Run the Code

When you run this code, you will see your motors move forwards, backwards, right turn, left turn, and then stop. If your motors don’t do this exact sequence, consider switching the position of the wire connections that both motors have to the L298N.

References