def testBpodClass(): from BpodClass import BpodObject bpodPort = AcademyUtils.findBpodUSBPort() myBpod = BpodObject(bpodPort) myBpod.set_subject('DummySubject') myBpod.disconnect() return None
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
This program is distributed WITHOUT ANY WARRANTY and without even the 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, time sys.path.append( os.path.join(os.path.dirname(__file__)[:-17], "Modules")) # Add Bpod system files to Python path # Initializing Bpod from BpodClass import BpodObject # Import BpodObject myBpod = BpodObject( 'COM13') # Create a new instance of a Bpod object on serial port COM13 myBpod.manualOverride( 'Output', 'Serial', 1, 65) # Send byte 65 on UART port 1 - by default, this is ASCII 'A' time.sleep(1) # Wait 1s myBpod.loadSerialMessage(1, 65, ( 66, 67, 68)) # Set byte 65 ('A') on UART port 1 to trigger a 3-byte message: 'BCD' myBpod.manualOverride('Output', 'Serial', 1, 65) # Now, the same command has a different result time.sleep(1) # Wait 1s myBpod.resetSerialMessages( ) # Reset the serial message library. Bytes will now pass through again. myBpod.manualOverride('Output', 'Serial', 1, 65) # Back to 'A'
This program is distributed WITHOUT ANY WARRANTY and without even the 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
This program is distributed WITHOUT ANY WARRANTY and without even the 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 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.addState('Name', 'Port1Light', # Add a state 'Timer', 0, 'StateChangeConditions', ('Serial2_3', 'Port2Light'), # Go to Port2Light when byte 0x3 arrives on UART port 2 'OutputActions', ('PWM1', 255)) sma.addState('Name', 'Port2Light', 'Timer', 0, 'StateChangeConditions', ('Tup', 'exit'), 'OutputActions', ('PWM2', 255)) myBpod.sendStateMachine(sma) # Send state machine description to Bpod device RawEvents = myBpod.runStateMachine() # Run state machine and return events print RawEvents.__dict__ # Print events to console
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ''' # Demonstration of AddTrialEvents used in a simple visual 2AFC session. # AddTrialEvents formats each trial's data in a human-readable struct, and adds to myBpod.data (to save to disk later) # Connect noseports to ports 1-3. import os, sys, random sys.path.append( os.path.join(os.path.dirname(__file__)[:-17], "Modules")) # Add Bpod system files to Python path 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 nTrials = 5 trialTypes = [1, 2] # 1 (rewarded left) or 2 (rewarded right) for i in range(nTrials): # Main loop print 'Trial: ' + str(i) thisTrialType = random.choice(trialTypes) # Randomly choose trial type = if thisTrialType == 1: stimulus = 'PWM1' # set stimulus channel for trial type 1 leftAction = 'Reward' rightAction = 'Punish' rewardValve = 1 elif thisTrialType == 2: stimulus = 'PWM3' # set stimulus channel for trial type 1 leftAction = 'Punish'
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
This program is distributed WITHOUT ANY WARRANTY and without even the 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, time sys.path.append( os.path.join(os.path.dirname(__file__)[:-17], "Modules")) # Add Bpod system files to Python path # Initializing Bpod from BpodClass import BpodObject # Import BpodObject myBpod = BpodObject( 'COM13') # Create a new instance of a Bpod object on serial port COM13 myBpod.manualOverride('Output', 'PWM', 2, 255) # Set LED of port 2 to max intensity time.sleep(0.25) # Wait 250ms myBpod.manualOverride('Output', 'PWM', 2, 8) # Set LED of port 2 to lower intensity time.sleep(0.25) # Wait 250ms myBpod.manualOverride('Output', 'PWM', 2, 0) # Set LED of port 2 to zero intensity time.sleep(1) # Wait 1s myBpod.manualOverride('Output', 'Valve', 1, 1) # Set valve of port 1 to "open" time.sleep(0.25) # Wait 250ms myBpod.manualOverride('Output', 'Valve', 1,