英文:
Minimize CPU usage for software PWM in the Raspberry PI
问题
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.
我想在树莓派4B上的GPIO引脚(5、6、19和26)上使用C语言的pigpio库生成4个软件PWM信号。我想生成8 kHz的PWM频率。我能够成功地移动和控制电机,并使用可变的占空比,但即使在控制器中没有运动时,它也会消耗大量的CPU使用率。只有在软件开始执行时才初始化了像gpioSetPWMfrequency和gpioSetPWMrange这样的函数。我在需要更改引脚的占空比的循环中使用了gpiopwm函数。
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?
我有4个PWM通道来操作连接到执行器的BTS7960 43A高功率电机驱动模块。有没有办法减少CPU使用率?根据Pigpio文档,它使用DMA访问。我需要帮助。有人能否指导我降低CPU使用率的方法以及更好的库来实现这一点?
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(kiShieldBackPWMPin,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%
我的CPU使用率目前为107%。
<details>
<summary>英文:</summary>
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%
答案1
得分: 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.
英文:
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.
答案2
得分: 1
@jpa usleep(10000) 无法帮助我,它只生成100赫兹。
目前,我的要求是在PWM上使用5千赫兹到20千赫兹的频率,而且它占用CPU最高。
英文:
@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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论