jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Using RPIO for controling dc motor speed

Fri Sep 19, 2014 7:24 pm

Hi! I want to use python with RPIO (pwm) to control a dc motor speed.
I have written this code:

Code: Select all

from RPIO import PWM
from time import sleep
servo = PWM.Servo()

RPIO.setmode(RPIO.BOARD)
Motor1A = 16
Motor1B = 18
Motor1E = 22

RPIO.setup(Motor1A, RPIO.OUT)
RPIO.setup(Motor1B, RPIO.OUT)
RPIO.setup(Motor1E, RPIO.OUT)

print("Turning Motor on direction 1")
RPIO.output(Motor1A, RPIO.HIGH)
RPIO.output(Motor1B, RPIO.LOW)
RPIO.output(Motor1E, RPIO.HIGH)
servo.set_servo(Motor1E, 10)
sleep(2)
servo.set_servo(Motor1E, 50)
sleep(2)
RPIO.output(Motor1E, RPIO.LOW)
RPIO.cleanup()
Now, there is something (obviously) wrong with it because I don't see any change in the speed.
This is the first time I write something like this and I wanted to know if I'm even close to the right solution.
(Or if is it even possible)
The motor itself is working and I can also change the direction.
Thanks!

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

Re: Using RPIO for controling dc motor speed

Fri Sep 19, 2014 7:31 pm

Presumably 1E is the enable pin for motor 1 on the motor driver.

The principal is correct. You are using PWM. I'm not sure about the servo function. Servos are only on for 5-10% of the PWM cycle. You probably want to have PWM which can be varied between 0 and 100%.

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 12:44 am

joan wrote:Presumably 1E is the enable pin for motor 1 on the motor driver.

The principal is correct. You are using PWM. I'm not sure about the servo function. Servos are only on for 5-10% of the PWM cycle. You probably want to have PWM which can be varied between 0 and 100%.
Yes, 1E is the enable pin for motor 1.

What do you mean by:
You probably want to have PWM which can be varied between 0 and 100%.
How do I do that?

Thanks!

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

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 6:47 am

I don't use the RPIO module so I'd have to read the documentation to find out how to set up PWM with a variable dutycycle between off and fully on. You could probably understand the documentation as quickly as me.

As I said servo pulses are only on for 1-2 ms in every 20ms cycle so with them you can only vary PWM between off, or 5-10% on. Not suitable for DC motor speed control.

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 7:02 am

Code copied from the RPIO site:

Code: Select all

rom RPIO import PWM

# Setup PWM and DMA channel 0
PWM.setup()
PWM.init_channel(0)

# Add some pulses to the subcycle
PWM.add_channel_pulse(0, 17, 0, 50)
PWM.add_channel_pulse(0, 17, 100, 50)

# Stop PWM for specific GPIO on channel 0
PWM.clear_channel_gpio(0, 17)

# Shutdown all PWM and DMA activity
PWM.cleanup()
Take a look here to see what this means:

http://pythonhosted.org/RPIO/pwm_py.html
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 7:32 am

Hello,

you should not use DMA channel 0.
With raspbian 2013-12-20-wheezy-raspbian, dma channel 0 is used for accelerating the X-Server.

I switched over to channel 1 in my setups, which is stable (unless someone tries to use this for other purpose).

regards,
Gerhard

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

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 7:37 am

Actually avoid DMA channels 0, 1, 2, 3, 6, 7. The GPU uses 1, 3, 6, 7. The frame buffer uses 0 and the SD card uses 2.

You can see the channels reserved for GPU use by

cat /sys/module/dma/parameters/dmachans

In addition channels 0 and 2 are used by the ARM software.

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 9:27 am

joan wrote:Actually avoid DMA channels 0, 1, 2, 3, 6, 7. The GPU uses 1, 3, 6, 7. The frame buffer uses 0 and the SD card uses 2.

You can see the channels reserved for GPU use by

cat /sys/module/dma/parameters/dmachans

In addition channels 0 and 2 are used by the ARM software.
You confused me completely :S what DMA channel to use?
Thanks!

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 9:33 am

Very interesting.
How is the cat /sys/module/dma/parameters/dmachans encoded ?

In my raspbian installation, it results in '32565', and I cannot clearly see what this means.

Regards,

Gerhard

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 9:41 am

Thank you all for the responses! Finally I managed to get it work but with GPIO.PWM:
From here: http://raspi.tv/2013/rpi-gpio-0-5-2a-no ... -to-use-it
Thanks again!

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

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 11:02 am

ghp wrote:Very interesting.
How is the cat /sys/module/dma/parameters/dmachans encoded ?

In my raspbian installation, it results in '32565', and I cannot clearly see what this means.

Regards,

Gerhard
Mine shows 32565 as well.

You need to convert it to binary

If bit n is 0 then channel n is reserved for GPU use.

Code: Select all

DMA channel 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
Bit          0  1  1  1  1  1  1  1  0  0  1  1  0  1  0  1

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

Re: Using RPIO for controling dc motor speed

Sat Sep 20, 2014 11:06 am

jdorfsman wrote:Thank you all for the responses! Finally I managed to get it work but with GPIO.PWM:
From here: http://raspi.tv/2013/rpi-gpio-0-5-2a-no ... -to-use-it
Thanks again!
Software PWM will probably be fine for DC motors. It is likely to be less useful for LEDs (too many glitches) and unless you have a very undemanding application it will be unusable for servos.

jamieboyd
Posts: 2
Joined: Mon Aug 22, 2016 6:39 pm

Re: Using RPIO for controling dc motor speed

Mon Aug 22, 2016 8:32 pm

Thanks to the info on this page, I got RPIO.PWM working on the Pi 2. One thing I noticed is that the timing is a bit off. With the 20000 microseconds default period, I should get 50 Hz, but I get 44.45 Hz, the period is off by a factor of 1.25. Similarly, the pulse widths are a little longer than they should be. Is the timing with RPIO.PWM more accurate on the Pi1?

Using the hardware PWM with Gordon's WiringPi, I can choose the clock divider to get a frequency of 49.867Hz and pulse widths are much more accurate, but you have to know the pi clock rate (19.2e6 for pi 2) to calculate it. Looking through pwm.c I see that PRIO.PWM uses the 500MHz PLLD clock as source - is there a difference in PLLD between pi1 and 2 that might account for the 1.25X period length difference?

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Using RPIO for controling dc motor speed

Mon Aug 22, 2016 8:47 pm

Try joan's pigpio, I use it for pwm and servo timing for Pi Droid Alpha.
jamieboyd wrote:Thanks to the info on this page, I got RPIO.PWM working on the Pi 2. One thing I noticed is that the timing is a bit off. With the 20000 microseconds default period, I should get 50 Hz, but I get 44.45 Hz, the period is off by a factor of 1.25. Similarly, the pulse widths are a little longer than they should be. Is the timing with RPIO.PWM more accurate on the Pi1?

Using the hardware PWM with Gordon's WiringPi, I can choose the clock divider to get a frequency of 49.867Hz and pulse widths are much more accurate, but you have to know the pi clock rate (19.2e6 for pi 2) to calculate it. Looking through pwm.c I see that PRIO.PWM uses the 500MHz PLLD clock as source - is there a difference in PLLD between pi1 and 2 that might account for the 1.25X period length difference?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

jamieboyd
Posts: 2
Joined: Mon Aug 22, 2016 6:39 pm

Re: Using RPIO for controling dc motor speed

Mon Aug 22, 2016 9:59 pm

jamieboyd wrote:Thanks to the info on this page, I got RPIO.PWM working on the Pi 2. One thing I noticed is that the timing is a bit off. With the 20000 microseconds default period, I should get 50 Hz, but I get 44.45 Hz, the period is off by a factor of 1.25. Similarly, the pulse widths are a little longer than they should be. Is the timing with RPIO.PWM more accurate on the Pi1?

Using the hardware PWM with Gordon's WiringPi, I can choose the clock divider to get a frequency of 49.867Hz and pulse widths are much more accurate, but you have to know the pi clock rate (19.2e6 for pi 2) to calculate it. Looking through pwm.c I see that PRIO.PWM uses the 500MHz PLLD clock as source - is there a difference in PLLD between pi1 and 2 that might account for the 1.25X period length difference?

Replying to my own post, I know, but I changed the clock divisor in ppm.c from 50 to 45, and re-installed. 49.38 Hz, and pulse timing is much more accurate. To do this yourself, look in pwm.c for:
clk_reg[PWMCLCK_DIV]=0x5A000000 | (50<<12); //set pwm div to 50, giving 10 MHz
and change the 50 to 45.
I think the problem is the line above that:
clk_reg[PWM_CLCK_CNTL] = 0x5A000006; // source = PLLD (500 MHZ)
and the constant referred to has changed between Pi versions, so that is selecting a different frequency than previously. The better fix would be to change the 0x5A000006 to actually select 500Mhz, but where that info is, I don't know.

I've heard about PIGPIO, but have not tested it yet.

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

Re: Using RPIO for controling dc motor speed

Tue Aug 23, 2016 6:29 am

From the command line

sudo pigpiod
pigs s 4 2000

will start 2000µs 50Hz pulses on (Broadcom) GPIO 4.

http://abyz.co.uk/rpi/pigpio/pigs.html#S/SERVO

Return to “Python”