The examples tell me (a script kiddie who can modify almost ANY script to do what I need it to do) absolutely nothing.
Basically, all I am trying to do is run 2 servos for x amount of time (in seconds). That's IT. Not difficult. I had this working using GPIO, but needed external power to prevent my Pi from browning out when the servos kicked on. I'll be damned if I can figure this out with the provided Servo_Example.py code.
All this min/max pulse out of 4096 jargon... Who are you people talking to? 4096 WHAT?
Sorry...I get extremely frustrated when example code of ANY kind is poorly commented. Examples are to teach usage...
So, back to the point on hand:
I have two servos connected to the HAT - 0 and 1. What I'd like to be able to do is run both servos, simultaneously, for x seconds. After x seconds, the servos stop and the program ends.
The servos: Hi-Tec HS-645MG (continuous rotation)
Once I know how to make them run for time, stop, then quit, I can add in anything else I need. Can someone help?
PWM/HAT for Pi - Example is lacking!
Moderators: adafruit_support_bill, adafruit
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: PWM/HAT for Pi - Example is lacking!
Servos are controlled by a pulse train where each pulse is between 1ms and 2ms wide and pulses arrive at about 60Hz.
The PCA9685 which does all the work on the Servo Hat has 16 independent 12-bit PWM output channels.
You set the pulse frequency with:
and set PWM duty cycles for each channel with values between:
To set the pulse width for a given channel, you use the channel number in:
If you want a constant speed, that's it. The PCA9685 is a set-it-and-forget-it device. When you reach the end of your timing period, use pwm.setPWM() to take each channel back to a default value.
WRT keeping track of time from within Python, that's more of a question about the standard libraries.
The PCA9685 which does all the work on the Servo Hat has 16 independent 12-bit PWM output channels.
You set the pulse frequency with:
Code: Select all
pwm.setPWMFreq( 60 )
Code: Select all
servoMin = 150 # Min pulse length out of 4096
servoMax = 600 # Max pulse length out of 4096
Code: Select all
pwm.setPWM( 0, 0, servoMax ) // channel 0 to maximum
pwm.setPWM( 1, 0, servoMin ) // channel 1 to minimum
etc.
WRT keeping track of time from within Python, that's more of a question about the standard libraries.
- MikeFalcor
- Posts: 2
- Joined: Thu Apr 02, 2015 5:27 pm
Re: PWM/HAT for Pi - Example is lacking!
Thank you! You gave me just what I needed.
When you simply run the example python script from the git, the servo spins...and spins...and just keeps going! CTRL+C has no effect on it at all! Why this was not included in the example, I have no idea.
I will go ahead and post my code, below for all to see. Do note the commenting...
What is it? It's a dual-servo cat feeder! In crontab, root runs the script at 7:30a and 7:30p. The script determines what time it is and feeds the cats dinner/breakfast or a snack if the time doesn't match an expectation.
I created the feeder and figured I would get good use out of a RasPi to control it. Now I can go on vacation without having to worry about the cats being fed! :)
When you simply run the example python script from the git, the servo spins...and spins...and just keeps going! CTRL+C has no effect on it at all! Why this was not included in the example, I have no idea.
I will go ahead and post my code, below for all to see. Do note the commenting...
Code: Select all
#!/usr/bin/python
# Clear the screen.
import os
os.system("clear")
# Configure and register Growl notifications.
import gntp.notifier
growl = gntp.notifier.GrowlNotifier(
applicationName = "Cat Feeder",
applicationIcon = "https://www.iconfinder.com/icons/175341/download/png/128",
notifications = ["Fed","Error"],
defaultNotifications = ["Fed"],
hostname = "X.X.X.X", # Defaults to localhost
password = "{HIDDEN}" # Defaults to a blank password
)
growl.register()
print "\nCat feeder script now running...\n\n" # Self explanatory...
# Import the driver from the library.
from Driver import PWM
import time
# We need to be able to tell time for the cats.
import datetime
now = datetime.datetime.now()
# Initialise the PWM device.
pwm = PWM(0x40)
# Set the min and max pulse lengths.
servoMin = 1500
servoMax = 1900
# Define the pulses for the program.
def setServoPulse(channel, pulse):
pulseLength = 1000000
pulseLength /= 60
print "%d us per period" % pulseLength
pulseLength /= 4096
print "%d us per bit" % pulseLength
pulse *= 1000
pulse /= pulseLength
pwm.setPWM(channel, 0, pulse)
pwm.setPWMFreq(60)
if time.strftime("%H") == "19" and time.strftime("%M") == "30":
# Start the servos moving!
print "Servos are on the move!\n\n"
pwm.setPWM(0, 0, servoMin)
pwm.setPWM(1, 0, servoMin)
# Time to sleep while the servos spin. Time in seconds.
time.sleep(10)
# Stop the servos.
print "Stopping servos.\n\n"
pwm.setPWM(0, 0, 0)
pwm.setPWM(1, 0, 0)
# Send Growl notification.
print "Sending notification...\n\n"
growl.notify(
noteType = "Fed",
title = "Cat Feeder",
description = "The cats were just fed dinner!",
icon = "https://www.iconfinder.com/icons/175341/download/png/128",
sticky = False,
priority = 1,
)
elif time.strftime("%H") == "07" and time.strftime("%M") == "30":
print "Servos are on the move!\n"
pwm.setPWM(0, 0, servoMin)
pwm.setPWM(1, 0, servoMin)
# Time to sleep while the servos spin. Time in seconds.
time.sleep(10)
# Stop the servos.
print "Stopping servos.\n"
pwm.setPWM(0, 0, 0)
pwm.setPWM(1, 0, 0)
# Send Growl notification.
print "Sending notification...\n\n"
growl.notify(
noteType = "Fed",
title = "Cat Feeder",
description = "The cats were just fed breakfast!",
icon = "https://www.iconfinder.com/icons/175341/download/png/128",
sticky = False,
priority = 1,
)
else:
print "Servos are on the move!\n"
pwm.setPWM(0, 0, servoMin)
pwm.setPWM(1, 0, servoMin)
# Time to sleep while the servos spin. Time in seconds.
time.sleep(3)
# Stop the servos.
print "Stopping servos.\n"
pwm.setPWM(0, 0, 0)
pwm.setPWM(1, 0, 0)
# Send Growl notification.
print "Sending notification...\n\n"
growl.notify(
noteType = "Fed",
title = "Cat Feeder",
description = "The cats were just fed a snack!",
icon = "https://www.iconfinder.com/icons/175341/download/png/128", #you can optionally define an image icon to appear with the notification
sticky = False,
priority = 1,
)
quit()
I created the feeder and figured I would get good use out of a RasPi to control it. Now I can go on vacation without having to worry about the cats being fed! :)
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: PWM/HAT for Pi - Example is lacking!
Interrupt handling in Python isn't beginner-friendly. All interrupts raise exceptions, and frankly we'd rather not have to try and explain:MikeFalcor wrote:CTRL+C has no effect on it at all! Why this was not included in the example, I have no idea.
Code: Select all
try:
foo()
except [ exception class name ]:
blah()
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/