Accuracy of Spartan 6 FPGA PWM - Page 1
Forum section: electronics / general

author: ciikucli
author_rank: Contributor
timestamp: March 02, 2013, 09:31:12 pm
content: How accurate is the FPGA in creating a PWM cycle? I was thinking about implementing a motor driver with one and seperate power circuitry, and I was curious about the accuracy. Is it a function of the accuracy of the interal clock on the FPGA? If so, what level of precision should I expect and could it be improved with an external oscillator?

author: codeboy2k
author_rank: Super Contributor
timestamp: March 03, 2013, 12:14:08 am
content: You need an external clock, always. The Spartan-6 doesn't have any internal RC clock, like some microprocessors do.What it does have is lots of clocking resources that are used to distribute and multiplex external clocks, internal clocks synthesized (from external clocks) , I/O clocks, and fabric clocks.Internally, there is PLL DFS ( digital frequency synthesis ) which can multiply and divide an external clock into almost any clock needed, and then that is buffered onto the internal global clock and I/O clock networks, which is then used by your logic. In your case, the PWM generator.so to answer your question, it will be as cycle-accurate and phase-accurate as your external clock is.Also, I hope you want to do more with that Spartan 6. It's overkill just for PWM. A small CPLD can do that.here's a guide on the Spartan 6 clock resources:www.xilinx.com/support/documentation/user_guides/ug382.pdf

author: ciikucli
author_rank: Contributor
timestamp: March 03, 2013, 12:30:50 am
content: Yeah, I'm planning on running multiple stepper motors, control circuitry, and data conversion on the thing. I'm thinking about using it as the core of a CNC machine. The new steppers are approaching 1/32 microstep resolution. I was hoping that I could replicate that with an FPGA and for cheaper parts wise. I've seen some oscillators that are within 1 to 2% error. I haven't done much of the math yet though. It's also a good sideproject to get ahead before I take some classes later.Recommend any good courses or books on Verilog/FPGA Design?

author: nctnico
author_rank: Super Contributor
timestamp: March 03, 2013, 12:55:17 am
content: One of the interesting parts of a CNC machine controller is calculating the stepping rates for movements along a line in 3D coordinates while maintaining a constant speed.

author: ciikucli
author_rank: Contributor
timestamp: March 03, 2013, 01:09:40 am
content: Yeah, an excuse to use all this vector calculus they've been making me learn. Plus there's plenty of established literature on CNC machines and open source 3d printer firmware. It won't be easy, but I like a challenge.

author: jeroen74
author_rank: Frequent Contributor
timestamp: March 03, 2013, 08:44:19 am
content: Why is this clock frequency accuracy important? A motor doesn't care whether it's being PWM driven at 20.0000KHz or 20.0001KHz. After all, it's the duty cycle that is important, the absolute frequency far less.

author: codeboy2k
author_rank: Super Contributor
timestamp: March 03, 2013, 09:41:21 am
content: Again, the Spartan 6 is probably overkill for your needs. It has a dedicated memory controller, it has 16 clock nets, 8 clock pins per side (4 differential pairs) multiple clock synthesizers on the CMT(clock management tiles), it supports several LVDS IO standards, at the same time..it can operate up to 500Mhz, maybe even some parts up to 1Ghz internally, I think. But none of this performance is needed to do PWM motor control.If you want to offload the PWM control of several motors, I'd look at a small to medium sized CPLD, controlled by a micro.The micro will have built in memory, a development environment that will have a fast turnaround cycle, a far easier debug cycle, and you can get most of your application logic running in no time flat.A small watch crystal will be accurate enough to get PWM control at 32Khz for all of your PWM modules inside the CPLDI think a CPLD + micro will be less $ cost and easier to develop (less dev cost) rather than trying to go all out with a single FPGA and a soft core micro.There are also several micros (look at Renenas) that have multiple PWM controllers built in. If you are controlling 3 motors, you can probably find one. if you are controlling 6 motors, and can only find a micro with 3 PWMs, I'd just use two micros and have them communicate.

author: AndyC_772
author_rank: Super Contributor
timestamp: March 03, 2013, 09:46:28 am
content: In terms of frequency, any ordinary off-the-shelf crystal oscillator module will give you +/- 50 parts per million. An oscillator that's within "1 or 2%" will be fine for this sort of application, though it may not give the type of nice, clean, sharp edges that an FPGA clock input pin will expect and require. A simple R-C oscillator may, depending on how it's constructed, not be suitable for use with an FPGA for reasons that have nothing to do with frequency accuracy.PWM is something you'll implement yourself in the FPGA with a counter. If you create, say, an 8 bit counter, and program an output to go high when that counter is in the range 0-63 and low otherwise, then for all practical purposes you'll get exactly 25% duty cycle on your output (unless your clock is rubbish).

author: jeroen74
author_rank: Frequent Contributor
timestamp: March 03, 2013, 10:00:49 am
content: I think you need no FPGA or CPLD at all. There are plenty of microcontrollers aimed at motor control that have the required resources, like fault input pins that set the PWM outputs to a safe state and that react in one or two clock cycles.If you want to microstep stepper motors, you're best bet is to use a dedicated stepper control chip for that. There are very nice chips out there that do everything and are SPI controlled. No need to roll your own.

author: mrflibble
author_rank: Super Contributor
timestamp: March 03, 2013, 11:02:16 am
content: Forget all about fpga's for stepper control. The development time is waaay higher than say a microcontroller implementation. There are enough MCUs out there that can give you PWM resolution that's more than adequate for microstepping. Another advantage of MCU's is that you of course pick on with some ADC's in it, so you can sample your motor current and do clever things with it. Instead of the comparator based variety you may have planned with the fpga.Quote from: jeroen74 on March 03, 2013, 10:00:49 amDo you happen to know any in the 5 - 10 Amp motor current range?

author: nctnico
author_rank: Super Contributor
timestamp: March 03, 2013, 12:23:11 pm
content: You can do that in an FPGA as well. And maybe it is better. Many years ago I went down the same road but never finished it. Back then large CPLDs where just emerging.A stepper motor actually is a permanent magnet AC motor. If you drive it with PWM the motor will have losses and you'll get a spike each time the winding is switched off. In my design I could run a 3A stepper engine at about 5000 steps/s (around 1250RPM) so those became a real problem. You can't shunt the spikes because that would mean you are shorting the winding and effectively put a brake on the motor.I never tried this but if I had to built a high speed stepper driver again I'd use an FPGA to make 4 class-D outputs (at 200kHz or so) to create a 4 phase sine wave. Basically a 4 phase motor drive. You can stop it at any angle which gives 4 DC outputs with a closely controlled ratio. The higher the PWM clock the better. An FPGA should be able to use 200MHz or more which gives a reasonable resolution at 200kHz. Another problem is that due to the back EMF you'd need to adjust the amplitude of the sine wave with the stepping frequency. This relationship is pretty linear though so you could skip measuring the motor current.

author: jeroen74
author_rank: Frequent Contributor
timestamp: March 03, 2013, 03:16:25 pm
content: Isn't that what microstepping actually does? Driving the stepper with sinusoidal signals. There several ways to modulate the current with PWM, the most usual one (I think) it to energize the coil, the let the current freehweel when it reaches the required current at that instant (basically SR flipflop, set by chopper clock; reset by the current sense comparator).Of course, anything digital any MCU can do can be done by an FPGA as well. But usually the cost is much higher (FPGA/config device/power supplies/no 5V/no Schmitt trigger inputs/no analogue), so it's only appropriate in special cases.

author: Kremmen
author_rank: Super Contributor
timestamp: March 03, 2013, 03:27:22 pm
content: Well, you can do that but that is not the way it is normally done. Usually a 2-phase stepper coils are driven by H-bridges where the freewheeling diodes are intrinsic in the MOSFETs or separate components in antiparallel with same. So there will be no spiking as such. The driving strategy is very similar to a SMPS in that the bridge current is measured and used to chop the bridge switches. The current setpoints can be used to define various stepping and microstepping strategies and the more advanced controllers change the stepping sequences on the fly to avoid electromechanical resonances inherent in steppers.As a rule, in order to aciheve reasonable step rates you need voltages substantially above those quoted in the motor plate. The nominal voltage drives a nominal DC current through the coil resistance, but that will get you nowhere in practice. There must be sufficient voltage to ramp up the current fast enough to generate useful torque during a step at speed. That means that current must be limited when stepping slowly. That is where the chopper comes into play.Class D amplifiers are essentially pwm controlled H-bridges. However your typical stepper is not necessarily a permanent magnet motor. A majority of steppers are variable reluctance machines with no magnets inside. For such motors back EMF calculations don't work the way you assume.

author: Kremmen
author_rank: Super Contributor
timestamp: March 03, 2013, 03:33:27 pm
content: Quote from: jeroen74 on March 03, 2013, 03:16:25 pmNo, microstepping as a concept is just stepwise control of the current. At the limit the steps merge into continuously variable current, but if you want max torque from a stepper, the currents won't be sinusoidal. Simple stepping alternates the two coil currents between + and - max current and microstepping just subdivides that. OTOH driving the coils sinusoidally never produces max coil currents simultaneously in both coils. At the 45 degree point and its multiples, the full step current is max in both coils, but just 0.707 in sinusoidal (sin(45 deg) = cos(45 deg) = 0.707...)edit: typos

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: March 03, 2013, 05:01:17 pm
content: Another approach to consider is using separate, small MCUs for each axis - there are many small MCUs with peripherals aimed at motor control, and by keeping them separate you avoid potential variable timing/jitter/latency issues that can occur once you start loading more tasks onto a micro.

author: nctnico
author_rank: Super Contributor
timestamp: March 03, 2013, 05:38:49 pm
content: Quote from: jeroen74 on March 03, 2013, 03:16:25 pmMicrostepping is keeping the ratio of the current between the windings so the rotor is kept between two steps. The problem with driving a stepper motor with PWM directly is that you cannot achieve high speeds and the freewheeling current needs to be absorbed somehow without 'shorting' the winding. At higher speeds it takes the freewheeling current so long to fall to zero you can't energize two windings at once but you can only energize one winding at a time. Pure sinusoidal drive eleminates all these problems but I doubt you can achieve this with a microcontroller if you want to get at 5000 steps per second. The microcontroller needs to be able to generate 4 sinusoidal PWM signals at 5kHz for several motors. Using a controller for each axis is a bad idea. For moving along a line in 2D or 3D 'space' you need a close correlation between the motor's speed and position.When I started my project (I was about 16 or so) I made two individual stepper boards out of TTL logic. I could load the number of steps, the requered speed and whether it should use automatic pull-in and pull-out. All pretty neat until I build the whole thing together. It turned out it could move along the X axis or Y axis but not along a line because the step frequencies where not related... Oops! In the end designed a new driver board using two Intel 8254 timer chips (each has 3 timers). IIRC one divided the master clock, the other timers where programmed with a divider ratio so the motors would move along a line synchronously and keep track of the number of steps.

author: jeroen74
author_rank: Frequent Contributor
timestamp: March 03, 2013, 06:08:04 pm
content: IIRC microstepping is useful (only) at lower speeds to achieve a smoother drive, less acoustical noise and less problems mechanically with resonances and wear if gears are used.We don't know if the OP regards the normal stepping control waveforms as PWM or that he means more advanced methods.

author: nctnico
author_rank: Super Contributor
timestamp: March 03, 2013, 07:08:21 pm
content: Quote from: jeroen74 on March 03, 2013, 06:08:04 pmWhen driving a stepper with pulses you'll always have resonances. What you describe as having less acoustical noise is sinusoidal drive. Actually driving a stepper with sine waves brings the best of a servo and stepper motor together.

author: jahonen
author_rank: Super Contributor
timestamp: March 03, 2013, 07:09:27 pm
content: I have been involved in hardware upgrade project (196 upgraded to ARM7) of old near-sinusoidal stepper drive in the past. It had 64 microsteps. It had two PWMs and H-bridges controlled by PID control loops (one for each coil in bipolar stepper), where reference for PID controllers came from sine/cosine table multiplied by the reference current. Although it would have made sense with current knowledge, it did not use FOC-like algorithm. That would have avoided the fact that now PID reference is constantly changing making dynamics worse instead of hiding the field rotation with FOC. I can remember that the result was really smooth rotation. Motor speed was controlled by making a DDS like system. Another interesting thing to try would be to add encoder to get true FOC.Regards,Janne

author: jeroen74
author_rank: Frequent Contributor
timestamp: March 03, 2013, 07:29:57 pm
content: Elektor long ago published a microstepping board, somewhere around 1987 IIRC. It was a big board, double eurocard or so. All that functionality can now be bought in a single little chip like the A3984

author: jahonen
author_rank: Super Contributor
timestamp: March 03, 2013, 09:04:01 pm
content: Taking advantage of true FOC with stepper motor one could achieve something like this:Regards,Janne

author: jeroen74
author_rank: Frequent Contributor
timestamp: March 03, 2013, 09:12:39 pm
content: Is that achieved without an additional encoder on the shaft? Or does FOC really need an encoder? I guess it must, otherwise you cannot achieve what's shown in the video. But I think the drive method (e.g. old-skool or sinusoidal drive achieved in whatever way) has nothing to do with this. The magic lies in the position feedback device, or better, the magic of feedback

author: nctnico
author_rank: Super Contributor
timestamp: March 03, 2013, 10:39:44 pm
content: If you look carefully you see there is an extra wire connected to something at the bottom of the right motor. So I guess there is an encoder mounted on the right motor.

author: jahonen
author_rank: Super Contributor
timestamp: March 04, 2013, 06:26:01 am
content: Yes, there is an encoder involved, as mentioned in the video comments. Field Oriented Control (FOC) needs something to infer the true position of the magnetic field (or current components Id and Iq), and the encoder is usually the easiest way to obtain that. But I guess if one is clever enough, one could build some kind of estimator to get the position without the encoder with some drawbacks. Usually for "servo-type" applications an external encoder is required.The "magic" shown in the video results directly from the fact that FOC always applies the coil currents so that the magnetic field always follows the rotor position optimally, no matter where it is or if you force the rotor externally, so from motor perspective, steps are never lost, thus the motor will not stall.Regards,Janne

author: ciikucli
author_rank: Contributor
timestamp: March 05, 2013, 10:33:37 pm
content: Woah, that was an interesting read. I had been planning a sine wave based microstepping arrangement, but know I'm definitely going to look into FOC. Anyone know some good encoders or motors with encoders for nema 17/23 stepper motors, I found some data on avago-heds-5xxx, but I'd like to hear from you.

author: Rufus
author_rank: Super Contributor
timestamp: March 05, 2013, 11:33:28 pm
content: Quote from: jahonen on March 03, 2013, 09:04:01 pmIf you are going to have the complexity of a full positional feedback servo loop why would you choose a stepper motor?

author: Hypernova
author_rank: Unknown
timestamp: March 06, 2013, 08:40:18 am
content: Quote from: Rufus on March 05, 2013, 11:33:28 pm^This, the whole point of steppers is so that you don't need encoders.

author: nctnico
author_rank: Super Contributor
timestamp: March 06, 2013, 04:32:11 pm
content: Actually you do need encoders. At least at the end and the beginning of a track or something similar. The problem with a stepper motor is that it is very tempting to skip the encoder but in the end you'll pay the price. An advantage is that you can use an encoder with lesser resolution but you can still detect you missed a step. Depending on the application it may be too late though.

author: Hypernova
author_rank: Unknown
timestamp: March 06, 2013, 04:52:19 pm
content: Quote from: nctnico on March 06, 2013, 04:32:11 pmThat's the job of indexing and limit switches, the point is with steppers you get commutation and incremental positioning for next to nothing which the the usual reason people go for them.

author: ciikucli
author_rank: Contributor
timestamp: March 06, 2013, 07:01:01 pm
content: I'm going with stepper motors because they are cheap with pretty good rotations and have a good torque. Virtually all hobbyist 3d printers use these for this reason. If I go with foc, I'll need them for control, otherwise I'll use them to ensure that the micro steps are being completed accurately. It's a combo 3d printer/light CNC machine/ multitool, so there may or may not be resistance to motion.

author: nctnico
author_rank: Super Contributor
timestamp: March 06, 2013, 10:15:42 pm
content: Quote from: Hypernova on March 06, 2013, 04:52:19 pmThat is true but if the load varies or can get stuck (like in a CNC machine) then you might be looking at missing steps. In such a system a stepper is not the best choice.

author: Hypernova
author_rank: Unknown
timestamp: March 07, 2013, 01:06:33 am
content: Quote from: nctnico on March 06, 2013, 10:15:42 pmIMO if you use budget tools expect budget results, besides by the time you use encoders there's really little advantage for steppers, you even get less wiring with PMSM (3 wires only), and with free FOC software libraries on the net it's not that hard to write code for.