Advanced Motor Control — Part Two

In the previous example we went over how to control a pair of DC-Motors using XOD in order to make a robot follow simple commands such as move forward and stop. In this example we will be able to build upon the previous idea as well as add in new commands to increase our control of the robot.

In this tutorial we will have a few new goals:

Teach the robot turning commands (right-turn, left-turn, etc).
Design a “figure-eight” path for the robot to follow and return back to its original position.
Design a feedback loop to allow the robot to repeat commands again and again.

Test Circuit

The circuit for this example is the same as the previous.

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

We will begin work in XOD where we left off with the previous example. In this example we have two patches that we have created forward and stop. These patches are linked together in main.

The image above shows the structure of the main patch on the left and the forward and stop patch on the right. Remember the structure of the forward and stop patch is the same with the only difference being the number bound to the gate which can be modified using the Inspector on the bottom left side of the screen.

Adding (more) States — right-turn

The next step in the process is to add more states to allow the robot to follow different commands. Let’s begin by making a right-turn patch which will turn the left motor on and the right motor off. The goal is to make the robot turn to the right roughly 90 degrees.

Begin by creating a new patch by selecting “File →New Patch” or (Ctrl+N). Name this patch right-turn. For this particular patch I recommend copying the structure from the forward patch and pasting it as a starting point. However, there is one key difference: we need to be able to send different numbers to each motor. For that we will add a second xod/core/gate-number to allow us to control each motor separately. While it is not necessary, as you can change the numbers bound to the gate in the inspector, I like to add a few xod/core/constant-number nodes above each gate and the time as an additional visual of what is happening. Make sure the left motor’s (M1) gate is set to 1 and the right motor’s gate (M2) is set to 0. In addition change the value of the delay to 0.4.

Note: You may have to test the value of the time for the delay to find what works best for your robot. For example if 0.4 is turning your robot past 90 degrees you may have to try a lower time.

Adding (more) States — left-turn

Now that we’ve implemented a right-turn patch let’s create a similar patch to allow the robot to turn left. For this we will be able to use the same structure with one key change; we want the right motor to be on and the left motor to be off.

Again create a new patch and name it left-turn. Copy the structure from the right-turn patch and make sure to change a few key items. For this patch the left motor’s (M1) gate should be set to 0 and the right motor’s (M2) should be set to 1. Leave the delay as 0.4.

Linking states together

Now we need to look at how to structure our main patch in order to accomplish the goal of having the robot move in a figure eight pattern. This involves having the robot follow a series of commands using the patches that we’ve already created.

Robot will start at bottom right. Right turns are black. Left turns are red.

As shown in the diagram above the robot will need follow the following steps to create our pattern.

1. Move forward -> Turn Right
2. Move forward -> Turn Left
3. Move forward -> Turn Left
4. Move forward -> Turn Left
5. Move forward -> Turn Left
6. Move forward -> Turn Right
7. Move forward -> Turn Right
8. Move forward -> Turn Right
9. Stop

Let’s create a main patch to show that structure.

Remember just like in the previous example everything begins on boot. From there we have a xod/core/defer-pulse node that allows the motors to initialize. After that the program follows a sequence of steps mapped out above using the turn-right, turn-left and forward patches that we created. At the end of the process we want to make sure we tell the robot to stop using the stop patch that we created.

You can now upload this project to the Arduino and see your robot move in a figure eight pattern!

Creating a loop

The last part of this project is to create a cycle so that after the robot has finished it begins the process over again. When working in XOD everything that we have done so far has been top to bottom in that we begin with some input at the top of our screen and end with some output on the bottom. Often in programming it is useful to send our program back to its initial state to repeat the process again. This is where a feedback loop comes in handy. Within XOD the defer structure allows feedback loops to be created. In our project after stop has finished let’s restart the figure eight process.

Within main connect the output of stop to xod/core/defer-pulse to initiate the feedback loop. There’s only one problem; XOD will not allow us to connect the output of our feedback defer-pulse to the input of a node that already has a link. Try connecting the link circled in red and you will get the error “Input can only have one link!”. We can solve this problem and fix the error using the xod/core/any node.

Notice by adding the any node we can now have multiple inputs and it will output a pulse from either of the inputs.

Upload the code to the Arduino and watch the robot move in a figure eight, wait two seconds, and then repeat the process again and again!