uraimo / SwiftyGPIO

A Swift library for hardware projects on Linux/ARM boards with support for GPIOs/SPI/I2C/PWM/UART/1Wire.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot use two PWM channels at the same time

robreuss opened this issue · comments

Board Type

RaspberryPi3

Operating System

Ubuntu Mate 16.04

Swift Version

4.1.3, pre-built

Description

Attempting to drive a motor controller (https://www.pololu.com/product/3754) using PWM. I have one channel each for the right and left sides of my robot vehicle. When I startPWM to one channel, the other channel stops. This occurs even if the period and duty are the same with each startPWM call. Any way to overcome this behavior?

Yes, because right now the clock that generates the pwm is being killed every time a new pwm is started, see here (I was more interested in building something to easily generate patterns than in the vanilla pwm, so a few basic thing were never implemented).

If the frequency/ns parameter is kept the same (the duty cycle doesn't matter) this should not be needed, so it could be either removed, or better performed only the first time startPWM is called checking a flag.
To try this out you can simply modify the local checkout of this library (under .build/...) in your project.
Let me know if it doesn't work, but I'm somewhat confident it should :)

This was discussed with Kaiede some time ago in #73, and in the end in his improved fork he cleaned up the implementation reducing the number of moving parts and removing the need for the clock reset.

Cool. Swapped in Kaiede's version and it works fine.

I used an online calculator to convert my desired frequency from 20Khz to 50000 nanoseconds but I wanted to confirm with you that that is the correct nanosecond value for that frequency.

Yep, correct, 1Hz=1/1sec, so 1/20000Hz gives you 50 microseconds or 50000 nanoseconds.

Btw, since this could be a common annoyance for a lot of people I plan to fix this (and a few other things) and make a new release.

I'll watch for the new release! Thanks!

I was wondering if this improvement was eventually made to SwiftyGPIO. I've run into the same problem using SwiftyGPIO so I'm assuming not, but wanted to confirm before I switch over to SingleBoard.