dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

Motor Control - Speed and Duration

Sun Mar 17, 2013 6:36 pm

I was just wondering how to run the motor controller at a constant level for a duration of time.

I'm coding in python using RPi.GPIO, I'm coding in python.

Any help would be great :)

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Motor Control - Speed and Duration

Sun Mar 17, 2013 9:38 pm

You'll need to use software PWM then and you can find an example in motor-rg.py

All you need to do is change it so it won't loop through a range of values, but stick to one value.

Have a go and report back if you need any further help. :D
Alex Eames RasPi.TV, RasP.iO

dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

Re: Motor Control - Speed and Duration

Sat Apr 13, 2013 9:21 pm

I'm not sure how to do this still, I am not familiar with PWM I just want to be able to have a process that runs the motor at a constant level. Would anyone be able to help?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Motor Control - Speed and Duration

Sun Apr 14, 2013 8:09 am

dawson345 wrote:I'm not sure how to do this still, I am not familiar with PWM I just want to be able to have a process that runs the motor at a constant level. Would anyone be able to help?
You're in luck, Ben has added soft PWM to RPi.GPIO. That should make things a LOT easier.

Here's an article I wrote about it a week or so ago...
http://raspi.tv/2013/rpi-gpio-0-5-2a-no ... -to-use-it

...it's only the basics. I plan a followup soon with leds and motors in. :D
Alex Eames RasPi.TV, RasP.iO

dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

Re: Motor Control - Speed and Duration

Thu Apr 18, 2013 12:48 am

Doe's this help me with the gertboard motor controller?

I'm approaching my deadline now, if you could supply the lines of code to have the motor run at a constant level then I would really appreciate it :)

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Motor Control - Speed and Duration

Thu Apr 18, 2013 7:14 am

dawson345 wrote:Doe's this help me with the gertboard motor controller?
Well yes it does. You've chosen RPi.GPIO and that's the only way to get speed control in RPi.GPIO
dawson345 wrote:I'm approaching my deadline now, if you could supply the lines of code to have the motor run at a constant level then I would really appreciate it :)
If you only want on off control just wire two ports to motor A and motor B.
When they're both high or both low the motor will not spin. When A is high and B is low it spin one way, when B is high and A is low it will spin the other way.

Use time.sleep(wait_period) to determine the length of time.

I've used the same ports as the motor program. Reading the appropriate section of the manual should help you understand how this works.

So your script will look something like this...

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
ports = [18,17]             # define which ports to be pulsed (using a list)

for port_num in ports:                       # set the ports up for output
    GPIO.setup(port_num, GPIO.OUT)           # set up GPIO output channel
    print ("setting up GPIO port:", port_num)
    GPIO.output(port_num, False)             # set both ports to OFF

wait_time = 5              # define your wait time in seconds

GPIO.output(17, True)      # switch port 17 on

time.sleep(wait_time)      #wait for a period of time

GPIO.output(17, False)     # switch port 17 off

GPIO.cleanup()              # reset all ports used by this program on finishing
You're quite likely to come back to me and say you don't want it running full speed. That's when you will need to go back to the page I pointed you at before and modify it so that you're using PWM instead of on off.

If you want to have a go at that and post your code if you get into issues, I'll be happy to help, but I'm not going to do it all for you. :D
Alex Eames RasPi.TV, RasP.iO

dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

Re: Motor Control - Speed and Duration

Thu Apr 18, 2013 12:27 pm

Thankyou very much, I'm new to electronics so it's been quite difficult to comprehend the relationship from the code to physical elements. Non the less it's been exciting and I have only blown one fuse so far :P

I'll have a good go now and hopefully just post thankyou very much in a while :)

Return to “HATs and other add-ons”