def send(self, channel, button, state): bin_list = self.command_as_bin_list(channel, button, state) packet = self.encode_packet(bin_list) for _ in range(self.repeat): for bit in packet: wiringpi.digitalWrite(self.pin, bit) wiringpi.delayMicroseconds(self.PULSE_WIDTH)
def main(): receiver = 27; sender = 1; state = 1; i = 0 global last_high, ones, zeros, bits_to_output, code while True: wiringpi.delayMicroseconds(25) if wiringpi.digitalRead(receiver) == False: if (bits_to_output > 0): sys.stdout.write("0") zeros += 1 if state < 80: state += 1 else: if state >= 2 and bits_to_output > 0: bits_to_output -= 1 #sys.stdout.write("\n") #sys.stdout.write(str(format(ones / (ones + zeros), '.2f')) + " ") #sys.stdout.write(str( (ones + zeros)) + " ") if ones / (ones + zeros) < 0.5: #sys.stdout.write("0") code += "0" else: #sys.stdout.write("1") code += "1" if bits_to_output == 0: sys.stdout.write(" " + code) sys.stdout.write(" " + compress_string(code)) #sys.stdout.write(" --\n") ones = 0 zeros = 0 if state == 80: calc_time() if (bits_to_output > 0): sys.stdout.write("1") ones += 1 if state >= 2: last_high = wiringpi.micros() state = 0 i += 1 if i == 40: sys.stdout.flush() i = 0
def transmitWaveForm(waveForm, signalObject): if(waveForm == 0): wiringpi.digitalWrite(TRANSMIT_PIN,1) wiringpi.delayMicroseconds(signalObject.SHORT_DELAY_ON) wiringpi.digitalWrite(TRANSMIT_PIN,0) wiringpi.delayMicroseconds(signalObject.SHORT_DELAY_OFF) elif(waveForm == 1): wiringpi.digitalWrite(TRANSMIT_PIN,1) wiringpi.delayMicroseconds(signalObject.LONG_DELAY_ON) wiringpi.digitalWrite(TRANSMIT_PIN,0) wiringpi.delayMicroseconds(signalObject.LONG_DELAY_OFF) elif(waveForm == 2): wiringpi.digitalWrite(TRANSMIT_PIN,0) wiringpi.delayMicroseconds(signalObject.SIGNAL_BEGIN)
def send_digit(digit): if digit == "0": wiringpi.digitalWrite(sender, 1) wiringpi.delayMicroseconds(int(code_duration * 0.25)) wiringpi.digitalWrite(sender, 0) wiringpi.delayMicroseconds(int(code_duration * 0.75)) else: wiringpi.digitalWrite(sender, 1) wiringpi.delayMicroseconds(int(code_duration * 0.75)) wiringpi.digitalWrite(sender, 0) wiringpi.delayMicroseconds(int(code_duration * 0.25))
def read_oneshot(self, diff_channel): """Restart/re-sync ADC and read the specified input pin pair. Arguments: 8-bit code value for differential input channel (See definitions for the REG_MUX register) Returns: Signed integer conversion result Use this function after waking up from STANDBY mode. When switching inputs during a running conversion cycle, invalid data is acquired. To prevent this, this function automatically restarts the conversion cycle after configuring the input channels. The resulting delay can be avoided. See functions: read_and_next_is(diff_channel) for cyclic single-channel reads and: read_sequence() for cyclic reads of multiple channels at once. """ self._chip_select() # Set input pin mux position for this cycle" self._send_uint8(CMD_WREG | REG_MUX, 0x00, diff_channel) # Restart/start the conversion cycle with set input pins self._send_uint8(CMD_SYNC) wp.delayMicroseconds(self._SYNC_TIMEOUT_US) self._send_uint8(CMD_WAKEUP) self.wait_DRDY() # Read data from ADC, which still returns the /previous/ conversion # result from before changing inputs self._send_uint8(CMD_RDATA) wp.delayMicroseconds(self._DATA_TIMEOUT_US) # The result is 24 bits little endian two's complement value by default int24_result = self._read_int24() # Release chip select and implement t_11 timeout self._chip_release() return int24_result
def dSPIN_Xfer(data): wp.digitalWrite(hd.dSPIN_CS, wp.GPIO.LOW) for i in range(8): wp.digitalWrite(hd.dSPIN_CLK, wp.GPIO.LOW) if (data & 0x80): wp.digitalWrite(hd.dSPIN_MOSI, wp.GPIO.HIGH) else: wp.digitalWrite(hd.dSPIN_MOSI, wp.GPIO.LOW) wp.delayMicroseconds(hd.dSPIN_SPI_CLOCK_DELAY) data <<= 1 if (wp.digitalRead(hd.dSPIN_MISO)): data |= 1 wp.digitalWrite(hd.dSPIN_CLK, wp.GPIO.HIGH) wp.delayMicroseconds(hd.dSPIN_SPI_CLOCK_DELAY) wp.digitalWrite(hd.dSPIN_CS, wp.GPIO.HIGH) wp.delayMicroseconds(hd.dSPIN_SPI_CLOCK_DELAY) return data
def switchRead(self): didWait = False # Switch to input wiringpi.pinMode(self.pinDD, GPIO.INPUT) wiringpi.digitalWrite(self.pinSEND, GPIO.LOW) # Wait at least 83 ns before checking state t(dir_change) # switchRead is always done after a rewrite, that already have a delay. #wiringpi.delay(2) # Wait for DD to go LOW (Chip is READY) while wiringpi.digitalRead(self.pinDD) == GPIO.HIGH: # Do 8 clock cycles for cnt in range(8): wiringpi.digitalWrite(self.pinDC, GPIO.HIGH) wiringpi.delayMicroseconds(self.delay_time_us) wiringpi.digitalWrite(self.pinDC, GPIO.LOW) wiringpi.delayMicroseconds(self.delay_time_us) # Let next function know that we did wait didWait = True # Wait t(sample_wait) if didWait: wiringpi.delayMicroseconds(self.delay_time_us) return 0
def drive_multi(self, motor_idx, steps): """ Function to drive multiple axes at once param: motor_idx - a list of motor indices to drive type: list of integers param: steps - the number of steps to move each corresponding index type: list of integers (positive or negative). NOTE: must be same size as motor_idx list """ if len(motor_idx) != len(steps): print("motor_idx and steps lists must be same size!") return for idx in motor_idx: if idx < 0 or idx > 5: print "motor_idx must be 0 to 5" return # parse off the direction dir = [] for idx in range(len(steps)): if steps[idx] > 0: dir.append(1) else: dir.append(0) steps[idx] = steps[idx] * -1 # loop the maximum number of steps of all motors for i in range(max(steps)): delay_us = (self._PULSE_TRANSMIT - (time.time() - self._last_pulse)) if delay_us > 0: wiringpi.delayMicroseconds(int(round(delay_us))) for idx in range(len(steps)): if steps[idx] > 0: self._pulse_motor(motor_idx[idx], dir[idx]) steps[idx] -= 1 self._last_pulse = time.time()
def writeBit(pin, bit): wiringpi.pinMode(pin, GPIO.OUTPUT) wiringpi.digitalWrite(pin, GPIO.LOW) wiringpi.delayMicroseconds(2) wiringpi.digitalWrite(pin, bit) wiringpi.delayMicroseconds(80) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.delayMicroseconds(1)
def getval(owpin): tl = [] #存放每个数据位的时间 tb = [] #存放数据位 gpio.pinMode(owpin, 1) #设置针脚为输出状态 gpio.digitalWrite(owpin, 1) #输出高电平 gpio.delay(1) gpio.digitalWrite(owpin, 0) #拉低20ms开始指令 gpio.delay(25) gpio.digitalWrite(owpin, 1) #抬高20-40us gpio.delayMicroseconds(30) gpio.pinMode(owpin, 0) #设针脚为输入状态 for i in range(45): #测试每个数据周期的时间(包括40bit数据加一个发送开始标志 tc = gpio.micros() #记下当前us数(从初始化开始算起,必要时重新初始化) ''' 一个数据周期,包括一个低电平,一个高电平,从DHT11第一次拉低信号线开始 到DHT11发送最后一个50us的低电平结束(然后被拉高,一直维持高电平,所以 最后的完成标志是一直为高,超过500ns) ''' while (gpio.digitalRead(owpin) == 0): pass while (gpio.digitalRead(owpin) == 1): if gpio.micros() - tc > 500: #如果超过500ms就结束了 break if gpio.micros() - tc > 500: #跳出整个循环 break tl.append(gpio.micros() - tc) #记录每个周期时间的us数,存到tl这个列表 tl = tl[1:] #去掉第一项,剩下40个数据位 for i in tl: if i > 100: #若数据位为1,时间为50us低电平+70us高电平=120us tb.append(1) else: tb.append(0) #若数据位为0,时间为50us低电平+25us高电平=75us #这里取大于100us就为1 return tb
def write(self, data): # Make sure DD is on output wiringpi.pinMode(self.pinDD, GPIO.OUTPUT) # Sent bytes for cnt in range(8): # First put data bit on bus if data & 0x80: wiringpi.digitalWrite(self.pinDD, GPIO.HIGH) else: wiringpi.digitalWrite(self.pinDD, GPIO.LOW) # Place clock on high (other end reads data) wiringpi.digitalWrite(self.pinDC, GPIO.HIGH) # Shift & Delay data = data << 1 wiringpi.delayMicroseconds(self.delay_time_us) # Place clock down wiringpi.digitalWrite(self.pinDC, GPIO.LOW) wiringpi.delayMicroseconds(self.delay_time_us) return 0
def getval(pin): tl = [] tb = [] wiringpi.wiringPiSetup() wiringpi.pinMode(pin, GPIO.OUTPUT) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.delay(1) wiringpi.digitalWrite(pin, GPIO.LOW) wiringpi.delay(25) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.delayMicroseconds(20) wiringpi.pinMode(pin, GPIO.INPUT) while (wiringpi.digitalRead(pin) == 1): pass for i in range(45): tc = wiringpi.micros() ''' ''' while (wiringpi.digitalRead(pin) == 0): pass while (wiringpi.digitalRead(pin) == 1): if wiringpi.micros() - tc > 500: break if wiringpi.micros() - tc > 500: break tl.append(wiringpi.micros() - tc) tl = tl[1:] for i in tl: if i > 100: tb.append(1) else: tb.append(0) return tb
def read_and_next_is(self, diff_channel): """Reads ADC data of presently running or already finished conversion, sets and synchronises new input channel config for next sequential read. Arguments: 8-bit code value for differential input channel (See definitions for the REG_MUX register) Returns: Signed integer conversion result for present read This enables rapid dycling through different channels and implements the timing sequence outlined in the ADS1256 datasheet (Sept.2013) on page 21, figure 19: "Cycling the ADS1256 Input Multiplexer". Note: In most cases, a fixed sequence of input channels is known beforehand. For that case, this module implements the function: read_sequence(ch_sequence) which automates the process for cyclic data acquisition. """ self._chip_select() self.wait_DRDY() # Setting mux position for next cycle" self._send_byte(CMD_WREG | REG_MUX) self._send_byte(0x00) self._send_byte(diff_channel) # Restart/start next conversion cycle with new input config self._send_byte(CMD_SYNC) wp.delayMicroseconds(self._SYNC_TIMEOUT_US) self._send_byte(CMD_WAKEUP) # The datasheet is a bit unclear if a t_11 timeout is needed here. # Assuming the extra timeout is the safe choice: wp.delayMicroseconds(self._T_11_TIMEOUT_US) # Read data from ADC, which still returns the /previous/ conversion # result from before changing inputs self._send_byte(CMD_RDATA) wp.delayMicroseconds(self._DATA_TIMEOUT_US) # The result is 24 bits little endian two's complement value by default byte_3 = self._read_byte() byte_2 = self._read_byte() byte_1 = self._read_byte() # Release chip select and implement t_11 timeout self._chip_release() # Concatenate the bytes int24_result = byte_3 << 16 | byte_2 << 8 | byte_1 # Take care of 24-bit two's complement if int24_result < 0x800000: return int24_result else: return int24_result - 0x1000000
def ISR(): SetDiffChannal(0) wp.delayMicroseconds(5) WriteCmd(CMD_SYNC) wp.delayMicroseconds(5) WriteCmd(CMD_WAKEUP) wp.delayMicroseconds(25) AdcNow = ReadData() return AdcNow
def switchOutlet(self, groupNumber, switchState): if ( 0 < groupNumber and groupNumber <= len(TelldusSwitch.sGroupId[:][0]) and 0 <= switchState and switchState < 4): # Sender id if (1 < switchState): signal = TelldusSwitch.sSenderId + TelldusSwitch.bGroupId[1] else: signal = TelldusSwitch.sSenderId + TelldusSwitch.bGroupId[0] for g in range(self.nRepeatTransmit): for h in range(0,6): # Signals go SYNC->SENDER->SWITCH->GROUP # Sync id self.sendSync() # Sender id loop for i in range(len(signal)): self.transmit(signal[i]) # Switch id loop (ie. on or off) for j in range(len(TelldusSwitch.sSwitchState[switchState][:])): self.transmit(TelldusSwitch.sSwitchState[switchState][j]) # Group id loop for k in range(len(TelldusSwitch.sGroupId[groupNumber-1][:])): self.transmit(TelldusSwitch.sGroupId[groupNumber-1][k]) wiringpi.delayMicroseconds(TelldusSwitch.tBlock) wiringpi.delayMicroseconds(TelldusSwitch.tBlock*2) return 1 else: return 0 # Switch all outlets on or off within this id def switchAll(self, switch): if (switch == 0): self.switchOutlet(1,2) elif (switch == 1): self.switchOutlet(1,3) # Transmits any number of pulses then pauses for tSymbol def transmit(self, pulses): # Loop for each pulse for i in range(pulses): wiringpi.digitalWrite(self.nPin, 1) wiringpi.delayMicroseconds(TelldusSwitch.tPulse) wiringpi.digitalWrite(self.nPin, 0) wiringpi.delayMicroseconds(TelldusSwitch.tPulse) # Symbol pause wiringpi.delayMicroseconds(TelldusSwitch.tSymbol)
def ISR(self): self.SetChannal(self.ch) #SetDiffChannal(0) wp.delayMicroseconds(5) self.WriteCmd(CMD_SYNC) wp.delayMicroseconds(5) self.WriteCmd(CMD_WAKEUP) wp.delayMicroseconds(25) AdcNow = self.ReadData() return AdcNow
def readBit(pin): wiringpi.pinMode(pin, GPIO.OUTPUT) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.digitalWrite(pin, GPIO.LOW) wiringpi.delayMicroseconds(2) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.pinMode(pin, GPIO.INPUT) wiringpi.delayMicroseconds(2) tmp = wiringpi.digitalRead(pin) wiringpi.delayMicroseconds(40) return tmp
def oneWireReset(pin): wiringpi.pinMode(pin, GPIO.OUTPUT) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.digitalWrite(pin, GPIO.LOW) wiringpi.delayMicroseconds(500) wiringpi.digitalWrite(pin, GPIO.HIGH) wiringpi.delayMicroseconds(60) wiringpi.pinMode(pin, GPIO.INPUT) if not wiringpi.digitalRead(pin): ack = 1 else: ack = 0 wiringpi.delayMicroseconds(500) return ack
def readSharpPM10Sensor(self): voMeasured = 0 for i in range(10): wiringpi.digitalWrite(self.ILEDPin, 1) # power on the LED wiringpi.delayMicroseconds(self.samplingTime) wiringpi.delayMicroseconds(self.deltaTime) voMeasured = self.readadc(self.pm10Pin) # read the dust value wiringpi.digitalWrite(self.ILEDPin, 0) # turn the LED off wiringpi.delayMicroseconds(self.sleepTime) # 0 - 5V mapped to 0 - 1023 integer values # recover voltage calcVoltage = voMeasured * (5.0 / 1024) # linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ # Chris Nafis (c) 2012 dustDensity = 0.17 * calcVoltage - 0.1 #print("{0}, {1}, {2}".format(voMeasured, calcVoltage, dustDensity)) return voMeasured
def readSequence(self): vo_measured = 0 readings = [] for i in range(10): wiringpi.digitalWrite(self.led_pin, 1) # power on the LED wiringpi.delayMicroseconds(self.sampling_time) wiringpi.delayMicroseconds(self.delta_time) vo_measured = self.adc.read(self.pm10_pin) # read the dust value wiringpi.digitalWrite(self.led_pin, 0) # turn the LED off wiringpi.delayMicroseconds( self.sleep_time ) # wait 9.68ms before the next sequence is repeated # Voltage 0 - 5V mapped to 0 - 1023 integer values calc_voltage = vo_measured * (5.0 / 1024) # linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ (Chris Nafis (c) 2012) dust_density = 0.17 * calc_voltage - 0.1 readings.append(dust_density) return median(readings)
def delayMicroseconds(self, delayus): wp.delayMicroseconds(delayus)
def return_home(): gpio.digitalWrite(X_ENABLE_PIN, GPIO.HIGH) #pin25输出为高电平 gpio.digitalWrite(Y_ENABLE_PIN, GPIO.HIGH) #pin25输出为高电平 gpio.digitalWrite(Z_ENABLE_PIN, GPIO.HIGH) #pin25输出为高电平 gpio.digitalWrite(X_DIR_PIN, GPIO.HIGH) ##方向 gpio.digitalWrite(Y_DIR_PIN, GPIO.HIGH) gpio.digitalWrite(Z_DIR_PIN, GPIO.HIGH) tx = int((30000 * X_STEPS_PER_MM) / FAST_XY_FEEDRATE) ty = int((30000 * X_STEPS_PER_MM) / FAST_XY_FEEDRATE) tz = int((30000 * Z_STEPS_PER_MM) / FAST_Z_FEEDRATE) while gpio.digitalRead(X_MIN_PIN): ####如果限位反馈是高电平,则 gpio.digitalWrite(X_STEP_PIN, GPIO.HIGH) gpio.delayMicroseconds(tx) gpio.digitalWrite(X_STEP_PIN, GPIO.LOW) ###发一个脉冲 gpio.delayMicroseconds(tx) while gpio.digitalRead(Y_MIN_PIN): gpio.digitalWrite(Y_STEP_PIN, GPIO.HIGH) gpio.delayMicroseconds(ty) gpio.digitalWrite(Y_STEP_PIN, GPIO.LOW) ###发一个脉冲 gpio.delayMicroseconds(ty) while gpio.digitalRead(Z_MIN_PIN): gpio.digitalWrite(Z_STEP_PIN, GPIO.HIGH) gpio.delayMicroseconds(tz) gpio.digitalWrite(Z_STEP_PIN, GPIO.LOW) ###发一个脉冲 gpio.delayMicroseconds(tz)
def do_step(step_pin): ######步进电机走一步 gpio.digitalWrite(step_pin, GPIO.HIGH) gpio.delayMicroseconds(5) gpio.digitalWrite(step_pin, GPIO.LOW) ###发一个脉冲 gpio.delayMicroseconds(5)
def dda_move(micro_delay): ###micro_delay脉冲之间的间隔时间 gpio.digitalWrite(X_ENABLE_PIN, GPIO.HIGH) #pin25输出为高电平 gpio.digitalWrite(Y_ENABLE_PIN, GPIO.HIGH) #pin25输出为高电平 gpio.digitalWrite(Z_ENABLE_PIN, GPIO.HIGH) #pin25输出为高电平 calculate_deltas() gpio.digitalWrite(X_DIR_PIN, direction.x) #pin25输出为高电平 gpio.digitalWrite(Y_DIR_PIN, direction.y) #pin25输出为高电平 gpio.digitalWrite(Z_DIR_PIN, direction.z) #pin25输出为高电平 max_delta = max(delta_steps.x, delta_steps.y) ##max_delta是需要走的最多步数,delta_steps需要走的步数 max_delta = max(delta_steps.z, max_delta) print 'max_delta' print max_delta # x_counter = -max_delta/2 # y_counter = -max_delta/2 # z_counter = -max_delta/2 x_can_step = False #########此处该为布尔型变量 y_can_step = False z_can_step = False if micro_delay >= 16383: ##micro_delay就是feedrate_micros,脉冲间隔时间,用来调节速度 milli_delay = long(micro_delay) / 1000 else: milli_delay = 0 x_start = 0 y_start = 0 z_start = 0 x_target = abs(target_steps.x - current_steps.x) y_target = abs(target_steps.y - current_steps.y) z_target = abs(target_steps.z - current_steps.z) if (x_start != x_target) and (y_start != y_target): print 'XY均有移动' ##插补算法,k为斜率 k = float(y_target) / float(x_target) #k=1 while True: ###x_can_step = can_step(X_MIN_PIN, X_MAX_PIN, current_steps.x, target_steps.x, direction.x) x_can_step = can_step(0, 0, x_start, x_target, direction.x) y_can_step = can_step(0, 0, y_start, y_target, direction.y) if (y_start >= float(x_start * k)) and x_can_step: gpio.digitalWrite(X_DIR_PIN, direction.x) print '插补direction.x=', direction.x do_step(X_STEP_PIN) x_start = x_start + 1 if x_start - x_target == 0: x_start = x_target y_start = y_target print '插补x' elif (y_start < float(x_start * k)) and y_can_step: gpio.digitalWrite(Y_DIR_PIN, direction.y) print '插补direction.y=', direction.y do_step(Y_STEP_PIN) y_start = y_start + 1 if y_start - y_target == 0: x_start = x_target y_start = y_target print '插补y' if milli_delay > 0: gpio.delay(int(milli_delay)) ###毫秒 else: gpio.delayMicroseconds(int(micro_delay)) ###微秒 if not (x_can_step or y_can_step): print '跳出插补' break elif (current_steps.x != target_steps.x) and (current_steps.y == target_steps.y): print '仅X有移动' x_target = abs(target_steps.x - current_steps.x) x_start = 0 while True: gpio.digitalWrite(X_DIR_PIN, direction.x) do_step(X_STEP_PIN) x_start = x_start + 1 if abs(x_start - x_target) < 1: x_start = x_target x_can_step = can_step(0, 0, x_start, x_target, direction.x) if milli_delay > 0: gpio.delay(int(milli_delay)) ###毫秒 else: gpio.delayMicroseconds(int(micro_delay)) ###微秒 if not x_can_step: break elif (current_steps.x == target_steps.x) and (current_steps.y != target_steps.y): print '仅Y有移动' y_target = abs(target_steps.y - current_steps.y) y_start = 0 while True: gpio.digitalWrite(Y_DIR_PIN, direction.y) do_step(Y_STEP_PIN) y_start = y_start + 1 if abs(y_start - y_target) < 1: y_start = y_target y_can_step = can_step(Y_MIN_PIN, Y_MAX_PIN, y_start, y_target, direction.y) if milli_delay > 0: gpio.delay(int(milli_delay)) ###毫秒 else: gpio.delayMicroseconds(int(micro_delay)) ###微秒 if not y_can_step: break elif current_steps.z != target_steps.z: print '仅Z有移动' z_target = abs(target_steps.z - current_steps.z) z_start = 0 while True: gpio.digitalWrite(Z_DIR_PIN, direction.z) do_step(Z_STEP_PIN) z_start = z_start + 1 if abs(z_start - z_target) < 1: z_start = z_target z_can_step = can_step(Z_MIN_PIN, Z_MAX_PIN, z_start, z_target, direction.z) if milli_delay > 0: gpio.delay(int(milli_delay)) ###毫秒 else: gpio.delayMicroseconds(int(micro_delay)) ###微秒 if not z_can_step: break current_units.x = target_units.x current_units.y = target_units.y current_units.z = target_units.z print 'current_units.x' print current_units.x
def transmit(self, bit): """Transmits a 0, a 1, or a 'sync', as specified in the pulse_map""" wiringpi.digitalWrite(self.pin, wiringpi.HIGH) wiringpi.delayMicroseconds(self.delay(bit, 'HIGH')) wiringpi.digitalWrite(self.pin, wiringpi.LOW) wiringpi.delayMicroseconds(self.delay(bit, 'LOW'))
def loop(): fft_size = 256 # 256-point FFT sampl_freq = 4500 # Sampling frequency is 4500Hz freq_low = 0 # Lower cut-off frequency freq_high = 1000 # Upper cut-off frequency n = 0 y = [] t_sample = 1 / sampl_freq t = time.time() t0_start = t while n < fft_size: if n > 0: dt = time.time() - t0_start tmp = int(1000000 * (t_sample * (n + 1) - dt)) if tmp > 0: wiringpi.delayMicroseconds( int(1000000 * (t_sample * (n + 1) - dt))) digitalVal = ADC0832.getResult() n = n + 1 y.append(3.3 * float(digitalVal) / 255) t = time.time() - t # 256_point sampling sampl_freq2 = fft_size / t # Calculate the actual sampling freq. print("real sample_freq: %d" % sampl_freq2) y_fft = np.fft.fft(y) # FFT y_fft_ampl = np.abs(y_fft) # Amplitude spectrum x = np.linspace(0, t, fft_size) freq = np.linspace(-sampl_freq / 2, sampl_freq / 2, fft_size) low_pos = freq_low * fft_size / sampl_freq # Normalization treatment high_pos = freq_high * fft_size / sampl_freq high_neg = fft_size - high_pos low_neg = fft_size - low_pos i = 1 smooth_fft = [y_fft[0]] # Keep DC offset while i < fft_size: # Frequency domain filtering if i < low_pos: # Below the lower cut-off frequency smooth_fft.append(0) i = i + 1 continue if i > high_pos and i < high_neg: # Higher than Upper cut-off frequency smooth_fft.append(0) i = i + 1 continue if i > low_neg: # Below the lower cut-off frequency smooth_fft.append(0) i = i + 1 continue smooth_fft.append(y_fft[i]) i = i + 1 smooth_org = np.fft.ifft(smooth_fft) # Fourier inversion smooth_fft_ampl = np.abs(smooth_fft) # Amplitude spectrum smooth = np.abs(smooth_org) output_count = 0 print(smooth) plt.figure(figsize=(8, 4)) plt.subplot(221) plt.plot(x, y) plt.title(u"Time Domain (Original)") plt.subplot(222) plt.plot(freq, np.fft.fftshift(y_fft_ampl)) plt.title(u"Frequency Domain (Original)") plt.subplot(223) plt.plot(x, smooth) plt.xlabel(u"t(s)") plt.title(u"Time Domain(smoothing)") plt.subplot(224) plt.plot(freq, np.fft.fftshift(smooth_fft_ampl)) plt.xlabel(u"freq(Hz)") plt.title(u"Frequency Domain(smoothing)") plt.subplots_adjust(hspace=0.4) plt.show() while 1: # Output signal output_count += 1 i = 0 t = time.time() t2_start = time.time() while i < fft_size: if i > 0: dt = time.time() - t2_start #print(int(1000000*(t_sample*(n+1) - dt))) tmp = int(1000000 * (t_sample * (i + 1) - dt)) if tmp > 0: wiringpi.delayMicroseconds(tmp) DAC.SendOneData(int(smooth[i] * 246 / 3.3)) i = i + 1 if output_count == 1: t = time.time() - t # 256_point sampling dac_freq = fft_size / t # Calculate the actual sampling freq. print("real dac_freq: %d" % dac_freq)
def DelayUS(micros): wp.delayMicroseconds(micros)
def delayMicroSeconds(self,howLong:int): # 1μs = 1/1000ms gpio.delayMicroseconds(howLong) return True
def triggerEcho(self): wpi.digitalWrite(TRIG_PIN, 1) wpi.delayMicroseconds(10) # hold high for 10us wpi.digitalWrite(TRIG_PIN, 0)
def DelayData(): wp.delayMicroseconds(10)
def SendByte(data): wp.delayMicroseconds(2) spi.xfer([data])
def send_sync(): wiringpi.digitalWrite(sender, 1) wiringpi.delayMicroseconds(400) wiringpi.digitalWrite(sender, 0) wiringpi.delayMicroseconds(10000)
wiringpi.pinMode(DIR_3, 1) wiringpi.digitalWrite(DIR_3, 1) wiringpi.pinMode(STEP_4, 1) wiringpi.pinMode(DIR_4, 1) wiringpi.digitalWrite(DIR_4, 1) wiringpi.pinMode(ALARM_LED, 1) wiringpi.digitalWrite(ALARM_LED, 0) print("Running") wiringpi.digitalWrite(EN, 1) try: while True: wiringpi.digitalWrite(EN, 0) for x in range(1): wiringpi.digitalWrite(STEP_4, 1) wiringpi.delayMicroseconds(500) wiringpi.digitalWrite(STEP_4, 0) wiringpi.delayMicroseconds(500) # wiringpi.digitalWrite(EN,1) # wiringpi.digitalWrite(EN,0) # for x in range(10): # wiringpi.digitalWrite(STEP_2,1) # wiringpi.delayMicroseconds(500) # wiringpi.digitalWrite(STEP_2,0) # wiringpi.delayMicroseconds(500) # wiringpi.digitalWrite(EN,1) # temp = input("X: Press 'q' to step positive, press 'w' to step negative\nY: Press 'a' to step positive, press 's' to step negative\nAlpha (upper swing): Press 'e' to rotate positive, press 'r' to rotate negative\nTheta (lower swing): Press 'd' to rotate positive, press 'f' to rotate negative\n") # if temp == "q" or temp == "w": # wiringpi.digitalWrite(EN,0) # if temp == "q":
def getResult(channel=0): # Get ADC result, input channal GPIO.setup(ADC_DIO, GPIO.OUT) GPIO.output(ADC_CS, 0) GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, channel) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 1) GPIO.output(ADC_DIO, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, 1) wiringpi.delayMicroseconds(2) dat1 = 0 for i in range(0, 8): GPIO.output(ADC_CLK, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 0) wiringpi.delayMicroseconds(2) GPIO.setup(ADC_DIO, GPIO.IN) dat1 = dat1 << 1 | GPIO.input(ADC_DIO) dat2 = 0 for i in range(0, 8): dat2 = dat2 | GPIO.input(ADC_DIO) << i GPIO.output(ADC_CLK, 1) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CLK, 0) wiringpi.delayMicroseconds(2) GPIO.output(ADC_CS, 1) GPIO.setup(ADC_DIO, GPIO.OUT) if dat1 == dat2: return dat1 else: return 0
def outputGraph(graph, time): while 1: for i in graph: DAC.SendOneData(i) wiringpi.delayMicroseconds(time)
import sys import wiringpi OUTPUT = 1 TPIN = 17 # Use BCM GPIO pin number RAW = str(sys.argv[1]) HIGH = False wiringpi.wiringPiSetupGpio() wiringpi.pinMode(TPIN, OUTPUT) # Set pin 17 to 1 ( OUTPUT ) print "Sending signal..." for x in RAW.split(): if HIGH: wiringpi.digitalWrite(TPIN, 0) HIGH = False else: wiringpi.digitalWrite(TPIN, 1) HIGH = True wiringpi.delayMicroseconds(int(x)) wiringpi.digitalWrite(TPIN, 0) print "... sent!"