simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Software PWM loop challenge

Tue Sep 18, 2012 9:37 pm

I've got this running in a loop

Code: Select all

if (motor != 0):
            GPIO.output(13, 1)
            print "on",motor
            time.sleep(motor/1000.0)
            GPIO.output(13, 0)
            print "off"
            time.sleep((100.0-motor)/1000.0)
which is controlling a little motor fine but I need to extend it to handle 6 outputs instead of just the 1
so I've changed motor to motor[0,0,0,0,0,0] and I've another list that indexes the pins - pinarray[11,12,13,15,16,18]

I'm looking for clever code to convert the single instance above to handle the 6 values in motor[]

Obviously it needs to switch each pin on for the correct amount of time and each one off for (near enough) the correct amount of time

Anyone got a super algorithim ? :)
regards
Simon
PS its for my Scratch GPIO Handler
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
Grumpy Mike
Posts: 973
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Software PWM loop challenge

Wed Sep 19, 2012 3:46 am


simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Software PWM loop challenge

Wed Sep 19, 2012 6:36 am

@grump mike - Thanks very much - it'd mean I'd have to switch from @croston's GPIO lib but looks like the code for the job.:)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Software PWM loop challenge

Sun Oct 07, 2012 8:09 pm

Since I can't get wiringpi to work in python :( I've come up with this little bit of code to handle 2 pwm outputs (which is all I need to control 2 dc motors)

Code: Select all

motorA = 0;
motorB = 0;
motor_timing = array('i',[0,0,100])
motor_order = array('i',[0,1])
while True:
#
#other code to get integer values for motorA and motorB
#
        if ((motorA > 0) or (motorB > 0)):
            if (motorA > motorB):
                motor_order[0]=0
                motor_order[1]=1
                #time before motorB goes off
                motor_timing[0]=motorB
                motor_timing[1]=motorA-motorB
                motor_timing[2]=100-motorA
            else:
                motor_order[0]=1
                motor_order[1]=0
                #time before motorA goes off
                motor_timing[0]=motorA
                motor_timing[1]=motorB-motorA
                motor_timing[2]=100-motorB


            #print 't0 t1 t2', motor_timing[0], motor_timing[1] , motor_timing[2]                
            #print 'pin: ' , GPIO_PINS[0]
            GPIO.output(GPIO_PINS[motor_order[0]], 1)
            if (motor_timing[0] > 0 ):
                GPIO.output(GPIO_PINS[motor_order[1]], 1)
            time.sleep(motor_timing[0]/10000.0)
            GPIO.output(GPIO_PIN_OUTPUT[motor_order[1]], 0)
            time.sleep(motor_timing[1]/10000.0)
            GPIO.output(GPIO_PIN_OUTPUT[motor_order[0]], 0)
            time.sleep(motor_timing[2]/10000.0)      
The code might be of use to others :)
(And of course it could always be improved)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Software PWM loop challenge

Mon Oct 08, 2012 12:45 pm

Whats the problem with installing wiringpi? Using the 'pip' method worked for me on a fresh raspbian install recently.

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Software PWM loop challenge

Mon Oct 08, 2012 3:56 pm

Whats the problem with installing wiringpi?
No problem in installing wiringpi :)

Great problem in getting it to ackle in Python :(
(Gordon doesn't supply/support wiringpi in Python :( )

What's "pip" ?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

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

Re: Software PWM loop challenge

Mon Oct 08, 2012 8:12 pm

I did it on a fresh raspbian yesterday...

Code: Select all

sudo apt-get update

sudo apt-get install python-dev python-pip
sudo pip install wiringpi
pip handles the compile and install for you, python-dev is a required library.

I haven't tried to use wiringpi for python yet mind, but it installed ok. :lol:
Alex Eames RasPi.TV, RasP.iO

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Software PWM loop challenge

Mon Oct 08, 2012 8:18 pm

Wiringpi works fine for me - as I,m sure i,ve said before, it seems to be the only python compatible method of using hardware pwm, so is driving my
Nokia Pi LCD's backlight correctly.

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Software PWM loop challenge

Mon Oct 08, 2012 8:25 pm

I haven't tried to use wiringpi for python yet mind, but it installed ok
Mine installed Ok as well (different method mind) - just doesn't work :)

I'm not going to spend anymore time on trying to get wiringpi to work as with my simple PWM method here - I've got the outcome I need to control 2 motors :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Return to “Python”