Create a Self-Driving Robot — Part One
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
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
Each project within XOD starts with a single patch called main. The basic flow of the program will be written here. Within main we will create a few state-nodes, “forward” and “stop”. The main patch will consequently go through each state controlling the motors. Within each state (forward and stop) we will design logic that sends values to each motor for a set period of time.
Begin by creating a new project within XOD. Notice that you should already have the main patch within the workspace. Let’s start work by creating two states: “forward” and “stop”.
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
The next goal is to have a way for these states to become active. A common way of moving between states is by using pulses. These pulses will control what happens when the state is entered and give us control over what will happen right before the state exits.
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.
This particular setup uses a XOD library to control the motors. The library that we need for this project is titled nkrkv/af-motor.
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.
The goal of the “forward” state is to turn the motor on for two seconds. We’ll use the combination of a few new nodes to make that happen. Setup the patch to look like this:
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.
Within the forward patch select all of the nodes by click dragging a box around them all (or Ctrl+A). Copy the nodes by selecting Edit -> Copy (or Ctrl+C). Move over to the stop patch and paste by selecting Edit -> Paste (or Ctrl+V). The only difference will be the number bound to the gate; within the inspector change the number of the gate to zero.
Linking together states within Main
Notice now in the main patch you should see purple “SET” and “DONE” connections on the top and bottom of each state. The states wait for a pulse to begin.
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!