def set_duty(duty): duty_counts = int(duty * PWM_RANGE) wiringpi.pwmWrite(PWMPIN, duty_counts) # Allowable values: 0 to PWM_RANGE
wiringpi.pwmSetRange(PWM_RANGE) ############################################################################### if len(sys.argv) == 1: duty = DEFDUTY else: try: duty = float(sys.argv[1])/100.0 except: print(file=sys.stderr) print(USAGE, file=sys.stderr) print(file=sys.stderr) exit(1) if duty > 1.0 or duty < 0.0: print(file=sys.stderr) print(USAGE, file=sys.stderr) print(file=sys.stderr) exit(1) duty_counts = int(duty*PWM_RANGE) duty_pct = 100.0*float(duty_counts)/PWM_RANGE print() print('Setting PWM duty cycle to %.3f %% (%d/%d)' % (duty_pct, duty_counts, PWM_RANGE)) wiringpi.pwmWrite(PWMPIN, duty_counts) # Allowable values: 0 to PWM_RANGE print('done.\n')
n = int(input("Number out of range, please try another one: ")) except ValueError: print( "Your input is either not a number or not an integer, please try again." ) else: accel = 0 for i in range(n): accel0 = list(acc.read_accel( )) #initial acceleration, includes accelerationof gravity accel1 = list(acc.read_accel()) #final acceleration accelx = np.sqrt(accel1[0] * accel1[0]) - np.sqrt( accel0[0] * accel0[0]) #the instantaneous acceleration in each direction accely = np.sqrt(accel1[1] * accel1[1]) - np.sqrt( accel0[1] * accel0[1]) accelz = np.sqrt(accel1[2] * accel1[2]) - np.sqrt( accel0[2] * accel0[2]) a_ins = np.sqrt(accelx * accelx + accely * accely + accelz * accelz) if a_ins < 0.5: a_ins = 0 else: a_ins = a_ins accel += a_ins accel_avg = accel / n #computes the average acceleration of the n measurements. print(accel_avg) pwm = int(accel_avg * PWM_RANGE / 60) wiringpi.pwmWrite(PWMPIN, pwm) wiringpi.delay(DELAY)
v01 = adc.readADCDifferential01( 4096, 128) * 0.001 #volage across thermistor Vt = v01 - v23 #voltage across thermistor I = v23 / (10 ^ 5) #current Rt = Vt / I #resistance of thermistor T = (2 * beta * T0 - alpha - np.sqrt( (alpha - 2 * beta * T0)**2 - 4 * beta * (beta * (T0**2) - alpha * T0 - np.log(Rt / R0)))) / (2 * beta) Tlist1.append(T) # stores temperature in a list time.sleep(0.02) Tarray = np.array(Tlist1, dtype=np.float16) Tavg = np.average(Tarray) if Tavg < i: DEFDUTY = 5 duty_counts = int(DEFDUTY * PWM_RANGE) wiringpi.pwmWrite(PWMPIN, duty_counts) elif Tavg > i: DEFDUTY = 0 duty_counts = int(DEFDUTY * PWM_RANGE) wiringpi.pwmWrite(PWMPIN, duty_counts) Tlist2 = [] #For boxcar averaging for j in range(5): #Update temperature measurement every 0.1 seconds v23 = adc.readADCDifferential23( 4096, 128) * 0.001 #voltage across resistance v01 = adc.readADCDifferential01(4096, 128) * 0.001 #volage across thermistor Vt = v01 - v23 #voltage across thermistor I = v23 / (10 ^ 5) #current Rt = Vt / I #resistance of thermistor T = (2 * beta * T0 - alpha - np.sqrt((alpha - 2 * beta * T0)**2 - 4 * beta *
for i in range(5): #Stores temperature every 0.5 seconds v23 = adc.readADCDifferential23( 4096, 128) * 0.001 #voltage across resistance v01 = adc.readADCDifferential01(4096, 128) * 0.001 #volage across thermistor Vt = v01 - v23 #voltage across thermistor I = v23 / (10 ^ 5) #current Rt = Vt / I #resistance of thermistor T = (2 * beta * T0 - alpha - np.sqrt((alpha - 2 * beta * T0)**2 - 4 * beta * (beta * (T0**2) - alpha * T0 - np.log(Rt / R0)))) / (2 * beta) if T < temp: DEFDUTY = 5 duty_counts = int(DEFDUTY * PWM_RANGE) wiringpi.pwmWrite(PWMPIN, duty_counts) elif T > temp: DEFDUTY = 0 #set pwm cycle to 0% duty_counts = int(DEFDUTY * PWM_RANGE) wiringpi.pwmWrite(PWMPIN, duty_counts) #set pwm cycle here time.sleep(0.1) # Plot x = np.arange(0, period + 1, 1) ybang = Tlist yrtd = T0rtd0lst plt.scatter(x, ybang, s=6, marker='.', c='yellowgreen', label='Thermister') plt.scatter(x, yrtd, s=6, marker='>', c='b', label='RTD') plt.xlabel("Time (s)")
file0append.write(str(T) + "\r\n") file0write.close() for i in range(10): v23 = adc.readADCDifferential23( 4096, 128) * 0.001 #voltage across resistance v01 = adc.readADCDifferential01(4096, 128) * 0.001 #volage across thermistor Vt = v01 - v23 #voltage across thermistor I = v23 / (10 ^ 5) #current Rt = Vt / I #resistance of thermistor T = 263.84 - 5000 * np.sqrt(0.002304 + 0.0004 * np.log(Rt / 15.73)) if T < temp - 3: #Sometimes the temperature measurement is no exact, so allow one degree of error DEFDUTY = 100 duty_counts = int(DEFDUTY * PWM_RANGE) wiringpi.pwmWrite( PWMPIN, duty_counts) #set pwm cycle here = 100 #set pwm cycle to 100% elif T > temp - 2: DEFDUTY = 0 #set pwm cycle to 0% duty_counts = int(DEFDUTY * PWM_RANGE) wiringpi.pwmWrite(PWMPIN, duty_counts) #set pwm cycle here time.sleep(0.1) # Plot of temperature over the 10 minutes x = np.arange(0, period + 1, 1) y = Tlist plt.scatter(x, y, s=5, c='yellowgreen') plt.xlabel("Time (s)") plt.ylabel("Temperature (Celcius)")