def red_light():
p = GPIO.PWM(R, 300)
p.start(100)
pwms.append(p)
while True:
p.ChangeDutyCycle(min(random.randint(75, 100) * math.pow(intensity + 0.1, 0.75), 100) if intensity > 0 else 0)
rand_flicker_sleep()
python类PWM的实例源码
def motor2(mag): # If needed, use on/off motor instead of PWM
GPIO.output(MOTORPIN, True)
time.sleep(int(mag))
GPIO.output(MOTORPIN, False)
return
def __init__(self, pin_fw, pin_bw):
self._invert = False
self.pin_fw = pin_fw
self.pin_bw = pin_bw
self._speed = 0
GPIO.setup(self.pin_fw, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.pin_bw, GPIO.OUT, initial=GPIO.LOW)
self.pwm_fw = GPIO.PWM(self.pin_fw, 100)
self.pwm_fw.start(0)
self.pwm_bw = GPIO.PWM(self.pin_bw, 100)
self.pwm_bw.start(0)
def init():
global p, q, a, b
# Initialise the PWM device using the default address
#use physical pin numbering
GPIO.setmode(GPIO.BOARD)
#print GPIO.RPI_REVISION
#set up digital line detectors as inputs
GPIO.setup(lineRight, GPIO.IN) # Right line sensor
GPIO.setup(lineLeft, GPIO.IN) # Left line sensor
#Set up IR obstacle sensors as inputs
GPIO.setup(irFL, GPIO.IN) # Left obstacle sensor
GPIO.setup(irFR, GPIO.IN) # Right obstacle sensor
#use pwm on inputs so motors don't go too fast
GPIO.setup(L1, GPIO.OUT)
p = GPIO.PWM(L1, 20)
p.start(0)
GPIO.setup(L2, GPIO.OUT)
q = GPIO.PWM(L2, 20)
q.start(0)
GPIO.setup(R1, GPIO.OUT)
a = GPIO.PWM(R1, 20)
a.start(0)
GPIO.setup(R2, GPIO.OUT)
b = GPIO.PWM(R2, 20)
b.start(0)
startServos()
# cleanup(). Sets all motors off and sets GPIO to standard values
def setupPins(self):
""" Set Raspberry Pi GPIO pins to the right mode.
"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.enapin, GPIO.OUT)
GPIO.setup(self.enbpin, GPIO.OUT)
GPIO.setup(self.inapin, GPIO.OUT)
GPIO.setup(self.inbpin, GPIO.OUT)
GPIO.setup(self.pwmpin, GPIO.OUT)
GPIO.setup(self.fanpin, GPIO.OUT)
GPIO.setup(self.diapin, GPIO.IN)
GPIO.setup(self.dibpin, GPIO.IN)
self.p = GPIO.PWM(self.pwmpin, self.freq)
def startPWM(self):
""" Start the PWM output.
"""
GPIO.output(self.fanpin, True)
GPIO.output(self.enapin, True)
GPIO.output(self.enbpin, True)
GPIO.output(self.inapin, True)
GPIO.output(self.inbpin, False)
# Setup PWM and DMA channel 0
self.p.start(0)
self.dutycycle = 0
def stopPWM(self):
""" Stop the PWM output.
"""
# Stop PWM
self.p.stop()
GPIO.output(self.enapin, False)
GPIO.output(self.enbpin, False)
GPIO.output(self.inapin, False)
GPIO.output(self.inbpin, False)
GPIO.output(self.fanpin, False)
def changeDutyCycle(self, duty):
""" Set the PWM duty cycle in percent.
@param float duty: PWM duty cycle 0 < duty < 100
"""
self.dutycycle = 0
if duty >= 0:
GPIO.output(self.inapin, True)
GPIO.output(self.inbpin, False)
else:
GPIO.output(self.inapin, False)
GPIO.output(self.inbpin, True)
self.p.ChangeDutyCycle(abs(duty))
def __init__(self, gpio):
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio, GPIO.OUT)
self.pwm = GPIO.PWM(gpio, 1)
def __init__(self, channel, init_state):
import RPi.GPIO as GPIO
GPIO.setup(channel, GPIO.OUT)
self._pwm = GPIO.PWM(channel, init_state.get('Frequency'))
self._pwm.start(init_state.get('DutyCycle'))
def InitPWM(self):
# Here we configure the GPIO settings for the left and right motors spinning speed.
# It defines the two GPIO pins used as input on the L298 H-Bridge to set the motor speed with a PWM signal.
self.leftmotorpwm.start(0)
self.leftmotorpwm.ChangeDutyCycle(0)
self.rightmotorpwm.start(0)
self.rightmotorpwm.ChangeDutyCycle(0)
def InitPWM(self):
# Here we configure the GPIO settings for the left and right motors spinning speed.
# It defines the two GPIO pins used as input on the L298 H-Bridge to set the motor speed with a PWM signal.
self.leftmotorpwm.start(0)
self.leftmotorpwm.ChangeDutyCycle(0)
self.rightmotorpwm.start(0)
self.rightmotorpwm.ChangeDutyCycle(0)
def get_new_color(pin, frequency):
GPIO.setup(pin, GPIO.OUT)
return GPIO.PWM(pin, frequency)
def get_new_led(pin, frequency):
GPIO.setup(pin, GPIO.OUT)
return GPIO.PWM(pin, frequency)
def get_new_led(pin, frequency):
GPIO.setup(pin, GPIO.OUT)
return GPIO.PWM(pin, frequency)
#execution
def __init__(self, motorPin):
self.motorPin = motorPin
# Setup GPIO
# Broadcom SOCKET channel is used as standard
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.motorPin, GPIO.OUT)
# Setup PWM with 100KHz
self.motor = GPIO.PWM(self.motorPin, 100)
# Start PWM
self.motor.start(0)
# Start on speed zero
self.setZero()
def __init__(self, servoPin):
self.servoPin = servoPin
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.servoPin, GPIO.OUT)
# Setup PWM with 100KHz
self.servo = GPIO.PWM(self.servoPin, 100)
# Start PWM with dutycycle of 0%
self.servo.start(0)
self.zeroPosition() # Set position to zero at start
def initGPIO():
global p, q, a, b
#use physical pin numbering
GPIO.setmode(GPIO.BOARD)
#set up digital line detectors as inputs
GPIO.setup(lineRight, GPIO.IN) # Right line sensor
GPIO.setup(lineLeft, GPIO.IN) # Left line sensor
#use pwm on inputs so motors don't go too fast
GPIO.setup(L1, GPIO.OUT)
p = GPIO.PWM(L1, 20)
p.start(0)
GPIO.setup(L2, GPIO.OUT)
q = GPIO.PWM(L2, 20)
q.start(0)
GPIO.setup(R1, GPIO.OUT)
a = GPIO.PWM(R1, 20)
a.start(0)
GPIO.setup(R2, GPIO.OUT)
b = GPIO.PWM(R2, 20)
b.start(0)
threadC = threading.Thread(target=wheelCount)
threadC.start()
# spinLeft(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
def setAllLEDs (red, green, blue):
for i in range(4):
setLED(i, red, green, blue)
# End of RGB LED Functions
#======================================================================
#======================================================================
# White LED Functions
# (Pi2Go-Lite only)
#
# LsetLED(LED, value): Sets the LED specified to OFF == 0 or ON == 1
# TODO: take value from 0 to 100 and use as percentage PWM value
def initOS():
global backlight
os.environ['SDL_FBDEV'] = '/dev/fb1'
os.environ['SDL_MOUSEDEV'] = '/dev/input/touchscreen'
os.environ['SDL_MOUSEDRV'] = 'TSLIB'
os.environ['SDL_VIDEODRIVER'] = 'fbcon'
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
backlight = GPIO.PWM(18, 1024)
backlight.start(100)