def __init__(self, pin, freq=50): """ may be centralize GPIO settings in another file later """ IO.setmode(IO.BOARD) IO.setup(pin, IO.OUT) self.pin = pin # GPIO pin self.freq = freq # PWM signal frequency, refer to data sheet of your motor self.rest_time = 0.5 # seconds # set up PWM signal to control servo motor self.p = IO.PWN(self.pin, self.freq) # set to natural position p.start(0)
# Change the brightness quicker at the beginning of the # transition, then slowing near the end def getChangeAmt(current, target): return round(abs(current - target) / 10) + 1 # You're going to change the duty cycle to change the intensity of the light. currentBrightness = 0 # HIT my server and get current brightness... windowDataUrl = "http://myRaspberryPiServer/windowData" windowData = requests.get(windowDataUrl, timeout=10).json() # need some sort of way to still update the physical window pi = GPIO.PWN(pin, 0) targetBrightness = brightness # Brightness increasing if targetBrightness > currentBrightness: while currentBrightness <= targetBrightness: pi.ChangeDutyCycle(currentBrightness) amt = getChangeAmt(currentBrightness, targetBrightness) currentBrightness = currentBrightness + amt time.sleep(0.05) # Brightness decreasing elif targetBrightness < currentBrightness:
#Pulse Width Modulation import RPi.GPIO as GPIO import time '''PWN Initialization''' pwm_obj = GPIO.PWN(18, 400) #Mark pin for PWN #Second argument is frequency pwm_obj.start(100) #Start generating PWN signal #Argument is duty cycle, 0 to 100 '''PWN Control''' pwm_obj.ChangeDutyCycle(50) #Assign new duty cycle #PWN frequency is not accurate # -Off by over 50% at 10kHz '''Frequency Control''' #Cannot easily control frequency # -No tone() function as on Arduino #Need to do it manually while True: GPIO.output(18, True) time.sleep(0.5) GPIO.output(18, False) time.sleep(0.5) #1Hz frequency
import RPi.GPIO as GPIO import time import random GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) buzz_pin = 32 GPIO.setup(buzz_pin, GPIO.OUT) Buzz = GPIO.PWN(buzz_pin, 1000) frequencies = [220, 440, 880, 1760] n = random.randint(0, 3) Buzz.ChangeFrequency(frequencies[n]) Buzz.start(50) time.sleep(0.5) Buzz.stop()
import RPi.GPIO as GPIO import tkinter as tk GPIO.setmode(GPIO.BOARD) LED = 11 GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW) p = GPIO.PWN(LED, 100) root = tk.Tk() led_val = tk.DoubleVar() led_val.set(0) p.start(0) def change_duty(dc): p.ChangeDutyCycle(led_val.get()) # rootに表示するスライドバーを定義 s = tk.Scale(root, label='LED', orient='h', from_=0, to=100, variable=led_val, command=change_duty) s.pack()
# Update the joystick positions # Speed control (inverted) speed = -gamepad.axis(joystickSpeed) # Steering control (not inverted) steer = gamepad.axis(joystickSteering) finally: # Ensure the background thread is always terminated when we are done gamepad.disconnect() action = {"speed": speed, "steer": steer} return action GPIO.setup(18, GPIO.OUT) GPIO.setup(19, GPIO.OUT) throttle = GPIO.PWM(18, 1000) steering = GPIO.PWN(19, 1000) throttle.start(0) steering.start(50) def perform_actions(actions, context): throttle.ChangeDutyCycle(actions["speed"]) #map the steering values into 0-100 pwm values. pwm_steer = np.interp(actions["steer"], [-100, 100], [0, 100]) steering.ChangeDutyCycle(pwm_steer) print(f"TAKING ACTIONS: Throttle: {actions['speed']}\tSteer: {pwm_steer}") def get_frame(rState, wState, context): cam = context['camera'] ret, frame = cam.read()