Converting square wave pwm to rc pwm?

Havoc340:
I can define the base frequency in mach3, if that helps.

It's no help when you don't tell us what it is.

What is the lowest possible frequency?

...R

I would be more concerned about the actual signal shape. Are they just 'standard' PWM (0 to 100% duty ratio) although operating at different frequencies? If so, easiest way would be to convert that to an analogue signal and use the arduino for the PWM output in the range you need.

If not a standard PWM, but something like a servo, in which there is a valid range of values and anything not within is ignored, the best solution is to use a timer to calculate the actual period/duty cycle. I Think Timer 1 Input Capture can be used for such purpose.

As oscilloscope plot of said PWM under different ratios would be useful.

I do know it outputs standard pwm. That's my issue in regards to controlling my hobby speed control (needs servo pwm)

Here is the best info I've found.

The base frequency and the kernel speed determine the resolution of the PWM output. The resolution is the number of PWM steps you have between 0 rpm and your maximum speed.

The resolution is determined by kernel Speed/PWM base frequency. So;

For Kernel speed of say 25KHz and a PWM base frequency of say 10Hz you end up with,

25000/10 = 2500 steps. If you have a PWM base frequency of say 100Hz then you get;
25000/100 = 250 steps.

If you have a spindle maximum speed of say 10,000rpm, then the resolution will be
4 rpm per step for the 1st example, 10000/2500 = 4. for the 2nd it will be 40rpm per step.
10000/250 = 40.

The higher you set the PWM frequency, the lower the resolution you can achieve. That said, an open loop speed control would be hard pressed to use a resolution of 4 rpm.

From a practical point of view I recommend a base frequency of 50hz. I provides good response and seems to be less affected by electrical noise.

  1. The PWM output is a mark/space waveform calculated as a % of the maximum spindle speed. So if you set the max pulley speed to 1000, then the PWM output ratio is calculated as; PWM = Speed/Max Pulley Speed * 100. So if you set speed to 300rpm by S300 the PWM ratio will be
    300/1000 *100 = 30%

Havoc340:
The base frequency and the kernel speed determine the resolution of the PWM output. The resolution is the number of PWM steps you have between 0 rpm and your maximum speed.

The resolution is determined by kernel Speed/PWM base frequency. So;

For Kernel speed of say 25KHz and a PWM base frequency of say 10Hz you end up with,

25000/10 = 2500 steps. If you have a PWM base frequency of say 100Hz then you get;
25000/100 = 250 steps.

I can't make sense of this. Normally PWM is a fixed frequency with a variable mark-space ratio or duty cycle.

I don't think you would ever run PWM at a frequency as low as 10Hz. That leads me to think that 25kHz is the frequency of the PWM signal and 10 or 100 just indicates the size of the steps between different mark-space ratios.

I think it would be tough for an Arduino to measure the mark-space ratio of a 25kHz signal. Each full cycle would only last 40 microsecs if my maths is correct.

Think about my suggestion for converting the PWM into a voltage that can be read by the Arduino analogWrite().

...R

I see. So what's the PWM resolution of your input signal to start with?

I doubt a high resolution makes sense. It would be very hard to avoid jittering due to noise or even differences between the master/slave clock accuracy just using a PWM signal. So first thing first you need to find out what's the resolution your receiver and your slave support and aim for half of the lowest. For example, a standard arduino PWM can only achieve 8 bit resolution or 256 steps. Timer 1 can be set to 16 bits or 65536 steps. You should aim for half of this as the master and slave clocks will have some drift and wont be able to accurately detect each step unless the PWM frequency is much lower than the CPU clock frequency.

Regarding the PWM resolution, a high frequency does not exactly mean a lower resolution. The reason for the lower resolution is that PWM frequency is a multiple of the clock source. For example: Arduino timer 1 has 16 bit resolution (native). This means that for each PWM cycle it has to count to 65536. If we divide the 16MHz (16000000Hz) by 65536 then the maximum frequency is 244Hz. We can decrease the timer resolution. For example to 8 bits. In this case the PWM frequency is 16000000/256 = 62.5KHz!

But this is not always true. Some uC such as the ATmega32u4 used on the Leonardo and teensy 2++ have a PLL that can clock the timer at 64Mhz Rather than 16Mhz. As such the PWM frequency now increases to 976Hz at 16 bits and 250KHz at 8 bits.

Robin, I Think he's only saying the theoretical maximum resolution for a given frequency, which is not correct, for the intended purpose. The resolution is always given by the timer. For example: Timer 1 can have up to 16bit PWM resolution and timer 2 only up to 8 bits. I believe arduino defaults them all to 8 bits (please correct me if not) so for higher resolutions the registers need to be set manually.

There's also the sync issue. If the master is outputting a 12 bit PWM and the slave is expecting a 8bit PWM they will not be in sync, so sometimes a 80% duty cycle can be recognized either at 79 or 81%. This is also true if the accuracy of the clocks is different, but that's a long subject...

PS: Edited for clarity

Admittedly this has gotten a touch more technical than I expected.

The Mach3 software is pc driven, and it takes the kernal speed as its clock. I can set the kernal speed.

I can also set the base frequency of the pwm output.

The kernel is then divided by my set frequency and I get my resolution. So in the 25khz example, setting my pwm hz at 100, my pwm output will be 0 to 100hz. But I'll have 2500 steps in that range for my resolution.

From my understanding of above, I'll need to choose a kemal speed that is divisible by the arduino clock in order to sync all steps of the resolution. Also choose a pwm frequency range that the arduino clock will be able to count.

Bear with me. I'll get this. But I need to think it through.
Here is my kernal options

Havoc340:
The kernel is then divided by my set frequency and I get my resolution. So in the 25khz example, setting my pwm hz at 100, my pwm output will be 0 to 100hz. But I'll have 2500 steps in that range for my resolution.

I don't quite follow that. It seems you're generating SPWM rather than a duty cycle, to directly drive a 3 phase servo motor with a 6pack IGBT? On those things the number of steps (resolution) will depend on the modulated frequency (AC Output) and the carrier frequency (PWM) [more or less] as you describe. Is this what you're trying to decode?

Havoc340:
So in the 25khz example, setting my pwm hz at 100, my pwm output will be 0 to 100hz.

I very much doubt if that is correct.

For one thing a PWM frequency of 0 Hz is quite impossible. For another thing, as I have already siad, PWM is normally at a constant frequency.

To put things in perspective the standard Arduino PWM frequency is 490 Hz and many ESCs use 1000s of Hz.

You need to check this with a MACH3 expert.

...R

Havoc340:
Admittedly this has gotten a touch more technical than I expected.

And maybe it is this technical - and all the suggestions (especially verifying with a Mach3 expert) would be useful.

That said - have you tried playing around with PulseIn()?

https://www.arduino.cc/en/Reference/PulseIn

For instance - this chap shows a way you maybe could use to measure the PWM output from Mach3:

https://tushev.org/articles/arduino/9

Once you have a number - dump it out to serial - sit in a loop, calling the function, dumping the output to serial and monitor it.

Then set up on Mach3 a simple (?) gcode to cycle the PWM up and down - and compare it to the numbers you get; if they match, or are proportional in some manner - then start taking notes, and maybe once you get some kind of idea of what is what, you can then map that to a servo.write() or similar to command your ESC (note - you will also need to include the arming routine in your code for the ESC, of course).

I'd say "play with it" - while continuing to ask questions, etc (here and/or on other forums) - plus maybe you'll have some data to match. It's not ideal (the ideal thing would be to see the waveforms of the PWM for various settings in Mach3 - using an oscilloscope) - but it's better than nothing.

Who knows - maybe it will be a simple linear mapping (or close to it), and you can get it sorted and move on to the next phase of your project?

Good luck.

:slight_smile:

https://www.machsupport.com/forum/index.php?topic=13583.0

They seem to say your PWM frequency is Kernel Speed / PWM base frequency setting.

The base PWM setting is in Config->Ports&Pins->SpindleSetup. The PWM output gets updated at the kernel rate.

cr0sh:
That said - have you tried playing around with PulseIn()?

I have been reluctant to suggest that until we know what the actual PWM frequency is. In addition it may find it very difficult to detect a very low duty cycle.

pulseIn() is often used with R/C signals - but there the duty cycle is severely limited and the repetition frequency is very low at 50Hz.

...R

I misread, your PWM frequency should be found in the Config->Ports&Pins->SpindleSetup menu.

The resolution is Kernel/PWM frequency apparently?

Johnny010:
I misread, your PWM frequency should be found in the Config->Ports&Pins->SpindleSetup menu.

The resolution is Kernel/PWM frequency apparently?

Yes. It's there, and you're correct.

And my pwm frequency is settable by me. I can set it at what i need to. It's a blank box. So I can set my pwm frequency at 100hz or I can set it at 2000hz. Or more. There is a limit high and low, but I'm not sure exactly at this moment. I'll look it up.

My main goal of this thread was to see if it was possible before doing all of the legwork. I didn't want to commit to the programming if it was just not going to happen.

It sounds doable, so I'm going to go all in, starting tomorrow.

It should be doable, but I insist on a oscilloscope plot under different PWM ratios just to clear all doubts. Until then we're all just speculating :wink:

a link with some info that may be relavent there are a couple scope pics in there.

i Should add that I do not want to run an rc filter because I plan on using the pid loop feature in mach3 to maintain my spinndle speed. I'm not sure if the filter will be responsive enough to changes to maintain a steady motor speed.

Havoc340:
i Should add that I do not want to run an rc filter because I plan on using the pid loop feature in mach3 to maintain my spinndle speed. I'm not sure if the filter will be responsive enough to changes to maintain a steady motor speed.

A 10KHz PWM, for example, needs a very small filter, with a delay in the order of a few clock cycles. Thats way faster than a mechanical machine such as a motor will be able to respond, pid controlled or not.

I sugest you try it out before scrapping the idea as it is your best bet before going to really complicated interrupt based duty cycle calculations.

casemod:
A 10KHz PWM, for example, needs a very small filter, with a delay in the order of a few clock cycles. Thats way faster than a mechanical machine such as a motor will be able to respond, pid controlled or not.

I sugest you try it out before scrapping the idea as it is your best bet before going to really complicated interrupt based duty cycle calculations.

Solid point. I'll get to work and report back.

Havoc340,

Not sure if you got this done yet. I had the same issue, i am now running my RC outrunner brushless motor as a spindle motor. It is controlled by Mach3 pwm output with an interface that converts it to the rc servo pulse train to feed the ESC. I got my solution from Tweaky on the Machsupport forum, it is a small ATTiny85 chip.
About $5 of hardware.

Arnie