def driveForward(seconds): GPIO.output(MOTOR_A_DIR, GPIO.LOW) GPIO.output(MOTOR_B_DIR, GPIO.HIGH) MOTOR_A_PWM.start(dc_A) MOTOR_B_PWM.start(dc_B) time.sleep(seconds) return #use BOARD pin numbering GPIO.setmode(GPIO.BOARD) #Motor pins MOTOR_A_PWM = 3 MOTOR_A_DIR = 5 MOTOR_B_PWM = 8 MOTOR_B_DIR = 10 dc_a = 50 dc_b = 50 GPIO.setup(engineLeft1, GPIO.OUT) GPIO.setup(engineLeft2, GPIO.OUT) GPIO.setup(engineRight1, GPIO.OUT) GPIO.setup(engineRight2, GPIO.OUT) MOTOR_A_PWM = GPIO.PWM(MOTOR_A_PWM,500) MOTOR_B_PWM = GPIO.PWM(MOTOR_B_PWM,500) pwmright1 = GPIO.PWM(engineRight1,10000) pwmright2 = GPIO.PWM(engineRight2,10000) driveForward(5) GPIO.cleanup
def setting(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(16, GPIO.OUT) GPIO.output(16, GPIO.LOW) GPIO.setup(20, GPIO.OUT) GPIO.output(20, GPIO.LOW) GPIO.setup(21, GPIO.OUT) GPIO.output(21, GPIO.LOW) pwmR = GPIO.PWM(20, 50) pwmG = GPIO.PWM(16, 50) pwmB = GPIO.PWM(21, 50)
def setup(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(Buzzer, GPIO.OUT) # Set pins' mode is output global Buzz # Assign a global variable to replace GPIO.PWM Buzz = GPIO.PWM(Buzzer, 440) # 440 is initial frequency. Buzz.start(50) # Start Buzzer pin with 50% duty ration
def setPin_B(self): GPIO.setup(self.IN3, GPIO.OUT) GPIO.setup(self.IN4, GPIO.OUT) GPIO.setup(self.ENB, GPIO.OUT) pwm = GPIO.PWM(self.ENB, 100) pwm.start(0) return pwm
def setPin_A(self): GPIO.setup(self.IN1, GPIO.OUT) GPIO.setup(self.IN2, GPIO.OUT) GPIO.setup(self.ENA, GPIO.OUT) pwm = GPIO.PWM(self.ENA, 100) pwm.start(0) return pwm
def led_effect_thread(): global trigger_led_effect GPIO.setup(config.LED_PIN, GPIO.OUT) # Set led_pin mode is output GPIO.output(config.LED_PIN, GPIO.LOW) # Set led_pin to low(0V) p = GPIO.PWM(config.LED_PIN, 1000) # set frequency to 1kHz p.start(0) # Duty Cycle = 0 while True: time.sleep(constant.LED_UPDATE_DELAY) while trigger_led_effect == constant.START_COMMAND: for dc in range(0, 101, 4): # Increase duty cycle: 0~100 p.ChangeDutyCycle(dc) # Change duty cycle time.sleep(0.05) time.sleep(1) for dc in range(100, -1, -4): # Decrease duty cycle: 100~0 p.ChangeDutyCycle(dc) time.sleep(0.05) time.sleep(1)
#!/usr/bin/env python import rospy from pam.msg import Gripper_mode from motor_current_drive import * import RPI.GPIO as GPIO Enable_pin = 3 Direction_pin = 4 PWM_pin = 12 GPIO.setmode(GPIO.BCM) GPIO.setup(Enable_pin, GPIO.OUT) GPIO.setup(Direction_pin, GPIO.OUT) GPIO.setup(PWM_pin, GPIO.OUT) p = GPIO.PWM(PWM_pin, 40) # channel=12 frequency=50Hz Direction = 1 GPIO.output(Direction_pin, Direction) p.start(0) def ON(): Enable = 1 GPIO.output(Enable_pin, Enable) time.sleep(2) dc = 40 p.ChangeDutyCycle(dc) time.sleep(2)
def tc1_handler: TC_GetStatus(TC0, 1); if AUX1 == 1: #If vehicle is e-stopped, reset all of the counters and errors counter_L = 0 lastcounter_L = 0 e_L = 0 e_sum_L = 0 MOTOR_L=0 counter_R = 0 lastcounter_R = 0 e_R = 0 e_sum_R = 0 MOTOR_R=0 break #Calculate Left RPM Dcounter_L = counter_L - lastcounter_L RPM_L = Dcounter_L * 3.0/ 4 # 500hz / 400 clicks per second / 50 gear ratio * 60 seconds lastcounter_L = counter_L #Calculate Right RPM Dcounter_R = counter_R - lastcounter_R RPM_R = Dcounter_R * 3.0/ 4 # 500hz / 400 clicks per second / 50 gear ratio * 60 seconds lastcounter_R = counter_R #PI Controller Left #The integral saturation filters makes sure our error sum doesn't go off to infinity if our wheels get stuck on something. e_L = MOTOR_L - RPM_L e_sum_L = e_sum_L + e_L if e_sum_L > 5000: e_sum_L = 5000 # Integral Saturation Filter if e_sum_L < -5000: e_sum_L = -5000 RPM_controller_L = Kp * e_L + Ki * e_sum_L if RPM_controller_L > 0: PWM_controller_L = (0.0099 * RPM_controller_L*RPM_controller_L - 0.1735*RPM_controller_L)*(7.2/12) else: PWM_controller_L = (-0.0099 * RPM_controller_L*RPM_controller_L - 0.1735*RPM_controller_L)*(7.2/12) #PI Controller Right e_R = MOTOR_R - RPM_R e_sum_R = e_sum_R + e_R if e_sum_R > 5000: e_sum_R = 5000 # Integral Saturation Filter if e_sum_R < -5000: e_sum_R = -5000 RPM_controller_R = Kp * e_R + Ki * e_sum_R if RPM_controller_R > 0: PWM_controller_R = (0.0099 * RPM_controller_R*RPM_controller_R - 0.1735*RPM_controller_R)*(7.2/12) else: PWM_controller_R = (-0.0099 * RPM_controller_R*RPM_controller_R - 0.1735*RPM_controller_R)*(7.2/12) #Motor Input Saturation Filter #PWM signals range from 0 to 255 so we saturate our motor controller output to accomodate that. #We account for the negative PWM values using the if/else cases below. #range is from 0 to 100 for Python's duty cycle if PWM_controller_L > 255: PWM_controller_L = 100 if PWM_controller_L < -255: PWM_controller_L = -100 if -255 <= PWM_controller_L <= 255 PWM_controller_L = (PWM_controller_L/255)*100 if PWM_controller_R > 255: PWM_controller_R = 100 if PWM_controller_R < -255: PWM_controller_R = 100 if -255 <= PWM_controller_R <= 255 PWM_controller_R = (PWM_controller_R/255)*100 #Left Motor Commands #If desired motor speed (from the Pi) is given as a zero, we are going to bypass the PI controller. #The PI controller will constantly try to apply current to the motor if we don't do this which will #1) waste power and 2) make an annoying high-pitch noise. #https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/ p1 =GPIO.PWM(channel(10), frequency) # change channel(10) to the correct pin p2 =GPIO.PWM(channel(3), frequency) p3 =GPIO.PWM(channel(9), frequency) p4 =GPIO.PWM(channel(5), frequency) if MOTOR_L == 0: p1.start(0) p2.start(0) else: if PWM_controller_L > 0: p1.start(0) p2.start(abs(PWM_controller_L)) elif PWM_controller_L == 0: p1.start(0) p2.start(0) elif PWM_controller_L < 0: p2.start(0) p1.start(abs(PWM_controller_L)) #Right Motor Commands if MOTOR_R == 0: p3.start(0) p4.start(0) else: if PWM_controller_R > 0: p3.start(0) p4.start(abs(PWM_controller_R)) elif PWM_controller_R == 0: p3.start(0) p4.start(0) elif PWM_controller_R < 0: p4.start(0) p3.start(abs(PWM_controller_R)) print(time.ctime()) threading.Timer(.004, tc1_handler).start()
import ThunderBorg import time port = 8004 ip_address = "192.168.0.1" #IP Address of PC control_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) control_socket.bind(('0.0.0.0', port)) print("waiting on port:", port) #Number the RPI IO pins using BCM GPIO.setmode(GPIO.BCM) #Set the first pin as an output GPIO.setup(18,GPIO.OUT) #Create a PWM instance sampler = GPIO.PWM(1,50) #Need to set the board address of each Thunderborg separately to addresses e.g. 10 11 # Board #1, address 10, two left motors, Motor1: Front, Motor2: Rear TB1 = ThunderBorg.ThunderBorg() TB1.i2cAddress = 10 TB1.Init() # Board #2, address 11, two right motors, Motor1: Front, Motor2: Rear TB2 = ThunderBorg.ThunderBorg() TB2.i2cAddress = 11 TB2.Init() while True:
MotorPWMList = [ArmPWMTopR, ArmPWMTopL, ArmPWMBotoomR, ArmPWMBotoomL] MotorDirectionList = [ ArmDirectionTopR, ArmDirectionTopL, ArmDirectionBotoomR, ArmDirectionBotoomL ] Direction = bool #Judge the direction print("please input Rotate speed") RotateSpeed = int(input()) GPIO.setup(MotorPWMList, GPIO.OUT) GPIO.setup(MotorDirectionList, GPIO.OUT) GPIO.SetWarnings(False) GPIO.PWM(MotorPWMList, 100) GPIO.output(MotorDirectionList, Direction) MotorPWMList.start(0) def HundMove(MotorPWMName, UseBottam): BottanName = UseBottam while True: MotorPWMName.ChangeDutyCycle(RotateSpeed) if (code == BottanName("code") and value == BottanName["value"]): print("Stop") break
def ON(power, pins): for i in range(len(pins) - 1): GPIO.PWM(pins[i], power)
def __init__(self, pin): self._pin = pin GPIO.setup(self._pin, GPIO.OUTPUT) self.pwm = GPIO.PWM(self._pin, 50) # 50Hz = 20ms period self.enabled = False
import cv2 import numpy as np import RPI.GPIO as GPIO from time import sleep servoPin = 12 SERVO_MAX_DUTY = 12 SERVO_MIN_DUTY = 3 GPIO.setmode(GPIO.BOARD) GPIO.setup(servoPin, GPIO.OUT) servo = GPIO.PWM(servoPin, 50) servo.start(0) def setServoPos(degree): if degree > 180: degree = 180 duty = SERVO_MIN_DUTY + (degree * (SERVO_MAX_DUTY - SERVO_MIN_DUTY) / 180.0) print("Degree: {} to {}(Duty)".format(degree, duty)) servo.ChangeDutyCycle(duty) thresh = 25 a, b, c = None, None, None
#! usr/bin/env python try: import RPI.GPIO as GPIO except ImportError: print('\nusing simulated GPIO \n') from sim_RPI import * # from gpiozero import Motor import time p = GPIO.PWM(1, 1000) p.start(25) p.ChangeDutyCycle(100) GPIO.setmode(GPIO.BCM) class Testbed(): def __init__(self): self.cone_pul = self.cone_dir = self.reel_pul = self.reel_dir = self.table_pwm = self.table_in1 = self.table_in2 = self.table_en = self.limit_pin =
#!/usr/bin/env python import RPI.GPIO as GPIO import time import signal import atexit atexit.register(GPIO.cleanup) servopin=23 GPIO.setmode(GPIO.BCM) GPIO.setup(servopin,GPIO.OUT,initial=False) p=GPIO.PWM(servopin,50) p.start(0) time.sleep(2) while(True): for i in range(0,360,10): p.ChangeDutyCycle(12.5-5*i/360) time.sleep(1) for i in range(0,360,10): p.ChangeCutyCycle(7.5-5*i/360) time.sleep(1)
h2oPin = 14 # BCM Pin 14, Board Pin 8 pumpPin = 21 # BCM Pin 21, Board Pin 40 ledPin = 15 # BCM valvePin1 = 4 valvePin2 = 17 # Pin Setup: GPIO.setmode(GPIO.BCM) GPIO.setup(h2oPin, GPIO.IN) GPIO.setup(ledPin, GPIO.OUT) GPIO.setup(pumpPin, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(valvePin1, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(valvePin2, GPIO.OUT, initial=GPIO.LOW) # PWM Initialization (Pin, Frequency): pumpPWM = GPIO.PWM(pumpPin, 25) ledPWM = GPIO.PWM(ledPin, 100) ledPWM.start(0) pumpPWM.start(0) # Sensing water_sense = 0 # Initial Water Sensor Value: False w_time_hr = 8 # Set Scheduler to Start at 8 AM watering_complete = False # initial value, may not be req'd running = True # Pump Actions def start_pump(): open_valve()
cap = cv2.VideoCapture(0) start = time.time() count = 1 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(16, GPIO.OUT) GPIO.output(16, GPIO.LOW) GPIO.setup(20, GPIO.OUT) GPIO.output(20, GPIO.LOW) GPIO.setup(21, GPIO.OUT) GPIO.output(21, GPIO.LOW) pwmR = GPIO.PWM(20, 50) pwmG = GPIO.PWM(16, 50) pwmB = GPIO.PWM(21, 50) while (1): ret, frame = cap.read() frame = cv2.resize(frame, (320, 320), fx=1, fy=1, interpolation=cv2.INTER_AREA) cv2.imshow("frame", frame) timeLapsed = time.time() - start if timeLapsed > count:
# The python script import RPI.GPIO as GPIO # import RPi Library GPIO.setmode(GPIO.BOARD) servo=11 GPIO.setup(servo,GPIO.OUT) pwm=GPIO.PWM(11,50) #set GPIO pin 11 to 50 Hz”frequency” pwm.start(5) for i in range (0,20): Position=input(“where do you want the servo? 0 - 108 ”) degree = 1./18.*(Position)+2 pwm.ChangeDutyCycle(degree) pwm.stop() #stop the pulse width modulations GPIO.cleanup() #to clear everything up
def __init__ (self): print ("\nWelcome to PiFunk!\n") self.data = [] print (sys.argv) try: os.system ("sudo apt-get install i2c-tools") os.system ("sudo apt-get install python-smbus") os.system ("sudo adduser pi i2c") os.system ("sudo i2cdetect -y 0") except: os.system ("sudo i2cdetect -y 1") try: os.system ("sudo modprobe w1-gpio") ## rpi 1-2 except: os.system ("sudo rmmod w1-gpio") ## rpi 3/4 base_dir = "/sys/bus/w1/devices/" device_folder = glob.glob (base_dir + "28*") [0] device_file = device_folder + "/w1_slave" cpid = os.fork () if not cpid: os._exit (0) os.waitpid (cpid, 0) pullup = 0 DEBUG = 1 LOGGER = 1 gpio_pin = 4 GPIO.initialize () GPIO.setwarnings (False) GPIO.setmode (GPIO.BCM) GPIO.setmode (GPIO.BOARD) GPIO.setup (4, GPIO.OUT) ## or 11 if used output = GPIO.output (4, TRUE) sensor_data = (gpio_pin, GPIO.PINS.GND, GPIO.PINS.RXD, GPIO.PINS.TXD) pi_pwm = GPIO.PWM (4, freq) pi_pwm.start (0) while True: for Duty in range (0, 60, 1) pi_pwm.ChangeDutyCycle (Duty) ##------------------------------------------------------------------------------ def soundfile (filename): filename = char (input ("\nEnter filename (*.wav): ")) if filename != 0: print ("\nFilename is: " + filename + " \n") return filename else: print ("\nNo custom filename specified! using standard file sound.wav !! \n") filename = char ("sound.wav") return filename def frequency (freq): freq = float (input ("\nEnter frequency (1 kHz-1000 MHz) with . as decimal (5 digit accuracy): ")) for samplerate in range (0.000001, 1000) if freq > 0: print ("\nFrequency is: " + freq " MHz \n") return freq else: freq = float (446.00000) print ("\nFrequency must be > 0 !! Using 446.00000 MHz instead! \n") return freq def sampler (samplerate): samplerate = int (input ("\nEnter samplerate (22050/44100/48000 kHz): ")) if samplerate > 0: print ("\nSamplerate is: " + samplerate + "\n") return samplerate else: samplerate = int (22050) print ("\nNo custom samplerate specified or negative! Using standard 22050 kHz!! \n") return samplerate def channels (channels): channels = int (input ("\nEnter number of channels (1 mono or 2 stereo): ")) if channels 1 || 2: print ("\nChannels: " + channels + " \n") return channels else:
try: import RPI.GPIO as gpio #really dumb gpioActive = True #set pins rCh = 20 bCh = 21 gCh = 22 #set mode gpio.setup(rCh, gpio.OUT) gpio.setup(gCh, gpio.OUT) gpio.setup(bCh, gpio.OUT) #set frequency of pwm pwmFreq = 255 #define pwm for pins redCh = gpio.PWM(rCh, pwmFreq) greenCh = gpio.PWM(gCh, pwmFreq) blueCh = gpio.PWM(bCh, pwmFreq) except: print('could not find gpio library') gpioActive = False try: import pyaudio from sense_hat import SenseHat sh = SenseHat() sh.clear hat = True gpioActive = False r, g, b = 0, 0, 0