PWM pin inversion

242 views
Skip to first unread message

Phill Carter

unread,
Jul 24, 2014, 9:25:18 AM7/24/14
to machi...@googlegroups.com
Is there a parameter to invert the output of a hal_pru_generic.pwmgen?

Viesturs Lācis

unread,
Jul 24, 2014, 10:06:44 AM7/24/14
to machi...@googlegroups.com


On Thursday, July 24, 2014 4:25:18 PM UTC+3, Phill Carter wrote:
Is there a parameter to invert the output of a hal_pru_generic.pwmgen?

You could use "scale" component, setp gain = -1


Viesturs

Alexander Rössler

unread,
Jul 24, 2014, 10:39:06 AM7/24/14
to Viesturs Lācis, machi...@googlegroups.com
Not sure the PWM generator works with negative values. If you want to invert the PWM output you can use the sum2 component. http://linuxcnc.org/docs/html/man/man9/sum2.9.html Just add -1 to the value you want to invert and then feed it to input of the pwmgen.


--
website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit
---
You received this message because you are subscribed to the Google Groups "Machinekit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to machinekit+...@googlegroups.com.
Visit this group at http://groups.google.com/group/machinekit.
For more options, visit https://groups.google.com/d/optout.

Viesturs Lācis

unread,
Jul 24, 2014, 11:54:52 AM7/24/14
to machi...@googlegroups.com
Well, adding -1 with sum component would not invert the output. If output initially is 0, it would become -1, initial output of +1 would become 0. 
IMHO scale component is the way to go, either with input or, if possible, with output of pwmgen. On Mesa cards pwmgen output is routed directly to gpio pin by firmware, so there is no access to pwmgen output in HAL, I am not sure about BBB, but I feel that it is the same.
BTW, since PID output can be negative, pwmgen should accept also negative input value.

Viesturs

Bas Laarhoven

unread,
Jul 24, 2014, 12:57:36 PM7/24/14
to Viesturs Lācis, machi...@googlegroups.com

The usual way to invert a (0 or 1) signal is to subtract it from 1. Maybe that's what was meant.

-- Bas

Alexander Rössler

unread,
Jul 24, 2014, 2:54:39 PM7/24/14
to Viesturs Lācis, machi...@googlegroups.com
The BBB PWM does not accept negative values that's why it is always combined with a limit component. Is there are subtraction component? If not add -1 and use a scale to invert the result.  

Alexander Rössler

unread,
Jul 24, 2014, 2:55:46 PM7/24/14
to Viesturs Lācis, machi...@googlegroups.com
Or wait maybe it does accept negative values and that's the reason it is combined with a limit component to prevent heating if the PID returns negative value.

Charles Steinkuehler

unread,
Jul 24, 2014, 4:38:56 PM7/24/14
to machi...@googlegroups.com
The PWM logic behaves the same as the hm2 PWM component, and IIRC allows
values from -1 to +1. IIRC, changing the sign of the drive signal
should invert the PWM output, but I'm currently away from home and
cannot verify.

Phill Carter

unread,
Jul 24, 2014, 6:51:02 PM7/24/14
to Charles Steinkuehler, machi...@googlegroups.com
Thanks for the suggestions. I am away until Monday evening, I will experiment after then. I have a Sherline lathe and am attempting to run it from a BBB with a Xylotex DB25-26 cape and am using the pwm for spindle control. I currently run it from a PC parallel port and want to keep the same signals and polarities so I can swap between both.

Bas de Bruijn

unread,
Jul 26, 2014, 3:34:14 AM7/26/14
to Bas Laarhoven, Viesturs Lācis, machi...@googlegroups.com
Or the 255 would have to become 0 and 0 become 255?

Phill Carter

unread,
Jul 28, 2014, 5:43:01 AM7/28/14
to machi...@googlegroups.com
I tried a scale component <http://linuxcnc.org/docs/html/man/man9/scale.9.html> with a gain of -1 on the input to the pwmgen (hal_pru_generic.pwmgen.00.out.00.value) which appeared in Hal Configuration-Watch to change the sign of the signal into the pwmgen but it seems that any negative input is treated as zero because the output didn't change with any negative number, nor was it inverted.
BTW I am using a scale of 3000 in the pwmgen to match my spindle speed and i did try a scale of -3000 there but that didn't behave any differently to 3000.

andy pugh

unread,
Jul 28, 2014, 4:46:04 PM7/28/14
to Phill Carter, machi...@googlegroups.com
On 28 July 2014 10:43, Phill Carter <phillc...@gmail.com> wrote:
> I tried a scale component
> <http://linuxcnc.org/docs/html/man/man9/scale.9.html> with a gain of -1 on
> the input to the pwmgen (hal_pru_generic.pwmgen.00.out.00.value)

I rather suspect that what you really need is a sum2 with a gain of -1
on the pwm amplitude to subtract it from the full-scale value (1, 255,
whatever you are using at that point).

Then 0 = 100% duty cycle, full-scale = 0% duty cycle.

--
atp
If you can't fix it, you don't own it.
http://www.ifixit.com/Manifesto

Phill Carter

unread,
Jul 29, 2014, 7:57:12 AM7/29/14
to andy pugh, machi...@googlegroups.com
Using a sum2 component at least gets the spindle speed going in the right direction.
But:
On startup when I press the toggle estop button the spindle runs at full speed. To stop it I need to M3S1 (or any other speed that is too low for my motor to operate)
When I do M5 the motor continues to run at the currently set speed. Using Watch, I can see that pwmgen enable changes state but I can still see pulses on the output pin on my oscilloscope.

On July 29, 2014 6:45:43 AM GMT+10:00, andy pugh <bodg...@gmail.com> wrote:
On 28 July 2014 10:43, Phill Carter <phillc...@gmail.com> wrote:
I tried a scale component
<http://linuxcnc.org/docs/html/man/man9/scale.9.html> with a gain of -1 on
the input to the pwmgen (hal_pru_generic.pwmgen.00.out.00.value)

I rather suspect that what you really need is a sum2 with a gain of -1
on the pwm amplitude to subtract it from the full-scale value (1, 255,
whatever you are using at that point).

Then 0 = 100% duty cycle, full-scale = 0% duty cycle.

-- from my Nexus7 via K-9 Mail

andy pugh

unread,
Jul 29, 2014, 8:23:44 AM7/29/14
to Phill Carter, machi...@googlegroups.com
On 29 July 2014 12:57, Phill Carter <phillc...@gmail.com> wrote:
> Using a sum2 component at least gets the spindle speed going in the right
> direction.
> But:
> On startup when I press the toggle estop button the spindle runs at full
> speed.

Can you post the spindle-related bits of HAL here?

I suspect that spindle-off turns off the PWM, which is duty-cycle
zero, which is full speed.
The solution may be to link the pwmgen enable differently, possibly to
machine.is-on

What is the PWM driving? Is there any scope for wiring so that it
doesn't need to be inverted?

Phillip Carter

unread,
Jul 29, 2014, 8:43:45 AM7/29/14
to andy pugh, machi...@googlegroups.com
HAL part is attached.
Spindle-off doesn't disable the pwm, it just continues running at the last set duty cycle.
Wiring is direct from the Xylotex DB25 to the DB25 on my BOB. I am trying to keep the wiring the same so I can run from my existing working PC or do some testing with the BBB.
--
Cheers, Phill
spindle.hal
Reply all
Reply to author
Forward
0 new messages