Exemplo n.º 1
0
    if cpuTemp < CUTOFF_TEMP:
        setFanSpeed(0)
        fanOn = False
    elif cpuTemp >= MAX_TEMP:
        setFanSpeed(FAN_MAX)
        fanOn = True
    # If temperature is between MIN_TEMP and MAX_TEMP, fan speed is calculated by linear interpolation
    # If temperature is between CUTOFF_TEMP and MIN_TEMP, keap current fan speed
    elif cpuTemp > MIN_TEMP:
        fanSpeed = calculateFanSpeed(cpuTemp)
        setFanSpeed(fanSpeed)
        fanOn = True
    return ()


try:
    # Setup GPIO pin
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW)
    fan = GPIO.PWM(FAN_PIN, PWM_FREQ)
    setFanSpeed(FAN_OFF)
    # Handle fan speed every WAIT_TIME sec
    while True:
        handleFanSpeed2()
        time.sleep(WAIT_TIME)
except KeyboardInterrupt:
    print("Fan ctrl interrupted by CTRL+C")
    GPIO.cleanup()
    sys.exit()
Exemplo n.º 2
0
import RPi.GPIO as GPIO, time
import math

port = 36
directionPort = 32
GPIO.setmode(GPIO.BOARD)
GPIO.setup(port, GPIO.OUT)
GPIO.setup(directionPort, GPIO.OUT)

speed = 700

p = GPIO.PWM(port, speed)
p.start(50)


class Stepper:
    def __init__(self, diameterInCM):
        self.revsPerSecond = 3.21  # 0.65
        self.diameter = diameterInCM
        self.circ = math.pi * self.diameter
        self.p = GPIO.PWM(port, speed)
        print "Initialized: circumfrence is %f" % self.circ

    def goSpoolDistance(self, distance, direction):

        print "Calculating revs to go %f" % distance
        travelled = 0
        revs = 0
        currentDiameter = self.diameter
        while travelled < distance:
            travelled += math.pi * currentDiameter
Exemplo n.º 3
0
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time

pin = 12

GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)

p = GPIO.PWM(pin, 50)
p.start(2.5)

try:
    while True:
        print('*** Start *** ')
        p.ChangeDutyCycle(2.5)
        time.sleep(1)
        p.ChangeDutyCycle(7.5)
        time.sleep(1)
        p.ChangeDutyCycle(12.5)
        time.sleep(3)

except KeyboardInterrupt:
    p.stop()

GPIO.cleanup()
Exemplo n.º 4
0
#This isn't much, but this helps to define color codes for different colors!
import random
#Setting up the Pi...
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(00, GPIO.OUT)
GPIO.setup(01, GPIO.OUT)
GPIO.setup(04, GPIO.OUT)
GPIO.output(00, 1)
GPIO.output(01, 1)
GPIO.output(04, 1)
r = GPIO.PWM(00, 60)
g = GPIO.PWM(01, 60)
b = GPIO.PWM(04, 60)
r.start(0)
g.start(0)
b.start(0)
complete = 0

print("Hello! Just follow the prompts.")
print(
    "If you're looking for a random color, just hit enter throughout the prompts without entering a number."
)
try:
    while True:
        #	r.ChangeDutyCycle(0)
        #	g.ChangeDutyCycle(0)
        #	b.ChangeDutyCycle(0)
        if complete == 1:
Exemplo n.º 5
0
import RPi.GPIO as GPIO
from time import sleep
from math import pow
GPIO.setmode(GPIO.BOARD)
red = 12
yellow = 11
btn1 = 13
btn2 = 15
GPIO.setup(btn1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(btn2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(yellow, GPIO.OUT)
pwmR = GPIO.PWM(red, 100)
pwmY = GPIO.PWM(yellow, 100)
feq = 2
pwmR.start(feq)
pwmY.start(feq)
try:
    while (1):
        if GPIO.input(btn1) == 0:
            feq = pow(feq, 2)
            if feq > 100:
                print("You have reached max frequency")
                feq = 100
            pwmR.ChangeDutyCycle(feq)
            sleep(.1)
            pwmY.ChangeDutyCycle(feq)
            sleep(.1)
            print("frequency = ", feq)
        if GPIO.input(btn2) == 0:
            feq = pow(feq, .5)
Exemplo n.º 6
0
GPIO_TRIGGER = 23
GPIO_ECHO = 24
servo_pin1 = 18
servo_pin2 = 26
print("Ultrasonic Measurement")

GPIO.setup(servo_pin1, GPIO.OUT)
GPIO.setup(servo_pin2, GPIO.OUT)

GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

GPIO.output(GPIO_TRIGGER, False)
time.sleep(2)  # 원래 2

p1 = GPIO.PWM(servo_pin1, 50)
p2 = GPIO.PWM(servo_pin2, 50)
p1.start(0)  # 원래 2.5
p2.start(12.5)  # 원래 2.5

GPIO.add_event_detect(button_pin, GPIO.RISING, callback=button_callback)

try:
    while True:
        distance = measure_average()
        print("Distance : %.1f" % distance)
        time.sleep(1)  #

        if distance <= 10:  #초음파 센서와 거리
            print("angle : 1")
            p1.ChangeDutyCycle(12.5)
Exemplo n.º 7
0
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
pins = {'pin_R': 11, 'pin_G': 12, 'pin_B': 13}  # pins is a dict

GPIO.setmode(GPIO.BOARD)  # Numbers GPIOs by physical location
for i in pins:
    GPIO.setup(pins[i], GPIO.OUT)  # Set pins' mode is output
    GPIO.output(pins[i], GPIO.HIGH)  # Set pins to high(+3.3V) to off led

p_R = GPIO.PWM(pins['pin_R'], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 2000)
p_B = GPIO.PWM(pins['pin_B'], 5000)

p_R.start(0)  # Initial duty Cycle = 0(leds off)
p_G.start(0)
p_B.start(0)


def map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min


def setColor(col):  # For example : col = 0x112233
    R_val = (col & 0xFF0000) >> 16
    G_val = (col & 0x00FF00) >> 8
    B_val = (col & 0x0000FF) >> 0

    R_val = map(R_val, 0, 255, 0, 100)
hierarchy = [0, 0, 0, 0]
window = ['wcp1', 'wcp2', 'wcp3', 'wcp4']
window2 = ['wccp1', 'wccp2', 'wccp3', 'wccp4']
window3 = ['wcccp1', 'wcccp2', 'wcccp3', 'wcccp4']

servo1 = 15
servo2 = 14
deltadt = 0.1
dt1 = 4
dt2 = 6

rp.setmode(rp.BCM)
rp.setwarnings(False)
rp.setup(servo1, rp.OUT)
rp.setup(servo2, rp.OUT)
pwm1 = rp.PWM(servo1, 50)
pwm2 = rp.PWM(servo2, 50)
pwm1.start(2)
pwm2.start(7)

cv.namedWindow('frame', cv.WINDOW_FREERATIO)
usingPiCamera = True
vs = VideoStream(src=0,
                 usePiCamera=usingPiCamera,
                 resolution=(240, 240),
                 framerate=60).start()
sleep(0.2)
pwm1.stop()
pwm2.stop()
################################################################################################################################
while True:
Exemplo n.º 9
0
import RPi.GPIO as GPIO
import time
servoPIN1 = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN1, GPIO.OUT)
p1 = GPIO.PWM(servoPIN1, 50)
p1.start(5.0)
servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50)
p.start(5.0)
try:
    while True:
        p.ChangeDutyCycle(3.0 / 50 * 50)
        '''p1.ChangeDutyCycle(10.0)
    time.sleep(0.5)
    
    p1.ChangeDutyCycle(7.5)
    time.sleep(0.5)
    
    p1.ChangeDutyCycle(5.0)
    time.sleep(0.5)'''

        p1.ChangeDutyCycle(3.0 / 50 * 50)
        #time.sleep(1)

except KeyboardInterrupt:

    p1.stop()
    GPIO.cleanup()
my_pwm.ChangeFrequency(1000) to change the Hz
my_pwm.stop() to stop the signal, but kills the loop
'''

GPIO.setwarnings(False)  # Ignore warnings
GPIO.setmode(GPIO.BOARD)  # Use physical pin numbering

# - Set the clock:
GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)

# - Set the reward PINs:
GPIO.setup(35, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(37, GPIO.OUT, initial=GPIO.LOW)

# - Initiate the analog objects:
my_pwm_negative = GPIO.PWM(35, 1000)  # (pin, Hz)
my_pwm_positive = GPIO.PWM(37, 1000)  # (pin, Hz)

# - Set the outputs with initial to LOW:
GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(13, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(15, GPIO.OUT, initial=GPIO.LOW)

# - Set the inputs (pulled low):
GPIO.setup(36, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(38, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# - Input F vector:
input_F_vector = [0] * 3
Exemplo n.º 11
0
brailleState = [0, 0, 0, 0, 0, 0]
binInput = 0
lastBinInput = 0

task = 1
taskFile = ''
TaskDir = './Tasks'
taskCount = len([
    name for name in os.listdir(TaskDir)
    if os.path.isfile(os.path.join(TaskDir, name))
])

GPIO.setmode(GPIO.BOARD)
for pin in range(len(c.BRAILLE_WRITE_PAD)):
    GPIO.setup(c.BRAILLE_WRITE_PAD[pin], GPIO.OUT)
    WRITE_PINS.append(GPIO.PWM(c.BRAILLE_WRITE_PAD[pin], 40000))
    WRITE_PINS[pin].start(0)

#Global callbacks to call when buttons are pressed


def callback0(channel):
    global binInput
    global brailleState
    if brailleState[0] == 0:
        binInput += 1
        brailleState[0] = 1
    WRITE_PINS[0].ChangeDutyCycle(60)
    print(brailleState)

Exemplo n.º 12
0
def servo_CCW(ServoPIN):
    servo = GPIO.PWM(ServoPIN, 100)
    servo.start(18)
    time.sleep(spinDuration)
    servo.stop()
Exemplo n.º 13
0
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

PIN_R = 11

Red_Signal_Frequency = 2000  # The frequency of the digital signal
Red_Signal_Duty_Cycle = 50  # The duty cycle of the digital signal. This is the on-time

GPIO.setmode(GPIO.BOARD)  # Numbers GPIOs by physical location
GPIO.setup(PIN_R, GPIO.OUT)  # Set the R pin to mode is output
GPIO.output(PIN_R, GPIO.HIGH)  # Turn off the LED

PWM_R_Pin = GPIO.PWM(
    PIN_R, Red_Signal_Frequency
)  # Set the pin to a pulse width modulation digital signal with a set frequency

while True:
    try:  # runs until Ctrl+C interupts
        PWM_R_Pin.start(
            Red_Signal_Duty_Cycle
        )  # Start the pwm on the designated pin with a set duty cycle
    except KeyboardInterrupt:  # `runs when Ctrl+C interupts
        break

print("End of program")
GPIO.cleanup()  # this ensures a clean exit
Exemplo n.º 14
0
# Set GPIO to Broadcom system and set RGB Pin numbers
GPIO.setmode(GPIO.BCM)
red = 17
green = 18
blue = 27

# Set pins to output mode
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)

Freq = 100  #Hz

# Setup all the LED colors with an initial
# duty cycle of 0 which is off
RED = GPIO.PWM(red, Freq)
RED.start(0)
GREEN = GPIO.PWM(green, Freq)
GREEN.start(0)
BLUE = GPIO.PWM(blue, Freq)
BLUE.start(0)


# Define a simple function to turn on the LED colors
def color(R, G, B, on_time):
    # Color brightness range is 0-100%
    RED.ChangeDutyCycle(R)
    GREEN.ChangeDutyCycle(G)
    BLUE.ChangeDutyCycle(B)
    time.sleep(on_time)
Exemplo n.º 15
0
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12, GPIO.OUT)

p = GPIO.PWM(12, 50)
trig = 16
echo = 18
p.start(7.5)
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)

try:
    GPIO.output(trig, False)

    time.sleep(2)
    while True:
        distance = 0
        p.ChangeDutyCycle(2.7)  # turn towards 90 degree
        print("0 derece")
        GPIO.output(trig, True)

        time.sleep(0.00001)

        GPIO.output(trig, False)

        while GPIO.input(echo) == 0:
            start1 = time.time()
import RPi.GPIO as GPIO  # Import Raspberry Pi GPIO library
from time import sleep  # Import the sleep function from the time module

GPIO.setwarnings(False)  # Ignore warning for now
GPIO.setmode(GPIO.BOARD)  # Use physical pin numbering
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.IN)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
servoPIN = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50)

#while True:
if GPIO.input(11) == False and GPIO.input(12) == False:
    print "1"
    GPIO.output(11, GPIO.LOW)
    sleep(0.5)
    p.start(2.5)
    GPIO.output(12, GPIO.LOW)
    sleep(0.5)
    p.stop()

elif GPIO.input(11) == True and GPIO.input(12) == False:
    print "2"
    GPIO.output(11, GPIO.HIGH)
    sleep(0.5)
    p.start(2.5)
    GPIO.output(12, GPIO.LOW)
Exemplo n.º 17
0
    # Timeout (wird regelmäßig aufgerufen)
    def timeout(self):
        # Temperatur ermitteln und anzeigen
        with open(fname, 'rt') as f:
            temp = int(f.readline()) / 1000
        print(temp)
        self.tempLbl.setText('%.1f°C' % (temp))

        # Status von Taster ermitteln
        status = gpio.input(21)
        if status == 0:
            self.switchLbl.setText('gedrückt')
        else:
            self.switchLbl.setText('nicht gedrückt')


# GPIO-Setup
gpio.setmode(gpio.BOARD)  # Pin-Nummern des J8-Headers
gpio.setup(21, gpio.IN)  # Pin 21 zur Dateneingabe
gpio.setup(26, gpio.OUT)  # Pin 26 zur Datenausgabe
pwm = gpio.PWM(26, 1000)  # Frequenz: 1000 Hertz
pwm.start(0)  # Duty 0 (dunkel)
fname = '/sys/class/thermal/thermal_zone0/temp'

# Fenster öffnen, auf Programmende warten
app = QtWidgets.QApplication([])
win = MyWindow()
win.show()
app.exec_()
gpio.cleanup()
Exemplo n.º 18
0
GPIO.setup(18, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(enb, GPIO.OUT)
GPIO.setup(ena, GPIO.OUT)
GPIO.setup(10, GPIO.IN)
GPIO.setup(38, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(40, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(35, GPIO.OUT, initial=GPIO.HIGH)
GPIO.output(18, False)
GPIO.output(16, False)
GPIO.output(26, False)
GPIO.output(24, False)
GPIO.input(10)
p = GPIO.PWM(ena, 1000)
p2 = GPIO.PWM(enb, 1000)
p.start(22)
p2.start(32)

with picamera.PiCamera() as camera:
    camera.resolution = (180, 90)
    camera.capture("/home/pi/Desktop/colors.jpg")
print("Picture Taken")


def stop(os):
    os.system(myCmd)


while getrgb == True:
Exemplo n.º 19
0
 def setup(self):
     GPIO.setup(self._motor_pin, GPIO.OUT)
     GPIO.setup(self._forward_pin, GPIO.OUT)
     GPIO.setup(self._backwards_pin, GPIO.OUT)
     self._motor = GPIO.PWM(self._motor_pin, 100)
Exemplo n.º 20
0
import RPi.GPIO as GPIO
import time
import socket 
import select 
import sys 
from thread import *

GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(19,GPIO.OUT)
GPIO.output(18,GPIO.HIGH)
GPIO.output(13,GPIO.HIGH)
GPIO.output(19,GPIO.HIGH)
p = GPIO.PWM(17, 100)

"""The first argument AF_INET is the address domain of the 
socket. This is used when we have an Internet Domain with 
any two hosts The second argument is the type of socket. 
SOCK_STREAM means that data or characters are read in 
a continuous flow."""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 

# checks whether sufficient arguments have been provided 
if len(sys.argv) != 3: 
    print "Correct usage: script, IP address, port number"
    exit() 

# takes the first argument from command prompt as IP address 
Exemplo n.º 21
0
import numpy as np
#####################################

# Load Model:
model = load_model('Patate_dir.h5')
model_a = load_model('model-AugmentedDataset-anticipation.h5')
print("Models Loaded")

#init GPIO with BCM numberings
GPIO.setmode(GPIO.BCM)
#init every used pins
GPIO.setup(23, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)

#set controls
POW = GPIO.PWM(18, 50)
DIR = GPIO.PWM(23, 50)

# Video settings
camera = PiCamera()
camera.resolution = (160, 128)
camera.framerate = 60
camera.hflip = True
camera.vflip = True
rawCapture = PiRGBArray(camera, size=(160, 128))

# Starting loop
print("Ready ! (press Ctrl+C to start/stop)...")
try:
    while True:
        pass
Exemplo n.º 22
0
import RPi.GPIO as GPIO
import time
import signal
import atexit

atexit.register(GPIO.cleanup)

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 50)  #50HZ
p.start(0)
time.sleep(2)

p.ChangeDutyCycle(2.5)
time.sleep(1)

p.ChangeDutyCycle(6.25)
time.sleep(1)
p.ChangeDutyCycle(2.5)
Exemplo n.º 23
0
#!/usr/bin/python3

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.OUT)

p = GPIO.PWM(11,20)
p.start(0)

try:
    while True:
        p.ChangeDutyCycle(12.5)
        time.sleep(2)
        p.ChangeDutyCycle(2.5)
        time.sleep(2)
        p.ChangeDutyCycle(7.5)
        time.sleep(2)
except KeyboardInterupt:
    p.stop()
    GPIO.cleanup()
Exemplo n.º 24
0
Arquivo: SERVO.py Projeto: ahmetse/IOT
import paho.mqtt.client as mqtt
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)


def SetAngle(angle):
    duty = float(angle) / 18 + 2
    print(duty)
    GPIO.output(40, True)
    pwm.ChangeDutyCycle(duty)
    sleep(1)
    GPIO.output(40, False)
    pwm.ChangeDutyCycle(0)


def baglanti_saglandiginda(client, userdata, flags, rc):
    print("Baglandi, rc:" + str(rc))
    client.subscribe("fatihmehmetergin/feeds/servo-feed", 0)


def mesaj_geldiginde(client, userdata, msg):
    derece = msg.payload
    SetAngle(derece)


istemci = mqtt.Client()
Exemplo n.º 25
0
# pwm_frequency = 50
# duty_cycle = 6.2
# pause = 1
# rate = 0.1

# von 1ms
# 1/20 = 0.05
# 1/25 = 0.04

# Ruhe: 1.5ms
# 1/20 = 0.075
# 1/25 = 0.06

#bis 2ms
# 1/20 = 0.1
# 1/25 = 0.08
p = GPIO.PWM(servoPIN, pwm_frequency)  # GPIO 18 als PWM mit 50Hz
p.start(duty_cycle)  # Initialisierung
try:
    while True:
        print 'freq=' + str(pwm_frequency) + ' dc=' + str(
            duty_cycle) + ' wait=' + str(pause)
        p.ChangeDutyCycle(duty_cycle)
        time.sleep(pause)
        duty_cycle = duty_cycle + rate

except KeyboardInterrupt:
    pass
p.stop()
GPIO.cleanup()
Exemplo n.º 26
0
import cv2
import time
import Motores as motor
import Variaveis as var
import RPi.GPIO as GPIO
import Sensores as sensor
import Configuracoes as definir


GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

definir.configuracoes()

ctr_vel_motor_esq, ctr_vel_motor_dir = GPIO.PWM(var.pin_ENB, 500), GPIO.PWM(var.pin_ENA, 500)
ctr_vel_motor_esq.start(0)
ctr_vel_motor_dir.start(0)

time.sleep(1)


A0_ATIVADO, A1_ATIVADO, A3_ATIVADO = False, False, False

FAIXA_CONTENCAO = False

desligar = False


tempoCorrecao = 0
Exemplo n.º 27
0
 def __init__(self, diameterInCM):
     self.revsPerSecond = 3.21  # 0.65
     self.diameter = diameterInCM
     self.circ = math.pi * self.diameter
     self.p = GPIO.PWM(port, speed)
     print "Initialized: circumfrence is %f" % self.circ
Exemplo n.º 28
0
#Import modules
import RPi.GPIO as GPIO
import time

#Define variables
buttonPin = 17
ledPin = 18
ledOn = False
previous_input = 0


#Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(ledPin, GPIO.OUT)
pwm = GPIO.PWM(ledPin, 100)
currentPwm = 0
pwm.start(0)

#Loop
while (True):
	#get current input from the button
	input = GPIO.input(buttonPin)

	#if button is pressed check previous button state
	if ((not previous_input) and input):		
		#increase currentPwm with 10% with each button press
		# when CurrentPwm reaches 100%, reset it back to 0
		if currentPwm == 100:
			currentPwm = 0
		pwm.ChangeDutyCycle(currentPwm)		
Exemplo n.º 29
0
Motor_R2_Pin = 10
Motor_L1_Pin = 27
Motor_L2_Pin = 22
Enable_1 = 17
Enable_2 = 11

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Motor_R1_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_R2_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_L1_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_L2_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Enable_1, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Enable_2, GPIO.OUT, initial=GPIO.LOW)

pwm_e1 = GPIO.PWM(Enable_1, 100)
pwm_e2 = GPIO.PWM(Enable_2, 100)
pwm_e1.start(0)
pwm_e2.start(0)


def brake():  # Use reverse rotation to hold the brakes.
    pwm_e1.ChangeDutyCycle(10)
    pwm_e2.ChangeDutyCycle(10)
    GPIO.output(Motor_R1_Pin, False)
    GPIO.output(Motor_R2_Pin, True)
    GPIO.output(Motor_L1_Pin, False)
    GPIO.output(Motor_L2_Pin, True)
    time.sleep(t)
    GPIO.output(Motor_R1_Pin, False)
    GPIO.output(Motor_R2_Pin, False)
Exemplo n.º 30
0
# Set the GPIO Pin mode to be Output
GPIO.setup(pinMotorAForwards, GPIO.OUT)
GPIO.setup(pinMotorABackwards, GPIO.OUT)
GPIO.setup(pinMotorBForwards, GPIO.OUT)
GPIO.setup(pinMotorBBackwards, GPIO.OUT)
# Set pins as output and input
GPIO.setup(pinTrigger, GPIO.OUT)  # Trigger
GPIO.setup(pinEcho, GPIO.IN)  # Echo

# Distance Variables
HowNear = 15.0
ReverseTime = 0.5
TurnTime = 0.75

# Set the GPIO to software PWM at 'Frequency' Hertz
pwmMotorAForwards = GPIO.PWM(pinMotorAForwards, Frequency)
pwmMotorABackwards = GPIO.PWM(pinMotorABackwards, Frequency)
pwmMotorBForwards = GPIO.PWM(pinMotorBForwards, Frequency)
pwmMotorBBackwards = GPIO.PWM(pinMotorBBackwards, Frequency)

# Start the software PWM with a duty cycle of 0 (i.e. not moving)
pwmMotorAForwards.start(Stop)
pwmMotorABackwards.start(Stop)
pwmMotorBForwards.start(Stop)
pwmMotorBBackwards.start(Stop)


# Turn all motors off
def StopMotors():
    pwmMotorAForwards.ChangeDutyCycle(Stop)
    pwmMotorABackwards.ChangeDutyCycle(Stop)