davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

RasPi 2B: control 2 motors simult. by ident. pwm: how to?

Tue Dec 29, 2015 9:37 am

hello,
with my Raspi 2B I need to control at least 2 motors simultaneously by identical pwm (plugged to a L298 H Bridge), but I could find only 1 pwm pin on the GPIO header:
how to do it then?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 9:45 am

Does it need to be the same PWM?
If so, you could connect 1 PWM output to the 2 inputs on the H-bridge.
If not, you could make software PWM (I think it's available in pigpio) for every pin. ;)

Laurens

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 9:45 am

PIGPIO will give you hardware timed PWM on any GPIO pin.
Doug.
Building Management Systems Engineer.

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 9:48 am

BMS Doug wrote:PIGPIO will give you hardware timed PWM on any GPIO pin.
I like pigpio.
Other DMA based gpio libraries can also provide stable PWM on any pin. AFAIK WiringPi and ServoBlaster can also do that.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 9:51 am

PiGraham wrote:
BMS Doug wrote:PIGPIO will give you hardware timed PWM on any GPIO pin.
I like pigpio.
Other DMA based gpio libraries can also provide stable PWM on any pin. AFAIK WiringPi and ServoBlaster can also do that.
I've heard that they can, I only have experience of gettingPWM working with PIGPIO, I've never tried servoblaster and not tried PWM with WiringPi.
Doug.
Building Management Systems Engineer.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 9:54 am

@ Laurens: there will be 2 different pwm signals needed, for a robot differential drive.

@ all: I am currently fine with pwm frequencies on my Arduinos so far (via analogWrite(0-255) - not quite sure, something like 1-4 kHz perhaps)
- which frequency/resolution will provide pigpio (or wiringPi )?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 10:08 am

davenull wrote:@ Laurens: there will be 2 different pwm signals needed, for a robot differential drive.

@ Doug: I am currently fine with pwm frequencies on my Arduinos so far (via analogWrite(0-255) - not quite sure, something like 1-4 kHz perhaps)
- which frequency/resolution will provide pigpio (or wiringPi )?
Have a read of the PIGPIO documentation but as I read it you can set the PWM rate up to 40000Hz and achieve up to 20000 step resolution.

I'm sure Joan will be along to correct any errors I have made.
Doug.
Building Management Systems Engineer.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 10:20 am

thank you for your link to the pigpio documentation - but that's not understandable to me, far too mauch information, I'm an absolute beginner to Raspi.
I am simply used to commands like
analogWrite(uint8_t pwm)

For the start I need no different settings, I just need to know about the default pwm frequency setting.

I had a look to wiringPi before, here a similar command is available like for the Arduino API, but I also don't see informations about pwm frequencies....
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

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

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 11:32 am

Why not use Python? The PWM is the same.

pigpio defaults to 800Hz frequency with a dutycycle between 0 (off) and 255 (fully on).

From C gpioPWM

From Python set_PWM_dutycycle

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 11:51 am

thank you very much, yes, I think 800 Hz will be fine, and I am using C (actually g++) !

BTW,
as you are the API author:
will pigpio be compatible then when I am also using wiringPi API functions for different purposes (not pwm) in the same program, simultaneously?
(e.g., delay, digitalWrite, analogRead, wiringSerial ?)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

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

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 12:09 pm

davenull wrote:thank you very much, yes, I think 800 Hz will be fine, and I am using C (actually g++) !

BTW,
as you are the API author:
will pigpio be compatible then when I am also using wiringPi API functions for different purposes (not pwm) in the same program, simultaneously?
(e.g., delay, digitalWrite, analogRead, wiringSerial ?)
Yes, pigpio is compatible with wiringPi.

However if all you are using is the PWM functions you would probably be better off using something like servoblaster or pi-blaster. They were designed for PWM.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi 2B: control 2 motors simult. by ident. pwm: how to

Tue Dec 29, 2015 12:11 pm

no, it's not the only purpose, there will be many API functions I will use (and need) both from pigpio and from WiringPi then.
Thank you!
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”