def reverse(t=wheel_pulse/2):
gpio.output(reverse_left, gpio.HIGH)
gpio.output(reverse_right, gpio.HIGH)
sleep(t)
gpio.output(reverse_left, gpio.LOW)
gpio.output(reverse_right, gpio.LOW)
##########################################################################
# cleanup
##########################################################################
python类LOW的实例源码
testWheels_Individual_4WD.py 文件源码
项目:RaspberryPi-Robot
作者: timestocome
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def reverse_left(t):
gpio.output(15, gpio.HIGH)
gpio.output(36, gpio.HIGH)
sleep(t)
gpio.output(15, gpio.LOW)
gpio.output(36, gpio.LOW)
# test
def _GPIO_Power_Set(pins, on):
if on==1:
GPIO.output( pins, GPIO.HIGH)
else :
GPIO.output( pins, GPIO.LOW )
return
#=====================================
def startwalking():
# Make the buzzer buzz on and off, half a second of
# sound followed by half a second of silence
# GPIO.output(PinRedPedestrian, GPIO.LOW)
# GPIO.output(PinGreenPedestrian, GPIO.HIGH)
iCount = 1
while iCount <= 4:
GPIO.output(PinBuzzer, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(PinBuzzer, GPIO.LOW)
time.sleep(0.5)
iCount += 1
# Turn the buzzer off and wait for 2 seconds
# (If you have a second green 'pedestrian' LED, make it flash on and
# off for the two seconds)
def flashingambergreen():
# Remember all code in the function is indented
GPIO.output(PinRed, GPIO.LOW)
iCount = 1
while iCount <= 6:
GPIO.output(PinAmber, GPIO.HIGH)
# GPIO.output(PinGreenPedestrian, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(PinAmber, GPIO.LOW)
# GPIO.output(PinGreenPedestrian, GPIO.LOW)
time.sleep(0.5)
iCount += 1
# Flash the amber for one more second
# (Turn the green 'pedestrian' LED off and the red on)
def send_gpio_order(param):
"""
GPIO????
type in(GPIO.IN) out(GPIO.OUT)
value 1 GPIO.HIGH 0 GPIO.LOW
:param param:
:return:
"""
print param
channel, type, value = param
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("????")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
if type == 'in':
GPIO.setup(channel, GPIO.IN)
GPIO.input(channel, value)
else:
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, value)
def Start(self):
#SCL
# ______
# | |______
#SDA
# ___
# | |_________
GPIO.setup(self.SDA, GPIO.OUT) #cnfigure SDA as output
GPIO.output(self.SDA, GPIO.HIGH)
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(1)
GPIO.output(self.SDA, GPIO.LOW)
self.tick(1)
GPIO.output(self.SCL, GPIO.LOW)
self.tick(2)
def ReadAck(self):
GPIO.setup(self.SDA, GPIO.IN)
readbuffer =0
for i in range(8):
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
readbuffer |= (GPIO.input(self.SDA)<< 7) >> i
GPIO.output(self.SCL, GPIO.LOW)
self.tick(2)
GPIO.setup(self.SDA, GPIO.OUT)
GPIO.output(self.SDA, GPIO.LOW)
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
GPIO.output(self.SCL, GPIO.LOW)
GPIO.output(self.SDA, GPIO.LOW)
self.tick(2)
return readbuffer
def ReadNack(self):
GPIO.setup(self.SDA, GPIO.IN)
readbuffer =0
for i in range(8):
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
readbuffer |= (GPIO.input(self.SDA)<< 7) >> i
GPIO.output(self.SCL, GPIO.LOW)
self.tick(2)
GPIO.setup(self.SDA, GPIO.OUT)
GPIO.output(self.SDA, GPIO.HIGH)
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
GPIO.output(self.SCL, GPIO.LOW)
GPIO.output(self.SDA, GPIO.LOW)
self.tick(2)
return readbuffer
def __init__(self, channel, invert=False):
from navio import adafruit_pwm_servo_driver as pwm
from navio import util
import RPi.GPIO as GPIO
util.check_apm()
#Navio+ requires the GPIO line 27 to be set to low
#before the PCA9685 is accesed
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
GPIO.output(27,GPIO.LOW)
#GPIO.cleanup()
self.frequency = 60
self.channel = channel
self.invert = invert
self.pwm = pwm.PWM()
self.pwm.setPWMFreq(self.frequency)
def __init__(self, ready):
Thread.__init__(self)
self.ready = ready
self.picked_up = False
self.on = True
self._ringer1 = 11
self._ringer2 = 13
self._button = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(self._ringer1, GPIO.OUT)
GPIO.output(self._ringer1, GPIO.LOW)
GPIO.setup(self._ringer2, GPIO.OUT)
GPIO.output(self._ringer2, GPIO.LOW)
GPIO.setup(self._button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self._last_state = 0
self._last_ts = time.time()
self._last_check = time.time()
self._ring = 1
self.max_ring_cnt = 2
def _pin_pulse(pin, initial_state=GPIO.LOW, pulse_width=PULSE_WIDTH_SEC):
# type: (int, bool, Union[int, float]) -> None
"""Sends one pulse to the specified pin.
The pin will be returned to the specified initial state after the pulse
Args:
pin: The pin to pulse.
initial_state: The negation of this will be used as the pulse.
Defaults to GPIO.LOW.
pulse_width: how long, in seconds, to pulse the pin.
Defaults to PULSE_WIDTH_SEC.
"""
GPIO.output(pin, not initial_state)
try:
time.sleep(pulse_width)
finally:
GPIO.output(pin, initial_state)
def beep(self):
if self.state is False:
cbpi.app.logger.error("BUZZER not working")
return
def play(sound):
try:
for i in sound:
if (isinstance(i, str)):
if i == "H" and self.beep_level == "HIGH":
GPIO.output(int(self.gpio), GPIO.HIGH)
elif i == "H" and self.beep_level != "HIGH":
GPIO.output(int(self.gpio), GPIO.LOW)
elif i == "L" and self.beep_level == "HIGH":
GPIO.output(int(self.gpio), GPIO.LOW)
else:
GPIO.output(int(self.gpio), GPIO.HIGH)
else:
time.sleep(i)
except Exception as e:
pass
start_new_thread(play, (self.sound,))
def on_message(mosq, obj, msg):
"""
Handle incoming messages
"""
if msg.topic == MONITOR_REFRESH:
refresh()
return
topicparts = msg.topic.split("/")
pin = int(topicparts[len(topicparts) - 1])
try:
value = int(float(msg.payload))
except ValueError:
value = 0
if pin not in GPIO_OUTPUT_PINS:
GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH)
GPIO_OUTPUT_PINS.append(pin)
if topicparts[2] == 'in':
if value == 1:
GPIO.output(pin, GPIO.LOW)
else:
GPIO.output(pin, GPIO.HIGH)
# End of MQTT callbacks
def read(self):
'''Reads 32 bits of the SPI bus & stores as an integer in self.data.'''
bytesin = 0
# Select the chip
GPIO.output(self.cs_pin, GPIO.LOW)
# Read in 32 bits
for i in range(32):
GPIO.output(self.clock_pin, GPIO.LOW)
bytesin = bytesin << 1
if (GPIO.input(self.data_pin)):
bytesin = bytesin | 1
GPIO.output(self.clock_pin, GPIO.HIGH)
# Unselect the chip
GPIO.output(self.cs_pin, GPIO.HIGH)
# Save data
self.data = bytesin
def read(self):
'''Reads 16 bits of the SPI bus & stores as an integer in self.data.'''
bytesin = 0
# Select the chip
GPIO.output(self.cs_pin, GPIO.LOW)
# Read in 16 bits
for i in range(16):
GPIO.output(self.clock_pin, GPIO.LOW)
time.sleep(0.001)
bytesin = bytesin << 1
if (GPIO.input(self.data_pin)):
bytesin = bytesin | 1
GPIO.output(self.clock_pin, GPIO.HIGH)
time.sleep(0.001)
# Unselect the chip
GPIO.output(self.cs_pin, GPIO.HIGH)
# Save data
self.data = bytesin
def init_gpio(self, rpi_settings):
"""
Initialize GPIO pin to a default value. Leds are off by default
Mute button is set as an input
:param rpi_settings: RpiSettings object
"""
# All led are off by default
if self.rpi_settings.pin_led_muted:
GPIO.setup(rpi_settings.pin_led_muted, GPIO.OUT, initial=GPIO.LOW)
if self.rpi_settings.pin_led_started:
GPIO.setup(rpi_settings.pin_led_started, GPIO.OUT, initial=GPIO.LOW)
if self.rpi_settings.pin_led_listening:
GPIO.setup(rpi_settings.pin_led_listening, GPIO.OUT, initial=GPIO.LOW)
if self.rpi_settings.pin_led_talking:
GPIO.setup(rpi_settings.pin_led_talking, GPIO.OUT, initial=GPIO.LOW)
# MUTE button
if self.rpi_settings.pin_mute_button:
GPIO.setup(rpi_settings.pin_mute_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(rpi_settings.pin_mute_button, GPIO.FALLING,
callback=self.switch_kalliope_mute_led,
bouncetime=500)
def __init__(self, clk_pin=11, data_pin=13, ce_pin=15):
# init GPIO
# no warnings
GPIO.setwarnings(False)
# use safer pin number (avoid GPIO renumber on each Pi release)
GPIO.setmode(GPIO.BOARD)
# set GPIO pins
self._clk_pin = clk_pin
self._data_pin = data_pin
self._ce_pin = ce_pin
# CLK and CE (sometime call RST) pin are always output
GPIO.setup(self._clk_pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self._ce_pin, GPIO.OUT, initial=GPIO.LOW)
# turn off WP (write protect)
self._start_tx()
self._w_byte(0x8e)
self._w_byte(0x00)
self._end_tx()
# charge mode is disabled
self._start_tx()
self._w_byte(0x90)
self._w_byte(0x00)
self._end_tx()
def _r_byte(self):
"""
Read byte from the chip.
:return: byte value
:rtype: int
"""
# data pin is now input (pull-down resistor embedded in chip)
GPIO.setup(self._data_pin, GPIO.IN)
# clock the byte from chip
byte = 0
for i in range(8):
# make a high pulse on CLK pin
GPIO.output(self._clk_pin, GPIO.HIGH)
time.sleep(self.CLK_DELAY)
GPIO.output(self._clk_pin, GPIO.LOW)
time.sleep(self.CLK_DELAY)
# chip out data on clk falling edge: store current bit into byte
bit = GPIO.input(self._data_pin)
byte |= ((2 ** i) * bit)
# return byte value
return byte
def _w_byte(self, byte):
"""
Write byte to the chip.
:param byte: byte value
:type byte: int
"""
# data pin is now output
GPIO.setup(self._data_pin, GPIO.OUT)
# clock the byte to chip
for _ in range(8):
GPIO.output(self._clk_pin, GPIO.LOW)
time.sleep(self.CLK_DELAY)
# chip read data on clk rising edge
GPIO.output(self._data_pin, byte & 0x01)
byte >>= 1
GPIO.output(self._clk_pin, GPIO.HIGH)
time.sleep(self.CLK_DELAY)
def stopwatch(hexUID, name):
start = time.time()
time.clock()
elapsed = 0
while elapsed < maxRaceTime and GPIO.input(laserPin) != GPIO.LOW:
elapsed = time.time() - start
millis = int(round(elapsed * 1000))
hours, millis = divmod(millis, 3600000)
minutes, millis = divmod(millis, 60000)
seconds, millis = divmod(millis, 1000)
s = "%02i:%02i:%03i" % (minutes, seconds, millis)
with canvas(disp) as draw:
draw.text((0, 0), "Timer:", font=defaultFont, fill=255)
draw.text((0, 20), s, font=timerFont, fill=255)
color(100, 0, 0, 0)
publishRaceTimes(hexUID, name, elapsed)
# Function to plubish the race times for the current user.
# It takes the hexUID, name and completionTime
# and publishes them to the AWS IoT topic.
def powerGood(self, state):
if state == gpio.LOW:
self.atxGood = True
print "ATX Good"
yield self.sr.shiftOut(reduce(lambda x, y: x | y, self.green))
yield utils.wait(200)
yield self.sr.shiftOut(0)
yield utils.wait(100)
yield self.sr.shiftOut(reduce(lambda x, y: x | y, self.green))
yield utils.wait(200)
yield self.setLEDStates()
# Start node 1
if self.autoOn:
self.autoOn = False
yield self.nodeOn(0)
else:
print "ATX bad"
self.atxGood = False
def greenButton(self, state):
if state == gpio.LOW:
o = reduce(lambda x, y: x | y, self.green)
yield self.sr.shiftOut(o)
self.autoOn = True
else:
# Button released
yield self.setLEDStates()
if self.states[0] == 2:
self.autoOn = False
# Node 1 is active, so start all the others
for i in range(4):
yield self.nodeOn(i)
if i < 3:
yield utils.wait(1000)
else:
self.atxOn.on()
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
start = time.time()
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
end = time.time()
# print end - start
# return measurement
return str(end - start)
# Connects the socket
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
start = time.time()
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
end = time.time()
# print end - start
# return measurement
return str(end - start)
# Main program loop
def light_sense():
count = 0
#Output on the pin for
GPIO.setup(pin_to_circuit, GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_to_circuit, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_to_circuit) == GPIO.LOW):
count += 1
if (count > 3000):
led5_on()
return count
else:
led5_off()
return count
def beepOn():
global DisableBeep
if DisableBeep == False:
GPIO.output(BeepPin, GPIO.LOW)
def ledOn():
GPIO.output(LedPin, GPIO.LOW)
def __init__(self, name = ''):
self.importlib = GPIO
self.logger = com_logger.Logger(name)
# self.setwarnings(False)
self.IN = GPIO.IN if GPIO is not None else None
self.OUT = GPIO.OUT if GPIO is not None else None
self.LOW = GPIO.LOW if GPIO is not None else None
self.HIGH = GPIO.HIGH if GPIO is not None else None
self.PUD_UP = GPIO.PUD_UP if GPIO is not None else None
self.PUD_DOWN = GPIO.PUD_DOWN if GPIO is not None else None
self.RISING = GPIO.RISING if GPIO is not None else None
def enable_tally(self, enable):
if enable:
GPIO.output(self.gpio_red, GPIO.LOW)
else:
GPIO.output(self.gpio_red, GPIO.HIGH)