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%