FPGA Robot

From Stephen Pfetsch
Jump to: navigation, search

This project is an autonomous robot that executes programs written in a custom microcode. The robot is intended to act as a LOGO turtle that uses a pen to draw patterns on paper as it drives around. The heart of the robot is the Digilent Basys FPGA prototype board with a Spartan-3E 100k-gate FPGA. The Basys board is interfaced to two stepper motors that move its wheels and a solenoid that controls its pen. Microcode programs that control the robot currently reside within a lookup table in the FPGA. In the future, a flash memory device such as an SD card will hold the LOGO programs.

The robot design is subdivided into modularized high- and low-level units of the system as a whole. Below is a block-diagram of the system, depicting the layered organization. At the top level, there is an execution loop that continuously fetches an instruction, dispatches it to the Execution module, advances the program counter (PC), and continues the cycle until the END instruction is executed. The layer below contains the instruction logic unit, which embodies the bulk of the microcode implementation, and the register file, which consists of seven 8-bit general purpose registers as well as a special read-only register that contains a byte corresponding to the positions of the eight slider-switches on the Basys board. The lowest layer residing within the FPGA is the interface layer, which contains modules that abstract the functionality of the hardware components. Finally, the bottom layer of the system is composed of the actual hardware peripherals: the flash memory card, the solenoid that pushes a pen against the paper, left and right stepper motors, LEDs (eight discrete LEDs and a four-digit seven-segment display), and a bank of slider switches.


Block Diagram of the FPGA LOGO Robot


Labeled photo of the robot's electronics


The behavior of the robot is entirely driven by the microcode programs it executes. The instruction set consists of 13 different operations, each 16 bits wide. The first four bits of each instruction contain its the OpCode and the remaining 12 bits containing data that is interpreted differently by each instruction.

F x – Forward x
15             0
Move forward x units, where x is a literal if R==0 or x is the address of a register if R==1.
B x – Backward x
15             0
Move backward x units, where x is a literal if R==0 or x is the address of a register if R==1.
R x – Rotate Right x
15             0
Rotate right x units, where x is a literal if R==0 or x is the address of a register if R==1.
L x – Rotate Left x
15             0
Rotate left x units, where x is a literal if R==0 or x is the address of a register if R==1.
PU – Pen Up
15             0
Move pen solenoid to “up” position.
PD – Pen Down
15             0
Move pen solenoid to “down” position.
SET x y – Assign Rx = y
15             0
Store y in register x, where y is a literal if R==0 or y is the address of a register if R==1.
INC x – Increment Rx
15             0
Increment the value stored in register x. Clamp result at 255.
DEC x – Decrement Rx
15             0
Decrement the value stored in register x. Clamp result at 0.
J k – Jump to label k
15             0
Set PC to j.
JZ x k – If x=0, Jump to label k
15             0
If value stored at register x==0, set PC to j.
JNZ x k – If x!=0, Jump to label k
15             0
If value stored at register x<>0, set PC to j.
END – Halt execution
15             0
Halt execution of microcode and stop robot.

Instructions F, B, R, L, PU, and PD are designed to modify the state of the hardware outside the FPGA, which enables programs to move the robot around its environment and make pen marks on the ground. Instructions SET, INC, and DEC, directly manipulate the values stored in the internal register file. These instructions are crucial in enabling the robot to perform simple computations. Instructions J, JZ, and JNZ support more complex program behavior such as conditional code and looping. The combination of register memory and conditional branching unlocks the potential for programs of modest length to perform interesting tasks.

Example Program

Microcode with Mnemonics Assembled Code
   SET    r0, 1    ; initial box size
   SET    r1, r7   ; number of rings
   FW     r0
   R      90
   FW     r0
   R      90
   FW     r0
   R      90
   FW     r0
   FW     1        ; Move to SW corner
   L      90
   FW     1
   R      180      ; Face North
   INC    r0
   INC    r0       ; boxsize += 2
   DEC    r1
   JNZ    r1, drawbox
00: 0110000000000001
01: 0110001111100000

02: 0101000000000000
03: 0000100000000000
04: 0010000001011010
05: 0000100000000000
06: 0010000001011010
07: 0000100000000000
08: 0010000001011010
09: 0000100000000000
10: 0100000000000000

11: 0000000000000001
12: 0011000001011010
13: 0000000000000001
14: 0010000010110100

15: 0111000000000000
16: 0111000000000000

17: 1000000000000001
18: 1011001000000010

19: 1100000000000000
Three concentric boxes drawn when the value "3" is encoded into the FPGA's DIP switches.

Youtube Clip

  • FPGA-based robot: early demo