sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

trouble with Gpio

Tue May 06, 2014 9:43 pm

Hi,
i'm commanding a stepper motor with my raspberry pi using a Stepper Motor Driver/Controller Cw5045 , i'm using two signals to command the driver ( direction signal and the other signal that contains pulses ,each pulse makes the mottor move one step, and the frequency of the pulses determinate the speed of the motor, when the frequency goes hight the speed increase)
my problem is that the stepper motor is making a vibration when running and some times it stops , and this is because the signal that contains the pulses have many perturbations, the period of the signal is not constant .
I'm using a simple loop to generate the signal
for(;;){
digitalWrite(pin2,0);
delayMicroseconds(2);
digitalWrite(pin2,1);
delayMicroseconds(2);
}

and the image signal in the attachement

so can anybody help me with this ?
Attachments
image.gif
image.gif (31.37 KiB) Viewed 5128 times

hunternet93
Posts: 336
Joined: Mon Dec 12, 2011 4:34 pm

Re: trouble with Gpio

Wed May 07, 2014 3:29 am

This is probably because Linux is not a real-time OS. A CPU can only do one thing at a time, the Linux kernel manages switching between all running programs very fast and runs bits of each program in sequence. You can tell Linux to give priority to specific programs with the nice command:

Code: Select all

nice -n 20 your_command_here
The "niceness" level sets how much CPU time is allotted to the process, from -20 to 19, the lower the number the higher priority the program will have. This should improve the program's accuracy, shutting down unneeded programs and services will also help.

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

Re: trouble with Gpio

Wed May 07, 2014 7:47 am

If you want accuracy you'll need to use something like pigpio.

The following example is from the command line. Normally you'd use C or Python.

For this example I'm using Broadcom gpio 21. pigpio only uses Broadcom gpio numbers. This was done on a Rev.1 board.

Code: Select all

~ $ sudo pigpiod

~ $ pigs m 21 w # set gpio 21 as an output

~ $ pigs wvag 0x200000 0 2 0 0x200000 2 # 2 micros on/off
2

~ $ pigs wvcre # create a waveform from the added pulses, id 0 returned
0

~ $ pigs wvag 0x200000 0 10 0 0x200000 10 # 10 micros on/off
2

~ $ pigs wvcre # create a waveform from the added pulses, id 1 returned
1

~ $ pigs wvtxr 1 # tx waveform with id 1 repeatedly
5

~ $ pigs wvtxr 0  # tx waveform with id 0 repeatedly
5

~ $ pigs wvtxr 1 # tx waveform with id 1 repeatedly
5

~ $ pigs wvhlt # stop waveform
pigs uses the socket interface to pigpio to send commands.

wvag uses triplets of gpios on, gpios off, and microsecond delay to define pulses.

A set bit selects a gpio. gpio 21 is selected by setting bit 1<<21 (or 0x200000)

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Wed May 07, 2014 10:29 am

hunternet93 : i have already used the command nice but i get the same resulat , i think i have to patch my raspbian to become a real time system

joan : and this pigpio library can generate a better signal without any loss in the period ?

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

Re: trouble with Gpio

Wed May 07, 2014 10:38 am

sabrovic89 wrote:...
joan : and this pigpio library can generate a better signal without any loss in the period ?
Yes. Here it's driving two four coil steppers, albeit at a slower speed as that is the maximum for the steppers I was using.

https://www.youtube.com/watch?v=Lp7oEj0iAe8

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Wed May 07, 2014 10:56 am

joan : thanks i will try it

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

Re: trouble with Gpio

Wed May 07, 2014 10:57 am

sabrovic89 wrote:joan : thanks i will try it
If it doesn't work I'll give you your money back!

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Thu May 08, 2014 10:25 am

Hi Joan ,
i need your help i can not generate a waveform with C code this is my code is it correct ?

if (gpioInitialise() < 0) return 1;
gpioPulse_t *pulses;
pulses->gpioOn=1;
pulses->gpioOff=0;
pulses->usDelay=3;
1<<23;
gpioWaveClear();
gpioWaveAddNew();
gpioWaveAddGeneric(3000,pulses);
gpioWaveCreate();
for(;;){
gpioWaveTxStart(1);
}

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

Re: trouble with Gpio

Thu May 08, 2014 10:51 am

That's not quite the way it works from C.

I have added a different interface to allow for multiple waveforms to be constructed and have deprecated some of the original C functions (although they still work).

For a simple on/off repetitive waveform have a look at.

Code: Select all

#include<stdio.h>

#include <pigpio.h>

int main(int argc, char *argv[])
{
   int on=100000, off=200000, gpio=22;
   int wave_id;
   gpioPulse_t pulse[2];

   if (argc > 3)
   {
      on   = atoi(argv[3]);
      off  = atoi(argv[2]);
      gpio = atoi(argv[1]);
   }
   else if (argc > 2)
   {
      on   = atoi(argv[2]);
      off  = on;
      gpio = atoi(argv[1]);
   }
   else if (argc > 1)
   {
      gpio = atoi(argv[1]);
   }

   printf("gpio=%d on=%d off=%d\n", gpio, on, off);

   if (gpioInitialise()<0) return -1;

   gpioSetMode(gpio, PI_OUTPUT);

   pulse[0].gpioOn = (1<<gpio);
   pulse[0].gpioOff = 0;
   pulse[0].usDelay = on;

   pulse[1].gpioOn = 0;
   pulse[1].gpioOff = (1<<gpio);
   pulse[1].usDelay = off;

   gpioWaveAddGeneric(2, pulse);

   wave_id = gpioWaveCreate();

   gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT);

   while (1) sleep(1);

   gpioTerminate();
}

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Thu May 08, 2014 1:16 pm

i got this message when i execute the program :

gpio=23 on=1 off=2
2014-05-08 13:15:18 gpioInitialise: Can't lock /var/run/pigpio.pid

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

Re: trouble with Gpio

Thu May 08, 2014 1:27 pm

sabrovic89 wrote:i got this message when i execute the program :

gpio=23 on=1 off=2
2014-05-08 13:15:18 gpioInitialise: Can't lock /var/run/pigpio.pid
It's meant to be a safety feature, to prevent more than one instance of the library running.

The lock file should be deleted when the library terminates.

Just

sudo rm /var/run/pigpio.pid

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Thu May 08, 2014 1:35 pm

i'v got this message now :
2014-05-08 13:33:39 gpioInitialise: bind to port 8888 failed (Address already in use)

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

Re: trouble with Gpio

Thu May 08, 2014 1:42 pm

sabrovic89 wrote:i'v got this message now :
2014-05-08 13:33:39 gpioInitialise: bind to port 8888 failed (Address already in use)
Probably something else is talking to another instance of the library. I say probably as pigpio uses port 8888 but so does one other Pi type program.

I'd do

sudo killall -9 pigpiod # kill the daemon
sudo rm /var/run/pigpio.pid

Check that nothing is running which was talking to pigpio via the socket interface. Normally one of piscope or the Python pigpio module.

Wait a minute for the port to expire.

User avatar
DougieLawson
Posts: 35814
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: trouble with Gpio

Thu May 08, 2014 1:53 pm

Install lsof and you can see who/what has a port allocated.

sudo apt-get install lsof
sudo lsof -i TCP:8888
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: trouble with Gpio

Thu May 08, 2014 2:19 pm

DougieLawson wrote:Install lsof and you can see who/what has a port allocated.

sudo apt-get install lsof
sudo lsof -i TCP:8888
Pity I didn't know that before. It would have saved me a lot of time a couple of months ago.

Code: Select all

mercury:~$ sudo lsof -i TCP:8888
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
piscope 32517 joan    9u  IPv4 1869187      0t0  TCP mercury.lan:57291->hard.lan:8888 (ESTABLISHED)
piscope 32517 joan   10u  IPv4 1869189      0t0  TCP mercury.lan:57292->hard.lan:8888 (ESTABLISHED)
mercury:~$

hard ~ $ sudo lsof -i TCP:8888
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
pigpiod 3540 root    0u  IPv4   5201      0t0  TCP *:8888 (LISTEN)
pigpiod 3540 root    3u  IPv4   5963      0t0  TCP hard.lan:8888->mercury.lan:57291 (ESTABLISHED)
pigpiod 3540 root    6u  IPv4   5964      0t0  TCP hard.lan:8888->mercury.lan:57292 (ESTABLISHED)
hard ~ $ 

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Mon May 12, 2014 9:22 pm

joan wrote:That's not quite the way it works from C.

I have added a different interface to allow for multiple waveforms to be constructed and have deprecated some of the original C functions (although they still work).

For a simple on/off repetitive waveform have a look at.

Code: Select all

#include<stdio.h>

#include <pigpio.h>

int main(int argc, char *argv[])
{
   int on=100000, off=200000, gpio=22;
   int wave_id;
   gpioPulse_t pulse[2];

   if (argc > 3)
   {
      on   = atoi(argv[3]);
      off  = atoi(argv[2]);
      gpio = atoi(argv[1]);
   }
   else if (argc > 2)
   {
      on   = atoi(argv[2]);
      off  = on;
      gpio = atoi(argv[1]);
   }
   else if (argc > 1)
   {
      gpio = atoi(argv[1]);
   }

   printf("gpio=%d on=%d off=%d\n", gpio, on, off);

   if (gpioInitialise()<0) return -1;

   gpioSetMode(gpio, PI_OUTPUT);

   pulse[0].gpioOn = (1<<gpio);
   pulse[0].gpioOff = 0;
   pulse[0].usDelay = on;

   pulse[1].gpioOn = 0;
   pulse[1].gpioOff = (1<<gpio);
   pulse[1].usDelay = off;

   gpioWaveAddGeneric(2, pulse);

   wave_id = gpioWaveCreate();

   gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT);

   while (1) sleep(1);

   gpioTerminate();
}
It worked just fine , thanks Joan , but i have another problem , when i increase the frequency of the wave which i sent to the stepper motor driver to 150 khz or more the stepper motor make a noise and didn't rotate , but when i sent the wave from a function generator the motor works just fine at 150 khz and 200 khz

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

Re: trouble with Gpio

Mon May 12, 2014 10:04 pm

sabrovic89 wrote:
joan wrote:That's not quite the way it works from C.

I have added a different interface to allow for multiple waveforms to be constructed and have deprecated some of the original C functions (although they still work).

For a simple on/off repetitive waveform have a look at.

Code: Select all

#include<stdio.h>

#include <pigpio.h>

int main(int argc, char *argv[])
{
   int on=100000, off=200000, gpio=22;
   int wave_id;
   gpioPulse_t pulse[2];

   if (argc > 3)
   {
      on   = atoi(argv[3]);
      off  = atoi(argv[2]);
      gpio = atoi(argv[1]);
   }
   else if (argc > 2)
   {
      on   = atoi(argv[2]);
      off  = on;
      gpio = atoi(argv[1]);
   }
   else if (argc > 1)
   {
      gpio = atoi(argv[1]);
   }

   printf("gpio=%d on=%d off=%d\n", gpio, on, off);

   if (gpioInitialise()<0) return -1;

   gpioSetMode(gpio, PI_OUTPUT);

   pulse[0].gpioOn = (1<<gpio);
   pulse[0].gpioOff = 0;
   pulse[0].usDelay = on;

   pulse[1].gpioOn = 0;
   pulse[1].gpioOff = (1<<gpio);
   pulse[1].usDelay = off;

   gpioWaveAddGeneric(2, pulse);

   wave_id = gpioWaveCreate();

   gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT);

   while (1) sleep(1);

   gpioTerminate();
}
It worked just fine , thanks Joan , but i have another problem , when i increase the frequency of the wave which i sent to the stepper motor driver to 150 khz or more the stepper motor make a noise and didn't rotate , but when i sent the wave from a function generator the motor works just fine at 150 khz and 200 khz
Could you have a look on an oscilloscope? I'm afraid I don't have the tools to be sure of what is happening at these sorts of frequencies.

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Tue May 13, 2014 12:54 pm

joan wrote:
sabrovic89 wrote:
joan wrote:That's not quite the way it works from C.

I have added a different interface to allow for multiple waveforms to be constructed and have deprecated some of the original C functions (although they still work).

For a simple on/off repetitive waveform have a look at.

Code: Select all

#include<stdio.h>

#include <pigpio.h>

int main(int argc, char *argv[])
{
   int on=100000, off=200000, gpio=22;
   int wave_id;
   gpioPulse_t pulse[2];

   if (argc > 3)
   {
      on   = atoi(argv[3]);
      off  = atoi(argv[2]);
      gpio = atoi(argv[1]);
   }
   else if (argc > 2)
   {
      on   = atoi(argv[2]);
      off  = on;
      gpio = atoi(argv[1]);
   }
   else if (argc > 1)
   {
      gpio = atoi(argv[1]);
   }

   printf("gpio=%d on=%d off=%d\n", gpio, on, off);

   if (gpioInitialise()<0) return -1;

   gpioSetMode(gpio, PI_OUTPUT);

   pulse[0].gpioOn = (1<<gpio);
   pulse[0].gpioOff = 0;
   pulse[0].usDelay = on;

   pulse[1].gpioOn = 0;
   pulse[1].gpioOff = (1<<gpio);
   pulse[1].usDelay = off;

   gpioWaveAddGeneric(2, pulse);

   wave_id = gpioWaveCreate();

   gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT);

   while (1) sleep(1);

   gpioTerminate();
}
It worked just fine , thanks Joan , but i have another problem , when i increase the frequency of the wave which i sent to the stepper motor driver to 150 khz or more the stepper motor make a noise and didn't rotate , but when i sent the wave from a function generator the motor works just fine at 150 khz and 200 khz
Could you have a look on an oscilloscope? I'm afraid I don't have the tools to be sure of what is happening at these sorts of frequencies.
you can have a look at this picture , this signal is generated by the raspberry , when the frequency of this signal is more than 125 khz the stepper motor make a noise and there is no rotation , if you have a skype ID i can show you by camera if it's ok with you
Attachments
tttt.gif
tttt.gif (16.58 KiB) Viewed 4710 times

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

Re: trouble with Gpio

Tue May 13, 2014 1:02 pm

I can't interpret oscilloscope traces. I don't know if that is showing a problem or not.

Does the trace suddenly get worse at a particular frequency?

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Tue May 13, 2014 1:30 pm

joan wrote:I can't interpret oscilloscope traces. I don't know if that is showing a problem or not.

Does the trace suddenly get worse at a particular frequency?
the trace have the same atitude less or more than 125 khz , but the stepper motor doesn't rotate , but when i shake the wire wich command the steppper motor driver , the motor rotate , it's bizare

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Tue May 13, 2014 1:31 pm

and this happens only when the frequency is higher than 125 khz

User avatar
GTR2Fan
Posts: 1601
Joined: Sun Feb 23, 2014 9:20 pm
Location: South East UK

Re: trouble with Gpio

Tue May 13, 2014 1:43 pm

I'm confused. Are you saying that you're driving the stepper motor with a 125kHz signal? Wouldn't closer to 125Hz be more usual?
Last edited by GTR2Fan on Tue May 13, 2014 1:47 pm, edited 2 times in total.
Pi2B Mini-PC/Media Centre: ARM=1GHz (+3), Core=500MHz, v3d=500MHz, h264=333MHz, RAM=DDR2-1200 (+6/+4/+4+schmoo). Sandisk Ultra HC-I 32GB microSD card on '50=100' OCed slot (42MB/s read) running Raspbian/KODI16, Seagate 3.5" 1.5TB HDD mass storage.

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

Re: trouble with Gpio

Tue May 13, 2014 1:46 pm

GTR2Fan wrote:I'm confused. Are you saying that you're driving the stepper motor with a 125kHz signal? Wouldn't closer to 125Hz be more usual?
It's a fast stepper.

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

Re: trouble with Gpio

Tue May 13, 2014 1:48 pm

sabrovic89 wrote:
joan wrote:I can't interpret oscilloscope traces. I don't know if that is showing a problem or not.

Does the trace suddenly get worse at a particular frequency?
the trace have the same atitude less or more than 125 khz , but the stepper motor doesn't rotate , but when i shake the wire wich command the steppper motor driver , the motor rotate , it's bizare
I'll go out on a limb because this is way outside my knowledge. Perhaps the gpios can't supply enough voltage/current fast enough to suit the stepper electronics. You say it works OK if you use a signal generator?

sabrovic89
Posts: 20
Joined: Tue May 06, 2014 7:34 pm

Re: trouble with Gpio

Tue May 13, 2014 3:07 pm

yes

Return to “Troubleshooting”