pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

pigpio software PWM jitters when run together with raspivid

Mon Jan 06, 2020 5:40 pm

I’m trying to run software PWM using the pigpio library to generate software PWM. Individually without running raspivid , the spftware works and I can spin a brushless motor. However when I turn on raspivid, the software PWM jitters — specifically, the software PWM is actually output at about 10% which is enough to spin the motor even though I did not output anything from my software. Any clues as to whats causing this?

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Mon Jan 06, 2020 5:42 pm

Specifically, raspivid is run in a separate ssh session (headless) , and my pigpio software is run on another ssh session also. The hardware used is raspi zero w. OS used is the latest raspbian.

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Mon Jan 06, 2020 5:55 pm

Presumably there is a conflict. Have you tried starting pigpio with the PWM clock rather than the PCM clock (or vice versa). See the -t option at http://abyz.me.uk/rpi/pigpio/pigpiod.html

If it's not that then perhaps there is a clash with DMA channel usage. I know nothing about raspivid and don't know if it uses DMA. If it does again see -d -e http://abyz.me.uk/rpi/pigpio/pigpiod.html to select a different DMA channel.

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Thu Jan 16, 2020 5:11 pm

I tried updating the clock source between PWM and PCM, but the issue still persists.

Also tried changing up the different channels for DMA primary and secondary channels, also have the same issue.

One condition that worked sometimes is that raspivid does not trigger the software PWMs, but what ends up happening is when I try to exit out of my code and call gpioTerminate(), the program never returns, i.e. it's stuck and does not even respond to a Ctrl+C. When I rerun gpioInitialize(), it turns up with the error

Code: Select all

cannot lock /var/run/pigpio.pid
, so I have to power cycle the pi, and then the raspivid will interfere with PWM again. Any idea what's causing this loop above to hang?

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Thu Jan 16, 2020 6:03 pm

I don't know what is happening.

You can delete the pid file so you don't need to reboot.

sudo rm /var/run/pigpio.pid

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7770
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: pigpio software PWM jitters when run together with raspivid

Fri Jan 17, 2020 2:06 pm

Which GPIOs are you using?

How busy is the CPU when running both apps?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Sat Jan 18, 2020 4:55 am

Thanks for your replies joan and 6by9.

joan:
You can delete the pid file so you don't need to reboot.

sudo rm /var/run/pigpio.pid
running this command does indeed delete that pigpio.pid, BUT the first program which had already run

Code: Select all

gpioInitialize(); 
gpioPWM(....);
gpioTerminate();
never exits, i.e. that fist program stays stuck, Ctrl+C yields no response and htop shows that the program is still running. So evern after I delete that pigpio.pid, I cannot call that program again without power-cycling the Pi.

6b9:
Which GPIOs are you using? I have tried a few GPIOs, including BCM 16,19,20 etc. and they all show

How busy is the CPU when running both apps? ~60% on a raspi zero w with latest raspbian OS (september 2019 I think) .

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Sat Jan 18, 2020 5:00 am

joan

If i run a second program in another terminal, I get this error:

Code: Select all

initInitialise: Can't lock /var/run/pigpio.pid
This is while the first program is stuck.

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Sat Jan 18, 2020 8:10 am

Tell me how to reproduce the fault and I will have a look.

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Sun Jan 19, 2020 9:16 pm

hardware:
raspi zero W
camera V2
brushless motor



OS:
Rasbian Jessie Lite OR

Raspbian Buster Lite
Minimal image based on Debian Buster
Version: September 2019
Release date: 2019-09-26
Kernel version: 4.19
Size: 435 MB

Code: Select all

unsigned sampleRateUs = 1; // default = 5!
unsigned configPeripheral = PI_CLOCK_PCM; // changing this does not result in any changes // PI_CLOCK_PWM (0) or PI_CLOCK_PCM (1, default)
if (gpioCfgClock(sampleRateUs, configPeripheral, 0)< 0) // last argument unused
{
std::cout"GpioInit - gpioCfgClock < 0";
}

// this line below also does not matter much in terms of changing behavior, I can comment it out and still see similar issues
gpioCfgDMAchannels(14, 6); //12);

if (gpioInitialise() < 0)
{
	//initialization failed
	gpioTerminate();
	std::cout<< " gpioInitialise < 0\n";
}


gpioSetMode(20, PI_OUTPUT);
// set PWM to zero at the beginning
gpioPWM(20, 0);
// set frequency
gpioSetPWMfrequency(20, 500);
// set PWM range such that gpioPWM below is in us
gpioSetPWMrange(20, 1e6/500);


// this value should not spin the motor
gpioPWM(20, 950);

// at this point, the motor should not spin
/*
// spin motor if desired, but not for htis test
while (1) {
	gpioPWM(20, 1500);
}
*/

Code: Select all

//*********************************** in a separate terminal
raspivid -t 600000 -o v.h264 -fps 30  -w 1920 -h 1080
If you run these in two separate terminals, you' should see what I see. I'm running the motor code first, then the video code. When the video code runs, I see the motors spin. You can probably probe the pins for PWM output also.

Variation of test:
At the beginning of this code, I also have changed

Code: Select all

uint8 sampleRateUs = 1; // default = 5!
to be 5, which then somehow turns off wifi and I have to reconnect SSH.

I have tried pins 16, 19 and have seen similar results.

Thanks!

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Sun Jan 19, 2020 10:15 pm

Thanks for the instructions. It might be a few days before I have a chance to try them out.

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Mon Jan 20, 2020 10:26 am

Cool thanks Joan! FYI the brushless is driven by a brushless ESC and batteries at 12V. That ESC, brushless motor and battery setup may be replaced with some kind of PWM reader. Otherwise the ESC signal can be driven by the GPIo and also grounded. Thanks!

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Wed Jan 22, 2020 7:06 pm

I have just had a quick look. The C code example is incomplete (it does not compile).

Also if I understand the example correctly it will only set PWM for a fraction of a second before the code exits.

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Thu Jan 23, 2020 9:11 pm

Hey Joan, this code should compile. There was std::cout earlier on. Also added a while (1) { ... ; usleep(1000);}

Code: Select all

#include <pigpio.h>
#include <stdio.h>
#include <unistd.h> // for usleep

int main(){
	unsigned sampleRateUs = 1; // default = 5!
	unsigned configPeripheral = PI_CLOCK_PCM; // changing this does not result in any changes // PI_CLOCK_PWM (0) or PI_CLOCK_PCM (1, default)
	if (gpioCfgClock(sampleRateUs, configPeripheral, 0)< 0) // last argument unused
	{
	printf("GpioInit - gpioCfgClock < 0\n");
	}

	// this line below also does not matter much in terms of changing behavior, I can comment it out and still see similar issues
	gpioCfgDMAchannels(14, 6); //12);

	if (gpioInitialise() < 0)
	{
		//initialization failed
		gpioTerminate();
		printf(" gpioInitialise < 0\n");
	}


	gpioSetMode(20, PI_OUTPUT);
	// set PWM to zero at the beginning
	gpioPWM(20, 0);
	// set frequency
	gpioSetPWMfrequency(20, 500);
	// set PWM range such that gpioPWM below is in us
	gpioSetPWMrange(20, 1e6/500);


	while (1){
	// this value should not spin the motor
	gpioPWM(20, 950);

	// at this point, the motor should not spin
	/*
	// spin motor if desired, but not for htis test
	gpioPWM(20, 1500);

	*/
	
	usleep(1000);
	}
}

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Thu Jan 23, 2020 9:12 pm

With the new code, it should just run with the minimum PWM required to spin the motor, i.e. (950). But running raspivid simultaneously will then jitter the PWM.

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Thu Jan 23, 2020 9:13 pm

Okay, I'll try again later. I only have a Pi3B+ to test on at the moment.

pidood1234
Posts: 12
Joined: Thu Jan 02, 2020 6:04 am

Re: pigpio software PWM jitters when run together with raspivid

Thu Jan 23, 2020 9:44 pm

OK, that should be fine, I think I've seen the issue on a Raspi 3B before. Full disclousure, I don't have a raspi to compile it on now but it should work, since it has minimal additional code other than the pigpio library functions (other than printf and usleep). Thanks!

User avatar
joan
Posts: 14604
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio software PWM jitters when run together with raspivid

Sun Jan 26, 2020 1:01 pm

I changed the code to the following.

Code: Select all

#include <pigpio.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
   int retval;

   retval = gpioCfgClock(2, PI_CLOCK_PCM, 0);
   if (retval < 0)
   {
      printf("gpioCfgClock failed (%d)\n", retval);
      return 1;
   }
   else printf("gpioCfgClock sampling set to 2us okay\n");

   retval = gpioInitialise();
   if (retval < 0)
   {
      printf(" gpioInitialise failed (%d)\n", retval);
      return 1;
   }


   gpioSetMode(20, PI_OUTPUT);
   // set PWM to zero at the beginning
   gpioPWM(20, 0);
   // set frequency
   retval = gpioSetPWMfrequency(20, 500);
   printf("frequency %d set\n", retval);

   // set PWM range such that gpioPWM below is in us
   retval = gpioSetPWMrange(20, 1e6/500);
   printf("range %d set\n", retval);

   gpioPWM(20, 950);
   printf("pulse width of 950us on GPIO 20 set\n");

   while (1)
   {
      sleep(1);
   }
}
I can't see any obvious jitter (or problems with raspivid) when the sampling rate is less than 1 MHz.

At 1 MHz there is jitter.

I'm afraid if this corresponds to what you are seeing then this is to be expected. The bus is being hit quite hard at 1 MHz sampling and I can see that something like raspivid would test the system to its limits and beyond.

Return to “Troubleshooting”