Пример #1
0
def addPoint(valveNum, pulseTime, numPulses):
    if numPulses > 200:
        print('Max number of pulses is 200; changing numPulses to 200.')
        numPulses = 200
    #from Bpod_Gen2.Python_3.Modules.BpodClass import BpodObject # Import BpodObject
    #from Bpod_Gen2_Python_3.Modules.StateMachineAssembler import stateMachine
    #import run_MARFID as rm
    pause = 0.3
    valveTime = pulseTime / 1000
    valveBin = 2**(valveNum - 1)
    # Find name of port
    portName = AcademyUtils.findBpodUSBPort()
    myBpod = BpodObject(portName)
    rc = myBpod.set_subject('Calibrator')
    rc.currentProtocol = 'Calibrate'
    calFolder = myBpod.calibrationFileFolder
    calFile = "valve_calibration_%d.json" % valveNum
    calPath = os.path.join(calFolder, calFile)
    for pulse in range(numPulses):
        sma = stateMachine(myBpod)
        sma.addState('Name', 'FirstPause', 'Timer', pause,
                     'StateChangeConditions', ('Tup', 'Pulse'),
                     'OutputActions', ())
        sma.addState('Name', 'Pulse', 'Timer', valveTime,
                     'StateChangeConditions', ('Tup', 'Pause'),
                     'OutputActions', ('ValveState', valveBin))
        sma.addState('Name', 'Pause', 'Timer', pause, 'StateChangeConditions',
                     ('Tup', 'exit'), 'OutputActions', ())
        myBpod.sendStateMachine(sma)
        RawEvents = myBpod.runStateMachine()
        print('pulse:', pulse + 1)
    myBpod.disconnect()

    vol = input('How many ml?')
    volume = float(vol)
    val = truncate(volume / numPulses, 4)
    pointDict = {str(float(pulseTime)): val}
    return calPath, pointDict
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__)[:-22], "Modules")) # Add Bpod system files to Python path

# Initializing Bpod
from BpodClass import BpodObject # Import BpodObject
from StateMachineAssembler import stateMachine # Import state machine assembler

myBpod = BpodObject('COM13') # Create a new instance of a Bpod object on serial port COM13
sma = stateMachine(myBpod) # Create a new state machine (events + outputs tailored for myBpod)
sma.setGlobalTimer('TimerID', 1, 'Duration', 3) # Set global timer 1 for 3 seconds
sma.addState('Name', 'TimerTrig', # Trigger global timer
    'Timer', 0,
    'StateChangeConditions', ('Tup', 'Port1Lit'),
    'OutputActions', ('GlobalTimerTrig', 1))
sma.addState('Name', 'Port1Lit', # Infinite loop (with next state). Only a global timer can save us.
    'Timer', .25,
    'StateChangeConditions', ('Tup', 'Port3Lit', 'GlobalTimer1_End', 'exit'),
    'OutputActions', ('PWM1', 255))
sma.addState('Name', 'Port3Lit',
    'Timer', .25,
    'StateChangeConditions', ('Tup', 'Port1Lit', 'GlobalTimer1_End', 'exit'),
    'OutputActions', ('PWM3', 255))
myBpod.sendStateMachine(sma) # Send state machine description to Bpod device
RawEvents = myBpod.runStateMachine() # Run state machine and return events
Пример #3
0
def runProtocol(bpodPort, reportCard):
    # Initializing Bpod
    from BpodClass import BpodObject # Import BpodObject
    from StateMachineAssembler import stateMachine # Import state machine assembler
    import random
    import datetime
    import time
    myBpod = BpodObject(bpodPort)
    myBpod.set_protocol('ProtocolTemplate')
    import numpy as np

    d = datetime.date.today()
    d.strftime("%b%d_%y")
    # Create a new instance of a Bpod object
    subject = reportCard.mouseID

    myBpod.set_subject(subject)
    maxWater = reportCard.maxWater
    rewardAmount = 4
    timeout = 5
    sessionDurationMinutes = 1
    
    LeftPort = int(1)
    CenterPort = int(2)
    RightPort = int(3)
    valveTimes = myBpod.getValveTimes(rewardAmount, [1, 2, 3])

    LeftValveTime = valveTimes[0]
    RightValveTime = valveTimes[2]
    CenterValveTime = valveTimes[1]

    LeftLED = 'PWM%d' % LeftPort
    CenterLED = 'PWM%d' % CenterPort
    RightLED = 'PWM%d' % RightPort
    LeftPortBin = 1
    CenterPortBin = 2
    RightPortBin = 4
    trialTypes = []
    myBpod.updateSettings({"Reward Amount": rewardAmount,
                           "Timeout": timeout,
                           "Session Duration (min)": sessionDurationMinutes})
    
    currentTrial = 0
    exitPauseTime = 1
    
    sessionWater = 0
    maxWater = reportCard.maxWater
    waterToday = reportCard.getWaterToday()

    startTime = time.time()
    elapsed_time = 0
    
    while elapsed_time < sessionDurationMinutes*60:
        sma = stateMachine(myBpod) # Create a new state machine (events + outputs tailored for myBpod)
        #choose random decimal between 0 and 1
        randomDec = random.random()
        
        if randomDec > 0.5:
            trialType = 'Left'
        else:
            trialType = 'Right'
        #update list of trial types to save to file
        trialTypes = trialTypes + [trialType]
        print('Trial %d, %s' % (currentTrial, trialType))
        
        if trialType == 'Left':
            leftCorrect = 'RewardLeft'
            rightCorrect = 'Timeout'
            displayLED = 'PWM1'
            rewardState = 'RewardLeft'
        else:
            leftCorrect = 'Timeout'
            rightCorrect = 'RewardRight'
            displayLED = 'PWM3'
            rewardState = 'RewardRight'
            
        sma.addState('Name', 'WaitForPoke',
                     'Timer', 0,
                     'StateChangeConditions', ('Port1In', leftCorrect, 'Port3In', rightCorrect),
                     'OutputActions', (displayLED, 200))

        sma.addState('Name', 'RewardLeft',
                 'Timer', LeftValveTime,
                 'StateChangeConditions', ('Tup', 'ExitPause'),
                 'OutputActions', ('ValveState', 1))

        sma.addState('Name', 'RewardRight',
                 'Timer', RightValveTime,
                 'StateChangeConditions', ('Tup', 'ExitPause'),
                 'OutputActions', ('ValveState', 4))

        sma.addState('Name', 'Timeout',
                     'Timer', timeout,
                     'StateChangeConditions', ('Tup', 'exit'),
                     'OutputActions', ())
                     
        sma.addState('Name', 'ExitPause',
                 'Timer', exitPauseTime,
                 'StateChangeConditions', ('Tup','exit'),
                 'OutputActions', ())
    
        
        myBpod.sendStateMachine(sma) # Send state machine description to Bpod device
        RawEvents = myBpod.runStateMachine() # Run state machine and return events
        myBpod.addTrialEvents(RawEvents)
        rawEventsDict = myBpod.structToDict(RawEvents)
        
        #Find reward times to update session water
        rewardTimes = getattr(myBpod.data.rawEvents.Trial[currentTrial].States, rewardState)
        rewarded = rewardTimes[0][0]>0
        
        #if correct and water rewarded, update water and reset streak
        if rewarded:
            sessionWater += 0.001*rewardAmount

        elapsed_time = time.time()-startTime
        currentTrial = currentTrial+1
        
        if sessionWater+waterToday >= maxWater:
            print('reached maxWater (%d)' % maxWater)
            break
            
    print('Session water:', sessionWater)
    myBpod.updateSettings({'Trial Types':trialTypes})
    myBpod.saveSessionData()
    reportCard.drankWater(sessionWater, myBpod.currentDataFile)
    reportCard.save()
    # Disconnect Bpod
    myBpod.disconnect() # Sends a termination byte and closes the serial port. PulsePal stores current params to its EEPROM.
    return myBpod, reportCard