def setup(self):
# Initialize class variables
self.max_val = [-1, -1, -1, -1, -1, -1]
self.min_val = [-1, -1, -1, -1, -1, -1]
self.start_time = -1
# Initialize value array to all negative values, which should never appear
# as an actual result
self.value = [-1.0, -1.0, -1.0, -1.0, -1.0, -1.0]
# A dictionary mapping each channel to the index it's value is located in
# the value array
self.sensor_indices = {29: 5, 36: 4, 37: 3, 31: 2, 32: 1, 33: 0}
self.updated = False
# For GPIO.BOARD
self.sensor_inputs = [33, 32, 31, 37, 36, 29] # Sensors from left to right
# Set the mode to GPIO.BOARD
GPIO.setmode(GPIO.BOARD)
python类setmode()的实例源码
def __init__(self, left_pin1, left_pin2, right_pin1, right_pin2, leftpwm_pin, rightpwm_pin):
io.setmode(io.BCM)
# Constant values
self.PWM_MAX = 100
# Here we configure the GPIO settings for the left and right motors spinning direction.
# It defines the four GPIO pins used as input on the L298 H-Bridge to set the motor mode (forward, reverse and stopp).
self.leftmotor_in1_pin = left_pin1
self.leftmotor_in2_pin = left_pin2
self.rightmotor_in1_pin = right_pin1
self.rightmotor_in2_pin = right_pin2
self.leftmotorpwm_pin = leftpwm_pin
self.rightmotorpwm_pin = rightpwm_pin
self.SetupGPIO()
self.leftmotorpwm = io.PWM(self.leftmotorpwm_pin,100)
self.rightmotorpwm = io.PWM(self.rightmotorpwm_pin,100)
self.InitPWM()
# Disable warning from GPIO
io.setwarnings(False)
def __init__(self, left_pin1, left_pin2, right_pin1, right_pin2, leftpwm_pin, rightpwm_pin):
io.setmode(io.BCM)
# Constant values
self.PWM_MAX = 100
# Here we configure the GPIO settings for the left and right motors spinning direction.
# It defines the four GPIO pins used as input on the L298 H-Bridge to set the motor mode (forward, reverse and stopp).
self.leftmotor_in1_pin = left_pin1
self.leftmotor_in2_pin = left_pin2
self.rightmotor_in1_pin = right_pin1
self.rightmotor_in2_pin = right_pin2
self.leftmotorpwm_pin = leftpwm_pin
self.rightmotorpwm_pin = rightpwm_pin
self.SetupGPIO()
self.leftmotorpwm = io.PWM(self.leftmotorpwm_pin,100)
self.rightmotorpwm = io.PWM(self.rightmotorpwm_pin,100)
self.InitPWM()
# Disable warning from GPIO
io.setwarnings(False)
def __init__(self, dout=4, pd_sck=18, gain=128, readBits=24, offset=-96096, scale=925):
self.PD_SCK = pd_sck
self.DOUT = dout
self.readBits = readBits
self.twosComplementOffset = 1 << readBits
self.twosComplementCheck = self.twosComplementOffset >> 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.PD_SCK, GPIO.OUT)
GPIO.setup(self.DOUT, GPIO.IN)
self.GAIN = 0
self.set_offset(offset)
self.set_scale(scale)
self.lastVal = 0
self.set_gain(gain)
def __init__(self):
self.led_gpio_red = 17 # red
self.led_gpio_green = 27 # green
self.led_gpio_blue = 22 # blue
self.r = 0.0
self.g = 0.0
self.b = 0.0
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.led_gpio_red, GPIO.OUT)
GPIO.setup(self.led_gpio_green, GPIO.OUT)
GPIO.setup(self.led_gpio_blue, GPIO.OUT)
GPIO.output(self.led_gpio_red, True)
GPIO.output(self.led_gpio_green, True)
GPIO.output(self.led_gpio_blue, True)
self.pwm_red = GPIO.PWM(self.led_gpio_red, 100)
self.pwm_green = GPIO.PWM(self.led_gpio_green, 100)
self.pwm_blue = GPIO.PWM(self.led_gpio_blue, 100)
self.pwm_red.start(0)
self.pwm_green.start(0)
self.pwm_blue.start(0)
def __init__(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(self.MotorFront1, GPIO.OUT)
GPIO.setup(self.MotorFront2, GPIO.OUT)
GPIO.setup(self.MotorFront, GPIO.OUT)
GPIO.output(self.MotorFront, 0)
GPIO.setup(self.MotorBack1, GPIO.OUT)
GPIO.setup(self.MotorBack2, GPIO.OUT)
GPIO.setup(self.MotorBack, GPIO.OUT)
GPIO.output(self.MotorBack, 0)
self.BackPWM = GPIO.PWM(self.MotorBack,100)
self.BackPWM.start(0)
self.BackPWM.ChangeDutyCycle(0)
self.direction = 0
def rotate_motor(duration,sleeptime):
import RPi.GPIO as GPIO
#import time
servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 500) # GPIO 18 als PWM mit 50Hz
p.start(2.5) # Initialisierung
try:
while duration > 0:
p.ChangeDutyCycle(1)
print 'motor running'+str(duration)
time.sleep(sleeptime)
duration -= 1
p.stop()
GPIO.cleanup()
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
def rotate_motor(duration,sleeptime):
import RPi.GPIO as GPIO
#import time
servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 500) # GPIO 18 als PWM mit 50Hz
p.start(2.5) # Initialisierung
try:
while duration > 0:
p.ChangeDutyCycle(1)
print 'motor running'+str(duration)
time.sleep(sleeptime)
duration -= 1
p.stop()
GPIO.cleanup()
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
def __initGPIOs(self):
#setup GPIO using Board numbering (pins, not GPIOs)
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
#setup defined pins and event_detectors or outputs and initial states (initial is always 0, low)
for pin in pins_in_use:
if pins_in_use[pin][0] == GPIO.IN:
if pin == 5:
GPIO.setup(pin, pins_in_use[pin][0], pull_up_down=GPIO.PUD_UP)
else:
GPIO.setup(pin, pins_in_use[pin][0])
GPIO.add_event_detect(pin, pins_in_use[pin][1], callback=self.shoutItOut, bouncetime=100)
self.gpio_states.update({pin: 1})
elif pins_in_use[pin][0] == GPIO.OUT:
GPIO.setup(pin, pins_in_use[pin][0], initial=0)
def __init__(self,pinA,pinB,button,callback, parent):
self.pinA = pinA
self.pinB = pinB
self.button = button
self.callback = callback
self.parent = parent
if self.pinA is not None and self.pinB is not None:
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(self.pinA, GPIO.IN)
GPIO.setup(self.pinB, GPIO.IN)
GPIO.add_event_detect(self.pinA, GPIO.FALLING,
callback=self.switch_event)
GPIO.add_event_detect(self.pinB, GPIO.FALLING,
callback=self.switch_event)
if self.button is not None:
GPIO.setup(self.button, GPIO.IN)
GPIO.add_event_detect(self.button, GPIO.BOTH,
callback=self.button_event, bouncetime=200)
return
# Call back routine called by switch events
def __init__(self, no, dir=IN):
self.gpio_no = no
print("GPIO ########## self.gpio_no, dir={}, {}".format(self.gpio_no, dir))
GPIO.setmode(GPIO.BCM)
#GPIO.setmode(GPIO.BOARD)
if dir==Pin.IN:
GPIO.setup(self.gpio_no, GPIO.IN)
print("GPIO >>>>>>>>>>>>>>>>>>>>>>>>>>> IN")
else:
GPIO.setup(self.gpio_no, GPIO.OUT)
print("GPIO <<<<<<<<<<<<<<<<<<<<<<<<<< OUT")
def setup_GPIO():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(35, GPIO.IN)
def setup_GPIO():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
def startup(self): # funciton that starts th GPIO board and pin required
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.channel, GPIO.OUT)
self.pwm = GPIO.PWM(self.channel, self.frequency)
self.pwm.start(self.frequency)
def setmodeboard(self):
if self.importlib is not None:
GPIO.setmode(GPIO.BOARD)
def setmodebcm(self):
if self.importlib is not None:
GPIO.setmode(GPIO.BCM)
def reset_led(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for gpio in self.gpios:
gpio = int(gpio)
GPIO.setup(gpio, GPIO.OUT)
GPIO.output(gpio, GPIO.HIGH)
def __init__(self):
gpio.setmode(gpio.BOARD)
# set up and init
self.trigger = 11
self.echo = 13
gpio.setup(self.trigger, gpio.OUT)
gpio.setup(self.echo, gpio.IN)
self.pulse_start = 0.
self.pulse_end = 0.
self.speed_of_sound = 343 * 100
self.car_length = 1
def __init__(self):
super().__init__()
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def _GPIO_Power_Regist(pins):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup( pins , GPIO.OUT)
return