def get_data(channel): tl = [] gpio.digitalWrite(channel, gpio.HIGH) # 输出高电平 gpio.delay(1) ###发开始指令,要求DHT11传输数据 gpio.digitalWrite(channel, gpio.LOW) # 拉低25ms开始指令 gpio.delay(25) gpio.digitalWrite(channel, gpio.HIGH) # 输出高电平,开始指令结束 gpio.pinMode(channel, gpio.INPUT) # 设针脚为输入状态 ###开始指令发送完毕,把管脚设置为高电平,并等待DHT11拉低管脚。传输数据 while (gpio.digitalRead(channel) == gpio.HIGH): pass # 如果管脚一直是1,则一直等待。 ###若被拉低,说明传输开始,应答信号+40位数据+结束标志共42位 ###下边共循环45次,故意多循环几次看结果。 ###开始接受数据,50us低 + 26-28us高=0,50us低 + 70us高 = 1 for i in range(45): # 测试每个数据周期的时间(包括40bit数据加一个发送开始标志 tc = gpio.micros() # 记下当前us数(从初始化开始算起,必要时重新初始化) ''' 一个数据周期,包括一个低电平,一个高电平,从DHT11第一次拉低信号线开始 到DHT11发送最后一个50us的低电平结束(然后被拉高,一直维持高电平,所以 最后的完成标志是一直为高,超过500us) ''' while (gpio.digitalRead(channel) == gpio.LOW): pass # 一位数据由一个低电平 while (gpio.digitalRead(channel) == gpio.HIGH): # 加一个高电平组成 if gpio.micros() - tc > 500: # 如果超过500us就结束了本次循环,传输结束后 break # 会被上拉电阻拉成高电平,防止进入死循环 tl.append(gpio.micros() - tc) # 记录每个周期时间的us数,存到tl这个列表 print(tl) return tl
def Update(self): data = wiringpi.digitalRead(self.pin) if data: # print self.pin if not self.isMidPulse: self.isMidPulse = True self.lastPulseStart = wiringpi.micros() if not data: if self.isMidPulse: self.isMidPulse = False pulseEnd = wiringpi.micros() thisPulseLen = pulseEnd - self.lastPulseStart self.pulses.append(thisPulseLen) numElements = len(self.pulses) if numElements > numSamplesForAvg: self.pulses.pop(0) # remove oldest sample if have enough averagedPulseLen = 0 for el in self.pulses: averagedPulseLen += el averagedPulseLen /= numElements self.currentAvgLen = averagedPulseLen if self.isPrintData and numElements >= numSamplesForAvg: currTime = time.time() if currTime - self.lastOutput > outputPeriod or self.lastOutput == 0.0: self.lastOutput = time.time() procComms.PrintData(self.name + ', ' + str(self.InPercent()))
def handleInterrupt(self): syncDur = 14000 time = wiringpi.micros() if self.lastTime == None: self.lastTime = time duration = time - self.lastTime if len( self.__timings ) > 2 and duration > syncDur: #and duration > self.__timings[0] - 200 and duration < self.__timings[0] + 200: #self.repeatCount += 1 print( "=======================================================================" ) print("rep: %i chg: %i" % (self.repeatCount, len(self.__timings))) #if self.repeatCount >= 2: #print("rep cnt: %i - duration: %i - count: %i - delay: %i" % (self.repeatCount, duration, len(self.__timings), delay)) print(wiringpi.micros(), [int(50 * round(t / 50, 0)) for t in self.__timings]) #ucode = "" #scode = "" #for i in range(self.changeCount): # p = self.__timings[i] # ucode += " " + str(p) # scode += str(p) # if i % 4 == 0: # ucode += " " #print("coded string: %s" % ucode) if self.receiveProtocol_Chacon(): print('value: %s' % self.__ReceivedValue) if self.receiveProtocol_DIO(): print('value: 0x%04X' % self.__ReceivedValue) #self.repeatCount = 0 self.__timings = list() #elif len(self.__timings) > 2 and duration > syncDur: # print('%i packets discarded (2)' % len(self.__timings)) # self.__timings = list() self.__timings.append(duration) self.lastTime = time
def trigger(mode): w1 = w2 = 0 while (w2 < 4): msec = wiringpi2.micros() while (wiringpi2.micros() - msec < 1500): wiringpi2.digitalWrite(ANTENNA, wiringpi2.GPIO.HIGH) wiringpi2.digitalWrite(STATUS_LED, wiringpi2.GPIO.HIGH) while (wiringpi2.micros() - msec < 2000): wiringpi2.digitalWrite(ANTENNA, wiringpi2.GPIO.LOW) wiringpi2.digitalWrite(STATUS_LED, wiringpi2.GPIO.LOW) w2 += 1 while (w1 < mode): msec = wiringpi2.micros() while (wiringpi2.micros() - msec < 500): wiringpi2.digitalWrite(ANTENNA, wiringpi2.GPIO.HIGH) wiringpi2.digitalWrite(STATUS_LED, wiringpi2.GPIO.HIGH) while (wiringpi2.micros() - msec < 1000): wiringpi2.digitalWrite(ANTENNA, wiringpi2.GPIO.LOW) wiringpi2.digitalWrite(STATUS_LED, wiringpi2.GPIO.LOW) w1 += 1
import wiringpi2 import sys OUTPUT = 1 PIN_TO_PWM = 1 # pin 1 to output pwm # wiringpi2.wiringPiSetup() wiringpi2.wiringPiSetupPhys() wiringpi2.pinMode(PIN_TO_PWM, OUTPUT) wiringpi2.softPwmCreate(PIN_TO_PWM, 0, 100) time = wiringpi2.millis() print "----------milliseconds-------------" print time print "-----------------------------------" time = wiringpi2.micros() print "----------microseconds-------------" print time print "-----------------------------------"
# -*- coding: utf-8 -*- import wiringpi2 as gpio owpin=29 #第8脚为1-wire脚 tl=[] #存放每个数据位的时间 gpio.wiringPiSetup() #初始化wiringpi库 gpio.pinMode(owpin,1) #设置针脚为输出状态 gpio.digitalWrite(owpin,1) #输出高电平 gpio.delay(1) ###发开始指令,要求DHT11传输数据 gpio.digitalWrite(owpin,0) #拉低25ms开始指令 gpio.delay(25) gpio.digitalWrite(owpin,1) #输出高电平,开始指令结束 gpio.pinMode(owpin,0) #设针脚为输入状态 ###开始指令发送完毕,把管脚设置为高电平,并等待DHT11拉低管脚。传输数据 while(gpio.digitalRead(owpin)==1): pass #如果管脚一直是1,则一直等待。 ###若被拉低,说明传输开始,应答信号+40位数据+结束标志共42位 ###下边共循环45次,故意多循环几次看结果。 for i in range(45): #测试每个数据周期的时间(包括40bit数据加一个发送开始标志 tc=gpio.micros() #记下当前us数(从初始化开始算起,必要时重新初始化) ''' 一个数据周期,包括一个低电平,一个高电平,从DHT11第一次拉低信号线开始 到DHT11发送最后一个50us的低电平结束(然后被拉高,一直维持高电平,所以 最后的完成标志是一直为高,超过500ms) ''' while(gpio.digitalRead(owpin)==0):pass #一位数据由一个低电平 while(gpio.digitalRead(owpin)==1): #加一个高电平组成 if gpio.micros()-tc>500: #如果超过500us就结束了本次循环,传输结束后 break #会被上拉电阻拉成高电平,防止进入死循环 tl.append(gpio.micros()-tc) #记录每个周期时间的us数,存到tl这个列表 print(tl)
import wiringpi2 import sys OUTPUT = 1 PIN_TO_PWM = 1 #pin 1 to output pwm #wiringpi2.wiringPiSetup() wiringpi2.wiringPiSetupPhys() wiringpi2.pinMode(PIN_TO_PWM, OUTPUT) wiringpi2.softPwmCreate(PIN_TO_PWM, 0, 100) time = wiringpi2.millis() print "----------milliseconds-------------" print time print "-----------------------------------" time = wiringpi2.micros() print "----------microseconds-------------" print time print "-----------------------------------"
wiringpi.wiringPiSetupGpio() wiringpi.pinMode(21, 0) # GPIO 21 as input midPulse = False pulseStart = 0 pulseEnd = 0 while True: try: out = wiringpi.digitalRead(21) #print out, if out: if not midPulse: midPulse = True pulseStart = wiringpi.micros() #pulseStart = int(round(time.time() * 1000)) #print 'start:\t' + str(pulseStart) if not out: if midPulse: midPulse = False pulseEnd = wiringpi.micros() #pulseEnd = int(round(time.time() * 1000)) #print 'end:\t' + str(pulseEnd) print 'delta:\t' + str(pulseEnd - pulseStart) except KeyboardInterrupt: break # time module is too slow but wiringpi.micros provides good enough resolution!!! yay no need # for additional microcontrollers to read radio input.
#timings = [10620, 1780, 1200, 240, 320, 300, 1260, 260, 300, 200, 1360, 240, 1360, 240, 340, 180, 1400, 240, 300, 260, 1300, 240, 340, 200, 340, 200, 1360, 240, 1360, 240, 360, 240, 1320, 240, 320, 240, 320, 240, 1360, 260, 280, 240, 1380, 260, 1360, 200, 360, 200, 320, 200, 1380, 260, 1360, 180, 340, 240, 1360, 240, 300, 340, 1220, 240, 360, 200, 1360, 240, 300, 240, 1360, 260, 300, 240, 1360, 220, 300, 240, 320, 240, 1360, 260, 300, 240, 1380, 180, 1360, 240, 300, 240, 360, 200, 1380, 240, 380, 120, 1400, 200, 300, 260, 1360, 260, 1340, 260, 300, 260, 280, 260, 1380, 220, 300, 240, 1340, 260, 300, 260, 1320, 260, 300, 240, 1320, 260, 300, 240, 1360, 260, 1340, 200, 340, 200, 300, 240, 1340, 260] #timings = [10620, 1140, 1880, 240, 320, 240, 1340, 240, 320, 260, 1320, 280, 1320, 220, 320, 320, 1240, 240, 380, 180, 1380, 240, 280, 240, 320, 220, 1400, 220, 1320, 240, 320, 220, 1400, 200, 320, 240, 280, 240, 1360, 220, 340, 220, 1380, 240, 1320, 220, 340, 240, 280, 320, 1280, 280, 1340, 240, 280, 280, 1320, 220, 320, 240, 1320, 240, 400, 180, 1320, 240, 340, 220, 1380, 300, 200, 240, 1380, 200, 340, 240, 280, 280, 1340, 240, 280, 220, 1360, 240, 1360, 220, 340, 220, 280, 240, 1420, 180, 340, 260, 1300, 240, 520, 120, 1260, 240, 1400, 180, 340, 240, 340, 220, 1400, 260, 240, 240, 1360, 240, 1340, 240, 280, 240, 340, 240, 1360, 240, 280, 240, 1360, 240, 1380, 260, 280, 240, 300, 220, 1420, 180] timings = [ 10650, 14400, 150, 100, 1300, 200, 1400, 250, 300, 200, 1350, 200, 250, 250, 250, 250, 1350, 200, 250, 200, 1350, 300, 1350, 200, 250, 250, 300, 200, 1350, 250, 1300, 250, 250, 250, 1350, 200, 300, 300, 1250, 250, 300, 200, 1350, 200, 350, 200, 1350, 200, 350, 200, 1350, 250, 300, 200, 350, 200, 1350, 200, 350, 200, 1350, 200, 1350, 200, 300, 300, 200, 350, 1250, 200, 350, 200, 1300, 200, 300, 300, 1250, 250, 1300, 250, 300, 300, 200, 250, 1300, 200, 350, 200, 1350, 200, 350, 150, 1400, 200, 300, 200, 1350, 200, 350, 200, 1350, 200, 1350, 200, 300, 300, 200, 250, 1350, 250 ] stime = wiringpi.micros() #if True: if False: print('start sending') for j in range(4): for i in range(4): s.SendRawTimings(timings) time.sleep(0.25) print('sent') if s.available(): value = s.getReceivedValue() if value == 0: print("Unknown encoding\n") else: print("Received %i\n" % s.getReceivedValue()) s.resetAvailable()
def rotate(self, direction, laps, rpm, at, dt): #------------------------------------------------------------------------ #set counter and speed control variables #------------------------------------------------------------------------ steps = int(float(laps) * self.steps_per_rev) rpm=float(rpm) at=float(at) dt=float(dt) direction = int(direction) at_steps=int(steps*(at/100)) dt_steps=int(steps*(dt/100)) peak_steps=steps - at_steps - dt_steps at_WaitTime = int(1000000 / self.steps_per_rev - 8) #waitTime controls speed peak_WaitTime = int(1000000 / (rpm / 60 * self.steps_per_rev) - 8) time_Range = (at_WaitTime - peak_WaitTime) time_Split = (1 + time_Range) * time_Range / 2 at_split_point = int(at_steps / time_Split ) dt_split_point = int(dt_steps / time_Range ) #------------------------------------------------------------------------ #Set direction of rotation #------------------------------------------------------------------------ if direction == -1: io1.digitalWrite(self.d_pin,0) elif direction == 1: io1.digitalWrite(self.d_pin,1) #------------------------------------------------------------------------ #------------------------------------------------------------------------ #Zero Step Counter #------------------------------------------------------------------------ StepCounter = 0 split_Counter = 0 #------------------------------------------------------------------------ #------------------------------------------------------------------------ #Turn the motor by send pulse #------------------------------------------------------------------------ split_Counter = at_split_point adtCounter = 1 # Start Acceleration loop while StepCounter < at_steps: #turning the gpio off and on tells the driver to take one step io1.digitalWrite(self.p_pin,0) if split_Counter == StepCounter: if at_WaitTime > peak_WaitTime: at_WaitTime -= 1 split_Counter = at_split_point * adtCounter + split_Counter adtCounter += 1 io1.delayMicroseconds(at_WaitTime) io1.digitalWrite(self.p_pin,1) StepCounter += 1 self.motor_address = self.motor_address + direction * StepCounter StepCounter = 0 # Start peak loop while StepCounter < peak_steps: #turning the gpio off and on tells the driver to take one step io1.digitalWrite(self.p_pin,0) io1.delayMicroseconds(peak_WaitTime) io1.digitalWrite(self.p_pin,1) StepCounter += 1 self.motor_address = self.motor_address + direction * StepCounter StepCounter = 0 adtCounter = dt_split_point # Start Deceleration loop while StepCounter < dt_steps: #turning the gpio off and on tells the driver to take one step io1.digitalWrite(self.p_pin,0) if adtCounter == StepCounter: at_WaitTime += 1 adtCounter += dt_split_point io1.delayMicroseconds(at_WaitTime) io1.digitalWrite(self.p_pin,1) StepCounter += 1 self.motor_address = self.motor_address + direction * StepCounter print io1.micros()