def readCompoundEye(): global irUpValue,irDownValue,irLeftValue,irRightValue,distance # <4> ledPin = 25 gpio.mode(ledPin, "out") # <5> gpio.write(ledPin, gpio.HIGH) #Wait for sensors to get ready time.sleep(0.05) # <6> irUpValue = mcp.readAnalog(0, 0) # <7> irDownValue = mcp.readAnalog(1, 0) irLeftValue = mcp.readAnalog(0, 1) irRightValue = mcp.readAnalog(1, 1) ambientLight = 0 gpio.write(ledPin, gpio.LOW) # <8> time.sleep(0.05) ambientLight = mcp.readAnalog(0, 0) # <8> irUpValue = irUpValue - ambientLight # <9> ambientLight = mcp.readAnalog(1, 0) # <10> irDownValue = irDownValue - ambientLight ambientLight = mcp.readAnalog(0, 1) irLeftValue = irLeftValue - ambientLight ambientLight = mcp.readAnalog(1, 1) irRightValue = irRightValue - ambientLight distance = (irUpValue+irDownValue+irLeftValue+irRightValue)/4 # <11>
def readCompoundEye(): global irUpValue, irDownValue, irLeftValue, irRightValue, distance # <4> ledPin = 25 gpio.mode(ledPin, "out") # <5> gpio.write(ledPin, gpio.HIGH) #Wait for sensors to get ready time.sleep(0.05) # <6> irUpValue = mcp.readAnalog(0, 0) # <7> irDownValue = mcp.readAnalog(1, 0) irLeftValue = mcp.readAnalog(0, 1) irRightValue = mcp.readAnalog(1, 1) ambientLight = 0 gpio.write(ledPin, gpio.LOW) # <8> time.sleep(0.05) ambientLight = mcp.readAnalog(0, 0) # <8> irUpValue = irUpValue - ambientLight # <9> ambientLight = mcp.readAnalog(1, 0) # <10> irDownValue = irDownValue - ambientLight ambientLight = mcp.readAnalog(0, 1) irLeftValue = irLeftValue - ambientLight ambientLight = mcp.readAnalog(1, 1) irRightValue = irRightValue - ambientLight distance = (irUpValue + irDownValue + irLeftValue + irRightValue) / 4 # <11>
def getKeyPress(): global lastReadTime foundKey = None if ((time.time() - lastReadTime) > bounceTime): # <4> #pulse columns and read pins for c in range(len(columns)): gpio.mode(columns[c], 'out') gpio.write(columns[c], gpio.LOW) # <5> for r in range(len(rows)): if gpio.read(rows[r]) == gpio.LOW: # <6> foundKey = keymap[r][c] # <7> gpio.write(columns[c], gpio.HIGH) gpio.mode(columns[c], 'in') # <8> if not foundKey == None: break # <9> lastReadTime = time.time() return foundKey
def readDistanceCm(): sigPin = 22 v = (331.5 + 0.6 * 20) gpio.interruptMode(sigPin, "both") # <3> gpio.mode(sigPin, "out") # <4> gpio.write(sigPin, gpio.LOW) # <5> time.sleep(0.5) # s gpio.write(sigPin, gpio.HIGH) # <6> time.sleep(1 / 1000.0 / 1000.0) # <7> gpio.mode(sigPin, "in") # <8> #Read high pulse width t = gpio.pulseInHigh(sigPin) # s # <9> d = t * v d = d / 2 # <10> return d * 100 # cm
def getKeyPress(): global lastReadTime foundKey = None if((time.time() - lastReadTime) > bounceTime): # <4> #pulse columns and read pins for c in range(len(columns)): gpio.mode(columns[c], 'out') gpio.write(columns[c], gpio.LOW) # <5> for r in range(len(rows)): if gpio.read(rows[r]) == gpio.LOW: # <6> foundKey = keymap[r][c] # <7> gpio.write(columns[c], gpio.HIGH) gpio.mode(columns[c], 'in') # <8> if not foundKey == None: break # <9> lastReadTime = time.time() return foundKey
def readDistanceCm(): sigPin=22 v=(331.5+0.6*20) gpio.interruptMode(sigPin, "both") # <3> gpio.mode(sigPin, "out") # <4> gpio.write(sigPin, gpio.LOW) # <5> time.sleep(0.5) # s gpio.write(sigPin, gpio.HIGH) # <6> time.sleep(1/1000.0/1000.0) # <7> gpio.mode(sigPin, "in") # <8> #Read high pulse width t = gpio.pulseInHigh(sigPin) # s # <9> d = t*v d = d/2 # <10> return d*100 # cm
def measureDistance(): trigPin = 22 # указываем номер контакта Raspberry Pi, к которому подключен Trig контакт датчика HC-SR04 echoPin = 27 # указываем номер контакта Raspberry Pi, к которому подключен Echo контакт датчика HC-SR04 v = ( 331.5 + 0.6 * 20 ) # скорость звука при температуре 20 градусов Цельсия(вы можете указать свое значение вместо 20) в м/с gpio.mode(trigPin, "out") # устанавливаем контакт как выход gpio.mode(echoPin, "in") # устанавливаем контакт как вход gpio.interruptMode( echoPin, "both" ) # режим прерывания, чтобы функция pulseInHigh вычислила длительность перехода сигнала с 0 до 1 и от 1 до 0 gpio.write(trigPin, gpio.LOW) # устанавливаем низкий уровень сигнала time.sleep(0.5) # задержка в пол секунда gpio.write(trigPin, gpio.HIGH) # устанавливаем высокий уровень сигнала time.sleep(1 / 1000000.0) # задержка в 1 мкс gpio.write(trigPin, gpio.LOW) # устанавливаем низкий уровень сигнала t = gpio.pulseInHigh(echoPin) # вычисляем длительность сигнала d = t * v * 50 # вычисляем пройденное расстояние return d # возвращаем значение
def sample(count): sendPin = 23 recievePin = 24 gpio.mode(sendPin,"out") gpio.mode(recievePin,"in") gpio.write(sendPin,0) total = 0 # set low for x in xrange(1,count): time.sleep(0.01) gpio.write(sendPin,gpio.HIGH) while(gpio.read(recievePin) == False): total += 1 gpio.write(sendPin,gpio.LOW) return total
def readDistanceCm(): # <3> triggerPin = 22 # <4> echoPin = 27 # <5> v = (331.5 + 0.6 * 20) # m/s # <6> gpio.mode(triggerPin, "out") # <7> gpio.mode(echoPin, "in") # <8> gpio.interruptMode(echoPin, "both") # <9> gpio.write(triggerPin, 0) # <10> time.sleep(0.5) # <11> gpio.write(triggerPin, 1) # <12> time.sleep(1 / 1000.0 / 1000.0) # <13> gpio.write(triggerPin, 0) # <14> t = gpio.pulseInHigh(echoPin) # s # <15> d = t * v # <16> d = d / 2 # <17> return d * 100 # cm # <18>
def readDistanceCm(): triggerPin = 22 # <1> echoPin = 27 v=(331.5+0.6*20) # m/s gpio.mode(triggerPin, "out") gpio.mode(echoPin, "in") gpio.interruptMode(echoPin, "both") gpio.write(triggerPin, gpio.LOW) time.sleep(0.5) gpio.write(triggerPin, gpio.HIGH) time.sleep(1/1000.0/1000.0) gpio.write(triggerPin, gpio.LOW) t = gpio.pulseInHigh(echoPin) # s d = t*v d = d/2 return d*100 # cm
def readDistanceCm(): triggerPin = 22 # <1> echoPin = 27 v = (331.5 + 0.6 * 19) # m/s gpio.mode(triggerPin, "out") gpio.mode(echoPin, "in") gpio.interruptMode(echoPin, "both") gpio.write(triggerPin, gpio.LOW) time.sleep(0.5) gpio.write(triggerPin, gpio.HIGH) time.sleep(1 / 1000.0 / 1000.0) gpio.write(triggerPin, gpio.LOW) t = gpio.pulseInHigh(echoPin) # s d = t * v d = d / 2 return d * 100 # cm
def initializeColorSensor(): ledPin = 25 gpio.mode(2,"out") # <2> gpio.mode(3,"out") gpio.mode(14,"out") gpio.mode(17,"out") gpio.mode(22,"out") gpio.mode(27,"out") gpio.write(2,gpio.LOW) gpio.write(3,gpio.LOW) gpio.write(14,gpio.LOW) gpio.write(17,gpio.LOW) gpio.write(22,gpio.LOW) gpio.write(27,gpio.LOW) gpio.mode(ledPin,"out") gpio.write(ledPin, gpio.HIGH) # <3>
def initializeColorSensor(): ledPin = 25 gpio.mode(2, "out") # <2> gpio.mode(3, "out") gpio.mode(14, "out") gpio.mode(17, "out") gpio.mode(22, "out") gpio.mode(27, "out") gpio.write(2, gpio.LOW) gpio.write(3, gpio.LOW) gpio.write(14, gpio.LOW) gpio.write(17, gpio.LOW) gpio.write(22, gpio.LOW) gpio.write(27, gpio.LOW) gpio.mode(ledPin, "out") gpio.write(ledPin, gpio.HIGH) # <3>