1

I want to generate some 4 software PWM signals on GPIO pins (5, 6, 19, and 26) on the Raspberry Pi 4B using the pigpio library (which is in C). I want to generate a PWM frequency of 8 kHz. I was able to successfully move and control the motor with a variable duty cycle, but it consumes high CPU usage even when there is no movement in the controller. Only functions like gpioSetPWMfrequency and gpioSetPWMrange are initialised when software starts executing. I used the gpiopwm function in the loop where I needed to change the duty cycle of the pin. I have 4 PWM channels to operate the BTS7960 43A High Power Motor Driver Module, which is connected to the actuator. Is there any way to minimise usage? As per the Pigpio documentation, it uses DMA access. I need help in this. Can anyone please show me directions to lower down CPU usage and any better libraries to do it? Psuedo-code of what I am doing

#include <pigpio.h>
static const int kiShieldFwdPWMIOPin = 5;//21;     //Motor-1 Outputs
static const int kiShieldBackPWMPin = 6;//22;
static const int kiShieldENIOPin = 13;//23;

static const int kiColumnFwdPWMIOPin = 19;//24;     //Motor-2 Outputs
static const int kiColumnBackPWMIOPin = 26;//25;
static const int kiColumnENIOPin = 12;//26;

void setup(){
        //intialize PWM Freq
        gpioSetPWMfrequency(kiShieldFwdPWMIOPin,8000);
        gpioSetPWMfrequency(kiShieldBackPWMIOPin,8000);
        gpioSetPWMfrequency(kiColumnFwdPWMIOPin,8000);
        gpioSetPWMfrequency(kiColumnBackPWMIOPin,8000);
       
        // Setting pwm range
        gpioSetPWMrange(kiShieldFwdPWMIOPin, 100);
        gpioSetPWMrange(kiShieldBackPWMIOPin, 100);
        gpioSetPWMrange(kiColumnFwdPWMIOPin, 100);
        gpioSetPWMrange(kiColumnBackPWMIOPin, 100);
    }
  void BerryController::move(STATE state, DIRECTION direction, const int &pwmValue){
          switch (state)
    {
    case STATE::Shield:
        switch (direction)
        {
        case DIRECTION::Open:
            gpioPWM(kiShieldBackPWMIOPin,PI_LOW);
            gpioPWM(kiShieldFwdPWMIOPin,pwmValue);
            break;
        case DIRECTION::Close:
            gpioPWM(kiShieldBackPWMIOPin,pwmValue);
            gpioPWM(kiShieldFwdPWMIOPin,PI_LOW);
            break;
        case DIRECTION::Stop:        
            gpioPWM(kiShieldFwdPWMIOPin,PI_LOW);
            gpioPWM(kiShieldBackPWMIOPin,PI_LOW);
            break;
        default:
            break;
        }
        break;
    case STATE::Column:
        switch (direction)
        {
        case DIRECTION::Open:                     
            gpioPWM(kiColumnBackPWMIOPin,PI_LOW);
            gpioPWM(kiColumnFwdPWMIOPin, pwmValue);
            break;
        case DIRECTION::Close:                   
            gpioPWM(kiColumnFwdPWMIOPin,PI_LOW);
            gpioPWM(kiColumnBackPWMIOPin, pwmValue);
            break;
        case DIRECTION::Stop:
            gpioPWM(kiColumnFwdPWMIOPin,PI_LOW);
            gpioPWM(kiColumnBackPWMIOPin,PI_LOW);
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
}

My CPU usage is currently 107% 
BhavinT
  • 338
  • 1
  • 10

2 Answers2

1

The pigpio library itself seems to be quite efficient in generating the PWM signal.

Perhaps your own code is running continuously in a loop? Try adding usleep(10000); at some point to delay for a short while.

Depending on exactly what you are doing, there may better ways to wait for an event, but the question does not contain enough information.

jpa
  • 10,351
  • 1
  • 28
  • 45
1

@jpa usleep(10000) can not help me, it generates only 100 Hz. Currently, my requirement is to use 5 Khz to 20 Khz frequency on pwm. and it has highest usage of CPU.

Bhavesh P
  • 21
  • 1
  • You can comment on answers instead of posting new answers. But do you need the PWM frequency to be 5kHz, or to update the PWM rate at 5kHz? – jpa May 09 '23 at 14:01