Skip to content

Instantly share code, notes, and snippets.

@soyunkope
Created June 26, 2012 20:54
Show Gist options
  • Select an option

  • Save soyunkope/2998891 to your computer and use it in GitHub Desktop.

Select an option

Save soyunkope/2998891 to your computer and use it in GitHub Desktop.
Skinner Box for the Arduino Uno
#!/usr/bin/python
import sys
import serial
import time
import datetime
'''''
tiene que llamarse con 3 arg: arturito, experimento, nRata
Hay que hacer que mande por serial el expe a realizar
1 train()
2 restExt(trial00)
3 restExt(trial25)
4 restExt(trial50)
5 restExt(trial75)
6 restExt(trial100)
7 progRat(ratio)
'''''
PORT = str(sys.argv[1]) # arturito
expe = str(sys.argv[2])
nRata = str(sys.argv[3])
date = time.asctime()
today = str(datetime.date.today())
with open("/Personal/Deberes/U/Magister/A2012S01/ProyectoTisis/data/"+nRata+"."+today+".log","a+") as f:
f.write(date+" "+nRata+"\n"+"\n")
ser = serial.Serial(PORT,9600)
time.sleep(2)
ser.write(expe)
def writeData(value):
with open("/Personal/Deberes/U/Magister/A2012S01/ProyectoTisis/data/"+nRata+"."+today+".log","a+") as f:
f.write(value)
f.write("\n")
while True:
if ser.inWaiting() > 0: # if we have \r\n in there, we will have two characters, we want more!
value = ser.readline() # Read the data from the
if value == 'End of Expe\r\n':
break
else:
value = value.strip('\r\n')
writeData(value)
#include <Streaming.h>
#include <StopWatch.h>
/*
Permite correr 2 tipos de experimentos en una caja de condicionamiento operante con 2 palancas y 3 luces.
los experimentos del 2-6 son distintos tipos de Resistencia a la Extincion (Hodos 1961)
el experimento 7 es un programa de refuerzos en razon progresiva con razon 5 y tiempo limite de 5 minutos
El experimento 1 es un entrenamiento, 20 trials cuando presiona la palanca se le da una recompensa.
*/
// Declaracion de pines
int ttlDE=8; // Palanca derecha
int ttlIZ=9; // Palanca izquierda
int dispRew=10; // Dispensador de recompensa
int lzDE=11; // Led a la derecha
int lzCE=12; // Led al centro
int lzIZ=13; // Led a la izquierda
//Constantes del Expe
unsigned int bloque=1; //# bloque
unsigned int trial=0; //# trial
unsigned int pressCount=1; //# presiones a palanca
unsigned long palancaPress; //tiempo en que presiono de palanca
unsigned long palancaRelease; //tiempo en que solto la palanca
unsigned long reward=0; //Recompensa, 0 por defecto
unsigned long rewStart; //Tiempo en que comenzo la rew
unsigned long rewEnd; //Tiempo en que termino la rew
unsigned long trialEnd; //Tiempo del fin del trial
int rewDur = 75; //Duracion de la entrega de rew, cuanto tiempo suelta el solenoide
//Datos para PR
int ratio=5; // Ratio de PRS
StopWatch timer; // Crea el timer para PR
int dura=60*5*1000; //tiempo limite antes de fin del expe PR
// Datos para RE
int trial00[]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Trial de extincion
int trial25[]={
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Trial 25% reward
int trial50[]={
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}; // Trial 50% reward
int trial75[]={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}; // Trial 75% reward
int trial100[]={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; // Trial 100% reward
// Datos para Luceo
int timerL=1000; // tiempo para luceo
int pinArray[]={
11,12,13}; // array para luceo
void setup()
{
Serial.begin(9600);
pinMode(ttlIZ, INPUT); // Palanca Izquierda
pinMode(ttlDE, INPUT); // Palanca Derecha
pinMode(lzDE,OUTPUT); // Led a la Derecha
pinMode(lzCE, OUTPUT); // Led al Centro
pinMode(lzIZ, OUTPUT); // Led a la Izquierda
pinMode(dispRew, OUTPUT); // Dispensador de Recompenza
digitalWrite(ttlDE, HIGH); // las palancas mandan a tierra
digitalWrite(ttlIZ, HIGH);
randomizeLST(trial25); //desordena las listas
randomizeLST(trial50);
randomizeLST(trial75);
}
void luceo()
// Esta funcion prende y apaga las luces, sirve para marcar fin de bloques
{
for(int c=0;c<3;c++)
{
digitalWrite(pinArray[c], HIGH);
delay(timerL);
}
for(int c=3;c>0;c--)
{
digitalWrite(pinArray[c], LOW);
delay(timerL);
}
digitalWrite(lzDE,LOW);
}
void randomizeLST(int list[]) // Desordena las listas, 1 vez por sesion
{
randomSeed(analogRead(3));
for (int a=0; a<20; a++){
int r=random(a,19);
int temp=list[a];
list[a]=list[r];
list[r]=temp;
}
}
void exeBlockRE(int lista[])
// Toma la lista desordenada y cuando se apreta la palanca da o no la rew segun ella
{
trial=0;
Serial << "bloque" << "\t" << "trial" << "\t" << "PalancaPress" << "\t" << "PalancaRelease" << "\t" << "rewardState" << "\t" << "rewStart" << "\t" << "rewEnd" << "\t"<< "trialEnd" << endl;
while (trial < 20)
{
reward=lista[trial];
digitalWrite(lzDE,HIGH);
if (digitalRead(ttlDE)!=1)
{
palancaPress=millis();
while (digitalRead(ttlDE) == 0)
{
delay(6);
}
palancaRelease=millis();
if (reward==1)
{
digitalWrite(dispRew, HIGH);
rewStart=millis();
delay(rewDur);
digitalWrite(dispRew, LOW);
rewEnd=millis();
}
else if (reward==0)
{
rewStart=millis();
delay(rewDur);
rewEnd=millis();
}
digitalWrite(lzCE, HIGH);
delay(2000);
digitalWrite(lzCE, LOW);
trialEnd=millis();
Serial << bloque << "\t" << trial+1 << "\t" << palancaPress << "\t" << palancaRelease << "\t" << reward << "\t" << rewStart << "\t" << rewEnd << "\t"<< trialEnd << endl;
trial++;
reward= lista[trial];
}
}
Serial <<"End of Block"<<endl;
digitalWrite(lzDE,LOW);
luceo();
}
void progRat(int ratio)
//cada (ratio*trial) presiones entrega una recompensa.
{
trial = 0;
Serial << "bloque" << "\t" << "trial" << "\t" << "pressCount" << "\t" << "palancaPress" << "\t" << "palancaRelease" << "\t" << "reward" << "\t" << "rewStart" << "\t" << "rewEnd" << "\t" << "trialEnd" << endl;
while (trial < 20 )
{
timer.start();
digitalWrite(lzDE, HIGH);
if ((digitalRead(ttlDE) != 1) && (pressCount == ratio*(trial+1)))
{
reward=1;
palancaPress=millis();
while (digitalRead(ttlDE) == 0)
{
delay(1);
}
palancaRelease=millis();
digitalWrite(dispRew, HIGH);
rewStart=millis();
delay(rewDur);
digitalWrite(dispRew, LOW);
rewEnd=millis();
digitalWrite(lzCE, HIGH);
delay(2000);
digitalWrite(lzCE,LOW);
trialEnd = millis();
Serial << bloque << "\t" << trial+1 << "\t" << pressCount << "\t" << palancaPress << "\t" << palancaRelease << "\t" << reward << "\t" << rewStart << "\t" << rewEnd << "\t" << trialEnd << endl;
trial++;
pressCount=1;
timer.reset();
}
else if ((digitalRead(ttlDE) != 1) && (pressCount != ratio*(trial+1)))
{
reward=0;
palancaPress=millis();
while (digitalRead(ttlDE) == 0)
{
delay(1);
}
palancaRelease=millis();
rewStart=millis();
rewEnd=millis();
trialEnd=millis();
Serial << bloque << "\t" << trial+1 << "\t" << pressCount << "\t" << palancaPress << "\t" << palancaRelease << "\t" << reward << "\t" << rewStart << "\t" << rewEnd << "\t" << trialEnd << endl;
pressCount++;
timer.reset();
}
if (timer.elapsed() > dura)
{
timer.reset();
break;
}
}
Serial << "End of Expe" << endl;
digitalWrite(lzDE,LOW);
luceo();
}
void train()
//entrenamiento
{
exeBlockRE(trial100);
Serial << "End of Expe" << endl;
}
void restExt(int lista[])
//2 bloques, el primero se da una rew segun la prob deseada, el segundo nunca
{
bloque=1;
exeBlockRE(lista);
bloque++;
exeBlockRE(trial00);
Serial << "End of Expe" << endl;
}
void loop()
{
if (Serial.available())
{
char x=Serial.read();
int expe=(x - '0');
if (expe==0)
{
Serial.println("Aun no comienza");
}
else if (expe ==1)
{
Serial << "Training Start:"<< "\t" << millis() << "\t"<<"100% probability rew"<< endl;
train();
}
else if (expe== 2){
Serial << "Resistance to Extintion Start:"<< "\t" << millis() << "\t"<< " 0% probabiliy Rew" << endl;
restExt(trial00);
}
else if (expe==3)
{
Serial << "Resistance to Extintion Start:"<< "\t" << millis() << "\t"<< " 25% probabiliy Rew" << endl;
restExt(trial25);
}
else if (expe==4)
{
Serial << "Resistance to Extintion Start:"<< "\t" << millis() << "\t"<< " 50% probabiliy Rew" << endl;
restExt(trial50);
}
else if (expe==5)
{
Serial << "Resistance to Extintion Start:"<< "\t" << millis() << "\t"<< " 75% probabiliy Rew" << endl;
restExt(trial75);
}
else if (expe==6)
{
Serial << "Resistance to Extintion Start:"<< "\t" << millis() << "\t"<< " 100% probabiliy Rew" << endl;
restExt(trial100);
}
else if (expe==7)
{
Serial << "Progressive Ratio"<< "\t" << millis() << "\t" <<"Ratio" << "\t" << ratio << endl;
progRat(ratio);
}
else
{
Serial << "No reconoce "<< expe <<" como valor"<< endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment