def import_arduino(com_ports): # verilen com_port'dan arduino import eden kod try: if type(com_ports) == list: # eğer birden fazla verilmiş ise for i in range(len(com_ports)): try: board = pyfirmata.ArduinoNano( str(com_ports[i] )) # tek tek dene, başarılı olursan döndür except: pass else: return board elif type(com_ports) == str: # tek bir tane ise onu import et try: board = pyfirmata.ArduinoNano(str(com_ports)) except: pass else: return board else: ### ERROR ### output_e = all_errors[INTERNAL_SYNTAX_ERR] # print(output_e + " # FROM IMPORT_ARDUINO FUNCTION") return output_e ### ERROR ### output_e = all_errors[ARDUINO_CONN_ERR] # print(output_e) return output_e except: ### ERROR ### output = all_errors[INTERNAL_SYNTAX_ERR] # print(output + " # FROM IMPORT_ARDUINO FUNCTION") return output
def measure(length = 25): #here we measure the passing through of light through the sample, it also measures the temperature. #A0 is for the measured light and A1 is for the temperature #D10 is used for red light; D9 for blue light; D8 for green light. #length is used to determine the amount of samples #we define the name of the file global measurement_title measurement_title = input('Enter file name: ') plt.close('all') arduino = pyfirmata.ArduinoNano('COM4') # might be another comport numer in your case time.sleep(1) it = pyfirmata.util.Iterator(arduino) it.start() #read num = length global reading0 reading0 = np.zeros((num,3,3)) pinRED = arduino.get_pin('d:10:o') pinRED.write(0) time.sleep(0.1) pinBLUE = arduino.get_pin('d:9:o') pinBLUE.write(0) time.sleep(0.1) pinGREEN = arduino.get_pin('d:8:o') pinGREEN.write(0) time.sleep(0.1) pinBEEP = arduino.get_pin('d:6:p') pinBEEP.write(0) time.sleep(0.1) ai0 = arduino.get_pin('a:0:i') #measurements of the light ai2 = arduino.get_pin('a:1:i') #temperature measurement ai3 = arduino.get_pin('a:2:i') #on/off switch; used only by achere, can be ignored. time.sleep(0.1) num_avg = 10 #wait; small_delay = 0.05 #the time it waits between measurements of different colour and before and after a temperature measurement long_delay = 0.01 #the waiting time between measurement cycles ## ! small_delay needs to be at least 0.05, otherwise it will not end well #actual measuring start = time.time() for ii in range(num): # PAUSE CHECK if ai3.read() > 0.8: isHigh = True while isHigh: time.sleep(0.05) pinBEEP.write(0.01) time.sleep(0.01) pinBEEP.write(0) if ai3.read() < 0.8: isHigh = False pinBEEP.write(0) time.sleep(0.05) print(ii) for jj in range(3): time.sleep(small_delay) #when waiting a little the voltages will stabilize when switching between temp. and light measurement, temp. will be more stable #reading the temperature if jj == 0: avg_vals = [] for n in range(num_avg): temp = ai2.read() avg_vals.append(temp) time.sleep(0.001) reading0[ii,jj,2] = 500*np.mean(avg_vals) time.sleep(small_delay) #which colour when supply = pinBLUE if jj == 0: supply = pinBLUE elif jj == 1: supply = pinGREEN elif jj == 2: supply = pinRED supply.write(1) #supplies voltage, set to 0 when disconnecting the LED time.sleep(small_delay) #measure light reading0[ii,jj,0] = ai0.read()*10 currentTime = time.time() reading0[ii,jj,1] = currentTime - start time.sleep(small_delay) supply.write(0) #no supply #When stopping before the determined time, we save the data after every cycle reading0_preliminary = reading0[0:ii, :, :] storeData(reading0_preliminary, measurement_title) time.sleep(long_delay) #buzzer sound for determined ending for ii in range(3): pinBEEP.write(0.2) time.sleep(0.08) pinBEEP.write(0) time.sleep(0.4) arduino.sp.close()
def measure(): global flag_running # because we defined the variable before this loop, we can only use it by naming it a global (rather than global flag_exiting # a local) variable global Database # basic arduino-initiation time.sleep(1) arduino = pyfirmata.ArduinoNano('COM8') time.sleep(1) it = pyfirmata.util.Iterator(arduino) it.start() # naming all variables and types of data data_gain1 = [[0.,0.,0.]] # for gain = 1, ai2 data_gain10 = [[0.,0.,0.]] # for gain = 10, ai0 data_gain100 = [[0.,0.,0.]] # for gain = 100, ai1 ttime = [[0.,0.,0.]] # mind the ttime, this because "time" is a command and therefore gets mixed up when calling an array "time" small_delay=0.1 long_delay=1 # defining in- and outputs supplyRED = arduino.get_pin('d:10:o') supplyBLUE = arduino.get_pin('d:9:o') supplyGREEN = arduino.get_pin('d:8:o') ai0 = arduino.get_pin('a:0:i') # 10x gain ai1 = arduino.get_pin('a:1:i') # 100x gain ai2 = arduino.get_pin('a:2:i') # 1x gain start = time.time() # start of time (axis) ii=0 # measurement number while (True): # this loop is run infinitely until further notice (pressing either of two buttons "kill" or "pause") # 1.2) Data acquisition if (flag_running == True): # this is true after pressing "start" until pressing "pause" time.sleep(long_delay) data_gain1=np.append(data_gain1,[[0,0,0]],axis=0) # appending vertically. Before every measurement, the array gets an additional row. This is slower than defining the data_gain10=np.append(data_gain10,[[0,0,0]],axis=0) #size of the matrix beforehand, but of course we do not know the amount of measurements at the start! data_gain100=np.append(data_gain100,[[0,0,0]],axis=0) ttime=np.append(ttime,[[0,0,0]],axis=0) time.sleep(small_delay) for jj in range(3): if jj==0: supply=supplyBLUE # Within one round of measurements, the supply switches. This increases speed. elif jj==1: supply=supplyGREEN elif jj==2: supply=supplyRED supply.write(1) time.sleep(0.2) # I experimented a bit with multiple values of the sleeping-time, but 0.2 seemed to be the most efficient. This doesn't data_gain1[ii,jj]=ai2.read() #take too long and is able to reduce the noise. time.sleep(0.2) data_gain10[ii,jj]=ai0.read() time.sleep(0.2) data_gain100[ii,jj]=ai1.read() time.sleep(0.2) ttime[ii,jj]=time.time()-start # it times this moment, but we need to subtract the previous timing in order to get the right values supply.write(0) np.savez(Database,data_gain1,data_gain10,data_gain100,ttime,start,time.time())#update the data every single time the while loop is run, and thus after each measurement ii+=1 # increasing measurement number. Naturally, only when the measurement is actually run (flag_running = TRUE) if (flag_exiting == True): # this code is run when kill-button is pressed supply.write(0) time.sleep(small_delay) arduino.sp.close() return # quits (while(TRUE) loop) supply.write(0) time.sleep(small_delay) arduino.sp.close()
def measure(length=25): """ measure ---- Does a measurement of the transmission of light through the sample, while also logging temperature. It uses the pins `D8`, `D9 ` and `D10` for respectively green, blue and red light. For measuring transmission, pin `A0` is used, while `A1` is used for measuring temperature. Parameters ---------- > length (int, optional) : The lenght of the measurement in samples. Notes ----- - """ # Define Name global measurement_title measurement_title = input('Enter file name: ') plt.close('all') arduino = pyfirmata.ArduinoNano( 'COM4') # might be another comport numer in your case time.sleep(1) it = pyfirmata.util.Iterator(arduino) it.start() # READINGS num = length global reading0 reading0 = np.zeros((num, 3, 3)) pinRED = arduino.get_pin('d:10:o') pinRED.write(0) time.sleep(0.1) pinBLUE = arduino.get_pin('d:9:o') pinBLUE.write(0) time.sleep(0.1) pinGREEN = arduino.get_pin('d:8:o') pinGREEN.write(0) time.sleep(0.1) pinBEEP = arduino.get_pin('d:6:p') pinBEEP.write(0) time.sleep(0.1) ai0 = arduino.get_pin('a:0:i') # Measurements ai2 = arduino.get_pin('a:1:i') # Temperature Measurement ai3 = arduino.get_pin('a:2:i') # Pause Button time.sleep(0.1) # Wait for definitions to apply # AVERAGING num_avg = 10 # DELAYS small_delay = 0.05 # Delay Between measurements of different colour / before and after temp. measurement long_delay = 0.01 # Delay Between measurement cycles """ DELAYS DO NOT, decrease the small_delay under 0.05. This wil clause major bleed """ # MEASURING ITSELF start = time.time() for ii in range(num): # PAUSE CHECK (SNIPPET 2) if ai3.read() > 0.8: isHigh = True while isHigh: time.sleep(0.05) pinBEEP.write(0.01) time.sleep(0.01) pinBEEP.write(0) if ai3.read() < 0.8: isHigh = False pinBEEP.write(0) time.sleep(0.05) print(ii) for jj in range(3): time.sleep(small_delay) # Temperature Reading ''' The delays before and after are required to allow time for voltages to stabilise between different types of measurements. This was found to give a more stable temperature reading. ''' if jj == 0: avg_vals = [] for n in range(num_avg): temp = ai2.read() avg_vals.append(temp) time.sleep(0.001) reading0[ii, jj, 2] = 500 * np.mean(avg_vals) time.sleep(small_delay) # Defining Colour rotation supply = pinBLUE if jj == 0: supply = pinBLUE elif jj == 1: supply = pinGREEN elif jj == 2: supply = pinRED supply.write(1) # Supply ON time.sleep(small_delay) # Light Reading reading0[ii, jj, 0] = ai0.read() currentTime = time.time() reading0[ii, jj, 1] = currentTime - start time.sleep(small_delay) supply.write(0) # Supply OFF # For early stop; Saving of data occurs after every cycle reading0_preliminary = reading0[0:ii, :, :] storeData(reading0_preliminary, measurement_title) time.sleep(long_delay) # BEEP for END (SNIPPET 1) for ii in range(3): pinBEEP.write(0.2) time.sleep(0.08) pinBEEP.write(0) time.sleep(0.4) arduino.sp.close()
main = threading.Thread(target=mainred) side = threading.Thread(target=sidered) with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: executor.submit(mainred) executor.submit(sidered) ''' main.start() side.start() main.join() side.join() ''' #%% plt.close('all') arduino = pyfirmata.ArduinoNano( 'COM4') # might be another comport numer in your case time.sleep(1) it = pyfirmata.util.Iterator(arduino) it.start() pinner = arduino.get_pin('a:0:i') time.sleep(0.1) listoa = [] for tt in range(25): listoa.append(50 * pinner.read()) time.sleep(0.05) plt.plot(listoa)
from datetime import date # date time library for spefic picture took from datetime import time from datetime import datetime timedata = datetime.time(datetime.now()) # Time data output for processing picture identify tracking Today = date.today() # Showing today #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Core operation of the control function import numpy as np from google_speech import Speech # speech synthesys in multiple languages import cv2 # Camera input import os import sys import pyfirmata # Serial communication library #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> try: hardware = pyfirmata.ArduinoNano('/dev/ttyUSB0') # Hardware serial communication except: print("Serial connection error please reconnect the hardware") # # Language for the Smar toilet machine lang1 = 'th' lang2 = 'en' lang3 = 'zh' #from googletrans import Translator # Google translate sox_effects = ('speed','1.02') # Sox effect for voice speed # Computer vision part cap = cv.VideoCapture(0) # Camera 1 detection try: Motionsense = hardware.get_pin('d:2:i') # Motion detector sensor input except: print("Pins error please recheck") it = pyfirmata.util.Iterator(hardware)
import matplotlib.pyplot as plt import pyfirmata import time import numpy as np time.sleep(1) arduino = pyfirmata.ArduinoNano('COM8') time.sleep(1) it = pyfirmata.util.Iterator(arduino) it.start() small_delay = 0.1 long_delay = 1 supplyRED = arduino.get_pin('d:10:o') supplyBLUE = arduino.get_pin('d:9:o') supplyGREEN = arduino.get_pin('d:8:o') ai2 = arduino.get_pin( 'a:2:i') # we now only need 1 pin to measure, as we don't use gain start = time.time() number_of_measurements = 10 # also different to previous code. 10 measurements is more than enough measurement = 0 # start at 0 data0 = np.zeros((number_of_measurements, 3)) # data per colour when LED is on data1 = np.zeros( (number_of_measurements, 1)) # data when LED is off, then of course we only need 1 array ttime = np.zeros(
def measure(): global flag_running global flag_exiting global Database # basic arduino-initiation time.sleep(1) arduino = pyfirmata.ArduinoNano('COM8') time.sleep(1) it = pyfirmata.util.Iterator(arduino) it.start() # naming all variables and types of data data_gain1 = [[0., 0., 0.]] data_gain10 = [[0., 0., 0.]] data_gain100 = [[0., 0., 0.]] ttime = [[0., 0., 0.]] small_delay = 0.1 long_delay = 1 # defining in- and outputs supplyRED = arduino.get_pin('d:10:o') supplyBLUE = arduino.get_pin('d:9:o') supplyGREEN = arduino.get_pin('d:8:o') supplyBUZZER = arduino.get_pin('d:2:o') ai0 = arduino.get_pin('a:0:i') ai1 = arduino.get_pin('a:1:i') ai2 = arduino.get_pin('a:2:i') start = time.time() ii = 0 while (True): # 1.2) Data acquisition if (flag_running == True): time.sleep(long_delay) data_gain1 = np.append(data_gain1, [[0, 0, 0]], axis=0) data_gain10 = np.append(data_gain10, [[0, 0, 0]], axis=0) data_gain100 = np.append(data_gain100, [[0, 0, 0]], axis=0) ttime = np.append(ttime, [[0, 0, 0]], axis=0) time.sleep(small_delay) for jj in range(3): if jj == 0: supply = supplyBLUE elif jj == 1: supply = supplyGREEN elif jj == 2: supply = supplyRED supply.write(1) time.sleep(0.2) data_gain1[ii, jj] = ai2.read() time.sleep(0.2) data_gain10[ii, jj] = ai0.read() time.sleep(0.2) data_gain100[ii, jj] = ai1.read() time.sleep(0.2) ttime[ii, jj] = time.time() - start supply.write(0) np.savez(Database, data_gain1, data_gain10, data_gain100, ttime, start, time.time()) if ( data_gain1[ii, 0] >= 1 or (time.time() - start) > 40000 ): # statement is only executed when over a certain value or time for buz in range(4): # buzzer goes off 4 times supplyBUZZER.write(1) # buzzer turned on time.sleep(1) # buzzer stays on supplyBUZZER.write(0) # buzzer turns off time.sleep(0.1) # buzzer stays off shortly kill() # function "kill()" is executed ii += 1 if (flag_exiting == True): supply.write(0) time.sleep(small_delay) arduino.sp.close() return supply.write(0) time.sleep(small_delay) arduino.sp.close()