# How to calculate the required number of FPGA logic elements?

I have a Lynxmotion hexapod which uses hobby servos for each of its 18 degrees of freedom (DOF).

The basic problem with this is that I cannot get data on the joint angles. I have modified the servos so that I can measure their orientation via the signal from the pot. For each DOF I want to read in the orientation of the servo via ADC and use a PID controller to generate the appropriate PWM signal. This works simply enough with an Arduino for one servo.

Unfortunately I have not found any microcontrollers with 18 10 bit (or higher) timers capable of generating PWM and 18 ADC. As such I am exploring the idea of using an FPGA. Specifically I'm considering the Papilio Pro FPGA. In reading about it I see that it has around 500,000 logic elements. While I'm new to FPGA I'm guessing this is the most crucial metric for my needs (if I'm wrong, please feel free to correct me).

The question in my mind is, how many logic elements do I need for my application? Are there any good rules of thumb for such calculations?

• Why do you need such high-resolution feedback of the position of all joints? If you're trying to do such precise movements, the real problem here is the fact that you're using hobby-servos at all. Furthermore, most hobby servos have something approximating a PID loop in their internal controller already. Commented Jul 8, 2013 at 8:11
• If you're dead-set on hobby servos, maybe you should look into the OpenServo project. It's a open-source controller board that replaces the normal controller in a standard servo. It then provides a I2C control interface, so you get bi-directional comms, and can query it's position, tweak it's PID loop, etc... Commented Jul 8, 2013 at 8:19
• You should be able to generate those 18 PWM channels in software on a single µC, if it still has some cycles (and 18 IO pins) to spare. Commented Jul 8, 2013 at 12:45
• Looking more closely @ConnorWolf I would say 8 bit ADCs would be fine. I have looked at the OpenServo project. In fact I have a SparkFun OpenServo. Unfortunately it doesn't fit into the servos and I can't find size specs for the v3. Commented Jul 8, 2013 at 15:20

For several years, FPGA vendors haven't really advertised "equivalent logic gates", because it's a very poor metric for how much functionality can be achieved in a given FPGA.

To get a more complete picture of what's available in the Papilio, you want to look at this table from the Spartan 6 Family Overview:

According to the Sparkfun page you linked, the device on the Papilio is the XC6SLX9, the second row in the table.

In a system like you're describing, the limiting resources are likely to be the flip-flops. It's fairly straight forward, once you start thinking about your design in detail, to work out how many flip-flops are required. Given 11,000 flip-flops, you should easily be able to manage 18 counters and control logic.

Another resource to look out for is I/O. With 18 10-bit ADC's you'll probably either need to use a serial interface to connect to them, or a multiplexed parallel interface. You'll also want to make sure that the Papilio board pins out enough of the I/O pins, and that they accessible I/O's can be selected to the voltage levels you need (which may require digging in to the Papilio documentation or the datasheet for the specific S6 device you're using).

If you were designing a microcontroller or doing DSP-type functions, the block RAM or DSP slices could be the limiting resource, but I doubt you'll have problems with those in your project.

I think it's not possible or not the best way to try to figure it out by hand. I'm thinking about designs using thousands of elements written in HDL where the direct relation to gates and logic cells is not obvious. In addition, most FPGA ICs grow by doubling its size from one model to the next one, so it does not make much sense to know exactly how much silicon you need, just an order of magnitude is fine. My advice is you try with Xilinx ISE or Altera Quartus, write a first approach to your code and try to fit it into a device beginning with the smallest ones. You'll easily find the right one. You should be using between 50% (otherwise you'd better get the smaller one) and 80%. Don't try to squeeze things up to the point of using 90+ % of it, as all designs change don't they? and you'll get into trouble if you find out a bug and need some extra hw there then it does not fit into your "optimized" device. Be a bit generous, that's why exact reckoning of resources is rarely used or not at all.