A Request ---> Software Derived Sinewave Generation Critique...

Go To Last Post
38 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Based on a discussion about poly-phase signal generation in one of the ARV forums a week or so ago, I am looking at implementing a software derived single phase sine-wave generator, using two hardware timers and any necessary software schemes at my disposal.  Once this goal is worked out, the plan is to move on to creating a 3-phase sine wave reference sub-system.
 

The following represents about a week of writing and rewriting a theoretical thesis on for a 3-phase system.  As the process of that thought experiment progressed, I realized there was much I did not know about creating a simply single phase sine wave generation.  As such, I have taken a step back and wrote the following detailed theoretical analysis required to create a low distortion single phase sine wave using the hardware/software resources of the AVR.

 

I really don't think there is much in the way of actually creating the software for this endeavor.  The real timer consuming part will be creating the 256 sine wave data table.

 

At any rate, I figured I would offer up my work on the forum, in the hopes that others with more experience in this area would have some meaningful input and direction.

 

GOAL:
My initial concept is to use two hardware timers.  One hardware timer is to be used to generate a 20.000KHz PWM carrier 
frequency.  The duty cycle of the 20.000KHz PWM carrier would then be modulated by a data table that builds a complete sine 
wave.  The frequency of this PWM derived sine wave will be selectable between 50Hz or 60Hz and, if possible, incorporate the 
ability to be continuously variable from 0Hz to 400Hz, with the mode of operation to be determined at system initialization.

APPROACH:
The first hardware timer, the Alpha timer, will be used to generate the PWM sin-wave being modulated onto the 
20.000KHz PWM carrier, using 8-bit CTC PWM mode and generate no output.  

The second hardware timer, the Beta timer, will be used to actually generate the 20.000KHz PWM being modulated Alpha timer 
by the incremental addressing of the sine wave data table.

The PWM hardware I/O output pin will be directly controlled by the Beta timer in 8-bit CTC PWM mode.  The final result 
should be a sine-wave output that, with proper filtering, will produce a low distortion sine-wave. 


RESOURCES & PARAMETERS:
The following resources and parameters are what I believe will be the absolute basic requirements for the implementation of 
a digitally derived sine-wave generator.

uC = ATMega328P.

Fosc - 18.432MHz.

Modulation frequency = 50Hz/60Hz - eventually, continuously variable.

Carrier frequency = 20.000KHz.

One 8-bit Alpha hardware modulation timer = 60 x 256 = 15.360 interrupts per second.  

One 8-bit Beta hardware PWM timer = 20.000 interrupts per second ---> 20.000KHz PWM.

Private: One 256 element array, used for the sine-wave data table, holding the 256 pre-calculated sine-wave PWM modulation data 
points that will be accessed via the Beta timer interrupt handler.

Private: One data table address counter - the source of the address used to access the sine wave data table 

Global: One Dwell-time register - Dwell time interrupt handler simply increments the Dwell-time register by 1 with every entry.  

Upon entry, the Beta Dwell time interrupt handler will take the current data table address counter and copy it into the 
Dwell-time register.  This address is used by the Beta timer interrupt handler to access the 256 element sine wave data table, 
the value of which, gets stored it in the Beta timer OCR register. Control of the PWM I/O pin is automatically 
turned on and off by the Beta timer.

I'm thinking that, the interrupt frequency relationship between the Alpha hardware timer interrupt event rate needing to be 
slower than the Beta hardware timer interrupt event rate is pretty important.  I think this relationship is important because, 
at least one Beta PWM timer interrupt event MUST be performed (or at least started) for every Alpha timer interrupt event - 
more beta events per Alpha event would be preferable.  This is necessary to ensure that the Beta counter never misses a 
sine wave data point.  As defined above, the ratio between the Alpha & Beta timers, is: 20,000 / 15,360 = 1.3.


SCOPE:
As stated above, the data used to generate the single phase sine-wave will be pre-calculated and held in a 256 element sine wave 
data table.  And, the data held in the Sine wave data table constitutes one complete sine wave cycle.  That is, a complete sine 
wave will be generated using all 256 elements or data points being held in the sine wave data table.

The PWM frequency of the Beta phase timer will be set to generate a 20.0KHz PWM output and needs to be established using a 
hardware timer with the highest interrupt priority.  The sine wave modulator, the Alpha timer, will generate an interrupt at 
a rate of 256 interrupts per single 50Hz or 60Hz sine-wave cycle and will be the lower of the two timer interrupt priorities.

Using the pre-calculated sine-wave data in the 256 element sine-wave data table, the Alpha and Beta counters will work in 
concert to modulate the duty cycle of the 20.000KHz frequency being generated by the Beta timer at the respective PWM I/O pin.

While the Beta timer has a higher interrupt priority over the Alpha timer, the movement of sine-wave data between the Alpha 
timer interrupt handler and the Beta timer interrupt handler is directly dependent on the incrementing of the 256 element data 
table address counter within the Alpha timer interrupt handler.

On entry, the Alpha timer interrupt handler moves the current value of data table address counter and copies it to the 
Dwell-time register.  The data table address counter is then incremented by 1, pointing to the address to be used in the 
next Alpha timer interrupt event.

The Beta timer interrupt handler uses the current data table address being held in the Dwell-time register to access the 
current  sine-wave data point and store it in the Beta timer respective OCR register.  This process is repeated until the 
entire 50Hz/60 Hz wave form is constructed.

What I am not sure of at this time is if there is a need to for some form of synchronization or hardware/software handshaking 
between the Alpha & Beta timer interrupt handlers, for smooth, reliable sine-wave data point transfer to take place.  Or, is 
the creation of a single phase sine-wave really that simple that data movement between the Alpha & Beta interrupt handlers 
be done asynchronously.
 

WHAT TIME IS IT, EXACTLY?
With the Fosc, PWM modulation (Alpha timer) and PWM carrier (Beta timer) frequencies defined, time and instruction budget 
calculations seem in order.  I.E. the  interrupt processing times and the number of instruction cycles for the Alpha & Beta 
interrupt.

Starting off, the Instruction Cycle Time is: ICT = 1 /  Fosc = 1 / 18.432000 = 54.25347222nS - non terminating.

If the waveform we are trying to develop is say, 60Hz (the more time constrained between 50Hz & 60Hz) and, if it takes 256 
steps to build one complete waveform then: the allotted Alpha Interrupt Cycle Time is:  1 / (60 * 256) = 65.1uS.

Similarly, the allotted Beta Interrupt Cycle Time is: 1 / (20.000KHz) = 50.0uS.

The number of single CLK cycle instructions that can be executed per Alpha interrupt event is: n = 65.1uS / 54.25nS = 1,200 
single cycle instructions per Alpha interrupt.

The number of single CLK cycle instructions that can be executed per Beta interrupt event is: n = 50.0uS / 54.253nS = 921.6 
single cycle instructions per Beta interrupt.

The interrupt time/instruction cycle budgets calculated above should be enough time to build a low distortion sine-wave using 
the ATMega328P microcontroller, running with an Fosc of 18.432MHz

If the above analysis of correct, I'm thinking that a 0Hz to 400Hz option isn't possible.

 

Thoughts and suggestions are most welcome !

 

Thanks !!!

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Carl,

You are already in the how-to phase. What is the goal ? Generate 3 phase powersource ?

 

Nard

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Plons wrote:

Carl,

You are already in the how-to phase. What is the goal ? Generate 3 phase powersource ?

 

Nard

Yes, the end goal is a poly-phase system, consisting of 3 phases.  But I seem to be in a mental block as to how, even a single phase sine wave would be created.  That, and I'm trying to follow my own advice ---> working thru the process in bite-sized steps, until I get a handle on what I'm doing.

 

My reason for creating this thread, is to hear from others, as to whether or not I've got the concept correct.  If my concept of the process is incorrect, where am I missing the point.  And, if I've got the concept mostly correct, where can I make improvements, while I'm still in the concept stage?

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Sun. Jan 4, 2015 - 09:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think DDS is a solution. One timer, one interrupt source and variable frequency.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A week ago I needed a tone-generator. Jesper's DDS concept, later adapted by Leon Heller for GCC, uses a R-2R network on an 8 bit port. I didn't have the luxuary of 8 free pins so I used fast PWM on one timer where OCRx got updated by the DDS software. A low pass filter did the rest.

 

I'll have to dig out Jesper's code .... 

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One other thing...

 

One of the great failings of the embedded hobby is that, we tend to get an idea, run a few experiments, with the result becoming a work that is a Kluge of cuts, pastes and other fixations.  In the end, we don't fully understand the processes under the hood (bonnet, to the blokes) of what we've created.  and, as we see in so many student coding, the documentation both usually sucks and never gets done.

 

By writing up an outline of what my project is, the goals, scope and implementation, I find that, while it seems to take excessively long to get from concept to implementation, when the implementation is complete, the project comes up with far less problems, the coding is more efficient and reliable and, there is near full understanding as to how things work.  That, and there is real documentation available for those who come behind us who have to work on the monsters we create.

 

In fact, I followed this practice, back when I worked at Schwan's Foods.  Without fail, every project came up with far less debugging and, in fact, several projects required not debugging at all.

 

Typically, my concept of good documentation is:

 

A  written description of the concept.

 

Hardware selection.

 

Flow charts, or Pseudo code - your preference.

 

Creation of any hardware drivers.

 

Close cooperation between hardware and software design & development .  I.E... is a process best to be done in hardware or software or a combination of both, basted on cost, software and hardware constraints.

 

Creation of the final technical description.

 

Creation of user documentation.

 

I'm thinking that, right now, I'm still brain-storming... 

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, .... and I MUST ask this: what happened to your face in the avatar ?

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Geesh, I've been using that avatar for nearly as long as I've been a member of AVRFreaks.net and... you just now ask?sad

 

If you remember way back when, there was a discussion about some pictures I posted of my playroom.  I don't remember all of the specific details but, at some point, SmileMicros posted a picture of me with a plume of fire coming out of my a$$.frown  In fact, that picture is still hanging on the wall in my playroom!

 

I'm supposing that, I got hit with blow-back and the flames did a number on my face.  laughlaughlaugh

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

quote:

GOAL:
My initial concept is to use two hardware timers.  One hardware timer is to be used to generate a 20.000KHz PWM carrier 
frequency.

You are not describing the goal there, but how to do it. Therefor my question.

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

About the avatar: it has not always been blurred like it is now, I remember a Carl much clearer than that.

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Plons wrote:

About the avatar: it has not always been blurred like it is now, I remember a Carl much clearer than that.

Is it the clarity of the picture that got lost, or our memory of what is was in the past?

 

OK, is this avatar any better ?

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Sun. Jan 4, 2015 - 10:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, is this avatar any better ?

Defenitely !

 

On topic: the DDS concept with the use of three OCRx's provides you a high frequency PWM which can be used to drive muscle-fets which drive a ferrite transformer. An AT90PWM3 is IMO a more suitable AVR for this purpose. I bet Atmel has an AN for it ....

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Plons wrote:

OK, is this avatar any better ?

Defenitely !

Yeah, but with the new avatar, now you know how ugly I really am, rather than just assuming the old avatar was a bad picture.

 

Plons wrote:

On topic: the DDS concept with the use of three OCRx's provides you a high frequency PWM which can be used to drive muscle-fets which drive a ferrite transformer

I was looking at that, as well.  The problem I had, was thinking I needed a fourth timer to set the timing for the modulation of the 50Hz/60Hz onto the 20.000KHz PWM carrier.

 

Plons wrote:

An AT90PWM3 is IMO a more suitable AVR for this purpose. I bet Atmel has an AN for it ....

I haven't really looked into the AT90PWM versions of the AVR.  I'll have to give it a look-see.

 

Thanks, Plons.

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Carl I like this thread

 

I think I can give some relevant input but I am confused by what you want

 

First confusion is you mention 50-60Hz but also 400 Hz as well, I think it would be best to do the 50-60Hz stuff first and see if you can get 400Hz later I don't think it will be an issue

 

The biggest confusion is the hardware, what are you planning on doing with the pulse train you develop? will you be driving an inverter through gate drive circuitry or are you just after low signal stuff?

 

When you mention wanting to generate three phase then I automatically think its a motor drive system which is right up my street, I am keen to discuss these matters and learn things myself

 

To generate a simple PWM sine wave using a uC can be achieved in a few ways and the classic way is to have a reference waveform (in a lookup table or even calculated on the fly!) and then to vary the duty cycle the same way the reference magnitude varies and voila, the PWM has the same time average integral as the reference

 

So think about this, we have a sample time ticking away (50us for 20kHz) and the pulse width might have a maximum of say 255 which is an output of 5VDC, a pulse width of 0 is 0VDC and 128 is 2.5VDC

 

So the peak of the sine wave is represented with max output (duty cycle of 255) and the minimum is represented with 0 dutycyle

 

Because the switching frequency is decently high compared to the fundamental then it doesn't take much to get rid of all the high frequency harmonics and just leave the fundamental and if you measure the pulses you generated through an RC filter you will see an AC sinewave with a peak of 2.5VDC

 

Now you might be thinking wtf is he on about here but its an important point to think about in terms of how much output you get and its the reason I ask what you want to do with the pulses

 

Because (without overmodulation) the AC output you get can only ever have a maximum peak value of 0.5 VDC this isn't a very good system for a motor drive as it will never be able to run at full speed, its actually dreadful because a peak of 0.5VDC results in a maximum AC RMS (the number we are interested in) of about 0.35VDC per phase hence the maximum line voltage output from an inverter with standard sine modulation is about 60% of the DC bus voltage

 

And this is when some smart arse noticed adding a third harmonic to the reference waveform could increase the maximum output voltage

 

So its something to think about in terms of hardware and practical issues

 

To generate three phase outputs Space vector modulation is the ultimate modulation strategy, and its probably the best one to work with using a uC and if you are interested I think I can explain it simply

 

I have done a bit using a 328 and the 8MHz onboard osc, I had little trouble generating a fixed frequency output but I never got the SVM stuff running right, I am just not a good enough programmer and I got lost in bit shifts =) ,its been a while since I last visited it let me come back

Last Edited: Sun. Jan 4, 2015 - 11:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Microcarl..'

 

You got rid of face fuzz..!!!????

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey, Bignoob...

 

While my goals seem clear (but less so, with every minute that passes) to me, I suppose I can understand why they seem unclear to others.  Clarity is all about the entry point of the problem, definition, solution process.  My ability to convey ideas and concepts, it seems, will always have need for further refinement.

 

As I stated in my original post, there was a discussion a week or so ago where that OP wanted to generate 3-phase using software only.  This concept held a certain intrigue with me.

 

Over the time span, from then until now, I had been processing that discussion and decided to look into it a bit deeper.

 

Originally, I had no real purpose as an end goal for developing a poly-phase system - it was strictly about learning something new.  And so, I began jotting down some thoughts.  As PWM verses sine wave outputs, that is really inconsequential at this point because the one, is derived by the other.  It's just that looking at a pretty sine wave is easier to do than deciphering the PWM representation of a sine wave.  So then, as a poof of concept, I had originally thought I would just filter the output of the modulated PWM and use the visual observation of a good quality sine wave on my scope as proof of concept.

 

But then, as I progressed on into this endeavor, I got to thinking that, I could possibly use this work to power some small RC type 3-phase motors and use them as replacements for the stepper motors on my table-top mill.  I actually developed an PWM axis drive and PID control, using a brushed DC motor that used quadrature encoder feedback and, in fact, it had great performance.  Sadly, the motors are simply too big and, I didn't like the idea of finding replacement brushed as, they are about 15 years old.

 

The idea of using 3-phase motors to drive my table-top mill does have a high degree of attraction for me.

 

Then, as I said early on, I started out addressing the 3-phase option first, and doing it with software only.  But it quickly became apparent to me that a software only approach to 3-phase production wasn't viable.  That, and I found myself unsure as to how to synchronize the 50Hz/60Hz modulation onto the 20.000KHz PWM carrier.  My concern is that sine wave data points might be missed, if the 50Hz/60Hz and 20.000KHz interrupt handlers weren't properly synchronized.  Hence, the reason for my taking a step back to getting a good understanding of a single phase modulated PWM system.  And, again, once I gain enough understanding of how to generate a low distortion sine wave, I'll be better prepared for the task of developing a 3-phase system and the issues of properly synchronizing the phases.

 

As for the 400Hz option, I was thinking that, once the task of developing 3-phase had been worked thru, the transition to a full blown variable Frequency Drive isn't really a huge leap.  But it's not really important that this whole thing even gets to that point.

 

So, I guess what I want to know is:

1. Do the concepts that I describe to generate a PWM modulated sine wave seem viable?

2. Are the interrupt priorities correct?

3. Is my reasoning behind the math I presented plausible?

4. Does there, or does there not need to be some synchronization between the modulation interrupt and the PWM carrier interrupt?

 

As for the suggestion to use and AT90PWM class controller...  Right now I just want to understand what it takes to generates a single phase sine wave.  Maybe I've got the cart before the horse here and, it may well be less of a brain-strain to use a device fashioned to the purpose of PWM control.  With that said, if after looking into the AT90PWM class controller proves to be more straight forward than the brain-strain I'm currently experiencing, I'll definitely be using an AT()PWM flavor of the AVR.  It does seem to be the logical next step.

 

Thanks!

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Mon. Jan 5, 2015 - 12:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ignoramus wrote:

Microcarl..'

 

You got rid of face fuzz..!!!????

The wife told me I had to shave... or else!

 

So I shaved...

 

You just can't mess with a Marine and have any reasonable expectation of winning the contest.

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Mon. Jan 5, 2015 - 12:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

A simple DDS:

volatile uint16_t finc = 164; //164 = 50Hz @20kHz,197 =60Hz,1310 = 400Hz
uint8_t sin_tab[256] = {......}
timer_isr()
{
    static uint16_t phase_acc;
    phase_acc+= finc;
    OCR =  sin_tab[phase_acc>>8];
}

If you want to control a motor with variable frequency, then you need to vary the amplitude. This adds a multiply into the equation. The AVR will be working reasonably hard. For multiple phases, you offset the index to the sin table or the phase accumulator.

 

Bignoob could probably tell us what is considered 'low distortion' for motor use? A couple of % THD? I dare say the motor doesn't care too much. There was some discussion of the harmonics recently.

Last Edited: Mon. Jan 5, 2015 - 03:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Carl,

 

I think THIS is the Thread that started your thinking on this topic, just as a reference.

 

You have more experience than I in uC's, but I wondered if you are simply working at this too hard.

If one uses a Timer/Counter to generate the PWM output, (@ 20 kHz PWM frequency, or whatever), then just use the other Timer/Counter to feed the PWM register.

I don't think you need to synchronize them in any way.

If the sample update rate is an exact integer multiple of the 50 / 60 Hz, then this T/C exactly defines the output frequency.

Where within the 20kHz period the next sample falls won't matter for the noise levels we are dealing with.

 

I think for many real world purposes the PWM output has to be passed through a LPF.

Even a crude sin wave, (few samples per period), looks very good after passing through a (multi-stage) LPF to remove the higher frequency components of the stair stepped PWM output.

Look back at that original Thread, the first O'scope photo is of a simple 4 step sin wave, (far cruder than your proposed 256 step sin wave), and the output looks great after LPF'ing.

 

Also, I know this started as a thought project.  However, if you elect to pursue it for your CNC motors, then make life a bit easier if you want and look at either the AT90PWM uC (which I've not used), or at the XmegaE5 which has a built in DAC.  It is essentially the "Tiny" equivalent within the Xmega series of micros.

 

A R2R uses up lots of pins and PWM uses up a T/C and doesn't go rail-to-rail.

Using a DAC means the project uses only one T/C and the output goes essentially rail-to-rail.

The XmegaE5 also gives you a priority interrupt controller, although for a single phase sin generation that isn't important.

It also gives you a  32 MHz clock, so even more instructions / update period, so you can easily increase the number of samples per sin period, if so desired, and also have time to do the symmetry calculations in case you decide to store only 1/2 or 1/4 of a sin period in your lookup table.  (And have more instruction cycles available for the remainder of the project.)

 

Like you, that Thread made me sit down and actually give it a try, (albeit with an old uC, not the E5 I'm suggesting for a "real" project!).

 

JC

 

Edit:

Nice new Avitar!

 

 

 

 

Last Edited: Mon. Jan 5, 2015 - 03:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, Doc.

 

You are always the gentleman.

 

I'm probably going to let this rest for a day or two as, it's been an every waking moment type of thing for the past week.

 

The AT90PWM or the XMega are definitely options to look at.

 

Have a great week!

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Carl,

 

Kicking around the numbers for a direct synthesis method using a pulse width modulation generation as in Doc's suggestion above:

 

With PWM, the precision of the output is given by the number of time-slots available. 256 slots gives 256 steps, so 48dB precision - which is probably enough to give a decent sine wave, given the filter. You also need to consider the sample rate of the baseband signal itself - Mr Nyquist tells us that it needs to be somewhat above twice the base frequency - in this case we need 60Hz, so say, 125 samples per second.

 

Generating a PWM signal is a doddle on the AVR: every time you get a sample interrupt, you set the output and preset the timer with the value of the next sample; let the PWM generator clear the output when it gets to the top value - or the other way up, if you prefer.

 

So you need a minimum clock feed to the base frequency of 256 * 125 = 32kHz - with a couple of MHz to play with you can increase the sample rate significantly and simplify the output filter. And you'll have plenty of processor time to do all else that you need. An 8MHz clock makes the numbers easy: the sample rate becomes 31250 samples per second and ten octaves higher than your filter corner.

 

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman opened my eyes with a bit of magic.  From a recent thread:

https://www.avrfreaks.net/comment/1411406#comment-1411406

 

I have no immediate need to apply this technique, but it is intriguing.  I do not pretend to grok the math ;)

 

More:

https://www.google.ca/search?q=%22magic+sine+waves%22

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

8 bits is 0-255, but the (max signal/min signal)  is 127/1 not 255/1, so 7bits*6dBperbit or 42 dB. 'I have heard'  one needs <5% THD, and then I heard that was 7 bits. 5% is 1/20, which is 40db down, so it sounds about right.

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm still not exactly clear what you want to input, but if you're looking to low-pass the PWM values to create a sine wave (and maybe even if not), I would try dithering.

You know the PWM frequency and the sine wave frequency and thus the sine lookup table sampling frequency.

You can also probably find a way to calculate some "exact" or smooth sine value (or deltas) for that exact point in time.

You could then keep count of the cumulative error and flicker the duty cycle length between sine[n] and sine[n+1] based on which the sine[n] + cumulative_error is closer to.

For PWM freqency >> sine frequency you can probably get fairly smooth sine waves after the low-pass filter. For higher frequencies it hardly matters anyway.

 

The timer setup you initially suggested seem overly complicated, though, assuming I understood it correctly.

The thing is, you don't even necessarily need interrupts if you wait in a busy loop.

Regardless, keeping a tab on the instruction count gives you plenty of cycles for just about any calculation.

 


arbrnt

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bignoob could probably tell us what is considered 'low distortion' for motor use? A couple of % THD

Kartman I don't think anyone here (me included) realised how good a question this little beauty was

 

When I read it yesterday I simply did not know the answer so I started reading my old favourite again and it soon became clear just how bad THD is as an indicator for the quality of a modulation scheme

 

Now it doesnt help that there are different definitions of THD but hey.....

 

 

A PWM output waveform can can be thought of as comprising a signal of fundamental frequency (50 Hz for this discussion) plus a series of harmonics that are generated as a by product and all together it makes up the true RMS

 

THD is the RMS ratio of the harmonics  to the RMS of thefundamental i.e how much of a % are the harmonics of the fundamental!!!

 

It all gets so hairy!

 

THD can be calculated by taking the RMS ratio of all the harmonics in a wave form divided by the fundamental we are interested in

 

So we can cut the crap out here and just think of a square wave of 50 Hz, peak +1VDC, min -1VDC

 

We can do a bit of fourier analysis and say its an odd function so it can be thought of as  a sum of sin waves, with odd multiples only

 

After a bit of integration we can find that the harmonics coefficients are given by the beautiful 4/(n*Pi)

 

So the fundamental signal

 

4/Pi sin(100*Pi*t)

 

4/pi =1.27

 

So the fundamental signal for a 1V 50Hz square has a peak amplitude of 1.27V (0.9V RMS)

 

We know that the negative portions square to the same as positive value so the true RMS of the square is 1V

 

 RMS^2 = Fund^2 +Harmonics^2

 

Harmonics^2 = RMS^2 - Fund^2

 

 

 

THD =sqrt(Harmonics^2/Fund^2)

 

THD =sqrt( (RMS^2 - Fund^2)/Fund^2)

 

 THD=0.4834 = 48.43%

 

So we have determined from a 1V square we get a 0.9V sine and the rest is all harmonics which collectively contain about half as much energy as the fundamental signal

 

Now thats all that we can tell from this, the standard THD does nothing to tell us where these components lie

 

Now if you look at different modulation schemes you will see that they all have about the same value of fundamental around 90% so if you think about this then if the fundamental is always about the same then of course the rest is harmonics and it is going to be about the same so the ratio and hence the THD will be about the same for all the different modulation schemes

 

The thing is this THD is the voltage THD and as we know the higher the frequency the harmonic the more an inductance will impede so the higher frequency harmonics will lead to much less current distortion which at the end of the day is what matters

 

We could have the same THD but because the harmonics are shifted higher then the resulting current distortion is much different so some smart alec came along and realised its much better to weight the harmonics so that the low order ones count for more and its quite complicated there seems to be a few ways and its not something I have ever done any way

 

I did a little simulation today and after a phone call and some thought I have actually learnt a stack of stuff and other things have fallen into place

 

heres the breakdown of harmonics for different schemes

 

 

And notice that they have a weighted THD which basically replaces the n in

 

4/(n*Pi)

 

With n^2

 

hence the higher order harmonics add less

 

Heres a summary of different WTHD

 

 

 

Last Edited: Tue. Jan 6, 2015 - 09:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry I cant get the pics to display the correct way up!

 

I tried all kinds

 

Any way Look at this screenshot which demonstrates something that I found surprising,

 

One square wave that produces a fundamental with a magnitude of 1, and a H bridge sine modulated to produce an output fundamental the same 1V

 

 

Look how the Voltage THD of a pure square wave is actually less THD than that of a sine modulated inverter!, its crazy, but notice how when the voltage is driven into an inductor how the resulting Current THD is significantly less despite having a higher voltage distortion its a fraction of a percent where as the square wave produces 12% current distortion

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bignoob,

 

Do you have an ISBN for your reference book?

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does what I said make sense?, I learned a lot of stuff from Kartmans simple question

Do you have an ISBN for your reference book?

Its an amazing book but it is expensive (edit its actually half price on that link atm, I paid well over £100 for my copy but I reckon I would pay £200!)

 

http://www.amazon.co.uk/Pulse-Wi...

 

ISBN 978-0-471-20814-3

 

I am a jammy git because work buy me lots of things and a book like this I just order it and its mine forever!!

 

The author Thomas Lipo is an extremely well respected researcher, possibly one of the best power elctronics engineers of all time he has covered so much and his papers can be accessed for free, he is actually an approachable person and if you email him a question he will reply back, the same can not be said for other equally as famous researchers

Last Edited: Wed. Jan 7, 2015 - 09:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See how i cause trouble ya wee jammy git? Sounds like you're from the northern sector.
Joey gives a link in msg #23 regarding 'magic sines' . If you're the grumpy old elf, you don't like magic but nanny plum will always jump at the chance, so i'm wondering if there is anything actually magic about these 'magic sinewaves'? Since you've got matlab out, how hard is it to simulate these and see if there is actual magic?
For those who didn't pick up on the elf and nanny plum references:http://en.m.wikipedia.org/wiki/Ben_and_Holly%27s_Little_Kingdom

Last Edited: Wed. Jan 7, 2015 - 11:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds like you're from the northern sector.

Thats right, do you know the UK?

 

Joey gives a link in msg #23 regarding 'magic sines'

I read it at the time and I had seen it sometime ago and the breakdown all sounds great from a 10 minute look into it its not clear how to implement it

 

http://electronics.stackexchange...

 

Thats interesting, it does all feel like a bit of a scam to me I mean

 

Look here

 

http://www.tinaja.com/glib/msinp...

 

Magic sinewaves potentially represent a billion dollar opportunity. At present, the following Synergetics development services are offered to you..

 

Custom Magic Sinewave consulting services are offered at $89 per hour.

A Don Lancaster personal training seminar is offered giving you all the very latest key tools and insider secrets needed for your own Magic Sinewave design and development. The price is $3900 per day plus expenses if held in your facility. Or $2300 per day if held at the Black Range Lodge , an outstanding Southwestern New Mexico wilderness lodge and retreat. The seminar may be videotaped. Up to eight students may attend

 

A research associate program is offered. You are provided with all of the Magic Sinewave info to date, new updates and developments as they occur, and 70 hours of ongoing consulting for an annual fee of $5900

Sounds like hes blagging it to me but thats not to say he is

 

how hard is it to simulate these and see if there is actual magic

Matlab would probably be the weapon of choice and it shouldnt take no putting together, the thing is right now I don't know how, I don't fancy shelling out $2300 for a day at the ranch!

 

Does it make sense to you?

 

 

If you're the grumpy old elf, you don't like magic but nanny plum will always jump at the chance

You definitely have young kids!, the regular show is the best kids programme easy its actually funny

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's just noise shaping. If you've ever done any work with digital audio this will be old news. It never occurred to me that it would be useful in another arena. Whatever genius level engineer Mr. Lancaster may be, his nomenclature may be a bit off-putting for some, like our favourite punching bag "Mr. LCD" ;) ... but it doesn't throw me (much). I still don't grok the math ;)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Jan 7, 2015 - 05:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let's assume these magic sinewaves are just a sigma delta bitstream, i can see the advantage of less transitions but i dare say the issue is how you modulate the amplitude - recompute the bitstream?

You don't fancy a stay at rancho relaxo? Maybe if the guv put up for it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Let's assume these magic sinewaves are just a sigma delta bitstream,
That's my read.  Remember the 'oversampling wars' among manufacturers of consumer CD players back in the eighties and early nineties?  Redbook CD format is 16-bit audio, but you could buy players with 2x oversampling, which was really just linear interpolation, later 4x oversampling, 8x, and so on, some with 'better' non-linear interpolating algorithms.  Some used 18-bit, 22-bit, or wider DACs to deliver this wider interpolated audio.

 

Then came MASH and the 1-bit DAC.  Oversampling players soon disappeared from the market. 

http://en.wikipedia.org/wiki/MASH_%28modulator%29

 

Again, I don't pretend to understand the theoretical underpinnings.  Some of the external links look useful, but at first glance they are quite beyond my level of understanding.

 

Quote:
You don't fancy a stay at rancho relaxo?
LOL - the first thing that came to mind was the Mexican restaurant down the street by that name.

 

So when you posted the PDF link, you were just taking the p*ss? ;)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was serious - i had stumbled on it years ago but not really investigated it. Only since this thread had i dug a little deeper based on your comments. Seems it is a little controversial. Easy enough to generate using compare and an interrupt or dma if your chip has it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look in mega forum for the ding dong thread. I found the link to the algorithmic sin generator that is 3 lines. From music-dsp forum.

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For all things DIY Motor Control, this is my go-to reference:

 

http://scolton.blogspot.co.uk/p/motor-controllers.html

 

A nice combination of theory, fundamentals and practical experimentation.

 

 

But, if you're serious about making a nice(FOC/SVM etc) 3ph motor driver, you're gonna need something a bit faster than an 8b AVR really.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

microcarl wrote:

Yes, the end goal is a poly-phase system, consisting of 3 phases.  But I seem to be in a mental block as to how, even a single phase sine wave would be created. 

Creating a fixed Frequency, fixed modulation depth single phase should not be too tricky - the tougher stuff comes from adding variable frequency, variable modulation depth and phase feedback .

 

There are some interesting products at

https://www.fairchildsemi.com/pr...