Create a Self-Driving Robot — Part One

Tommy Warner
Jan 15, 2018 · 6 min read

This article is the first in a series where we will learn to make an autonomous, self-driving robot with no prior programming experience using a visual programming language called XOD. You can buy the robot on Amazon and have everything required to make your own robot for about $35.

We will be programming using drag and drop flow based programming in XOD. In doing this we will be able to do complex programming in a visual programming environment which is simple and easy to learn. You can follow along in the browser version or download the desktop IDE here.

We will begin by controlling DC-Motors. The same concepts could be applied to different types of motors and different hardware. Using different states we will be able to program DC-Motors to run quickly or slowly and in the positive and negative directions. For this application it will allow the robot to follow commands such as moving forward, turning, or moving in reverse.

The goal of this first part is to teach the robot two commands: start and stop.

· Start — Set both motors to 1 (On full speed) for 2 seconds

· Stop — Set both motors to 0 (Off) for 2 seconds

Test Circuit

This particular circuit uses the Adafruit Motor Shield in conjunction with an external power supply and 2 DC motors. One motor is connected to the left wheel and one is connected to the right wheel.

Program Structure Overview

Adding States

Within the main patch create a new node by selecting “File -> New Patch” (or Ctr+N). Name the first patch “forward” and the second patch “stop”.

By creating a new patch we now have a new state where the motor can be controlled within the “main” patch. Notice that you can now drag your newly created nodes from the project browser into the workspace or search for them by double clicking and using them within your project.

One note is to make sure when working with motors in multiple states is that we allow a brief period of time for those motors to initialize. We’ll use a defer node to allow that to happen. Now we can go ahead and setup the flow of our program.

Boot →Defer →Forward →Stop

Select the xod/core/boot node to begin the process. Next add a xod/core/defer-pulse node to allow the motors to initialize and lastly add our new state patches forward and stop. Notice as of right now there is no way to connect our newly made forward and stop patches. We’ll design those inputs and outputs as we configure the states in the next step.

Designing the state patches

Open up your forward patch and add a xod/patch-node/input-pulse and a xod/patch-node/output-pulse to the state. Think of the input as “begin” out the output as “stop”. You can name the pulses whatever you like but it is common within XOD to name the input pulse “SET” and the output pulse “DONE”. You can change the name of the pulses by clicking the green flag in the Inspector on the bottom left.

Adding Libraries

Installation of the library is simple. Go to xod.io/libs/ and copy the name of the library. Then click “File -> Add Library” paste in the name of the library and click to install.

Controlling Motors

The patch is entered from the SET pulse. From there xod/core/delay sets an output to true for a set period of time. Set the value of T to 2 seconds. xod/core/gate-number allows a number to be sent through the gate when it is open (set to true). We want that number to be 1 since we want both motors to be fully on. We’ll control the motors using the nkrkv/af-motor/dc-motors node from the library we just added. Connect the number from the gate to the input M1 and M2. Notice we are using motors 1 and 2 based on the circuit diagram for this project. Lastly we need to add just one more node; xod/core/defer-pulse will make sure that the gate to motor is closed before moving on to DONE.

Copying to the “stop” node.

Linking together states within Main

Connect the states and upload the project to your Arduino.

Now you will see both motors turn on full speed for 2 seconds then turn off!

XODlang

XOD visual programming language use cases and projects