Beispiel #1
0
class Assay:
    # Initialize the pumps
    def __init__(self, fname, nrepeat=10, ard_port="/dev/ttyACM0", pump_port="/dev/ttyUSB0"):
        self.__observers = []  # define an observer
        self.step = -1  # before execution
        self.conf_label = "R" + "CUCI" * nrepeat
        print(self.conf_label)
        self.n_session = 1 + nrepeat * 4
        self.sessions = len(self.conf_label)  # the number of steps
        self.__construct__()
        self.time_flag = pd.Series(np.zeros(self.n_session), index=list(self.conf_label))
        ard_reset(ard_port)
        self.ard = Arduino(ard_port)
        self.ard.output([pin_LED, pin_Servo])
        self.ard.setLow(pin_LED)  # Initial
        self.fname = fname
        self.pump_init(pump_port)

    def blink_start(self, nblink=5):
        # give a blinking start, the LED blinks at 1 Hz
        for ii in range(nblink):
            self.ard.setHigh(pin_LED)
            time.sleep(0.50)
            self.ard.setLow(pin_LED)
            time.sleep(0.50)
            print(ii + 1)

        millis = int(round(time.time() * 1000))
        self.onset = millis

    def pump_init(self, pump_port):
        self.pser = serial.Serial(pump_port, 19200, timeout=0.1)
        print("connected to", self.pser.name)
        pumps = new_era.find_pumps(self.pser)
        self.pump = pumps[0]
        self.infuse = False
        new_era.set_diameter(self.pser, self.pump, dia_1ml)
        new_era.set_direct(self.pser, self.pump, 1)
        new_era.set_rate(self.pser, self.pump, flowrate)

    def register_observer(self, observer):
        self.__observers.append(observer)

    def notify_observers(self, *args, **kwargs):
        for observer in self.__observers:
            observer.notify(self, *args, **kwargs)

    def __construct__(self):
        # construct the configuration array
        self.CS_config = np.zeros([self.n_session, 4])
        ii = 0
        for ch in self.conf_label:
            if ch == "R":
                self.CS_config[ii] = CONFIG_REST
            elif ch == "C":
                self.CS_config[ii] = CONFIG_CS
            elif ch == "U":
                self.CS_config[ii] = CONFIG_US
            else:
                self.CS_config[ii] = CONFIG_INTER
            ii += 1
        # then, we need to update duration of intersessions
        inter_pos = self.CS_config[:, 0] < 0  # take out the positions of intersessions, a boolean array
        inter_dur = self.CS_config[inter_pos, 0]
        self.CS_config[inter_pos, 0] = np.random.randint(40, 70, size=len(inter_dur))

        print(self.CS_config)
        self.duration = self.CS_config[:, 0].sum() / 60.0
        print("Total experimental time: %4.1f min" % self.duration)

    def blink(self, duration, freq=2):
        # blink the LED
        # duration: the duration of blinking
        nblink = int(duration * freq)
        thp = 1 / (freq * 2.0)  # half-period

        for ii in range(nblink):
            self.ard.setHigh(pin_LED)
            time.sleep(thp)
            self.ard.setLow(pin_LED)
            time.sleep(thp)

    def run_step(self, sblink=False):
        millis = int(round(time.time() * 1000))
        self.step += 1
        self.time_flag[self.step] = millis
        self.notify_observers(self.step)
        config_list = self.CS_config[self.step]
        print(config_list[0])
        # take the row
        # turn on or off all the stimuli

        # turn on or off the delivery
        if config_list[2] == 0:
            if self.infuse == True:
                new_era.stop_pump(self.pser, self.pump)
                self.infuse = False
        elif config_list[2] == 1:
            if self.infuse == False:
                new_era.set_rate(self.pser, self.pump, flowrate)
                new_era.run_all(self.pser)
                self.infuse = True

        if config_list[1] == 0:
            self.ard.setLow(pin_LED)  # keep the LED off
            time.sleep(config_list[0])
        elif config_list[1] == 1:
            if sblink:
                self.blink(config_list[0])
            else:
                self.ard.setHigh(pin_LED)
                time.sleep(config_list[0])

        # insert or retract the feeding tube

    #         if config_list[3] == 0:
    #             self.ard.analogWrite(pin_Servo, 0)
    #         elif config_list[3] == 0:
    #             self.ard.analogWrite(pin_Servo, 80)

    #         time.sleep(config_list[0])
    #         self.terminate_step()

    def terminate_step(self):
        # terminate a step

        self.ard.setLow(pin_LED)
        self.ard.analogWrite(pin_Servo, 0)
        new_era.stop_pump(self.pser, self.pump)

    def run_through(self, sblink=False):
        self.blink_start()
        self.step = -1
        ii = 0
        for ii in range(self.n_session):
            self.run_step(sblink)
            print("The" + str(ii) + "th session.")

    #     def shuffle_steps(self):
    #         # shuffle the steps of
    #         CS_total = np.inner(self.CS_config[:,0], self.CS_config[:,1]) # inner product of the first two columns, total duration of CS
    #         US_total = np.inner(self.CS_config[:,0], self.CS_config[:,2]) # total duration of US
    #         time_total = self.CS_config[:,0].sum() # The total amount of time should be equal
    #
    #         # use 10 seconds as a unit of the
    #         NC_chunk = np.round(CS_total/10.).astype('int')
    #         NU_chunk = np.round(US_total/10.).astype('int')
    #         Total_chunk = np.round(time_total/10.).astype('int')
    #
    #         shuff_LED = random_split(NC_chunk)
    #

    def close(self):
        # destruct the class
        self.ard.setLow(pin_LED)
        self.ard.close()

        np.save("../Data/" + self.fname, self.time_flag)
        pd.Series.to_csv(self.time_flag, "../Data/" + self.fname + ".csv")
        print("Arduino port closed. ")
Beispiel #2
0
class Assay():
    # Initialize the pumps
    def __init__(self,
                 fname,
                 nrepeat=10,
                 ard_port='/dev/ttyACM0',
                 pump_port='/dev/ttyUSB0'):
        self.__observers = []  # define an observer
        self.step = -1  # before execution
        self.conf_label = 'R' + 'CUCI' * nrepeat
        print(self.conf_label)
        self.n_session = 1 + nrepeat * 4
        self.sessions = len(self.conf_label)  # the number of steps
        self.__construct__()
        self.time_flag = pd.Series(np.zeros(self.n_session),
                                   index=list(self.conf_label))
        ard_reset(ard_port)
        self.ard = Arduino(ard_port)
        self.ard.output([pin_LED, pin_Servo])
        self.ard.setLow(pin_LED)  # Initial
        self.fname = fname
        self.pump_init(pump_port)

    def blink_start(self, nblink=5):
        # give a blinking start, the LED blinks at 1 Hz
        for ii in range(nblink):
            self.ard.setHigh(pin_LED)
            time.sleep(0.50)
            self.ard.setLow(pin_LED)
            time.sleep(0.50)
            print(ii + 1)

        millis = int(round(time.time() * 1000))
        self.onset = millis

    def pump_init(self, pump_port):
        self.pser = serial.Serial(pump_port, 19200, timeout=.1)
        print('connected to', self.pser.name)
        pumps = new_era.find_pumps(self.pser)
        self.pump = pumps[0]
        self.infuse = False
        new_era.set_diameter(self.pser, self.pump, dia_1ml)
        new_era.set_direct(self.pser, self.pump, 1)
        new_era.set_rate(self.pser, self.pump, flowrate)

    def register_observer(self, observer):
        self.__observers.append(observer)

    def notify_observers(self, *args, **kwargs):
        for observer in self.__observers:
            observer.notify(self, *args, **kwargs)

    def __construct__(self):
        # construct the configuration array
        self.CS_config = np.zeros([self.n_session, 4])
        ii = 0
        for ch in self.conf_label:
            if (ch == 'R'):
                self.CS_config[ii] = CONFIG_REST
            elif (ch == 'C'):
                self.CS_config[ii] = CONFIG_CS
            elif (ch == 'U'):
                self.CS_config[ii] = CONFIG_US
            else:
                self.CS_config[ii] = CONFIG_INTER
            ii += 1
        # then, we need to update duration of intersessions
        inter_pos = self.CS_config[:,
                                   0] < 0  # take out the positions of intersessions, a boolean array
        inter_dur = self.CS_config[inter_pos, 0]
        self.CS_config[inter_pos, 0] = np.random.randint(40,
                                                         70,
                                                         size=len(inter_dur))

        print(self.CS_config)
        self.duration = self.CS_config[:, 0].sum() / 60.0
        print('Total experimental time: %4.1f min' % self.duration)

    def blink(self, duration, freq=2):
        # blink the LED
        # duration: the duration of blinking
        nblink = int(duration * freq)
        thp = 1 / (freq * 2.0)  # half-period

        for ii in range(nblink):
            self.ard.setHigh(pin_LED)
            time.sleep(thp)
            self.ard.setLow(pin_LED)
            time.sleep(thp)

    def run_step(self, sblink=False):
        millis = int(round(time.time() * 1000))
        self.step += 1
        self.time_flag[self.step] = millis
        self.notify_observers(self.step)
        config_list = self.CS_config[self.step]
        print(config_list[0])
        # take the row
        # turn on or off all the stimuli

        # turn on or off the delivery
        if config_list[2] == 0:
            if (self.infuse == True):
                new_era.stop_pump(self.pser, self.pump)
                self.infuse = False
        elif config_list[2] == 1:
            if (self.infuse == False):
                new_era.set_rate(self.pser, self.pump, flowrate)
                new_era.run_all(self.pser)
                self.infuse = True

        if config_list[1] == 0:
            self.ard.setLow(pin_LED)  # keep the LED off
            time.sleep(config_list[0])
        elif config_list[1] == 1:
            if sblink:
                self.blink(config_list[0])
            else:
                self.ard.setHigh(pin_LED)
                time.sleep(config_list[0])

        # insert or retract the feeding tube
#         if config_list[3] == 0:
#             self.ard.analogWrite(pin_Servo, 0)
#         elif config_list[3] == 0:
#             self.ard.analogWrite(pin_Servo, 80)

#         time.sleep(config_list[0])
#         self.terminate_step()

    def terminate_step(self):
        # terminate a step

        self.ard.setLow(pin_LED)
        self.ard.analogWrite(pin_Servo, 0)
        new_era.stop_pump(self.pser, self.pump)

    def run_through(self, sblink=False):
        self.blink_start()
        self.step = -1
        ii = 0
        for ii in range(self.n_session):
            self.run_step(sblink)
            print('The' + str(ii) + 'th session.')


#     def shuffle_steps(self):
#         # shuffle the steps of
#         CS_total = np.inner(self.CS_config[:,0], self.CS_config[:,1]) # inner product of the first two columns, total duration of CS
#         US_total = np.inner(self.CS_config[:,0], self.CS_config[:,2]) # total duration of US
#         time_total = self.CS_config[:,0].sum() # The total amount of time should be equal
#
#         # use 10 seconds as a unit of the
#         NC_chunk = np.round(CS_total/10.).astype('int')
#         NU_chunk = np.round(US_total/10.).astype('int')
#         Total_chunk = np.round(time_total/10.).astype('int')
#
#         shuff_LED = random_split(NC_chunk)
#

    def close(self):
        # destruct the class
        self.ard.setLow(pin_LED)
        self.ard.close()

        np.save('../Data/' + self.fname, self.time_flag)
        pd.Series.to_csv(self.time_flag, '../Data/' + self.fname + '.csv')
        print("Arduino port closed. ")
Beispiel #3
0
from arduino import Arduino
import time

b = Arduino('/dev/ttyUSB0')
pin = 9

#declare output pins as a list/tuple
b.output([pin])

brightness = 0
fadeAmount = 3

b.output([pin])

while (True):
    b.analogWrite(pin, brightness)
    time.sleep(0.05)
    print b.analogRead(pin)
    brightness = brightness + fadeAmount

    if (brightness == 0 or brightness == 255):
        fadeAmount = -fadeAmount
Beispiel #4
0
from arduino import Arduino
from arduino import mode

bot = Arduino("COM5")

bot.pinMode(6, mode.OUT)
bot.pinMode(7, mode.OUT)

_in = ''

while _in != 'q':
    _in = input()

    if _in == 'w':
        bot.analogWrite(6, 50)
        bot.digitalWrite(7, 0)

    if _in == 'd':
        bot.analogWrite(6, 50)
        bot.digitalWrite(7, 1)

    if _in == 's':
        bot.analogWrite(6, 0)
        bot.digitalWrite(7, 0)

    if _in == 'u':
        print(bot.sonicRead(10, 8))
        print(bot.sonicRead(10, 8))
        print(bot.sonicRead(10, 8))
from arduino import Arduino
import time

b = Arduino('/dev/ttyUSB0')
pin = 9

#declare output pins as a list/tuple
b.output([pin])

brightness = 0
fadeAmount = 3

b.output([pin])

while (True):
    b.analogWrite(pin, brightness)
    time.sleep(0.05)
    print b.analogRead(pin)
    brightness = brightness + fadeAmount

    if (brightness == 0 or brightness == 255):
        fadeAmount = -fadeAmount
Beispiel #6
0
        ard.analogWrite(5, A[i])
        if aller == 0: V.append(ard.analogRead(1) * m / 1023)   
        msleep(delay)
        
stop()
ard.digitalWrite(5, ard.LOW)

ard.end()

plt.plot(X, V, "o")
plt.show()
'''

print(ard.analogRead(1))
ard.end()
'''
for i in range(15):
    alpha = sin()

alpha = 0.5
ard.analogWrite(5, floor(alpha * 255))

freq = 2
T = 1./freq

for i in range(5):
    right()
    msleep((T / 2) * 1000)
    left()
    msleep((T / 3) * 1000)